5 /* Some code to allow the editing of a command-line. */
6 /* You can also move around with the left and right */
7 /* arrow keys, and recall previous commands with the */
9 /* This code is released to the public domain. */
10 /* "Share and enjoy...." ;) */
11 /* See the UNLICENSE file for details. */
14 /* a) Add support for Home and End (of line) keys. */
15 /* b) Add support for function keys. */
16 /* c) Add support for Insert key so that text can be */
18 /* d) Put much of the code into a header file. */
19 /* e) Change so that pressing Enter adds the current */
20 /* line of commands to the command-history. */
21 /* ( May look at using Ctrl-D to exit, as Python */
22 /* does with its command-line. ) */
23 /* f) Add support for copying and pasting text via */
24 /* Ctrl-C and Ctrl-V. */
31 /* This implementation of getch() is from here - */
32 /* http://wesley.vidiqatch.org/ */
34 static struct termios old, new;
36 /* Initialize new terminal i/o settings */
37 void initTermios(int echo) {
38 tcgetattr(0, &old); /* grab old terminal i/o settings */
39 new = old; /* make new settings same as old settings */
40 new.c_lflag &= ~ICANON; /* disable buffered i/o */
41 new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
42 tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
46 /* Restore old terminal i/o settings */
47 void resetTermios(void) {
48 tcsetattr(0, TCSANOW, &old);
52 /* Read 1 character - echo defines echo mode */
53 char getch_(int echo) {
62 /* Read 1 character without echo */
68 /* Read 1 character with echo */
75 /* Helper function, to let us see if a file */
76 /* exists in the current directory. */
77 int fexists(char *fname)
80 fptr = fopen(fname, "r") ;
81 if ( !fptr ) return -1 ; /* File does not exist in dir. */
83 return 0; /* File DOES exist in dir. */
87 /* Helper function to chop newlines off the lines read in. */
88 /* Without this being done, an extra newline is inserted */
89 /* (which is usually not what is wanted). */
92 s[strcspn(s,"\n")] = '\0';
97 /* An array to store the command-history file in. */
98 /* Only 20 lines are read. */
103 /* Read the file into the array of strings. */
104 void readfile(char *fname)
106 int retval = fexists(fname);
110 /* File exists, so open it. */
111 /* We open it in read-write mode so we can */
112 /* append new commands to it. */
114 fptr = fopen(fname, "rw");
118 chop(fgets(hist[i], 80, fptr) );
123 else puts("Error! File does not exist. \n");
128 /* Helper function to print the command-history file. */
142 /* Helper function. Print a previous command-line WITHOUT */
144 void putline(char *str)
146 char *line = chop(str);
156 /* Our "command-history" file. */
157 readfile("test.txt");
160 /* "Pointer" for the history file. */
169 /* Printable chars. */
170 if ( (key >= 32) && (key <= 126) )
172 /* We have a printable key so print it. */
176 /* Up arrow is 27, 91, 65. ( ESC [ A ) */
177 /* Down arrow is 27, 91, 66. ( ESC [ B ) */
178 /* Right arrow is 27, 91, 67. ( ESC [ C ) */
179 /* Left arrow is 27, 91, 68. ( ESC [ D ) */
181 /* F2 is 27, 79, 81. */
182 /* F3 is 27, 79, 82. */
183 /* F4 is 27, 79, 83. */
188 /* Move left 1 char and delete that char */
194 /* We have an escape key-sequence */
201 if (key == 65) /* Up Arrow */
203 /* Move one command "back" in history. */
205 /* Clear to end of line. */
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 /* Print the pointed-at command-sequence. */
220 putline(hist[histpnt]);
223 if(key == 67) /* Left arrow */
225 /* Move one character to the left. */
229 if(key == 68) /* Right arrow */
231 /* Move one character to the right. */
235 } /* End of key=27 key sequence. */
238 /* The Enter key exits. Enter is 10 decimal */
242 puts("Exiting... \n");