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