3 /* pd_readline2.c */
\r
4 /* Status (as at 26th Aug 2012) : useful progress. */
\r
5 /* Keystroke sequences (along with the special flags */
\r
6 /* like Esc, Ctrl, Alt etc are now stored in a buffer */
\r
7 /* ( an array of structs ). */
\r
9 /* The code can now distinguish between a printable key */
\r
10 /* and a key that should not be printed. */
\r
12 /* Backspace key, and left and right arrow keys now work. */
\r
14 /* It will still be some time before this is a REAL */
\r
15 /* readline, but we are "on the way"...... */
\r
16 /* This code is released to the public domain. */
\r
17 /* "Share and enjoy...." ;) */
\r
20 #include <string.h>
\r
22 #include <termios.h> /* For getch() */
\r
24 /* This implementation of getch() is from here - */
\r
25 /* http://wesley.vidiqatch.org/ */
\r
26 /* Thanks, Wesley! */
\r
27 static struct termios old, new;
\r
29 /* Initialize new terminal i/o settings */
\r
30 void initTermios(int echo) {
\r
31 tcgetattr(0, &old); /* grab old terminal i/o settings */
\r
32 new = old; /* make new settings same as old settings */
\r
33 new.c_lflag &= ~ICANON; /* disable buffered i/o */
\r
34 new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
\r
35 tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
\r
39 /* Restore old terminal i/o settings */
\r
40 void resetTermios(void) {
\r
41 tcsetattr(0, TCSANOW, &old);
\r
45 /* Read 1 character - echo defines echo mode */
\r
46 char getch_(int echo) {
\r
55 /* Read 1 character without echo */
\r
61 /* Read 1 character with echo */
\r
68 /* Helper function, to let us see if a .history file */
\r
69 /* exists in the current directory. */
\r
70 int fexists(char *fname)
\r
75 fptr = fopen(fname, "r") ;
\r
77 if ( !fptr ) return -1 ; /* File does not exist in dir. */
\r
80 return 0; /* File DOES exist in dir. */
\r
85 /* Helper function to print escape sequences to terminal */
\r
86 void prtseq(char *what)
\r
88 if (strcmp(what, "left") ) printf("\033[1D");
\r
89 else if (strcmp(what, "right") ) printf("\033[1C");
\r
90 else if (strcmp(what, "bs") )
\r
99 /* Struct to store key sequences */
\r
106 int lbr ; /* For left-bracket ([) of escape sequences */
\r
117 printf("Public Domain Readline \n");
\r
119 /* Buffer - an array of keyseq structs. */
\r
120 /* Note - now that we store the keystrokes in here, */
\r
121 /* we can look at the various flags and decide whether to */
\r
122 /* "echo" the key (as normal) or suppress it (as with an */
\r
124 keyseq buffer[80] ;
\r
126 /* Buffer "pointer" */
\r
130 /* Test for existence of history file. */
\r
132 exists = fexists(".history");
\r
133 printf("Result: %d \n", exists);
\r
138 int key = getch();
\r
140 /* Printable chars. */
\r
141 if ( (key >= 32) && (key <= 126) )
\r
143 /* We have a printable key so print it. */
\r
145 buffer[bufpnt].key = key;
\r
149 /* Up arrow is 27, 91, 65. ( ESC [ A ) */
\r
150 /* Down arrow is 27, 91, 66. ( ESC [ B ) */
\r
151 /* Right arrow is 27, 91, 67. ( ESC [ C ) */
\r
152 /* Left arrow is 27, 91, 68. ( ESC [ D ) */
\r
153 /* Function keys. */
\r
154 /* F2 is 27, 79, 81. */
\r
155 /* F3 is 27, 79, 82. */
\r
156 /* F4 is 27, 79, 83. */
\r
159 else if(key == 127)
\r
161 buffer[bufpnt].key = key;
\r
162 /* Move 1 char to left */
\r
167 else if(key == 27)
\r
169 buffer[bufpnt].esc = 1;
\r
172 buffer[bufpnt].lbr = 1;
\r
174 if(key == 65) /* Up arrow */
\r
176 buffer[bufpnt].key = key;
\r
178 if(key == 66) /* Down arrow */
\r
180 buffer[bufpnt].key = key;
\r
182 if(key == 67) /* Left arrow */
\r
184 buffer[bufpnt].key = key;
\r
187 if(key == 68) /* Right arrow */
\r
189 buffer[bufpnt].key = key;
\r
198 /* The Enter key exits. Enter is 10 decimal */
\r
199 else if(key == 10)
\r
203 /* Print the array of structs. */
\r
204 for (j=0; j<bufpnt; j++)
\r
206 printf("Fnkey: %d ", buffer[j].fnkey ) ;
\r
207 printf("Ctrl: %d ", buffer[j].ctrl ) ;
\r
208 printf("Alt: %d ", buffer[j].alt ) ;
\r
209 printf("Shf: %d ", buffer[j].shf ) ;
\r
210 printf("Esc: %d ", buffer[j].esc ) ;
\r
211 printf("Lbr: %d ", buffer[j].lbr ) ;
\r
212 printf("Key: %d \n", buffer[j].key ) ;
\r
216 } /* Key = Enter */
\r