Advertisement
ptkrisada

termios

Feb 29th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 0 0
  1. /****************************************************************************
  2.  * Terminal I/O                                                             *
  3.  * Normally, on Unix, ctrl-C refers to "cancel" and ctrl-D refers to EOF.   *
  4.  * What if we want to change that? This program will change the default     *
  5.  * keys to something else using <termios.h> defined in POSIX.1 standard.    *
  6.  * ** Note: this program can't run on ideone.com as we have to enter input  *
  7.  * interactively. I presume that ideone.com only take input in HTML form    *
  8.  * prior to running the program. So I use pastebin.com, instead.            *
  9.  * You can still copy the code and paste it on Unix terminal to test it.    *
  10.  ****************************************************************************/
  11.  
  12. #include <unistd.h>     /* isatty(3), fpathconf(2) */
  13. #include <termios.h>    /* tcgetattr(3), tcsetattr(3) */
  14. #include <stdio.h>
  15. #include <stdlib.h>     /* exit(3) */
  16.  
  17. int main(void)
  18. {
  19.     struct termios newterm, oldterm; /* Declare new and old terminal. */
  20.     long vdisable;
  21.  
  22.     /* Check if stdin is a terminal (tty) using isatty(3).                  *
  23.      * isatty stands for "Is A TTY?".                                       *
  24.      * Note: stdin is a (FILE *) and  STDIN_FILENO is a file descriptor.    *
  25.      * But both refer to the same thing.                                    */
  26.     if (isatty(STDIN_FILENO) == 0) {
  27.         fprintf(stderr, "standard input is not a terminal device");
  28.         exit(EXIT_FAILURE);
  29.     }
  30.  
  31.     /* pathconf or fpathconf family is used to get configurable pathname    *
  32.      * variables. It returns the current variable value.                    *
  33.      * - pathconf(2) takes (FILE *) as the first argument.                  *
  34.      * - fpathconf(2) takes a file descriptor as the first argument.        *
  35.      * The following lines of code, we are trying to get terminal character *
  36.      * disabling value (_PC_VDISABLE). We can also get the others value,    *
  37.      * see fpathconf(2). The following lines, we call fpathconf(2) and set  *
  38.      * char disabling value to vdisable.                                    */
  39.     if ((vdisable = fpathconf(STDIN_FILENO, _PC_VDISABLE)) == -1) {
  40.         fprintf(stderr, "fpathconf error or _POSIX_VDISABLE not in effect");
  41.         exit(EXIT_FAILURE);
  42.     }
  43.  
  44.     /* tcgetattr stands for Terminal Control Get Attribute. We get the      *
  45.      * current attribute of STDIN_FILENO (stdin) and store it in 'oldterm'. */
  46.     if (tcgetattr(STDIN_FILENO, &oldterm) == -1) {
  47.         fprintf(stderr, "tcgetattr");
  48.         exit(EXIT_FAILURE);
  49.     }
  50.     newterm = oldterm;              /* Copy "oldterm" to "newterm".         */
  51.     newterm.c_cc[VINTR] = vdisable; /* Disable INTR character in "newterm". */
  52.     newterm.c_cc[VEOF] = 2;         /* EOF is now ctrl-B in "newterm".      */
  53.     /* We can find c_cc index in /usr/include/termios.h . */
  54.  
  55.     /* We now set the attribute of our modified "newterm" to STDIN_FILENO.  *
  56.      * For details for the 2nd arg, consult man pages tcsetattr(3).         *
  57.      * This example, we want it to take into effect immediately. So we use  *
  58.      * TCSANOW.                                                             */
  59.     if (tcsetattr(STDIN_FILENO, TCSANOW, &newterm) == -1) {
  60.         fprintf(stderr, "tcsetattr");
  61.         exit(EXIT_FAILURE);
  62.     }
  63.     printf("try repeating ^C, ^D then close with ^B or <enter>:");
  64.     getchar();
  65.  
  66.     /* We now restore "oldterm", which we previously saved, back to normal  *
  67.      * attribute, prior to end of program.                                  */
  68.     if (tcsetattr(STDIN_FILENO, TCSANOW, &oldterm) == -1) {
  69.         fprintf(stderr, "tcsetattr");
  70.         exit(EXIT_FAILURE);
  71.     }
  72.     fputs("\n", stdout);
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement