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. |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 28 | Ctrl-E also works as End. |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 29 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 30 | |
| 31 | Editor with vertical scrolling and completion by |
| 32 | Vladimir Oleynik. vodz@usa.net (c) 2001 |
| 33 | |
| 34 | Small bug: not true work if terminal size (x*y symbols) less |
| 35 | size (prompt + editor`s line + 2 symbols) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 38 | |
| 39 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 40 | #include "busybox.h" |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 41 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 42 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
| 43 | |
| 44 | #include <stdio.h> |
| 45 | #include <errno.h> |
| 46 | #include <unistd.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <string.h> |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 49 | #include <sys/ioctl.h> |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 50 | #include <ctype.h> |
| 51 | #include <signal.h> |
| 52 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 53 | #ifdef BB_FEATURE_SH_TAB_COMPLETION |
| 54 | #include <sys/stat.h> |
| 55 | #endif |
| 56 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 57 | static const int 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] | 58 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 59 | enum { |
| 60 | ESC = 27, |
| 61 | DEL = 127, |
| 62 | }; |
| 63 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 64 | #define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0) |
| 65 | #define whitespace(c) (((c) == ' ') || ((c) == '\t')) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 66 | |
| 67 | static struct history *his_front = NULL; /* First element in command line list */ |
| 68 | static struct history *his_end = NULL; /* Last element in command line list */ |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 69 | |
| 70 | /* ED: sparc termios is broken: revert back to old termio handling. */ |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 71 | |
| 72 | #if #cpu(sparc) |
| 73 | # include <termio.h> |
| 74 | # define termios termio |
| 75 | # define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp) |
| 76 | # define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp) |
| 77 | #else |
| 78 | # include <termios.h> |
| 79 | # define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) |
| 80 | # define getTermSettings(fd,argp) tcgetattr(fd, argp); |
| 81 | #endif |
| 82 | |
| 83 | /* Current termio and the previous termio before starting sh */ |
Eric Andersen | 63a8622 | 2000-11-07 06:52:13 +0000 | [diff] [blame] | 84 | static struct termios initial_settings, new_settings; |
Erik Andersen | 8ea7d8c | 2000-05-20 00:40:08 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | #ifndef _POSIX_VDISABLE |
| 88 | #define _POSIX_VDISABLE '\0' |
| 89 | #endif |
| 90 | |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 91 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 92 | static |
| 93 | volatile int cmdedit_termw; /* actual terminal width */ |
| 94 | static int history_counter = 0; /* Number of commands in history list */ |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 95 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 96 | static |
| 97 | volatile int handlers_sets = 0; /* Set next bites |
| 98 | when atexit() has been called |
| 99 | and set many "terminates" signal handlers |
| 100 | and winchg signal handler |
| 101 | and if the terminal needs to be reset upon exit |
| 102 | */ |
| 103 | enum { |
| 104 | SET_ATEXIT = 1, |
| 105 | SET_TERM_HANDLERS = 2, |
| 106 | SET_WCHG_HANDLERS = 4, |
| 107 | SET_RESET_TERM = 8, |
| 108 | }; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 109 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 110 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 111 | static int cmdedit_x; /* real x terminal position, |
| 112 | require put prompt in start x position */ |
| 113 | static int cmdedit_y; /* pseudoreal y terminal position */ |
| 114 | static int cmdedit_prmt_len; /* for fast running, without duplicate calculate */ |
| 115 | |
| 116 | static int cursor; /* required global for signal handler */ |
| 117 | static int len; /* --- "" - - "" - -"- --""-- --""--- */ |
| 118 | static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */ |
| 119 | static const char *cmdedit_prompt;/* --- "" - - "" - -"- --""-- --""--- */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 120 | |
| 121 | /* Link into lash to reset context to 0 |
| 122 | * on ^C and such */ |
| 123 | extern unsigned int shell_context; |
| 124 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 125 | |
| 126 | struct history { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 127 | char *s; |
| 128 | struct history *p; |
| 129 | struct history *n; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 132 | static void cmdedit_setwidth(int w, int redraw_flg); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 133 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 134 | static void win_changed(int nsig) |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 135 | { |
| 136 | struct winsize win = { 0, 0, 0, 0 }; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 137 | static __sighandler_t previous_SIGWINCH_handler; /* for reset */ |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 138 | |
Eric Andersen | 4bbdd78 | 2001-01-30 22:23:17 +0000 | [diff] [blame] | 139 | /* emulate signal call if not called as a sig handler */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 140 | if(nsig == -SIGWINCH || nsig == SIGWINCH) { |
| 141 | ioctl(0, TIOCGWINSZ, &win); |
| 142 | if (win.ws_col > 0) { |
| 143 | cmdedit_setwidth( win.ws_col, nsig == SIGWINCH ); |
Eric Andersen | 4bbdd78 | 2001-01-30 22:23:17 +0000 | [diff] [blame] | 144 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 145 | } |
Eric Andersen | 4bbdd78 | 2001-01-30 22:23:17 +0000 | [diff] [blame] | 146 | |
| 147 | /* Unix not all standart in recall signal */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 148 | |
Eric Andersen | 4bbdd78 | 2001-01-30 22:23:17 +0000 | [diff] [blame] | 149 | if(nsig == -SIGWINCH) /* save previous handler */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 150 | previous_SIGWINCH_handler = signal(SIGWINCH, win_changed); |
| 151 | else if(nsig == SIGWINCH) /* signaled called handler */ |
Eric Andersen | 4bbdd78 | 2001-01-30 22:23:17 +0000 | [diff] [blame] | 152 | signal(SIGWINCH, win_changed); /* set for next call */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 153 | else /* set previous handler */ |
| 154 | signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */ |
| 155 | } |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 156 | |
| 157 | static void cmdedit_reset_term(void) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 158 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 159 | if((handlers_sets & SET_RESET_TERM)!=0) { |
Erik Andersen | a6c7522 | 2000-04-18 00:00:52 +0000 | [diff] [blame] | 160 | /* sparc and other have broken termios support: use old termio handling. */ |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 161 | setTermSettings(fileno(stdin), (void*) &initial_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 162 | handlers_sets &= ~SET_RESET_TERM; |
| 163 | } |
| 164 | if((handlers_sets & SET_WCHG_HANDLERS)!=0) { |
| 165 | /* reset SIGWINCH handler to previous (default) */ |
| 166 | win_changed(0); |
| 167 | handlers_sets &= ~SET_WCHG_HANDLERS; |
| 168 | } |
| 169 | fflush(stdout); |
Eric Andersen | b040d4f | 2000-07-25 18:01:20 +0000 | [diff] [blame] | 170 | #ifdef BB_FEATURE_CLEAN_UP |
| 171 | if (his_front) { |
| 172 | struct history *n; |
| 173 | //while(his_front!=his_end) { |
| 174 | while(his_front!=his_end) { |
| 175 | n = his_front->n; |
| 176 | free(his_front->s); |
| 177 | free(his_front); |
| 178 | his_front=n; |
| 179 | } |
| 180 | } |
| 181 | #endif |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | /* special for recount position for scroll and remove terminal margin effect */ |
| 187 | static void cmdedit_set_out_char(int c, int next_char) { |
| 188 | putchar(c); |
| 189 | if(++cmdedit_x>=cmdedit_termw) { |
| 190 | /* terminal is scrolled down */ |
| 191 | cmdedit_y++; |
| 192 | cmdedit_x=0; |
| 193 | |
| 194 | if(!next_char) |
| 195 | next_char = ' '; |
| 196 | /* destroy "(auto)margin" */ |
| 197 | putchar(next_char); |
| 198 | putchar('\b'); |
| 199 | } |
| 200 | cursor++; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 203 | /* Move to end line. Bonus: rewrite line from cursor without use |
| 204 | special control terminal strings, also saved size and speed! */ |
| 205 | static void input_end (void) { |
| 206 | while(cursor < len) |
| 207 | cmdedit_set_out_char(command_ps[cursor], 0); |
| 208 | } |
| 209 | |
| 210 | /* Go to the next line */ |
| 211 | static void goto_new_line(void) { |
| 212 | input_end(); |
| 213 | cmdedit_set_out_char('\n', 0); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | static inline void out1str(const char *s) { fputs (s, stdout); } |
| 218 | static inline void beep (void) { putchar('\007'); } |
| 219 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 220 | /* Go to HOME position */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 221 | static void input_home(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 222 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 223 | while(cmdedit_y>0) { /* up to start y */ |
| 224 | out1str("\033[A"); |
| 225 | cmdedit_y--; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 226 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 227 | putchar('\r'); |
| 228 | cursor = 0; |
| 229 | out1str(cmdedit_prompt); |
| 230 | cmdedit_x = cmdedit_prmt_len; |
| 231 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 234 | /* Move back one charactor */ |
| 235 | static void input_backward(void) { |
| 236 | if (cursor > 0) { |
| 237 | cursor--; |
| 238 | if(cmdedit_x!=0) { /* no first position in terminal line */ |
| 239 | putchar('\b'); |
| 240 | cmdedit_x--; |
| 241 | } |
| 242 | else { |
| 243 | out1str("\033[A"); /* up */ |
| 244 | cmdedit_y--; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 245 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 246 | /* to end in current terminal line */ |
| 247 | while(cmdedit_x<(cmdedit_termw-1)) { |
| 248 | out1str("\033[C"); |
| 249 | cmdedit_x++; |
| 250 | } |
| 251 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 252 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 255 | /* Delete the char in front of the cursor */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 256 | static void input_delete(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 257 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 258 | int j = cursor; |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 259 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 260 | if (j == len) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 261 | return; |
| 262 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 263 | memmove (command_ps + j, command_ps + j + 1, BUFSIZ - j - 1); |
| 264 | len--; |
| 265 | input_end(); /* rewtite new line */ |
| 266 | cmdedit_set_out_char(' ', 0); /* destroy end char */ |
| 267 | while (j < cursor) |
| 268 | input_backward(); /* back to old pos cursor */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 271 | /* Delete the char in back of the cursor */ |
| 272 | static void input_backspace(void) |
| 273 | { |
| 274 | if (cursor > 0) { |
| 275 | input_backward(); |
| 276 | input_delete (); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 281 | /* Move forward one charactor */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 282 | static void input_forward(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 283 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 284 | if (cursor < len) |
| 285 | cmdedit_set_out_char(command_ps[cursor], command_ps[cursor + 1]); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 289 | static void clean_up_and_die(int sig) |
| 290 | { |
| 291 | goto_new_line(); |
| 292 | if (sig!=SIGINT) |
| 293 | exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */ |
| 294 | cmdedit_reset_term(); |
| 295 | } |
| 296 | |
| 297 | static void cmdedit_setwidth(int w, int redraw_flg) |
| 298 | { |
| 299 | cmdedit_termw = cmdedit_prmt_len+2; |
| 300 | if (w > cmdedit_termw) { |
| 301 | |
| 302 | cmdedit_termw = w; |
| 303 | |
| 304 | if(redraw_flg) { |
| 305 | int sav_cursor = cursor; |
| 306 | |
| 307 | /* set variables for new terminal size */ |
| 308 | cmdedit_y = sav_cursor/w; |
| 309 | cmdedit_x = sav_cursor-cmdedit_y*w; |
| 310 | |
| 311 | /* redraw */ |
| 312 | input_home(); |
| 313 | input_end(); |
| 314 | while(sav_cursor<cursor) |
| 315 | input_backward(); |
| 316 | } |
| 317 | } else { |
| 318 | error_msg("\n*** Error: minimum screen width is %d\n", cmdedit_termw); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | extern void cmdedit_init(void) |
| 323 | { |
| 324 | if((handlers_sets & SET_WCHG_HANDLERS)==0) { |
Eric Andersen | 4bbdd78 | 2001-01-30 22:23:17 +0000 | [diff] [blame] | 325 | /* pretend we received a signal in order to set term size and sig handling */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 326 | win_changed(-SIGWINCH); |
| 327 | handlers_sets |= SET_WCHG_HANDLERS; |
| 328 | } |
| 329 | |
| 330 | if((handlers_sets & SET_ATEXIT)==0) { |
| 331 | atexit(cmdedit_reset_term); /* be sure to do this only once */ |
| 332 | handlers_sets |= SET_ATEXIT; |
| 333 | } |
| 334 | if((handlers_sets & SET_TERM_HANDLERS)==0) { |
| 335 | signal(SIGKILL, clean_up_and_die); |
| 336 | signal(SIGINT, clean_up_and_die); |
| 337 | signal(SIGQUIT, clean_up_and_die); |
| 338 | signal(SIGTERM, clean_up_and_die); |
| 339 | handlers_sets |= SET_TERM_HANDLERS; |
| 340 | } |
| 341 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 342 | |
| 343 | #ifdef BB_FEATURE_SH_TAB_COMPLETION |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 344 | |
| 345 | #ifdef BB_FEATURE_USERNAME_COMPLETION |
| 346 | static char** username_tab_completion(char *ud, int *num_matches) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 347 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 348 | static struct passwd *entry; |
| 349 | int userlen; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 350 | char **matches = (char **) NULL; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 351 | char *temp; |
| 352 | int nm = 0; |
| 353 | |
Eric Andersen | 4bbdd78 | 2001-01-30 22:23:17 +0000 | [diff] [blame] | 354 | setpwent (); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 355 | userlen = strlen (ud + 1); |
| 356 | |
Eric Andersen | 4bbdd78 | 2001-01-30 22:23:17 +0000 | [diff] [blame] | 357 | while ((entry = getpwent ()) != NULL) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 358 | /* Null usernames should result in all users as possible completions. */ |
| 359 | if (!userlen || !strncmp (ud + 1, entry->pw_name, userlen)) { |
| 360 | |
| 361 | temp = xmalloc (3 + strlen (entry->pw_name)); |
| 362 | sprintf(temp, "~%s/", entry->pw_name); |
| 363 | |
| 364 | matches = xrealloc(matches, (nm+1)*sizeof(char *)); |
| 365 | matches[nm++] = temp; |
| 366 | } |
| 367 | } |
| 368 | |
Eric Andersen | 4bbdd78 | 2001-01-30 22:23:17 +0000 | [diff] [blame] | 369 | endpwent (); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 370 | (*num_matches) = nm; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 371 | return (matches); |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 372 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 373 | #endif |
| 374 | |
| 375 | enum { |
| 376 | FIND_EXE_ONLY = 0, |
| 377 | FIND_DIR_ONLY = 1, |
| 378 | FIND_FILE_ONLY = 2, |
| 379 | }; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 380 | |
| 381 | #include <dirent.h> |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 382 | |
| 383 | static int path_parse(char ***p, int flags) |
| 384 | { |
| 385 | int npth; |
| 386 | char *tmp; |
| 387 | char *pth; |
| 388 | |
| 389 | if(flags!=FIND_EXE_ONLY || (pth=getenv("PATH"))==0) { |
| 390 | /* if not setenv PATH variable, to search cur dir "." */ |
| 391 | (*p) = xmalloc(sizeof(char *)); |
| 392 | (*p)[0] = xstrdup("."); |
| 393 | return 1; |
| 394 | } |
| 395 | |
| 396 | tmp = pth; |
| 397 | npth=0; |
| 398 | |
| 399 | for(;;) { |
| 400 | npth++; /* count words is + 1 count ':' */ |
| 401 | tmp = strchr(tmp, ':'); |
| 402 | if(tmp) |
| 403 | tmp++; |
| 404 | else |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | *p = xmalloc(npth*sizeof(char *)); |
| 409 | |
| 410 | tmp = pth; |
| 411 | (*p)[0] = xstrdup(tmp); |
| 412 | npth=1; /* count words is + 1 count ':' */ |
| 413 | |
| 414 | for(;;) { |
| 415 | tmp = strchr(tmp, ':'); |
| 416 | if(tmp) { |
| 417 | (*p)[0][(tmp-pth)]=0; /* ':' -> '\0'*/ |
| 418 | tmp++; |
| 419 | } else |
| 420 | break; |
| 421 | (*p)[npth++] = &(*p)[0][(tmp-pth)]; /* p[next]=p[0][&'\0'+1] */ |
| 422 | } |
| 423 | |
| 424 | return npth; |
| 425 | } |
| 426 | |
| 427 | static char** exe_n_cwd_tab_completion(char* command, int *num_matches, int type) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 428 | { |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 429 | char *dirName; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 430 | char **matches = 0; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 431 | DIR *dir; |
| 432 | struct dirent *next; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 433 | char cmd [BUFSIZ+4]; |
| 434 | char *dirbuf; |
| 435 | char found [BUFSIZ+4]; |
| 436 | int nm = *num_matches; |
| 437 | struct stat st; |
| 438 | char **paths; |
| 439 | int npaths; |
| 440 | int i; |
| 441 | char full_pth[BUFSIZ+4+PATH_MAX]; |
| 442 | |
| 443 | |
| 444 | strcpy(cmd, command); /* save for change (last '/' to '\0') */ |
| 445 | |
| 446 | dirName = strrchr(cmd, '/'); |
| 447 | if(dirName==NULL) { |
| 448 | /* no dir, if flags==EXE_ONLY - get paths, else "." */ |
| 449 | npaths = path_parse(&paths, type); |
| 450 | if(npaths==0) |
| 451 | return 0; |
| 452 | } else { |
| 453 | /* with dir */ |
| 454 | |
| 455 | /* save dir */ |
| 456 | dirbuf = xstrdup(cmd); |
| 457 | /* set only dirname */ |
| 458 | dirbuf[(dirName-cmd)+1]=0; |
| 459 | |
| 460 | /* strip dirname in cmd */ |
| 461 | strcpy(cmd, dirName+1); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 462 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 463 | paths = xmalloc(sizeof(char*)); |
| 464 | paths[0] = dirbuf; |
| 465 | npaths = 1; /* only 1 dir */ |
| 466 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 467 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 468 | for(i=0; i < npaths; i++) { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 469 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 470 | dir = opendir(paths[i]); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 471 | if (!dir) { |
| 472 | /* Don't print an error, just shut up and return */ |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 473 | return (matches); |
| 474 | } |
| 475 | while ((next = readdir(dir)) != NULL) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 476 | /* matched ? */ |
| 477 | if(strncmp(next->d_name, cmd, strlen(cmd))) |
| 478 | continue; |
| 479 | /* not see .name without .match */ |
| 480 | if(*next->d_name == '.' && *cmd != '.') |
| 481 | continue; |
| 482 | sprintf(full_pth, "%s/%s", paths[i], next->d_name); |
| 483 | /* hmm, remover in progress? */ |
| 484 | if(stat(full_pth, &st)<0) |
| 485 | continue; |
| 486 | /* Cool, found a match. */ |
| 487 | if (S_ISDIR(st.st_mode)) { |
| 488 | /* name is directory */ |
| 489 | strcpy(found, next->d_name); |
| 490 | strcat(found, "/"); |
| 491 | if(type==FIND_DIR_ONLY) |
| 492 | strcat(found, " "); |
| 493 | } else { |
| 494 | /* not put found file if search only dirs for cd */ |
| 495 | if(type==FIND_DIR_ONLY) |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 496 | continue; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 497 | strcpy(found, next->d_name); |
| 498 | strcat(found, " "); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 499 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 500 | /* Add it to the list */ |
| 501 | matches = xrealloc(matches, (nm+1)*sizeof(char *)); |
| 502 | matches[nm++] = xstrdup(found); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 505 | free(paths[0]); /* allocate memory only in first member */ |
| 506 | free(paths); |
| 507 | *num_matches = nm; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 508 | return (matches); |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 509 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 510 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 511 | static void input_tab(int lastWasTab) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 512 | { |
| 513 | /* Do TAB completion */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 514 | static int num_matches; |
| 515 | static char **matches; |
| 516 | |
| 517 | char matchBuf[BUFSIZ]; |
| 518 | |
| 519 | int pos = cursor; |
| 520 | int find_type=FIND_FILE_ONLY; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 521 | |
| 522 | |
| 523 | if (lastWasTab == FALSE) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 524 | char *tmp, *tmp1; |
| 525 | int len_found; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 526 | |
| 527 | /* For now, we will not bother with trying to distinguish |
| 528 | * whether the cursor is in/at a command extression -- we |
Eric Andersen | 74c66ad | 2000-06-16 19:57:44 +0000 | [diff] [blame] | 529 | * will always try all possible matches. If you don't like |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 530 | * that then feel free to fix it. |
| 531 | */ |
| 532 | |
| 533 | /* Make a local copy of the string -- up |
| 534 | * to the position of the cursor */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 535 | memset(matchBuf, 0, BUFSIZ); |
| 536 | tmp = strncpy(matchBuf, command_ps, cursor); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 537 | |
| 538 | /* skip past any command seperator tokens */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 539 | while ( (tmp1=strpbrk(tmp, ";|&{(`")) != NULL) { |
| 540 | tmp = ++tmp1; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* skip any leading white space */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 544 | while (*tmp == ' ') |
| 545 | tmp++; |
| 546 | |
| 547 | if(strncmp(tmp, "cd ", 3)==0) |
| 548 | find_type = FIND_DIR_ONLY; |
| 549 | else if(strchr(tmp, ' ')==NULL) |
| 550 | find_type = FIND_EXE_ONLY; |
| 551 | |
| 552 | /* find begin curent word */ |
| 553 | if( (tmp1=strrchr(tmp, ' ')) != NULL) { |
| 554 | tmp = ++tmp1; |
| 555 | } |
| 556 | strcpy(matchBuf, tmp); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 557 | |
| 558 | /* Free up any memory already allocated */ |
| 559 | if (matches) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 560 | while(num_matches>0) |
| 561 | free(matches[--num_matches]); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 562 | free(matches); |
| 563 | matches = (char **) NULL; |
| 564 | } |
| 565 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 566 | #ifdef BB_FEATURE_USERNAME_COMPLETION |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 567 | /* If the word starts with `~' and there is no slash in the word, |
| 568 | * then try completing this word as a username. */ |
| 569 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 570 | if (matchBuf[0]=='~' && strchr(matchBuf, '/')==0) { |
| 571 | matches = username_tab_completion(matchBuf, &num_matches); |
| 572 | } |
| 573 | #endif |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 574 | /* Try to match any executable in our path and everything |
| 575 | * in the current working directory that matches. */ |
| 576 | if (!matches) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 577 | matches = exe_n_cwd_tab_completion(matchBuf, &num_matches, find_type); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 578 | |
| 579 | /* Did we find exactly one match? */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 580 | if(!matches || num_matches>1) { |
| 581 | beep(); |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | len_found = strlen(matches[0]); |
| 586 | |
| 587 | /* have space to placed match? */ |
| 588 | if ( (len_found-strlen(matchBuf)+len) < BUFSIZ ) { |
| 589 | |
| 590 | int recalc_pos = len; |
| 591 | |
| 592 | /* before word for match */ |
| 593 | command_ps[pos-strlen(matchBuf)]=0; |
| 594 | |
| 595 | /* tail line */ |
| 596 | strcpy(matchBuf, command_ps+pos); |
| 597 | |
| 598 | /* add match */ |
| 599 | strcat(command_ps, matches[0]); |
| 600 | /* add tail */ |
| 601 | strcat(command_ps, matchBuf); |
| 602 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 603 | /* write out the matched command */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 604 | len=strlen(command_ps); |
| 605 | recalc_pos = len-recalc_pos+pos; |
| 606 | input_end(); /* write */ |
| 607 | while(recalc_pos<cursor) |
| 608 | input_backward(); |
Eric Andersen | a75466e | 2000-11-02 17:02:26 +0000 | [diff] [blame] | 609 | return; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 610 | } |
| 611 | } else { |
| 612 | /* Ok -- the last char was a TAB. Since they |
| 613 | * just hit TAB again, print a list of all the |
| 614 | * available choices... */ |
| 615 | if ( matches && num_matches>0 ) { |
| 616 | int i, col; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 617 | int sav_cursor = cursor; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 618 | |
| 619 | /* Go to the next line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 620 | goto_new_line(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 621 | for (i=0,col=0; i<num_matches; i++) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 622 | printf("%s ", matches[i]); |
| 623 | col += strlen(matches[i])+2; |
| 624 | col -= (col/cmdedit_termw)*cmdedit_termw; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 625 | if (col > 60 && matches[i+1] != NULL) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 626 | putchar('\n'); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 627 | col = 0; |
| 628 | } |
| 629 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 630 | /* Go to the next line and rewrite the prompt */ |
| 631 | printf("\n%s", cmdedit_prompt); |
| 632 | cmdedit_x = cmdedit_prmt_len; |
| 633 | cmdedit_y = 0; |
| 634 | cursor = 0; |
| 635 | input_end(); /* Rewrite the command */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 636 | /* Put the cursor back to where it used to be */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 637 | while (sav_cursor < cursor) |
| 638 | input_backward(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | } |
| 642 | #endif |
| 643 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 644 | static void get_previous_history(struct history **hp, char* command) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 645 | { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 646 | if ((*hp)->s) |
| 647 | free((*hp)->s); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 648 | (*hp)->s = strdup(command); |
| 649 | *hp = (*hp)->p; |
| 650 | } |
| 651 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 652 | static void get_next_history(struct history **hp, char* command) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 653 | { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 654 | if ((*hp)->s) |
| 655 | free((*hp)->s); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 656 | (*hp)->s = strdup(command); |
| 657 | *hp = (*hp)->n; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 660 | /* |
| 661 | * This function is used to grab a character buffer |
| 662 | * from the input file descriptor and allows you to |
| 663 | * a string with full command editing (sortof like |
| 664 | * a mini readline). |
| 665 | * |
| 666 | * The following standard commands are not implemented: |
| 667 | * ESC-b -- Move back one word |
| 668 | * ESC-f -- Move forward one word |
| 669 | * ESC-d -- Delete back one word |
| 670 | * ESC-h -- Delete forward one word |
| 671 | * CTL-t -- Transpose two characters |
| 672 | * |
| 673 | * Furthermore, the "vi" command editing keys are not implemented. |
| 674 | * |
| 675 | * TODO: implement TAB command completion. :) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 676 | */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 677 | extern void cmdedit_read_input(char* prompt, char command[BUFSIZ]) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 678 | { |
| 679 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 680 | int inputFd=fileno(stdin); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 681 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 682 | int j = 0; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 683 | int break_out = 0; |
| 684 | int ret = 0; |
| 685 | int lastWasTab = FALSE; |
| 686 | char c = 0; |
| 687 | struct history *hp = his_end; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 688 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 689 | len = 0; |
| 690 | cursor = 0; |
| 691 | command_ps = command; |
| 692 | |
| 693 | if (new_settings.c_cc[VMIN]==0) { |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 694 | |
| 695 | getTermSettings(inputFd, (void*) &initial_settings); |
| 696 | memcpy(&new_settings, &initial_settings, sizeof(struct termios)); |
| 697 | new_settings.c_cc[VMIN] = 1; |
| 698 | new_settings.c_cc[VTIME] = 0; |
| 699 | new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */ |
| 700 | new_settings.c_lflag &= ~ICANON; /* unbuffered input */ |
| 701 | new_settings.c_lflag &= ~(ECHO|ECHOCTL|ECHONL); /* Turn off echoing */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 702 | } |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 703 | setTermSettings(inputFd, (void*) &new_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 704 | handlers_sets |= SET_RESET_TERM; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 705 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 706 | memset(command, 0, BUFSIZ); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 707 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 708 | cmdedit_init(); |
| 709 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 710 | /* Print out the command prompt */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 711 | cmdedit_prompt = prompt; |
| 712 | cmdedit_prmt_len = strlen(prompt); |
| 713 | printf("%s", prompt); |
| 714 | cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */ |
| 715 | cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */ |
| 716 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 717 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 718 | while (1) { |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 719 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 720 | fflush(stdout); /* buffered out to fast */ |
| 721 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 722 | if ((ret = read(inputFd, &c, 1)) < 1) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 723 | return; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 724 | //fprintf(stderr, "got a '%c' (%d)\n", c, c); |
Erik Andersen | f3b3d17 | 2000-04-09 18:24:05 +0000 | [diff] [blame] | 725 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 726 | switch (c) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 727 | case '\n': |
| 728 | case '\r': |
| 729 | /* Enter */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 730 | *(command + len) = c; |
| 731 | len++; |
| 732 | input_end (); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 733 | break_out = 1; |
| 734 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 735 | case 1: |
| 736 | /* Control-a -- Beginning of line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 737 | input_home(); |
| 738 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 739 | case 2: |
| 740 | /* Control-b -- Move back one character */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 741 | input_backward(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 742 | break; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 743 | case 3: |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 744 | /* Control-c -- stop gathering input */ |
| 745 | |
| 746 | /* Link into lash to reset context to 0 on ^C and such */ |
| 747 | shell_context = 0; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 748 | |
| 749 | /* Go to the next line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 750 | goto_new_line(); |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 751 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 752 | #if 0 |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 753 | /* Rewrite the prompt */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 754 | printf("%s", prompt); |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 755 | |
| 756 | /* Reset the command string */ |
Eric Andersen | 4ac6cb5 | 2000-07-14 01:13:37 +0000 | [diff] [blame] | 757 | memset(command, 0, BUFSIZ); |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 758 | len = cursor = 0; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 759 | #endif |
| 760 | return; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 761 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 762 | case 4: |
| 763 | /* Control-d -- Delete one character, or exit |
| 764 | * if the len=0 and no chars to delete */ |
| 765 | if (len == 0) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 766 | printf("exit"); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 767 | clean_up_and_die(0); |
| 768 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 769 | input_delete(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 770 | } |
| 771 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 772 | case 5: |
| 773 | /* Control-e -- End of line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 774 | input_end(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 775 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 776 | case 6: |
| 777 | /* Control-f -- Move forward one character */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 778 | input_forward(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 779 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 780 | case '\b': |
| 781 | case DEL: |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 782 | /* Control-h and DEL */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 783 | input_backspace(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 784 | break; |
| 785 | case '\t': |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 786 | #ifdef BB_FEATURE_SH_TAB_COMPLETION |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 787 | input_tab(lastWasTab); |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 788 | #endif |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 789 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 790 | case 14: |
| 791 | /* Control-n -- Get next command in history */ |
| 792 | if (hp && hp->n && hp->n->s) { |
| 793 | get_next_history(&hp, command); |
| 794 | goto rewrite_line; |
| 795 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 796 | beep(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 797 | } |
| 798 | break; |
| 799 | case 16: |
| 800 | /* Control-p -- Get previous command from history */ |
| 801 | if (hp && hp->p) { |
| 802 | get_previous_history(&hp, command); |
| 803 | goto rewrite_line; |
| 804 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 805 | beep(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 806 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 807 | break; |
| 808 | case ESC:{ |
| 809 | /* escape sequence follows */ |
| 810 | if ((ret = read(inputFd, &c, 1)) < 1) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 811 | return; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 812 | |
| 813 | if (c == '[') { /* 91 */ |
| 814 | if ((ret = read(inputFd, &c, 1)) < 1) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 815 | return; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 816 | |
| 817 | switch (c) { |
| 818 | case 'A': |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 819 | /* Up Arrow -- Get previous command from history */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 820 | if (hp && hp->p) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 821 | get_previous_history(&hp, command); |
| 822 | goto rewrite_line; |
| 823 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 824 | beep(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 825 | } |
| 826 | break; |
| 827 | case 'B': |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 828 | /* Down Arrow -- Get next command in history */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 829 | if (hp && hp->n && hp->n->s) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 830 | get_next_history(&hp, command); |
| 831 | goto rewrite_line; |
| 832 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 833 | beep(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 834 | } |
| 835 | break; |
| 836 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 837 | /* Rewrite the line with the selected history item */ |
| 838 | rewrite_line: |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 839 | /* return to begin of line */ |
| 840 | input_home (); |
| 841 | /* for next memmoves without set '\0' */ |
| 842 | memset (command, 0, BUFSIZ); |
| 843 | /* change command */ |
| 844 | strcpy (command, hp->s); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 845 | /* write new command */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 846 | for (j=0; command[j]; j++) |
| 847 | cmdedit_set_out_char(command[j], 0); |
| 848 | ret = cursor; |
| 849 | /* erase tail if required */ |
| 850 | for (j = ret; j < len; j++) |
| 851 | cmdedit_set_out_char(' ', 0); |
| 852 | /* and backward cursor */ |
| 853 | for (j = ret; j < len; j++) |
| 854 | input_backward(); |
| 855 | len = cursor; /* set new len */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 856 | break; |
| 857 | case 'C': |
| 858 | /* Right Arrow -- Move forward one character */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 859 | input_forward(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 860 | break; |
| 861 | case 'D': |
| 862 | /* Left Arrow -- Move back one character */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 863 | input_backward(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 864 | break; |
| 865 | case '3': |
| 866 | /* Delete */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 867 | input_delete(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 868 | break; |
| 869 | case '1': |
| 870 | /* Home (Ctrl-A) */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 871 | input_home(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 872 | break; |
| 873 | case '4': |
| 874 | /* End (Ctrl-E) */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 875 | input_end(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 876 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 877 | default: |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 878 | beep(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 879 | } |
| 880 | if (c == '1' || c == '3' || c == '4') |
| 881 | if ((ret = read(inputFd, &c, 1)) < 1) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 882 | return; /* read 126 (~) */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 883 | } |
| 884 | if (c == 'O') { |
| 885 | /* 79 */ |
| 886 | if ((ret = read(inputFd, &c, 1)) < 1) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 887 | return; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 888 | switch (c) { |
| 889 | case 'H': |
| 890 | /* Home (xterm) */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 891 | input_home(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 892 | break; |
| 893 | case 'F': |
| 894 | /* End (xterm) */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 895 | input_end(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 896 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 897 | default: |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 898 | beep(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | c = 0; |
| 902 | break; |
| 903 | } |
| 904 | |
| 905 | default: /* If it's regular input, do the normal thing */ |
| 906 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 907 | if (!isprint(c)) { /* Skip non-printable characters */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 908 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 909 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 910 | |
| 911 | if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */ |
| 912 | break; |
| 913 | |
| 914 | len++; |
| 915 | |
| 916 | if (cursor == (len - 1)) { /* Append if at the end of the line */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 917 | *(command + cursor) = c; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 918 | cmdedit_set_out_char(c, command[cursor+1]); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 919 | } else { /* Insert otherwise */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 920 | memmove(command + cursor + 1, command + cursor, |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 921 | len - cursor - 1); |
| 922 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 923 | *(command + cursor) = c; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 924 | j = cursor+1; |
| 925 | /* rewrite from cursor */ |
| 926 | input_end (); |
| 927 | /* to prev x pos + 1 */ |
| 928 | while(cursor > j) |
| 929 | input_backward(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 932 | break; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 933 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 934 | if (c == '\t') |
| 935 | lastWasTab = TRUE; |
| 936 | else |
| 937 | lastWasTab = FALSE; |
| 938 | |
| 939 | if (break_out) /* Enter is the command terminator, no more input. */ |
| 940 | break; |
| 941 | } |
| 942 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 943 | setTermSettings (inputFd, (void *) &initial_settings); |
| 944 | handlers_sets &= ~SET_RESET_TERM; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 945 | |
| 946 | /* Handle command history log */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 947 | if (len>1) { /* no put empty line (only '\n') */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 948 | |
| 949 | struct history *h = his_end; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 950 | char *ss; |
| 951 | |
| 952 | command[len-1] = 0; /* destroy end '\n' */ |
| 953 | ss = strdup(command); /* duplicate without '\n' */ |
| 954 | command[len-1] = '\n'; /* restore '\n' */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 955 | |
| 956 | if (!h) { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 957 | /* No previous history -- this memory is never freed */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 958 | h = his_front = xmalloc(sizeof(struct history)); |
| 959 | h->n = xmalloc(sizeof(struct history)); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 960 | |
| 961 | h->p = NULL; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 962 | h->s = ss; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 963 | h->n->p = h; |
| 964 | h->n->n = NULL; |
| 965 | h->n->s = NULL; |
| 966 | his_end = h->n; |
| 967 | history_counter++; |
| 968 | } else { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 969 | /* Add a new history command -- this memory is never freed */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 970 | h->n = xmalloc(sizeof(struct history)); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 971 | |
| 972 | h->n->p = h; |
| 973 | h->n->n = NULL; |
| 974 | h->n->s = NULL; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 975 | h->s = ss; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 976 | his_end = h->n; |
| 977 | |
| 978 | /* After max history, remove the oldest command */ |
| 979 | if (history_counter >= MAX_HISTORY) { |
| 980 | |
| 981 | struct history *p = his_front->n; |
| 982 | |
| 983 | p->p = NULL; |
| 984 | free(his_front->s); |
| 985 | free(his_front); |
| 986 | his_front = p; |
| 987 | } else { |
| 988 | history_counter++; |
| 989 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 990 | } |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 991 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 992 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 993 | return; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 996 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 997 | /* Undo the effects of cmdedit_init(). */ |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 998 | extern void cmdedit_terminate(void) |
| 999 | { |
| 1000 | cmdedit_reset_term(); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1001 | if((handlers_sets & SET_TERM_HANDLERS)!=0) { |
| 1002 | signal(SIGKILL, SIG_DFL); |
| 1003 | signal(SIGINT, SIG_DFL); |
| 1004 | signal(SIGQUIT, SIG_DFL); |
| 1005 | signal(SIGTERM, SIG_DFL); |
| 1006 | signal(SIGWINCH, SIG_DFL); |
| 1007 | handlers_sets &= ~SET_TERM_HANDLERS; |
| 1008 | } |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | |
| 1012 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1013 | #endif /* BB_FEATURE_SH_COMMAND_EDITING */ |