4 /* Some code to allow the editing of a command-line. */
5 /* You can also move around with the left and right */
6 /* arrow keys, and recall previous commands with the */
8 /* This code is released to the public domain. */
9 /* "Share and enjoy...." ;) */
10 /* See the UNLICENSE file for details. */
17 /* This implementation of getch() is from here - */
18 /* http://wesley.vidiqatch.org/ */
20 static struct termios old, new;
22 /* Initialize new terminal i/o settings */
23 void initTermios(int echo) {
24 tcgetattr(0, &old); /* grab old terminal i/o settings */
25 new = old; /* make new settings same as old settings */
26 new.c_lflag &= ~ICANON; /* disable buffered i/o */
27 new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
28 tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
32 /* Restore old terminal i/o settings */
33 void resetTermios(void) {
34 tcsetattr(0, TCSANOW, &old);
38 /* Read 1 character - echo defines echo mode */
39 char getch_(int echo) {
48 /* Read 1 character without echo */
54 /* Read 1 character with echo */
61 /* Helper function, to let us see if a file */
62 /* exists in the current directory. */
63 int fexists(char *fname)
66 fptr = fopen(fname, "r") ;
67 if ( !fptr ) return -1 ; /* File does not exist in dir. */
69 return 0; /* File DOES exist in dir. */
73 /* Helper function to chop newlines off the lines read in. */
74 /* Without this being done, an extra newline is inserted */
75 /* (which is usually not what is wanted). */
78 s[strcspn(s,"\n")] = '\0';
83 /* An array to store the command-history file in. */
84 /* Only 20 lines are read. */
88 /* Read the file into the array of strings. */
89 void readfile(char *fname)
91 int retval = fexists(fname);
95 /* File exists, so open it. */
96 /* We open it in read-write mode so we can */
97 /* append new commands to it. */
99 fptr = fopen(fname, "rw");
103 chop(fgets(hist[i], 80, fptr) );
108 else puts("Error! File does not exist. \n");
113 /* Helper function to print the command-history file. */
127 /* Helper function. Print a previous command-line WITHOUT */
129 void putline(char *str)
131 char *line = chop(str);
141 /* Our "command-history" file. */
142 readfile("test.txt");
144 /* Main buffer for the command-line. */
147 /* Main buffer "pointer" */
150 /* "Pointer" for the history file. */
158 buffer[bufpnt] = key;
161 /* Printable chars. */
162 if ( (key >= 32) && (key <= 126) )
164 /* We have a printable key so print it. */
169 /* Up arrow is 27, 91, 65. ( ESC [ A ) */
170 /* Down arrow is 27, 91, 66. ( ESC [ B ) */
171 /* Right arrow is 27, 91, 67. ( ESC [ C ) */
172 /* Left arrow is 27, 91, 68. ( ESC [ D ) */
174 /* F2 is 27, 79, 81. */
175 /* F3 is 27, 79, 82. */
176 /* F4 is 27, 79, 83. */
181 /* Move left 1 char and delete that char */
186 /* Move 1 char to left again */
190 /* We have an escape key-sequence */
197 if (key == 65) /* Up Arrow */
199 /* Move one command "back" in history. */
201 /* Clear to end of line. */
204 /* Move buffer pointer to start of line */
206 /* Clear the array. */
207 memset(buffer, 0, sizeof(char)*80);
208 /* Print the pointed-at command-sequence. */
209 putline(hist[histpnt]);
212 if(key == 66) /* Down Arrow */
214 /* Move one command "forward" in history. */
216 /* Clear to end of line. */
219 /* Move buffer pointer to start of line */
221 /* Clear the array. */
222 memset(buffer, 0, sizeof(char)*80);
223 /* Print the pointed-at command-sequence. */
224 putline(hist[histpnt]);
227 if(key == 67) /* Left arrow */
229 /* Move one character to the left. */
233 if(key == 68) /* Right arrow */
235 /* Move one character to the right. */
239 } /* End of key=27 key sequence. */
242 /* The Enter key exits. Enter is 10 decimal */
246 puts("Exiting... \n");