#include #include #include #include #if 1 #define DPRINT(x) #define DPRIN2(x,y) #define DPRIN3(x,y,z) #else #define DPRINT(x) fprintf (stderr, x) #define DPRIN2(x,y) fprintf (stderr, x, y) #define DPRIN3(x,y,z) fprintf (stderr, x, y, z) #endif #include "g2_term.h" static int i1 = 0; static int i2 = 0; static int i3 = 0; static int i4 = 0; int erase_char, kill_char, werase_char; /* ***** RAW_MODE ***** */ void raw_mode (int on) { /* 27-73 */ static int curr_on = 0; struct termios s; static struct termios save_term; if (on == curr_on) return; if (on) { /* 35-65 */ tcgetattr(2, &s); save_term = s; erase_char = s.c_cc[2]; kill_char = s.c_cc[3]; werase_char = s.c_cc[14]; s.c_lflag &= ~(0 | 0000002 | 0000010 | 0000020 | 0000040 | 0000100 ); s.c_oflag |= (0 | 0014000 | 0000001 | 0000004 ); s.c_oflag &= ~(0 | 0000010 | 0000020 | 0000040 ); s.c_cc[6 ] = 1; s.c_cc[5 ] = 0; s.c_cc[15 ] = 0; } /* 35-65 */ else s = save_term; tcsetattr (2, 1 , &s); curr_on = on; return; } /* 27-73 */ static jmp_buf read_label; int reading; /* ***** IREAD ***** */ int iread (int fd, char *buf, unsigned int len) { /* 80-96 */ register int n; if (__sigsetjmp ((read_label ), 0)) { /* 84-88 */ reading = 0; sigsetmask (0); return (-2); } /* 84-88 */ /* flush () was here */ reading = 1; n = read (fd, buf, len); reading = 0; if (n < 0) return (-1); return (n); } /* 80-96 */ static int tty; /* ***** OPEN_GETCHR ***** */ void open_getchr (void) { /* 102-106 */ tty = open ("/dev/tty", 0) ; if (tty < 0) tty = 2; return; } /* 102-106 */ /* ***** CLOSE_GETCHR ***** */ void close_getchr (void) { /* 110-112 */ return; } /* 110-112 */ /* ***** GETCHR ***** */ int getchr (void) { /* 116-130 */ char c; int result; /* CTEST_IGNORE E160 */ do { /* 121-126 */ result = iread (tty, &c, sizeof (char)); if (result == (-2)) return (-2); if (result < 0) return (0); if (c == '\0') c = '\340'; } /* 121-126 */ while (result != 1); return (c & 0377); } /* 116-130 */ /* ***** KBHIT ***** */ int kbhit (void) { /* 134-146 */ fd_set rfds; struct timeval tv; if (i1 != 0) return (1); FD_ZERO (&rfds); FD_SET (0, &rfds); tv.tv_sec = 0; tv.tv_usec = 0; if (select (1, &rfds, NULL, NULL, &tv)) return (1); return (0); } /* 134-146 */ /* ***** TIME_CHAR ***** */ int time_char (void) { /* 150-168 */ int j; clock_t iclock, jclock; iclock = jclock = clock (); while (jclock-iclock < CLOCKS_PER_SEC/2) { /* 156-160 */ j = kbhit (); if (j) break; jclock = clock (); } /* 156-160 */ if (j == 0) DPRINT ("\t\t\t\t\tTime out.\n"); if (j == 0) return (0); j = getchr (); if (j > 31) DPRIN3 ("\t\t\t\t\tchar is %d (%c).\n", j, j); else DPRIN2 ("\t\t\t\t\tchar is %d.\n", j); return (j); } /* 150-168 */ /* **** GETCH ***** */ int getch (void) { /* 172-518 */ int c, j; if (i1 != 0) { /* 175-179 */ c = i1, i1 = i2, i2 = i3, i3 = i4, i4 = 0; DPRINT ("\t\t\t\tNextchar.\n"); return (c); } /* 175-179 */ c = getchr (); if (c != ESCAPE) return (c); DPRINT ("\t\t\t\tEscape\n"); /* Okay, here's the deal. If the character is an */ /* ESCAPE character (27), then it *could* be the */ /* start of an arrow-key sequence and should be */ /* processed as such. On the other hand, since it */ /* could also be a naked ESCAPE, we're going to */ /* wait a half second for another character. */ /* If the other character comes along and it is not */ /* a special character ([ or O), then we keep it */ /* here and send it immediately the next time */ /* this function is called. */ i1 = time_char (); if (i1 == 0) return (c); DPRINT ("\t\t\t\tKbhit\n"); /* Left bracket case. */ if (i1 == '[') { /* 205-431 */ DPRINT ("\t\t\t\tSpecial [\n"); i2 = time_char (); if (i2 == 0) return (c); if (i2 == 'A') { /* 211-214 */ i1 = i2 = 0; return (UP_ARROW); } /* 211-214 */ if (i2 == 'B') { /* 216-219 */ i1 = i2 = 0; return (DOWN_ARROW); } /* 216-219 */ if (i2 == 'C') { /* 221-224 */ i1 = i2 = 0; return (RIGHT_ARROW); } /* 221-224 */ if (i2 == 'D') { /* 226-229 */ i1 = i2 = 0; return (LEFT_ARROW); } /* 226-229 */ if (i2 == 'G') { /* 231-234 */ i1 = i2 = 0; return (CENTER); } /* 231-234 */ if (i2 == '1') { /* 236-288 */ DPRINT ("\t\t\t\tSpecial [ 1\n"); i3 = time_char (); if (i3 == 0) return (c); if (i3 == TWIDDLE) { /* 241-245 */ DPRINT ("\t\t\t\tSpecial [ 1 ~\n"); i1 = i2 = i3 = 0; return (HOME); } /* 241-245 */ if (i3 == CARROT) { /* 247-251 */ DPRINT ("\t\t\t\tSpecial [ 1 ^\n"); i1 = i2 = i3 = 0; return (CNTL_HOME); } /* 247-251 */ if (isdigit (i3) == 0) return (c); i4 = time_char (); if (i4 == 0) return (c); if (i4 == TWIDDLE) { /* 255-270 */ DPRINT ("\t\t\t\tSpecial [ 1 * ~\n"); switch (i3) { /* 258-269 */ case '0': return (c); case '1': i1 = i2 = i3 = i4 = 0; return (F_ONE); case '2': i1 = i2 = i3 = i4 = 0; return (F_ONE-1); case '3': i1 = i2 = i3 = i4 = 0; return (F_ONE-2); case '4': i1 = i2 = i3 = i4 = 0; return (F_ONE-3); case '5': i1 = i2 = i3 = i4 = 0; return (F_ONE-4); case '6': return (c); case '7': i1 = i2 = i3 = i4 = 0; return (F_ONE-5); case '8': i1 = i2 = i3 = i4 = 0; return (F_ONE-6); case '9': i1 = i2 = i3 = i4 = 0; return (F_ONE-7); } /* 258-269 */ } /* 255-270 */ else if (i4 == CARROT) { /* 272-287 */ DPRINT ("\t\t\t\tSpecial [ 1 * ^\n"); switch (i3) { /* 275-286 */ case '0': return (c); case '1': i1 = i2 = i3 = i4 = 0; return (CNTL_F_ONE); case '2': i1 = i2 = i3 = i4 = 0; return (CNTL_F_ONE-1); case '3': i1 = i2 = i3 = i4 = 0; return (CNTL_F_ONE-2); case '4': i1 = i2 = i3 = i4 = 0; return (CNTL_F_ONE-3); case '5': i1 = i2 = i3 = i4 = 0; return (CNTL_F_ONE-4); case '6': return (c); case '7': i1 = i2 = i3 = i4 = 0; return (CNTL_F_ONE-5); case '8': i1 = i2 = i3 = i4 = 0; return (CNTL_F_ONE-6); case '9': i1 = i2 = i3 = i4 = 0; return (CNTL_F_ONE-7); } /* 275-286 */ } /* 272-287 */ } /* 236-288 */ else if (i2 == '2') { /* 290-350 */ DPRINT ("\t\t\t\tSpecial [ 2\n"); i3 = time_char (); if (i3 == 0) return (c); if (i3 == TWIDDLE) { /* 295-298 */ i1 = i2 = i3 = 0; return (INSERT); } /* 295-298 */ if (i3 == CARROT) { /* 300-304 */ DPRINT ("\t\t\t\tSpecial [ 2 ^\n"); i1 = i2 = i3 = 0; return (CNTL_INSERT); } /* 300-304 */ if (isdigit (i3) == 0) return (c); i4 = time_char (); if (i4 == 0) return (c); if (i4 == TWIDDLE) { /* 308-323 */ DPRINT ("\t\t\t\tSpecial [ 2 * ~\n"); switch (i3) { /* 311-322 */ case '0': i1 = i2 = i3 = i4 = 0; return (F_ONE-8); case '1': i1 = i2 = i3 = i4 = 0; return (F_TEN); case '2': return (c); case '3': i1 = i2 = i3 = i4 = 0; return (SHIFT_F_ONE); case '4': i1 = i2 = i3 = i4 = 0; return (SHIFT_F_ONE-1); case '5': i1 = i2 = i3 = i4 = 0; return (SHIFT_F_ONE-2); case '6': i1 = i2 = i3 = i4 = 0; return (SHIFT_F_ONE-3); case '7': return (c); case '8': i1 = i2 = i3 = i4 = 0; return (SHIFT_F_ONE-4); case '9': i1 = i2 = i3 = i4 = 0; return (SHIFT_F_ONE-5); } /* 311-322 */ } /* 308-323 */ else if (i4 == CARROT) { /* 325-336 */ DPRINT ("\t\t\t\tSpecial [ 2 * ^\n"); switch (i3) { /* 328-335 */ case '0': i1 = i2 = i3 = i4 = 0; return (CNTL_F_ONE-8); case '1': i1 = i2 = i3 = i4 = 0; return (CNTL_F_TEN); case '2': return (c); case '3': i1 = i2 = i3 = i4 = 0; return (CNTL_F_TEN-1); case '4': i1 = i2 = i3 = i4 = 0; return (CNTL_F_TEN-2); default: return (c); } /* 328-335 */ } /* 325-336 */ else if (i4 == DOLLAR) { /* 338-349 */ DPRINT ("\t\t\t\tSpecial [ 2 * $\n"); switch (i3) { /* 341-348 */ case '0': return (c); case '1': return (c); case '2': return (c); case '3': i1 = i2 = i3 = i4 = 0; return (SHIFT_F_TEN-1); case '4': i1 = i2 = i3 = i4 = 0; return (SHIFT_F_TEN-2); default: return (c); } /* 341-348 */ } /* 338-349 */ } /* 290-350 */ else if (i2 == '3') { /* 352-376 */ DPRINT ("\t\t\t\tSpecial [ 3\n"); i3 = time_char (); if (i3 == 0) return (c); if (i3 == TWIDDLE) { /* 357-360 */ i1 = i2 = i3 = 0; return (DEL); } /* 357-360 */ if (isdigit (i3) == 0) return (c); i4 = time_char (); if (i4 == 0) return (c); if (i4 == TWIDDLE) { /* 364-375 */ DPRINT ("\t\t\t\tSpecial [ 3 * ~\n"); switch (i3) { /* 367-374 */ case '0': return (c); case '1': i1 = i2 = i3 = i4 = 0; return (SHIFT_F_ONE-6); case '2': i1 = i2 = i3 = i4 = 0; return (SHIFT_F_ONE-7); case '3': i1 = i2 = i3 = i4 = 0; return (SHIFT_F_ONE-8); case '4': i1 = i2 = i3 = i4 = 0; return (SHIFT_F_TEN); default: return (c); } /* 367-374 */ } /* 364-375 */ } /* 352-376 */ else if (i2 == '4') { /* 378-394 */ DPRINT ("\t\t\t\tSpecial [ 4\n"); i3 = time_char (); if (i3 == 0) return (c); if (i3 == TWIDDLE) { /* 383-387 */ DPRINT ("\t\t\t\tSpecial [ 4 ~\n"); i1 = i2 = i3 = 0; return (END); } /* 383-387 */ if (i3 == CARROT) { /* 389-393 */ DPRINT ("\t\t\t\tSpecial [ 2 ^\n"); i1 = i2 = i3 = 0; return (CNTL_END); } /* 389-393 */ } /* 378-394 */ else if (i2 == '5') { /* 396-412 */ DPRINT ("\t\t\t\tSpecial [ 5\n"); i3 = time_char (); if (i3 == 0) return (c); if (i3 == TWIDDLE) { /* 401-405 */ DPRINT ("\t\t\t\tSpecial [ 5 ~\n"); i1 = i2 = i3 = 0; return (PAGE_UP); } /* 401-405 */ if (i3 == CARROT) { /* 407-411 */ DPRINT ("\t\t\t\tSpecial [ 2 ^\n"); i1 = i2 = i3 = 0; return (CNTL_PGUP); } /* 407-411 */ } /* 396-412 */ else if (i2 == '6') { /* 414-430 */ DPRINT ("\t\t\t\tSpecial [ 6\n"); i3 = time_char (); if (i3 == 0) return (c); if (i3 == TWIDDLE) { /* 419-423 */ DPRINT ("\t\t\t\tSpecial [ 6 ~\n"); i1 = i2 = i3 = 0; return (PAGE_DOWN); } /* 419-423 */ if (i3 == CARROT) { /* 425-429 */ DPRINT ("\t\t\t\tSpecial [ 2 ^\n"); i1 = i2 = i3 = 0; return (CNTL_PGDN); } /* 425-429 */ } /* 414-430 */ } /* 205-431 */ /* Capital O case. */ else if (i1 == 'O') { /* 435-515 */ DPRINT ("\t\t\t\tSpecial O\n"); i2 = time_char (); if (i2 == 0) return (c); if (i2 == 'a') { /* 441-444 */ i1 = i2 = 0; return (CNTL_UP); } /* 441-444 */ if (i2 == 'b') { /* 446-449 */ i1 = i2 = 0; return (CNTL_DOWN); } /* 446-449 */ if (i2 == 'c') { /* 451-454 */ i1 = i2 = 0; return (CNTL_RIGHT); } /* 451-454 */ if (i2 == 'd') { /* 456-459 */ i1 = i2 = 0; return (CNTL_LEFT); } /* 456-459 */ if (i2 == 'n') { /* 461-464 */ i1 = i2 = 0; return (DEL); } /* 461-464 */ if (i2 == 'p') { /* 466-469 */ i1 = i2 = 0; return (INSERT); } /* 466-469 */ if (i2 == 'q') { /* 471-474 */ i1 = i2 = 0; return (END); } /* 471-474 */ if (i2 == 'r') { /* 476-479 */ i1 = i2 = 0; return (DOWN_ARROW); } /* 476-479 */ if (i2 == 's') { /* 481-484 */ i1 = i2 = 0; return (PAGE_DOWN); } /* 481-484 */ if (i2 == 't') { /* 486-489 */ i1 = i2 = 0; return (LEFT_ARROW); } /* 486-489 */ if (i2 == 'u') { /* 491-494 */ i1 = i2 = 0; return (CENTER); } /* 491-494 */ if (i2 == 'v') { /* 496-499 */ i1 = i2 = 0; return (RIGHT_ARROW); } /* 496-499 */ if (i2 == 'w') { /* 501-504 */ i1 = i2 = 0; return (HOME); } /* 501-504 */ if (i2 == 'x') { /* 506-509 */ i1 = i2 = 0; return (UP_ARROW); } /* 506-509 */ if (i2 == 'y') { /* 511-514 */ i1 = i2 = 0; return (PAGE_UP); } /* 511-514 */ } /* 435-515 */ return (c); } /* 172-518 */ /* ***** MAIN ***** */ int main () { /* 522-597 */ int c, j; time_t itime, jtime, ktime; clock_t iclock, jclock, kclock; printf ("clocks per second = %d\n", CLOCKS_PER_SEC); raw_mode (1); /* open_getchr () was here */ while (1) { /* 532-594 */ c = getch (), printf ("The character is "); if (c > 31) printf ("%d (%c).\n", c, c); else if (c >= 0) printf ("%d.\n", c); else { /* 537-569 */ if (c == HOME) printf ("HOME.\n"); else if (c == UP_ARROW) printf ("UP_ARROW.\n"); else if (c == PAGE_UP) printf ("PAGE_UP.\n"); else if (c == LEFT_ARROW) printf ("LEFT_ARROW.\n"); else if (c == RIGHT_ARROW) printf ("RIGHT_ARROW.\n"); else if (c == END) printf ("END.\n"); else if (c == DOWN_ARROW) printf ("DOWN_ARROW.\n"); else if (c == PAGE_DOWN) printf ("PAGE_DOWN.\n"); else if (c == INSERT) printf ("INSERT.\n"); else if (c == DEL) printf ("DELETE.\n"); else if (c == CENTER) printf ("CENTER.\n"); else if (c == CNTL_HOME) printf ("CNTL_HOME.\n"); else if (c == CNTL_END) printf ("CNTL_END.\n"); else if (c == CNTL_PGUP) printf ("CNTL_PAGE_UP.\n"); else if (c == CNTL_PGDN) printf ("CNTL_PAGE_DOWN.\n"); else if (c == CNTL_UP) printf ("CNTL_UP.\n"); else if (c == CNTL_DOWN) printf ("CNTL_DOWN.\n"); else if (c == CNTL_LEFT) printf ("CNTL_LEFT.\n"); else if (c == CNTL_RIGHT) printf ("CNTL_RIGHT.\n"); else if (c == CNTL_INSERT) printf ("CNTL_INSERT.\n"); else if (c == CNTL_CENTER) printf ("CNTL_CENTER.\n"); else if (c <= F_ONE && c >= F_TEN) printf ("Function Key %d\n", F_ONE+1-c); else if (c <= SHIFT_F_ONE && c >= SHIFT_F_TEN-2) printf ("Shift Fcn Key %d\n", SHIFT_F_ONE+1-c); else if (c <= CNTL_F_ONE && c >= CNTL_F_TEN-2) printf ("Ctrl Fcn Key %d\n", CNTL_F_ONE+1-c); else printf ("unknown.\n"); } /* 537-569 */ if (c == 'k') { /* 571-592 */ itime = time (NULL), iclock = clock (); printf ("Waiting . . ."), fflush (stdout); for (c = 1; c <= 100000; c++) { /* 575-578 */ j = kbhit (); if (j) break; } /* 575-578 */ printf ("\n"); jtime = time (NULL), jclock = clock ();; printf ("Waiting . . ."), fflush (stdout); for (c = 1; c <= 100000; c++) { /* 583-587 */ j = kbhit (); j = kbhit (); if (j) break; } /* 583-587 */ ktime = time (NULL), kclock = clock (); printf ("%d %d\n", jtime-itime, ktime-jtime); printf ("%g %g\n", (double) (jclock-iclock)/CLOCKS_PER_SEC, (double) (kclock-jclock)/CLOCKS_PER_SEC); } /* 571-592 */ if (c == 'q') break; } /* 532-594 */ raw_mode (0); return (0); } /* 522-597 */