#include #include #include #include #if 0 #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 */ #if G2_WINAPI == 0 static int curr_on = 0; static struct termio current_io; static struct termio original_io; int result; if (on == curr_on) return; if (on) { /* 37-58 */ /* get original Terminal settings to restore */ /* mode when we are finished */ result = ioctl (0, TCGETA, &original_io); if (result == -1) { /* 42-45 */ perror ("ioctl(): TIOCGETD"); exit (1); } /* 42-45 */ memcpy (¤t_io, &original_io, sizeof (struct termio)); current_io.c_cc[VMIN] = 1; /* wait for one character */ current_io.c_cc[VTIME] = 0; /* and don't wait to return it */ current_io.c_lflag &= ~ICANON; /* unbuffered input */ current_io.c_lflag &= ~ECHO; /* turn off local display */ /* set new input mode */ result = ioctl (0, TCSETA, ¤t_io); if (result == -1) { /* 54-57 */ perror ("ioctl(): TIOCSETP"); exit (1); } /* 54-57 */ } /* 37-58 */ else { /* 60-68 */ /* reset original terminal mode */ result = ioctl (0, TCSETA, &original_io); if (result == -1) { /* 64-67 */ perror ("ioctl(): TIOCSETP to reset original io"); exit (1); } /* 64-67 */ } /* 60-68 */ curr_on = on; #endif return; } /* 27-73 */ /* ***** GETCHR ***** */ int getchr (void) { /* 77-81 */ int j; j = fgetc (stdin); return (j); } /* 77-81 */ /* ***** KBHIT ***** */ int kbhit (void) { /* 85-110 */ #if G2_WINAPI int i; i = g_kbhit (); return (i); #else 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 1 if (select (1, &rfds, NULL, NULL, &tv)) return (1); return (0); #else select (1, &rfds, NULL, NULL, &tv); if (FD_ISSET(0, &rfds)) return (1); #endif #endif return (0); } /* 85-110 */ /* ***** TIME_CHAR ***** */ int time_char (void) { /* 114-132 */ int j; clock_t iclock, jclock; iclock = jclock = clock (); while (jclock-iclock < CLOCKS_PER_SEC) { /* 120-124 */ j = kbhit (); if (j) break; jclock = clock (); } /* 120-124 */ 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); } /* 114-132 */ /* **** GETCH ***** */ int getch (void) { /* 136-487 */ int c, j; #if G2_WINAPI c = g_getch (); #else if (i1 != 0) { /* 143-147 */ c = i1, i1 = i2, i2 = i3, i3 = i4, i4 = 0; DPRINT ("\t\t\t\tNextchar.\n"); return (c); } /* 143-147 */ 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 == '[') { /* 173-399 */ DPRINT ("\t\t\t\tSpecial [\n"); i2 = time_char (); if (i2 == 0) return (c); if (i2 == 'A') { /* 179-182 */ i1 = i2 = 0; return (UP_ARROW); } /* 179-182 */ if (i2 == 'B') { /* 184-187 */ i1 = i2 = 0; return (DOWN_ARROW); } /* 184-187 */ if (i2 == 'C') { /* 189-192 */ i1 = i2 = 0; return (RIGHT_ARROW); } /* 189-192 */ if (i2 == 'D') { /* 194-197 */ i1 = i2 = 0; return (LEFT_ARROW); } /* 194-197 */ if (i2 == 'G') { /* 199-202 */ i1 = i2 = 0; return (CENTER); } /* 199-202 */ if (i2 == '1') { /* 204-256 */ DPRINT ("\t\t\t\tSpecial [ 1\n"); i3 = time_char (); if (i3 == 0) return (c); if (i3 == TWIDDLE) { /* 209-213 */ DPRINT ("\t\t\t\tSpecial [ 1 ~\n"); i1 = i2 = i3 = 0; return (HOME); } /* 209-213 */ if (i3 == CARROT) { /* 215-219 */ DPRINT ("\t\t\t\tSpecial [ 1 ^\n"); i1 = i2 = i3 = 0; return (CNTL_HOME); } /* 215-219 */ if (isdigit (i3) == 0) return (c); i4 = time_char (); if (i4 == 0) return (c); if (i4 == TWIDDLE) { /* 223-238 */ DPRINT ("\t\t\t\tSpecial [ 1 * ~\n"); switch (i3) { /* 226-237 */ 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); } /* 226-237 */ } /* 223-238 */ else if (i4 == CARROT) { /* 240-255 */ DPRINT ("\t\t\t\tSpecial [ 1 * ^\n"); switch (i3) { /* 243-254 */ 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); } /* 243-254 */ } /* 240-255 */ } /* 204-256 */ else if (i2 == '2') { /* 258-318 */ DPRINT ("\t\t\t\tSpecial [ 2\n"); i3 = time_char (); if (i3 == 0) return (c); if (i3 == TWIDDLE) { /* 263-266 */ i1 = i2 = i3 = 0; return (INSERT); } /* 263-266 */ if (i3 == CARROT) { /* 268-272 */ DPRINT ("\t\t\t\tSpecial [ 2 ^\n"); i1 = i2 = i3 = 0; return (CNTL_INSERT); } /* 268-272 */ if (isdigit (i3) == 0) return (c); i4 = time_char (); if (i4 == 0) return (c); if (i4 == TWIDDLE) { /* 276-291 */ DPRINT ("\t\t\t\tSpecial [ 2 * ~\n"); switch (i3) { /* 279-290 */ 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); } /* 279-290 */ } /* 276-291 */ else if (i4 == CARROT) { /* 293-304 */ DPRINT ("\t\t\t\tSpecial [ 2 * ^\n"); switch (i3) { /* 296-303 */ 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); } /* 296-303 */ } /* 293-304 */ else if (i4 == DOLLAR) { /* 306-317 */ DPRINT ("\t\t\t\tSpecial [ 2 * $\n"); switch (i3) { /* 309-316 */ 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); } /* 309-316 */ } /* 306-317 */ } /* 258-318 */ else if (i2 == '3') { /* 320-344 */ DPRINT ("\t\t\t\tSpecial [ 3\n"); i3 = time_char (); if (i3 == 0) return (c); if (i3 == TWIDDLE) { /* 325-328 */ i1 = i2 = i3 = 0; return (DEL); } /* 325-328 */ if (isdigit (i3) == 0) return (c); i4 = time_char (); if (i4 == 0) return (c); if (i4 == TWIDDLE) { /* 332-343 */ DPRINT ("\t\t\t\tSpecial [ 3 * ~\n"); switch (i3) { /* 335-342 */ 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); } /* 335-342 */ } /* 332-343 */ } /* 320-344 */ else if (i2 == '4') { /* 346-362 */ DPRINT ("\t\t\t\tSpecial [ 4\n"); i3 = time_char (); if (i3 == 0) return (c); if (i3 == TWIDDLE) { /* 351-355 */ DPRINT ("\t\t\t\tSpecial [ 4 ~\n"); i1 = i2 = i3 = 0; return (END); } /* 351-355 */ if (i3 == CARROT) { /* 357-361 */ DPRINT ("\t\t\t\tSpecial [ 2 ^\n"); i1 = i2 = i3 = 0; return (CNTL_END); } /* 357-361 */ } /* 346-362 */ else if (i2 == '5') { /* 364-380 */ DPRINT ("\t\t\t\tSpecial [ 5\n"); i3 = time_char (); if (i3 == 0) return (c); if (i3 == TWIDDLE) { /* 369-373 */ DPRINT ("\t\t\t\tSpecial [ 5 ~\n"); i1 = i2 = i3 = 0; return (PAGE_UP); } /* 369-373 */ if (i3 == CARROT) { /* 375-379 */ DPRINT ("\t\t\t\tSpecial [ 2 ^\n"); i1 = i2 = i3 = 0; return (CNTL_PGUP); } /* 375-379 */ } /* 364-380 */ else if (i2 == '6') { /* 382-398 */ DPRINT ("\t\t\t\tSpecial [ 6\n"); i3 = time_char (); if (i3 == 0) return (c); if (i3 == TWIDDLE) { /* 387-391 */ DPRINT ("\t\t\t\tSpecial [ 6 ~\n"); i1 = i2 = i3 = 0; return (PAGE_DOWN); } /* 387-391 */ if (i3 == CARROT) { /* 393-397 */ DPRINT ("\t\t\t\tSpecial [ 2 ^\n"); i1 = i2 = i3 = 0; return (CNTL_PGDN); } /* 393-397 */ } /* 382-398 */ } /* 173-399 */ /* Capital O case. */ else if (i1 == 'O') { /* 403-483 */ DPRINT ("\t\t\t\tSpecial O\n"); i2 = time_char (); if (i2 == 0) return (c); if (i2 == 'a') { /* 409-412 */ i1 = i2 = 0; return (CNTL_UP); } /* 409-412 */ if (i2 == 'b') { /* 414-417 */ i1 = i2 = 0; return (CNTL_DOWN); } /* 414-417 */ if (i2 == 'c') { /* 419-422 */ i1 = i2 = 0; return (CNTL_RIGHT); } /* 419-422 */ if (i2 == 'd') { /* 424-427 */ i1 = i2 = 0; return (CNTL_LEFT); } /* 424-427 */ if (i2 == 'n') { /* 429-432 */ i1 = i2 = 0; return (DEL); } /* 429-432 */ if (i2 == 'p') { /* 434-437 */ i1 = i2 = 0; return (INSERT); } /* 434-437 */ if (i2 == 'q') { /* 439-442 */ i1 = i2 = 0; return (END); } /* 439-442 */ if (i2 == 'r') { /* 444-447 */ i1 = i2 = 0; return (DOWN_ARROW); } /* 444-447 */ if (i2 == 's') { /* 449-452 */ i1 = i2 = 0; return (PAGE_DOWN); } /* 449-452 */ if (i2 == 't') { /* 454-457 */ i1 = i2 = 0; return (LEFT_ARROW); } /* 454-457 */ if (i2 == 'u') { /* 459-462 */ i1 = i2 = 0; return (CENTER); } /* 459-462 */ if (i2 == 'v') { /* 464-467 */ i1 = i2 = 0; return (RIGHT_ARROW); } /* 464-467 */ if (i2 == 'w') { /* 469-472 */ i1 = i2 = 0; return (HOME); } /* 469-472 */ if (i2 == 'x') { /* 474-477 */ i1 = i2 = 0; return (UP_ARROW); } /* 474-477 */ if (i2 == 'y') { /* 479-482 */ i1 = i2 = 0; return (PAGE_UP); } /* 479-482 */ } /* 403-483 */ #endif return (c); } /* 136-487 */ /* ***** MAIN0 ***** */ int main () { /* 491-566 */ 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) { /* 501-563 */ c = getch (), printf ("The character is "); if (c > 31) printf ("%d (%c).\n", c, c); else if (c >= 0) printf ("%d.\n", c); else { /* 506-538 */ 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"); } /* 506-538 */ if (c == 'k') { /* 540-561 */ itime = time (NULL), iclock = clock (); printf ("Waiting . . ."), fflush (stdout); for (c = 1; c <= 100000; c++) { /* 544-547 */ j = kbhit (); if (j) break; } /* 544-547 */ printf ("\n"); jtime = time (NULL), jclock = clock ();; printf ("Waiting . . ."), fflush (stdout); for (c = 1; c <= 100000; c++) { /* 552-556 */ j = kbhit (); j = kbhit (); if (j) break; } /* 552-556 */ 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); } /* 540-561 */ if (c == 'q') break; } /* 501-563 */ raw_mode (0); return (0); } /* 491-566 */