Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 2 | /* |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 3 | * Termios command line History and Editting, originally |
| 4 | * intended for NetBSD sh (ash) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 5 | * Copyright (c) 1999 |
| 6 | * Main code: Adam Rogoyski <rogoyski@cs.utexas.edu> |
| 7 | * Etc: Dave Cinege <dcinege@psychosis.com> |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 8 | * Majorly adjusted/re-written for busybox: |
| 9 | * Erik Andersen <andersee@debian.org> |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 10 | * |
| 11 | * You may use this code as you wish, so long as the original author(s) |
| 12 | * are attributed in any redistributions of the source code. |
| 13 | * This code is 'as is' with no warranty. |
| 14 | * This code may safely be consumed by a BSD or GPL license. |
| 15 | * |
| 16 | * v 0.5 19990328 Initial release |
| 17 | * |
| 18 | * Future plans: Simple file and path name completion. (like BASH) |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | Usage and Known bugs: |
| 24 | Terminal key codes are not extensive, and more will probably |
| 25 | need to be added. This version was created on Debian GNU/Linux 2.x. |
| 26 | Delete, Backspace, Home, End, and the arrow keys were tested |
| 27 | to work in an Xterm and console. Ctrl-A also works as Home. |
| 28 | Ctrl-E also works as End. The binary size increase is <3K. |
| 29 | |
| 30 | Editting will not display correctly for lines greater then the |
| 31 | terminal width. (more then one line.) However, history will. |
| 32 | */ |
| 33 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 34 | #include "busybox.h" |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 35 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
| 36 | |
| 37 | #include <stdio.h> |
| 38 | #include <errno.h> |
| 39 | #include <unistd.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 42 | #include <sys/ioctl.h> |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 43 | #include <ctype.h> |
| 44 | #include <signal.h> |
| 45 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 46 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 47 | #define MAX_HISTORY 15 /* Maximum length of the linked list for the command line history */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 48 | |
| 49 | #define ESC 27 |
| 50 | #define DEL 127 |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 51 | #define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0) |
| 52 | #define whitespace(c) (((c) == ' ') || ((c) == '\t')) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 53 | |
| 54 | static struct history *his_front = NULL; /* First element in command line list */ |
| 55 | static struct history *his_end = NULL; /* Last element in command line list */ |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 56 | |
| 57 | /* ED: sparc termios is broken: revert back to old termio handling. */ |
| 58 | #ifdef BB_FEATURE_USE_TERMIOS |
| 59 | |
| 60 | #if #cpu(sparc) |
| 61 | # include <termio.h> |
| 62 | # define termios termio |
| 63 | # define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp) |
| 64 | # define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp) |
| 65 | #else |
| 66 | # include <termios.h> |
| 67 | # define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) |
| 68 | # define getTermSettings(fd,argp) tcgetattr(fd, argp); |
| 69 | #endif |
| 70 | |
| 71 | /* Current termio and the previous termio before starting sh */ |
Eric Andersen | 63a8622 | 2000-11-07 06:52:13 +0000 | [diff] [blame] | 72 | static struct termios initial_settings, new_settings; |
Erik Andersen | 8ea7d8c | 2000-05-20 00:40:08 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | #ifndef _POSIX_VDISABLE |
| 76 | #define _POSIX_VDISABLE '\0' |
| 77 | #endif |
| 78 | |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 82 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 83 | static int cmdedit_termw = 80; /* actual terminal width */ |
| 84 | static int cmdedit_scroll = 27; /* width of EOL scrolling region */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 85 | static int history_counter = 0; /* Number of commands in history list */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 86 | static int reset_term = 0; /* Set to true if the terminal needs to be reset upon exit */ |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 87 | static int exithandler_set = 0; /* Set to true when atexit() has been called */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | /* Link into lash to reset context to 0 |
| 91 | * on ^C and such */ |
| 92 | extern unsigned int shell_context; |
| 93 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 94 | |
| 95 | struct history { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 96 | char *s; |
| 97 | struct history *p; |
| 98 | struct history *n; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 101 | #define xwrite write |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 102 | |
Mark Whitley | 5538070 | 2000-07-13 17:20:23 +0000 | [diff] [blame] | 103 | /* |
| 104 | * TODO: Someday we want to implement 'horizontal scrolling' of the |
| 105 | * command-line when the user has typed more than the current width. This |
| 106 | * would allow the user to see a 'window' of what he has typed. |
| 107 | */ |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 108 | static void cmdedit_setwidth(int w) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 109 | { |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 110 | if (w > 20) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 111 | cmdedit_termw = w; |
| 112 | cmdedit_scroll = w / 3; |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 113 | } else { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 114 | error_msg("\n*** Error: minimum screen width is 21\n"); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 115 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 118 | static void win_changed(int junk) |
| 119 | { |
| 120 | struct winsize win = { 0, 0, 0, 0 }; |
| 121 | ioctl(0, TIOCGWINSZ, &win); |
| 122 | if (win.ws_col > 0) { |
| 123 | cmdedit_setwidth( win.ws_col - 1); |
| 124 | } |
| 125 | } |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 126 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 127 | |
| 128 | static void cmdedit_reset_term(void) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 129 | { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 130 | if (reset_term) |
Erik Andersen | a6c7522 | 2000-04-18 00:00:52 +0000 | [diff] [blame] | 131 | /* sparc and other have broken termios support: use old termio handling. */ |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 132 | setTermSettings(fileno(stdin), (void*) &initial_settings); |
Eric Andersen | b040d4f | 2000-07-25 18:01:20 +0000 | [diff] [blame] | 133 | #ifdef BB_FEATURE_CLEAN_UP |
| 134 | if (his_front) { |
| 135 | struct history *n; |
| 136 | //while(his_front!=his_end) { |
| 137 | while(his_front!=his_end) { |
| 138 | n = his_front->n; |
| 139 | free(his_front->s); |
| 140 | free(his_front); |
| 141 | his_front=n; |
| 142 | } |
| 143 | } |
| 144 | #endif |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 147 | static void clean_up_and_die(int sig) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 148 | { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 149 | cmdedit_reset_term(); |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame^] | 150 | printf("\n"); |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 151 | if (sig!=SIGINT) |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 152 | exit(EXIT_SUCCESS); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 155 | /* Go to HOME position */ |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 156 | static void input_home(int outputFd, int *cursor) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 157 | { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 158 | while (*cursor > 0) { |
| 159 | xwrite(outputFd, "\b", 1); |
| 160 | --*cursor; |
| 161 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 164 | /* Go to END position */ |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 165 | static void input_end(int outputFd, int *cursor, int len) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 166 | { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 167 | while (*cursor < len) { |
| 168 | xwrite(outputFd, "\033[C", 3); |
| 169 | ++*cursor; |
| 170 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 173 | /* Delete the char in back of the cursor */ |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 174 | static void input_backspace(char* command, int outputFd, int *cursor, int *len) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 175 | { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 176 | int j = 0; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 177 | |
Eric Andersen | 72965e3 | 2000-07-04 06:22:18 +0000 | [diff] [blame] | 178 | /* Debug crap */ |
| 179 | //fprintf(stderr, "\nerik: len=%d, cursor=%d, strlen(command)='%d'\n", *len, *cursor, strlen(command)); |
| 180 | //xwrite(outputFd, command, *len); |
| 181 | //*cursor = *len; |
| 182 | |
| 183 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 184 | if (*cursor > 0) { |
| 185 | xwrite(outputFd, "\b \b", 3); |
| 186 | --*cursor; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 187 | memmove(command + *cursor, command + *cursor + 1, |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 188 | BUFSIZ - *cursor + 1); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 189 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 190 | for (j = *cursor; j < (BUFSIZ - 1); j++) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 191 | if (!*(command + j)) |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 192 | break; |
| 193 | else |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 194 | xwrite(outputFd, (command + j), 1); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | xwrite(outputFd, " \b", 2); |
| 198 | |
| 199 | while (j-- > *cursor) |
| 200 | xwrite(outputFd, "\b", 1); |
| 201 | |
| 202 | --*len; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 203 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 206 | /* Delete the char in front of the cursor */ |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 207 | static void input_delete(char* command, int outputFd, int cursor, int *len) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 208 | { |
| 209 | int j = 0; |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 210 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 211 | if (cursor == *len) |
| 212 | return; |
| 213 | |
| 214 | memmove(command + cursor, command + cursor + 1, |
| 215 | BUFSIZ - cursor - 1); |
| 216 | for (j = cursor; j < (BUFSIZ - 1); j++) { |
| 217 | if (!*(command + j)) |
| 218 | break; |
| 219 | else |
| 220 | xwrite(outputFd, (command + j), 1); |
| 221 | } |
| 222 | |
| 223 | xwrite(outputFd, " \b", 2); |
| 224 | |
| 225 | while (j-- > cursor) |
| 226 | xwrite(outputFd, "\b", 1); |
| 227 | --*len; |
| 228 | } |
| 229 | |
| 230 | /* Move forward one charactor */ |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 231 | static void input_forward(int outputFd, int *cursor, int len) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 232 | { |
| 233 | if (*cursor < len) { |
| 234 | xwrite(outputFd, "\033[C", 3); |
| 235 | ++*cursor; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /* Move back one charactor */ |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 240 | static void input_backward(int outputFd, int *cursor) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 241 | { |
| 242 | if (*cursor > 0) { |
| 243 | xwrite(outputFd, "\033[D", 3); |
| 244 | --*cursor; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | |
| 249 | |
| 250 | #ifdef BB_FEATURE_SH_TAB_COMPLETION |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 251 | static char** username_tab_completion(char* command, int *num_matches) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 252 | { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 253 | char **matches = (char **) NULL; |
| 254 | *num_matches=0; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 255 | fprintf(stderr, "\nin username_tab_completion\n"); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 256 | return (matches); |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 257 | } |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 258 | |
| 259 | #include <dirent.h> |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 260 | static char** exe_n_cwd_tab_completion(char* command, int *num_matches) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 261 | { |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 262 | char *dirName; |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 263 | char **matches; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 264 | DIR *dir; |
| 265 | struct dirent *next; |
| 266 | |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 267 | matches = xmalloc( sizeof(char*)*50); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 268 | |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 269 | /* Stick a wildcard onto the command, for later use */ |
| 270 | strcat( command, "*"); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 271 | |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 272 | /* Now wall the current directory */ |
| 273 | dirName = get_current_dir_name(); |
| 274 | dir = opendir(dirName); |
| 275 | if (!dir) { |
| 276 | /* Don't print an error, just shut up and return */ |
| 277 | *num_matches=0; |
| 278 | return (matches); |
| 279 | } |
| 280 | while ((next = readdir(dir)) != NULL) { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 281 | |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 282 | /* Some quick sanity checks */ |
| 283 | if ((strcmp(next->d_name, "..") == 0) |
| 284 | || (strcmp(next->d_name, ".") == 0)) { |
| 285 | continue; |
| 286 | } |
| 287 | /* See if this matches */ |
| 288 | if (check_wildcard_match(next->d_name, command) == TRUE) { |
| 289 | /* Cool, found a match. Add it to the list */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 290 | matches[*num_matches] = xmalloc(strlen(next->d_name)+1); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 291 | strcpy( matches[*num_matches], next->d_name); |
| 292 | ++*num_matches; |
| 293 | //matches = realloc( matches, sizeof(char*)*(*num_matches)); |
| 294 | } |
| 295 | } |
| 296 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 297 | return (matches); |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 298 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 299 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 300 | static void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len, int lastWasTab) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 301 | { |
| 302 | /* Do TAB completion */ |
| 303 | static int num_matches=0; |
| 304 | static char **matches = (char **) NULL; |
Eric Andersen | a75466e | 2000-11-02 17:02:26 +0000 | [diff] [blame] | 305 | int pos = *cursor; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 306 | |
| 307 | |
| 308 | if (lastWasTab == FALSE) { |
| 309 | char *tmp, *tmp1, *matchBuf; |
| 310 | |
| 311 | /* For now, we will not bother with trying to distinguish |
| 312 | * whether the cursor is in/at a command extression -- we |
Eric Andersen | 74c66ad | 2000-06-16 19:57:44 +0000 | [diff] [blame] | 313 | * will always try all possible matches. If you don't like |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 314 | * that then feel free to fix it. |
| 315 | */ |
| 316 | |
| 317 | /* Make a local copy of the string -- up |
| 318 | * to the position of the cursor */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 319 | matchBuf = (char *) xcalloc(BUFSIZ, sizeof(char)); |
Eric Andersen | a75466e | 2000-11-02 17:02:26 +0000 | [diff] [blame] | 320 | strncpy(matchBuf, command, *cursor); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 321 | tmp=matchBuf; |
| 322 | |
| 323 | /* skip past any command seperator tokens */ |
| 324 | while (*tmp && (tmp1=strpbrk(tmp, ";|&{(`")) != NULL) { |
| 325 | tmp=++tmp1; |
| 326 | /* skip any leading white space */ |
| 327 | while (*tmp && isspace(*tmp)) |
| 328 | ++tmp; |
| 329 | } |
| 330 | |
| 331 | /* skip any leading white space */ |
| 332 | while (*tmp && isspace(*tmp)) |
| 333 | ++tmp; |
| 334 | |
| 335 | /* Free up any memory already allocated */ |
| 336 | if (matches) { |
| 337 | free(matches); |
| 338 | matches = (char **) NULL; |
| 339 | } |
| 340 | |
| 341 | /* If the word starts with `~' and there is no slash in the word, |
| 342 | * then try completing this word as a username. */ |
| 343 | |
| 344 | /* FIXME -- this check is broken! */ |
| 345 | if (*tmp == '~' && !strchr(tmp, '/')) |
| 346 | matches = username_tab_completion(tmp, &num_matches); |
| 347 | |
| 348 | /* Try to match any executable in our path and everything |
| 349 | * in the current working directory that matches. */ |
| 350 | if (!matches) |
| 351 | matches = exe_n_cwd_tab_completion(tmp, &num_matches); |
| 352 | |
| 353 | /* Don't leak memory */ |
| 354 | free( matchBuf); |
| 355 | |
| 356 | /* Did we find exactly one match? */ |
| 357 | if (matches && num_matches==1) { |
| 358 | /* write out the matched command */ |
| 359 | strncpy(command+pos, matches[0]+pos, strlen(matches[0])-pos); |
Eric Andersen | a75466e | 2000-11-02 17:02:26 +0000 | [diff] [blame] | 360 | *len=strlen(command); |
| 361 | *cursor=*len; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 362 | xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos); |
Eric Andersen | a75466e | 2000-11-02 17:02:26 +0000 | [diff] [blame] | 363 | return; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 364 | } |
| 365 | } else { |
| 366 | /* Ok -- the last char was a TAB. Since they |
| 367 | * just hit TAB again, print a list of all the |
| 368 | * available choices... */ |
| 369 | if ( matches && num_matches>0 ) { |
| 370 | int i, col; |
| 371 | |
| 372 | /* Go to the next line */ |
| 373 | xwrite(outputFd, "\n", 1); |
| 374 | /* Print the list of matches */ |
| 375 | for (i=0,col=0; i<num_matches; i++) { |
| 376 | char foo[17]; |
| 377 | sprintf(foo, "%-14s ", matches[i]); |
| 378 | col += xwrite(outputFd, foo, strlen(foo)); |
| 379 | if (col > 60 && matches[i+1] != NULL) { |
| 380 | xwrite(outputFd, "\n", 1); |
| 381 | col = 0; |
| 382 | } |
| 383 | } |
| 384 | /* Go to the next line */ |
| 385 | xwrite(outputFd, "\n", 1); |
| 386 | /* Rewrite the prompt */ |
| 387 | xwrite(outputFd, prompt, strlen(prompt)); |
| 388 | /* Rewrite the command */ |
Eric Andersen | a75466e | 2000-11-02 17:02:26 +0000 | [diff] [blame] | 389 | xwrite(outputFd, command, *len); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 390 | /* Put the cursor back to where it used to be */ |
Eric Andersen | a75466e | 2000-11-02 17:02:26 +0000 | [diff] [blame] | 391 | for (cursor=len; *cursor > pos; cursor--) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 392 | xwrite(outputFd, "\b", 1); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | #endif |
| 397 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 398 | static void get_previous_history(struct history **hp, char* command) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 399 | { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 400 | if ((*hp)->s) |
| 401 | free((*hp)->s); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 402 | (*hp)->s = strdup(command); |
| 403 | *hp = (*hp)->p; |
| 404 | } |
| 405 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 406 | static void get_next_history(struct history **hp, char* command) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 407 | { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 408 | if ((*hp)->s) |
| 409 | free((*hp)->s); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 410 | (*hp)->s = strdup(command); |
| 411 | *hp = (*hp)->n; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 414 | /* |
| 415 | * This function is used to grab a character buffer |
| 416 | * from the input file descriptor and allows you to |
| 417 | * a string with full command editing (sortof like |
| 418 | * a mini readline). |
| 419 | * |
| 420 | * The following standard commands are not implemented: |
| 421 | * ESC-b -- Move back one word |
| 422 | * ESC-f -- Move forward one word |
| 423 | * ESC-d -- Delete back one word |
| 424 | * ESC-h -- Delete forward one word |
| 425 | * CTL-t -- Transpose two characters |
| 426 | * |
| 427 | * Furthermore, the "vi" command editing keys are not implemented. |
| 428 | * |
| 429 | * TODO: implement TAB command completion. :) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 430 | */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 431 | extern void cmdedit_read_input(char* prompt, char command[BUFSIZ]) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 432 | { |
| 433 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 434 | int inputFd=fileno(stdin); |
| 435 | int outputFd=fileno(stdout); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 436 | int nr = 0; |
| 437 | int len = 0; |
| 438 | int j = 0; |
| 439 | int cursor = 0; |
| 440 | int break_out = 0; |
| 441 | int ret = 0; |
| 442 | int lastWasTab = FALSE; |
| 443 | char c = 0; |
| 444 | struct history *hp = his_end; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 445 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 446 | if (!reset_term) { |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 447 | |
| 448 | getTermSettings(inputFd, (void*) &initial_settings); |
| 449 | memcpy(&new_settings, &initial_settings, sizeof(struct termios)); |
| 450 | new_settings.c_cc[VMIN] = 1; |
| 451 | new_settings.c_cc[VTIME] = 0; |
| 452 | new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */ |
| 453 | new_settings.c_lflag &= ~ICANON; /* unbuffered input */ |
| 454 | new_settings.c_lflag &= ~(ECHO|ECHOCTL|ECHONL); /* Turn off echoing */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 455 | reset_term = 1; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 456 | } |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 457 | setTermSettings(inputFd, (void*) &new_settings); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 458 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 459 | memset(command, 0, BUFSIZ); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 460 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 461 | /* Print out the command prompt */ |
| 462 | xwrite(outputFd, prompt, strlen(prompt)); |
| 463 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 464 | while (1) { |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 465 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 466 | if ((ret = read(inputFd, &c, 1)) < 1) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 467 | return; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 468 | //fprintf(stderr, "got a '%c' (%d)\n", c, c); |
Erik Andersen | f3b3d17 | 2000-04-09 18:24:05 +0000 | [diff] [blame] | 469 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 470 | switch (c) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 471 | case '\n': |
| 472 | case '\r': |
| 473 | /* Enter */ |
| 474 | *(command + len++ + 1) = c; |
| 475 | xwrite(outputFd, &c, 1); |
| 476 | break_out = 1; |
| 477 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 478 | case 1: |
| 479 | /* Control-a -- Beginning of line */ |
| 480 | input_home(outputFd, &cursor); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 481 | case 2: |
| 482 | /* Control-b -- Move back one character */ |
| 483 | input_backward(outputFd, &cursor); |
| 484 | break; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 485 | case 3: |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 486 | /* Control-c -- stop gathering input */ |
| 487 | |
| 488 | /* Link into lash to reset context to 0 on ^C and such */ |
| 489 | shell_context = 0; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 490 | |
| 491 | /* Go to the next line */ |
| 492 | xwrite(outputFd, "\n", 1); |
| 493 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 494 | #if 0 |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 495 | /* Rewrite the prompt */ |
| 496 | xwrite(outputFd, prompt, strlen(prompt)); |
| 497 | |
| 498 | /* Reset the command string */ |
Eric Andersen | 4ac6cb5 | 2000-07-14 01:13:37 +0000 | [diff] [blame] | 499 | memset(command, 0, BUFSIZ); |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 500 | len = cursor = 0; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 501 | #endif |
| 502 | return; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 503 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 504 | case 4: |
| 505 | /* Control-d -- Delete one character, or exit |
| 506 | * if the len=0 and no chars to delete */ |
| 507 | if (len == 0) { |
| 508 | xwrite(outputFd, "exit", 4); |
| 509 | clean_up_and_die(0); |
| 510 | } else { |
| 511 | input_delete(command, outputFd, cursor, &len); |
| 512 | } |
| 513 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 514 | case 5: |
| 515 | /* Control-e -- End of line */ |
| 516 | input_end(outputFd, &cursor, len); |
| 517 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 518 | case 6: |
| 519 | /* Control-f -- Move forward one character */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 520 | input_forward(outputFd, &cursor, len); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 521 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 522 | case '\b': |
| 523 | case DEL: |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 524 | /* Control-h and DEL */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 525 | input_backspace(command, outputFd, &cursor, &len); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 526 | break; |
| 527 | case '\t': |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 528 | #ifdef BB_FEATURE_SH_TAB_COMPLETION |
Eric Andersen | a75466e | 2000-11-02 17:02:26 +0000 | [diff] [blame] | 529 | input_tab(command, prompt, outputFd, &cursor, |
| 530 | &len, lastWasTab); |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 531 | #endif |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 532 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 533 | case 14: |
| 534 | /* Control-n -- Get next command in history */ |
| 535 | if (hp && hp->n && hp->n->s) { |
| 536 | get_next_history(&hp, command); |
| 537 | goto rewrite_line; |
| 538 | } else { |
| 539 | xwrite(outputFd, "\007", 1); |
| 540 | } |
| 541 | break; |
| 542 | case 16: |
| 543 | /* Control-p -- Get previous command from history */ |
| 544 | if (hp && hp->p) { |
| 545 | get_previous_history(&hp, command); |
| 546 | goto rewrite_line; |
| 547 | } else { |
| 548 | xwrite(outputFd, "\007", 1); |
| 549 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 550 | break; |
| 551 | case ESC:{ |
| 552 | /* escape sequence follows */ |
| 553 | if ((ret = read(inputFd, &c, 1)) < 1) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 554 | return; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 555 | |
| 556 | if (c == '[') { /* 91 */ |
| 557 | if ((ret = read(inputFd, &c, 1)) < 1) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 558 | return; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 559 | |
| 560 | switch (c) { |
| 561 | case 'A': |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 562 | /* Up Arrow -- Get previous command from history */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 563 | if (hp && hp->p) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 564 | get_previous_history(&hp, command); |
| 565 | goto rewrite_line; |
| 566 | } else { |
| 567 | xwrite(outputFd, "\007", 1); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 568 | } |
| 569 | break; |
| 570 | case 'B': |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 571 | /* Down Arrow -- Get next command in history */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 572 | if (hp && hp->n && hp->n->s) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 573 | get_next_history(&hp, command); |
| 574 | goto rewrite_line; |
| 575 | } else { |
| 576 | xwrite(outputFd, "\007", 1); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 577 | } |
| 578 | break; |
| 579 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 580 | /* Rewrite the line with the selected history item */ |
| 581 | rewrite_line: |
| 582 | /* erase old command from command line */ |
| 583 | len = strlen(command)-strlen(hp->s); |
Eric Andersen | 72965e3 | 2000-07-04 06:22:18 +0000 | [diff] [blame] | 584 | |
| 585 | while (len>cursor) |
| 586 | input_delete(command, outputFd, cursor, &len); |
| 587 | while (cursor>0) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 588 | input_backspace(command, outputFd, &cursor, &len); |
| 589 | input_home(outputFd, &cursor); |
| 590 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 591 | /* write new command */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 592 | strcpy(command, hp->s); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 593 | len = strlen(hp->s); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 594 | xwrite(outputFd, command, len); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 595 | cursor = len; |
| 596 | break; |
| 597 | case 'C': |
| 598 | /* Right Arrow -- Move forward one character */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 599 | input_forward(outputFd, &cursor, len); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 600 | break; |
| 601 | case 'D': |
| 602 | /* Left Arrow -- Move back one character */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 603 | input_backward(outputFd, &cursor); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 604 | break; |
| 605 | case '3': |
| 606 | /* Delete */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 607 | input_delete(command, outputFd, cursor, &len); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 608 | break; |
| 609 | case '1': |
| 610 | /* Home (Ctrl-A) */ |
| 611 | input_home(outputFd, &cursor); |
| 612 | break; |
| 613 | case '4': |
| 614 | /* End (Ctrl-E) */ |
| 615 | input_end(outputFd, &cursor, len); |
| 616 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 617 | default: |
| 618 | xwrite(outputFd, "\007", 1); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 619 | } |
| 620 | if (c == '1' || c == '3' || c == '4') |
| 621 | if ((ret = read(inputFd, &c, 1)) < 1) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 622 | return; /* read 126 (~) */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 623 | } |
| 624 | if (c == 'O') { |
| 625 | /* 79 */ |
| 626 | if ((ret = read(inputFd, &c, 1)) < 1) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 627 | return; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 628 | switch (c) { |
| 629 | case 'H': |
| 630 | /* Home (xterm) */ |
| 631 | input_home(outputFd, &cursor); |
| 632 | break; |
| 633 | case 'F': |
| 634 | /* End (xterm) */ |
| 635 | input_end(outputFd, &cursor, len); |
| 636 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 637 | default: |
| 638 | xwrite(outputFd, "\007", 1); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | c = 0; |
| 642 | break; |
| 643 | } |
| 644 | |
| 645 | default: /* If it's regular input, do the normal thing */ |
| 646 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 647 | if (!isprint(c)) { /* Skip non-printable characters */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 648 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 649 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 650 | |
| 651 | if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */ |
| 652 | break; |
| 653 | |
| 654 | len++; |
| 655 | |
| 656 | if (cursor == (len - 1)) { /* Append if at the end of the line */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 657 | *(command + cursor) = c; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 658 | } else { /* Insert otherwise */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 659 | memmove(command + cursor + 1, command + cursor, |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 660 | len - cursor - 1); |
| 661 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 662 | *(command + cursor) = c; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 663 | |
| 664 | for (j = cursor; j < len; j++) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 665 | xwrite(outputFd, command + j, 1); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 666 | for (; j > cursor; j--) |
| 667 | xwrite(outputFd, "\033[D", 3); |
| 668 | } |
| 669 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 670 | cursor++; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 671 | xwrite(outputFd, &c, 1); |
| 672 | break; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 673 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 674 | if (c == '\t') |
| 675 | lastWasTab = TRUE; |
| 676 | else |
| 677 | lastWasTab = FALSE; |
| 678 | |
| 679 | if (break_out) /* Enter is the command terminator, no more input. */ |
| 680 | break; |
| 681 | } |
| 682 | |
| 683 | nr = len + 1; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 684 | setTermSettings(inputFd, (void *) &initial_settings); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 685 | reset_term = 0; |
| 686 | |
| 687 | |
| 688 | /* Handle command history log */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 689 | if (*(command)) { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 690 | |
| 691 | struct history *h = his_end; |
| 692 | |
| 693 | if (!h) { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 694 | /* No previous history -- this memory is never freed */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 695 | h = his_front = xmalloc(sizeof(struct history)); |
| 696 | h->n = xmalloc(sizeof(struct history)); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 697 | |
| 698 | h->p = NULL; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 699 | h->s = strdup(command); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 700 | h->n->p = h; |
| 701 | h->n->n = NULL; |
| 702 | h->n->s = NULL; |
| 703 | his_end = h->n; |
| 704 | history_counter++; |
| 705 | } else { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 706 | /* Add a new history command -- this memory is never freed */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 707 | h->n = xmalloc(sizeof(struct history)); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 708 | |
| 709 | h->n->p = h; |
| 710 | h->n->n = NULL; |
| 711 | h->n->s = NULL; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 712 | h->s = strdup(command); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 713 | his_end = h->n; |
| 714 | |
| 715 | /* After max history, remove the oldest command */ |
| 716 | if (history_counter >= MAX_HISTORY) { |
| 717 | |
| 718 | struct history *p = his_front->n; |
| 719 | |
| 720 | p->p = NULL; |
| 721 | free(his_front->s); |
| 722 | free(his_front); |
| 723 | his_front = p; |
| 724 | } else { |
| 725 | history_counter++; |
| 726 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 727 | } |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 728 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 729 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 730 | return; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | extern void cmdedit_init(void) |
| 734 | { |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 735 | win_changed(0); |
| 736 | signal(SIGWINCH, win_changed); |
| 737 | |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 738 | if(exithandler_set == 0) { |
| 739 | atexit(cmdedit_reset_term); /* be sure to do this only once */ |
| 740 | exithandler_set = 1; |
| 741 | } |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 742 | signal(SIGKILL, clean_up_and_die); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 743 | signal(SIGINT, clean_up_and_die); |
| 744 | signal(SIGQUIT, clean_up_and_die); |
| 745 | signal(SIGTERM, clean_up_and_die); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 746 | } |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 747 | |
| 748 | /* |
| 749 | ** Undo the effects of cmdedit_init() as good as we can: |
| 750 | ** I am not aware of a way to revoke an atexit() handler, |
| 751 | ** but, fortunately, our particular handler can be made |
| 752 | ** a no-op by setting reset_term = 0. |
| 753 | */ |
| 754 | extern void cmdedit_terminate(void) |
| 755 | { |
| 756 | cmdedit_reset_term(); |
| 757 | reset_term = 0; |
| 758 | signal(SIGKILL, SIG_DFL); |
| 759 | signal(SIGINT, SIG_DFL); |
| 760 | signal(SIGQUIT, SIG_DFL); |
| 761 | signal(SIGTERM, SIG_DFL); |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 762 | signal(SIGWINCH, SIG_DFL); |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | |
| 766 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 767 | #endif /* BB_FEATURE_SH_COMMAND_EDITING */ |