/* termstuff.c - omnibus demo of various terminal operations. */ #include <stdio.h> #include <termios.h> void main() { struct termios oldmodes, newmodes; tcflag_t iflag[] = { ICRNL, ISTRIP, BRKINT, IGNPAR, IMAXBEL, IXANY, 0 }; char * iflagname[] = {"ICRNL", "ISTRIP", "BRKINT", "IGNPAR", "IMAXBEL", "IXANY" , (char *) 0}; char buf[81]; int i; tcflag_t oflag[] = { OCRNL, OLCUC, ONLCR, 0 }; char * oflagname[] = {"OCRNL", "OLCUC", "ONLCR", (char *) 0}; /* Don't allow cheating. */ if( !isatty(0)) { fprintf(stderr,"stdin must be a terminal\n"); exit(1); } /* Terminal name functions. */ printf("ctermid(NULL) = %s\n", ctermid(NULL)); printf("ttyname(0) = %s\n", ttyname(0)); /* Of course, the easy way to see how your terminal is set up is */ system("stty -a"); if( tcgetattr(0, &oldmodes) < 0) { perror("tcgetattr"); exit(1); } /* Look at some iflag settings. */ for( i = 0; iflag[i] != NULL; i ++) { printf("%s is %s\n", iflagname[i], iflag[i] & oldmodes.c_iflag ? "on" : "off"); } /* Turn off ICRNL and see what happens. */ printf("Enter won't work as usual - use ^J to get a line accepted.\n"); printf("About to read two lines:\n"); newmodes = oldmodes; newmodes.c_iflag &= ~ICRNL; tcsetattr(0, TCSANOW, &newmodes); gets(buf); gets(buf); tcsetattr(0, TCSANOW, &oldmodes); printf("OK, ICRNL now back on.\n"); printf("Reading another line:\n"); gets(buf); /* Look at some oflag settings. */ for( i = 0; oflag[i] != NULL; i ++) { printf("%s is %s\n", oflagname[i], oflag[i] & oldmodes.c_oflag ? "on" : "off"); } /* Turn ONLCR off and see what happens. */ newmodes = oldmodes; newmodes.c_oflag &= ~ONLCR; tcsetattr(0, TCSANOW, &newmodes); printf("There is a \\n right here =>\n"); printf("This would be a new line\n"); tcsetattr(0, TCSANOW, &oldmodes); printf("Are we back to normal?\n"); printf("Are we?\n"); /* Turn ECHO off. */ newmodes = oldmodes; newmodes.c_lflag &= ~ECHO; tcsetattr(0, TCSANOW, &newmodes); printf("Enter something: "); gets(buf); printf("You entered %s\n", buf); tcsetattr(0, TCSANOW, &oldmodes); /* Turn ECHOCTL on. */ newmodes = oldmodes; newmodes.c_lflag |= ECHOCTL; tcsetattr(0, TCSANOW, &newmodes); gets(buf); tcsetattr(0, TCSANOW, &oldmodes); }