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) Add support for Home and End (of line) keys. */
14 /* b) Add support for function keys. */
15 /* c) Add support for Insert key so that text can be */
17 /* d) Put much of the code into a header file. */
18 /* e) Change so that pressing Enter adds the current */
19 /* line of commands to the command-history. */
20 /* ( May look at using Ctrl-D to exit, as Python */
21 /* does with its command-line. ) */
22 /* f) Add support for copying and pasting text via */
23 /* Ctrl-C and Ctrl-V. */
30 /* This implementation of getch() is from here - */
31 /* http://wesley.vidiqatch.org/ */
33 static struct termios old, new;
35 /* Initialize new terminal i/o settings */
36 void initTermios(int echo) {
37 tcgetattr(0, &old); /* grab old terminal i/o settings */
38 new = old; /* make new settings same as old settings */
39 new.c_lflag &= ~ICANON; /* disable buffered i/o */
40 new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
41 tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
45 /* Restore old terminal i/o settings */
46 void resetTermios(void) {
47 tcsetattr(0, TCSANOW, &old);
51 /* Read 1 character - echo defines echo mode */
52 char getch_(int echo) {
61 /* Read 1 character without echo */
67 /* Read 1 character with echo */
74 /* Helper function, to let us see if a file */
75 /* exists in the current directory. */
76 int fexists(char *fname)
79 fptr = fopen(fname, "r") ;
80 if ( !fptr ) return -1 ; /* File does not exist in dir. */
82 return 0; /* File DOES exist in dir. */
86 /* Helper function to chop newlines off the lines read in. */
87 /* Without this being done, an extra newline is inserted */
88 /* (which is usually not what is wanted). */
91 s[strcspn(s,"\n")] = '\0';
96 /* An array to store the command-history file in. */
97 /* Only 20 lines are read. */
102 /* Read the file into the array of strings. */
103 void readfile(char *fname)
105 int retval = fexists(fname);
109 /* File exists, so open it. */
110 /* We open it in read-write mode so we can */
111 /* append new commands to it. */
113 fptr = fopen(fname, "rw");
117 chop(fgets(hist[i], 80, fptr) );
122 else puts("Error! File does not exist. \n");
127 /* Helper function to print the command-history file. */
141 /* Helper function. Print a previous command-line WITHOUT */
143 void putline(char *str)
145 char *line = chop(str);
155 /* Our "command-history" file. */
156 readfile("test.txt");
159 /* "Pointer" for the history file. */
168 /* Printable chars. */
169 if ( (key >= 32) && (key <= 126) )
171 /* We have a printable key so print it. */
175 /* Up arrow is 27, 91, 65. ( ESC [ A ) */
176 /* Down arrow is 27, 91, 66. ( ESC [ B ) */
177 /* Right arrow is 27, 91, 67. ( ESC [ C ) */
178 /* Left arrow is 27, 91, 68. ( ESC [ D ) */
180 /* F2 is 27, 79, 81. */
181 /* F3 is 27, 79, 82. */
182 /* F4 is 27, 79, 83. */
187 /* Move left 1 char and delete that char */
193 /* We have an escape key-sequence */
200 if (key == 65) /* Up Arrow */
202 /* Move one command "back" in history. */
204 /* Clear to end of line. */
207 /* Print the pointed-at command-sequence. */
208 putline(hist[histpnt]);
211 if(key == 66) /* Down Arrow */
213 /* Move one command "forward" in history. */
215 /* Clear to end of line. */
218 /* Print the pointed-at command-sequence. */
219 putline(hist[histpnt]);
222 if(key == 67) /* Left arrow */
224 /* Move one character to the left. */
228 if(key == 68) /* Right arrow */
230 /* Move one character to the right. */
234 } /* End of key=27 key sequence. */
237 /* The Enter key exits. Enter is 10 decimal */
241 puts("Exiting... \n");