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
8 /* It will still be some time before this is a REAL */
\r
9 /* readline, but we are "on the way"...... */
\r
10 /* This code is released to the public domain. */
\r
11 /* "Share and enjoy...." ;) */
\r
14 #include <string.h>
\r
18 /* Helper function, to let us see if a .history file */
\r
19 /* exists in the current directory. */
\r
20 int fexists(char *fname)
\r
25 fptr = fopen(fname, "r") ;
\r
27 if ( !fptr ) return -1 ; /* File does not exist in dir. */
\r
30 return 0; /* File DOES exist in dir. */
\r
35 /* Struct to store key sequences */
\r
42 int lbr ; /* For left-bracket ([) of escape sequences */
\r
47 /* A custom getchar to test the character type. */
\r
48 int mygetchar(void)
\r
50 int ch = getchar();
\r
51 if ( (ch >= 32) && (ch <= 126) )
\r
68 printf("Public Domain Readline \n");
\r
69 printf("NOTE! - at the moment, we are using \n");
\r
70 printf("NON-echoing reads, storing the keystrokes \n");
\r
71 printf("in a buffer \n");
\r
74 /* Buffer - an array of keyseq structs. */
\r
75 /* Note - now that we store the keystrokes in here, */
\r
76 /* we can look at the various flags and decide whether to */
\r
77 /* "echo" the key (as normal) or suppress it (as with an */
\r
79 keyseq buffer[80] ;
\r
81 /* Buffer "pointer" */
\r
85 /* Test for existence of history file. */
\r
87 exists = fexists(".history");
\r
88 printf("Result: %d \n", exists);
\r
93 int key = mygetchar();
\r
95 /* Printable chars. */
\r
96 if ( (key >= 32) && (key <= 126) )
\r
99 buffer[bufpnt].key = key;
\r
103 /* Up arrow is 27, 91, 65. ( ESC [ A ) */
\r
104 /* Down arrow is 27, 91, 66. ( ESC [ B ) */
\r
105 /* Right arrow is 27, 91, 67. ( ESC [ C ) */
\r
106 /* Left arrow is 27, 91, 68. ( ESC [ D ) */
\r
107 /* Function keys. */
\r
108 /* F2 is 27, 79, 81. */
\r
109 /* F3 is 27, 79, 82. */
\r
110 /* F4 is 27, 79, 83. */
\r
113 else if(key == 27)
\r
115 buffer[bufpnt].esc = 1;
\r
116 key = mygetchar();
\r
118 buffer[bufpnt].lbr = 1;
\r
119 key = mygetchar();
\r
120 if( (key >= 65) && (key <= 68) )
\r
122 buffer[bufpnt].key = key;
\r
129 /* The Enter key exits. Enter is 10 decimal */
\r
130 else if(key == 10)
\r
133 /* Print the array of structs. */
\r
134 for (j=0; j<10; j++)
\r
136 printf("Fnkey: %d ", buffer[j].fnkey ) ;
\r
137 printf("Ctrl: %d ", buffer[j].ctrl ) ;
\r
138 printf("Alt: %d ", buffer[j].alt ) ;
\r
139 printf("Shf: %d ", buffer[j].shf ) ;
\r
140 printf("Esc: %d ", buffer[j].esc ) ;
\r
141 printf("Lbr: %d ", buffer[j].lbr ) ;
\r
142 printf("Key: %d \n", buffer[j].key ) ;
\r
146 } /* Key = Enter */
\r