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