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 /* Struct to store key sequences */
\r
92 int lbr ; /* For left-bracket ([) of escape sequences */
\r
103 printf("Public Domain Readline \n");
\r
105 /* Buffer - an array of keyseq structs. */
\r
106 /* Note - now that we store the keystrokes in here, */
\r
107 /* we can look at the various flags and decide whether to */
\r
108 /* "echo" the key (as normal) or suppress it (as with an */
\r
110 keyseq buffer[80] ;
\r
112 /* Buffer "pointer" */
\r
116 /* Test for existence of history file. */
\r
118 exists = fexists(".history");
\r
119 printf("Result: %d \n", exists);
\r
124 int key = getch();
\r
126 /* Printable chars. */
\r
127 if ( (key >= 32) && (key <= 126) )
\r
129 /* We have a printable key so print it. */
\r
131 buffer[bufpnt].key = key;
\r
135 /* Up arrow is 27, 91, 65. ( ESC [ A ) */
\r
136 /* Down arrow is 27, 91, 66. ( ESC [ B ) */
\r
137 /* Right arrow is 27, 91, 67. ( ESC [ C ) */
\r
138 /* Left arrow is 27, 91, 68. ( ESC [ D ) */
\r
139 /* Function keys. */
\r
140 /* F2 is 27, 79, 81. */
\r
141 /* F3 is 27, 79, 82. */
\r
142 /* F4 is 27, 79, 83. */
\r
145 else if(key == 127)
\r
147 buffer[bufpnt].key = key;
\r
148 /* Move 1 char to left */
\r
151 printf("\033[1D");
\r
155 else if(key == 27)
\r
157 buffer[bufpnt].esc = 1;
\r
160 buffer[bufpnt].lbr = 1;
\r
162 if(key == 65) /* Up arrow */
\r
164 buffer[bufpnt].key = key;
\r
166 if(key == 66) /* Down arrow */
\r
168 buffer[bufpnt].key = key;
\r
170 if(key == 67) /* Left arrow */
\r
172 buffer[bufpnt].key = key;
\r
173 printf("\033[1C");
\r
175 if(key == 68) /* Right arrow */
\r
177 buffer[bufpnt].key = key;
\r
178 printf("\033[1D");
\r
186 /* The Enter key exits. Enter is 10 decimal */
\r
187 else if(key == 10)
\r
191 /* Print the array of structs. */
\r
192 for (j=0; j<bufpnt; j++)
\r
194 printf("Fnkey: %d ", buffer[j].fnkey ) ;
\r
195 printf("Ctrl: %d ", buffer[j].ctrl ) ;
\r
196 printf("Alt: %d ", buffer[j].alt ) ;
\r
197 printf("Shf: %d ", buffer[j].shf ) ;
\r
198 printf("Esc: %d ", buffer[j].esc ) ;
\r
199 printf("Lbr: %d ", buffer[j].lbr ) ;
\r
200 printf("Key: %d \n", buffer[j].key ) ;
\r
204 } /* Key = Enter */
\r