Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!pacbell!sactoh0!siva!root From: root@siva.UUCP (Personage) Newsgroups: comp.sys.att Subject: Re: termio under SysV (kbdhit() and getch()) Summary: Dr. Rebecca Thomas Message-ID: <8@siva.UUCP> Date: 4 Mar 89 09:45:41 GMT References: <1174@naucse.UUCP> <1182@naucse.UUCP> <7851@chinet.chi.il.us> Distribution: usa Organization: siva Lines: 90 In article <7851@chinet.chi.il.us>, les@chinet.chi.il.us (Leslie Mikesell) writes: > In article <1182@naucse.UUCP> jdc@naucse.UUCP (John Campbell) writes: > >And the bad news is that I no longer have a valid plan for implementing > >a peek into the typeahead buffer at all! Anyone know if this can be done > >under SysV? (And, of course, how it is done?) There is a way (several ways???). > What you have to do is read the characters into your own buffer and then > you can peek all you like. True, but then ungetch() is useless if needed. #define DISCLAIMER #ifdef DISLAIMER In a recent 'pitch to pay for knowledge' I received this neat 'scrap' of code. It is attributed to Dr. Rebecca Thomas who is the 'controller' of the 'Unix World' WIZARDS GRABBAG coloumn. Code follows: #endif /* DISCLAIMER */ --------------------------cut above--------------------------------------------- /* for readability, set your tab stops to 3. */ #include #include #include #define DEBUG #define ECHO_OFF 1 #define ECHO_ON 0 #ifdef DEBUG main() { int i, ch, getch(); char s[BUFSIZ]; printf("Type [Enter] to quit\n"); do { ch = getch(ECHO_OFF); s[i++] = ch; } while(ch != '\f'); s[--i] = '\0'; i = 0; while(s[i]) { if(isprint(s[i])) { printf("%c",s[i]); } else { printf("\\%d",s[i]); } i++; } putchar('\n'); } #endif int getch(echo) int echo; { struct termio tsaved, tnew; char buf; ioctl(0, TCGETA, &tsaved); tnew = tsaved; tnew.c_lflag &= ~ICANON; tnew.c_lflag &= ~ISIG; if(echo == ECHO_OFF) tnew.c_lflag &= ~ECHO; else tnew.c_lflag &= ECHO; tnew.c_cc[VMIN] = 1; tnew.c_cc[VTIME] = 0; ioctl(0, TCSETA, &tnew); read(0, &buf, 1); ioctl(0, TCSETA, &tsaved); if(buf == tsaved.c_cc[VEOF]) return(EOF); return(buf); } -------------------------cut below---------------------------------------------- This topic of terminal independent character input interests me a great deal; So: 1) is a timeout acceptable in a 'keystroke input loop' ? 2) is having to strike a given key (most notably Escape) twice (more than once) an acceptable work around? 3) if you are so smart, would you mind sharing the secret?? (P.S. the clock granularity is insufficient to make reliable non-blocking reads and expect any timing related grouping; like an 8mhz i286) (:-> :-) ;-% B-S) (my friends and I smile, hopefully) -- "I only did what you didn't tell me not to do..." Users Credo siva!uumgr Mark Marsh