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. */
13 /* a) Fix the "stack smash" problem. */
14 /* b) Put much of the code into a header file. */
15 /* c) Change so that pressing Enter adds the current */
16 /* line of commands to the command-history. */
17 /* ( May look at using Ctrl-D to exit, as Python */
18 /* does with its command-line. ) */
19 /* d) Add support for copying and pasting text via */
20 /* Ctrl-C and Ctrl-V. */
27 /* This implementation of getch() is from here - */
28 /* http://wesley.vidiqatch.org/ */
30 static struct termios old, new;
32 /* Initialize new terminal i/o settings */
33 void initTermios(int echo) {
34 tcgetattr(0, &old); /* grab old terminal i/o settings */
35 new = old; /* make new settings same as old settings */
36 new.c_lflag &= ~ICANON; /* disable buffered i/o */
37 new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
38 tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
42 /* Restore old terminal i/o settings */
43 void resetTermios(void) {
44 tcsetattr(0, TCSANOW, &old);
48 /* Read 1 character - echo defines echo mode */
49 char getch_(int echo) {
58 /* Read 1 character without echo */
64 /* Read 1 character with echo */
71 /* Helper function, to let us see if a file */
72 /* exists in the current directory. */
73 int fexists(char *fname)
76 fptr = fopen(fname, "r") ;
77 if ( !fptr ) return -1 ; /* File does not exist in dir. */
79 return 0; /* File DOES exist in dir. */
83 /* Helper function to chop newlines off the lines read in. */
84 /* Without this being done, an extra newline is inserted */
85 /* (which is usually not what is wanted). */
88 s[strcspn(s,"\n")] = '\0';
93 /* An array to store the command-history file in. */
94 /* Only 20 lines are read. */
98 /* Read the file into the array of strings. */
99 void readfile(char *fname)
101 int retval = fexists(fname);
105 /* File exists, so open it. */
106 /* We open it in read-write mode so we can */
107 /* append new commands to it. */
109 fptr = fopen(fname, "rw");
113 chop(fgets(hist[i], 80, fptr) );
118 else puts("Error! File does not exist. \n");
123 /* Helper function to print the command-history file. */
137 /* Helper function. Print a previous command-line WITHOUT */
139 void putline(char *str)
141 char *line = chop(str);
151 /* Our "command-history" file. */
152 readfile("test.txt");
154 /* Main buffer for the command-line. */
157 /* Main buffer "pointer" */
160 /* "Pointer" for the history file. */
168 buffer[bufpnt] = key;
171 /* Printable chars. */
172 if ( (key >= 32) && (key <= 126) )
174 /* We have a printable key so print it. */
179 /* Up arrow is 27, 91, 65. ( ESC [ A ) */
180 /* Down arrow is 27, 91, 66. ( ESC [ B ) */
181 /* Right arrow is 27, 91, 67. ( ESC [ C ) */
182 /* Left arrow is 27, 91, 68. ( ESC [ D ) */
184 /* F2 is 27, 79, 81. */
185 /* F3 is 27, 79, 82. */
186 /* F4 is 27, 79, 83. */
191 /* Move left 1 char and delete that char */
196 /* Move 1 char to left again */
200 /* We have an escape key-sequence */
207 if (key == 65) /* Up Arrow */
209 /* Move one command "back" in history. */
211 /* Clear to end of line. */
214 /* Move buffer pointer to start of line */
216 /* Clear the array. */
217 memset(buffer, 0, sizeof(char)*80);
218 /* Print the pointed-at command-sequence. */
219 putline(hist[histpnt]);
222 if(key == 66) /* Down Arrow */
224 /* Move one command "forward" in history. */
226 /* Clear to end of line. */
229 /* Move buffer pointer to start of line */
231 /* Clear the array. */
232 memset(buffer, 0, sizeof(char)*80);
233 /* Print the pointed-at command-sequence. */
234 putline(hist[histpnt]);
237 if(key == 67) /* Left arrow */
239 /* Move one character to the left. */
243 if(key == 68) /* Right arrow */
245 /* Move one character to the right. */
249 } /* End of key=27 key sequence. */
252 /* The Enter key exits. Enter is 10 decimal */
256 puts("Exiting... \n");