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 | /* |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 3 | * Termios command line History and Editting. |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 5 | * Copyright (c) 1986-2001 may safely be consumed by a BSD or GPL license. |
| 6 | * Written by: Vladimir Oleynik <vodz@usa.net> |
| 7 | * |
| 8 | * Used ideas: |
| 9 | * Adam Rogoyski <rogoyski@cs.utexas.edu> |
| 10 | * Dave Cinege <dcinege@psychosis.com> |
| 11 | * Jakub Jelinek (c) 1995 |
| 12 | * Erik Andersen <andersee@debian.org> (Majorly adjusted for busybox) |
| 13 | * |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 14 | * This code is 'as is' with no warranty. |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 15 | * |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | Usage and Known bugs: |
| 21 | Terminal key codes are not extensive, and more will probably |
| 22 | need to be added. This version was created on Debian GNU/Linux 2.x. |
| 23 | Delete, Backspace, Home, End, and the arrow keys were tested |
| 24 | 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] | 25 | Ctrl-E also works as End. |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 26 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 27 | Small bugs (simple effect): |
| 28 | - not true viewing if terminal size (x*y symbols) less |
| 29 | size (prompt + editor`s line + 2 symbols) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 32 | |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 33 | //#define TEST |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 34 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
| 36 | #include <errno.h> |
| 37 | #include <unistd.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <sys/ioctl.h> |
| 41 | #include <ctype.h> |
| 42 | #include <signal.h> |
| 43 | #include <limits.h> |
| 44 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 45 | #ifndef TEST |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 46 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 47 | #include "busybox.h" |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 48 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 49 | #define D(x) |
| 50 | |
| 51 | #else |
| 52 | |
Eric Andersen | 94456f5 | 2001-02-18 20:26:48 +0000 | [diff] [blame] | 53 | #define BB_FEATURE_SH_COMMAND_EDITING |
| 54 | #define BB_FEATURE_SH_TAB_COMPLETION |
| 55 | #define BB_FEATURE_SH_USERNAME_COMPLETION |
| 56 | #define BB_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 57 | #define BB_FEATURE_BASH_STYLE_PROMT |
| 58 | #define BB_FEATURE_CLEAN_UP |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 59 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 60 | #define D(x) x |
| 61 | |
| 62 | #endif /* TEST */ |
| 63 | |
Eric Andersen | 5165fbe | 2001-02-20 06:42:29 +0000 | [diff] [blame^] | 64 | #ifdef BB_FEATURE_SH_TAB_COMPLETION |
| 65 | #include <dirent.h> |
| 66 | #include <sys/stat.h> |
| 67 | #endif |
| 68 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 69 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
| 70 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 71 | #ifndef BB_FEATURE_SH_TAB_COMPLETION |
Eric Andersen | 28a78ab | 2001-02-16 20:26:50 +0000 | [diff] [blame] | 72 | #undef BB_FEATURE_SH_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 73 | #endif |
| 74 | |
Eric Andersen | 28a78ab | 2001-02-16 20:26:50 +0000 | [diff] [blame] | 75 | #if defined(BB_FEATURE_SH_USERNAME_COMPLETION) || defined(BB_FEATURE_BASH_STYLE_PROMT) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 76 | #define BB_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 77 | #endif |
| 78 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 79 | #ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 80 | #ifndef TEST |
Eric Andersen | af4ac77 | 2001-02-01 22:43:49 +0000 | [diff] [blame] | 81 | #include "pwd_grp/pwd.h" |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 82 | #else |
| 83 | #include <pwd.h> |
| 84 | #endif /* TEST */ |
| 85 | #endif /* advanced FEATURES */ |
Eric Andersen | af4ac77 | 2001-02-01 22:43:49 +0000 | [diff] [blame] | 86 | |
| 87 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 88 | #ifdef TEST |
| 89 | void *xrealloc(void *old, size_t size) |
| 90 | { |
| 91 | return realloc(old, size); |
| 92 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 93 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 94 | void *xmalloc(size_t size) |
| 95 | { |
| 96 | return malloc(size); |
| 97 | } |
| 98 | char *xstrdup(const char *s) |
| 99 | { |
| 100 | return strdup(s); |
| 101 | } |
| 102 | |
| 103 | void *xcalloc(size_t size, size_t se) |
| 104 | { |
| 105 | return calloc(size, se); |
| 106 | } |
| 107 | |
| 108 | #define error_msg(s, d) fprintf(stderr, s, d) |
| 109 | #endif |
| 110 | |
| 111 | |
| 112 | struct history { |
| 113 | char *s; |
| 114 | struct history *p; |
| 115 | struct history *n; |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 118 | /* Maximum length of the linked list for the command line history */ |
| 119 | static const int MAX_HISTORY = 15; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 120 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 121 | /* First element in command line list */ |
| 122 | static struct history *his_front = NULL; |
| 123 | |
| 124 | /* Last element in command line list */ |
| 125 | static struct history *his_end = NULL; |
| 126 | |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 127 | |
| 128 | /* ED: sparc termios is broken: revert back to old termio handling. */ |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 129 | |
| 130 | #if #cpu(sparc) |
| 131 | # include <termio.h> |
| 132 | # define termios termio |
| 133 | # define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp) |
| 134 | # define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp) |
| 135 | #else |
| 136 | # include <termios.h> |
| 137 | # define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) |
| 138 | # define getTermSettings(fd,argp) tcgetattr(fd, argp); |
| 139 | #endif |
| 140 | |
| 141 | /* Current termio and the previous termio before starting sh */ |
Eric Andersen | 63a8622 | 2000-11-07 06:52:13 +0000 | [diff] [blame] | 142 | static struct termios initial_settings, new_settings; |
Erik Andersen | 8ea7d8c | 2000-05-20 00:40:08 +0000 | [diff] [blame] | 143 | |
| 144 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 145 | #ifndef _POSIX_VDISABLE |
| 146 | #define _POSIX_VDISABLE '\0' |
Erik Andersen | 8ea7d8c | 2000-05-20 00:40:08 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 149 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 150 | static |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 151 | volatile int cmdedit_termw = 80; /* actual terminal width */ |
| 152 | static int history_counter = 0; /* Number of commands in history list */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 153 | static |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 154 | volatile int handlers_sets = 0; /* Set next bites: */ |
| 155 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 156 | enum { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 157 | SET_ATEXIT = 1, /* when atexit() has been called and |
| 158 | get euid,uid,gid to fast compare */ |
| 159 | SET_TERM_HANDLERS = 2, /* set many terminates signal handlers */ |
| 160 | SET_WCHG_HANDLERS = 4, /* winchg signal handler */ |
| 161 | SET_RESET_TERM = 8, /* if the terminal needs to be reset upon exit */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 162 | }; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 163 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 164 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 165 | static int cmdedit_x; /* real x terminal position */ |
| 166 | static int cmdedit_y; /* pseudoreal y terminal position */ |
| 167 | static int cmdedit_prmt_len; /* lenght prompt without colores string */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 168 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 169 | static int cursor; /* required global for signal handler */ |
| 170 | static int len; /* --- "" - - "" - -"- --""-- --""--- */ |
| 171 | static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */ |
| 172 | static |
| 173 | #ifndef BB_FEATURE_BASH_STYLE_PROMT |
| 174 | const |
| 175 | #endif |
| 176 | char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */ |
| 177 | |
| 178 | /* Link into lash to reset context to 0 on ^C and such */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 179 | extern unsigned int shell_context; |
| 180 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 181 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 182 | #ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 183 | static char *user_buf = ""; |
| 184 | static char *home_pwd_buf = ""; |
| 185 | static int my_euid; |
| 186 | #endif |
| 187 | |
| 188 | #ifdef BB_FEATURE_BASH_STYLE_PROMT |
| 189 | static char *hostname_buf = ""; |
| 190 | static int num_ok_lines = 1; |
| 191 | #endif |
| 192 | |
| 193 | |
| 194 | #ifdef BB_FEATURE_SH_TAB_COMPLETION |
| 195 | |
| 196 | #ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 197 | static int my_euid; |
| 198 | #endif |
| 199 | |
| 200 | static int my_uid; |
| 201 | static int my_gid; |
| 202 | |
| 203 | #endif /* BB_FEATURE_SH_TAB_COMPLETION */ |
| 204 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 205 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 206 | static void cmdedit_setwidth(int w, int redraw_flg); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 207 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 208 | static void win_changed(int nsig) |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 209 | { |
| 210 | struct winsize win = { 0, 0, 0, 0 }; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 211 | static __sighandler_t previous_SIGWINCH_handler; /* for reset */ |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 212 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 213 | /* emulate || signal call */ |
| 214 | if (nsig == -SIGWINCH || nsig == SIGWINCH) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 215 | ioctl(0, TIOCGWINSZ, &win); |
| 216 | if (win.ws_col > 0) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 217 | cmdedit_setwidth(win.ws_col, nsig == SIGWINCH); |
| 218 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 219 | } |
Eric Andersen | 4bbdd78 | 2001-01-30 22:23:17 +0000 | [diff] [blame] | 220 | /* Unix not all standart in recall signal */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 221 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 222 | if (nsig == -SIGWINCH) /* save previous handler */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 223 | previous_SIGWINCH_handler = signal(SIGWINCH, win_changed); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 224 | else if (nsig == SIGWINCH) /* signaled called handler */ |
| 225 | signal(SIGWINCH, win_changed); /* set for next call */ |
| 226 | else /* nsig == 0 */ |
| 227 | /* set previous handler */ |
| 228 | signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 229 | } |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 230 | |
| 231 | static void cmdedit_reset_term(void) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 232 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 233 | if ((handlers_sets & SET_RESET_TERM) != 0) { |
Erik Andersen | a6c7522 | 2000-04-18 00:00:52 +0000 | [diff] [blame] | 234 | /* sparc and other have broken termios support: use old termio handling. */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 235 | setTermSettings(fileno(stdin), (void *) &initial_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 236 | handlers_sets &= ~SET_RESET_TERM; |
| 237 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 238 | if ((handlers_sets & SET_WCHG_HANDLERS) != 0) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 239 | /* reset SIGWINCH handler to previous (default) */ |
| 240 | win_changed(0); |
| 241 | handlers_sets &= ~SET_WCHG_HANDLERS; |
| 242 | } |
| 243 | fflush(stdout); |
Eric Andersen | b040d4f | 2000-07-25 18:01:20 +0000 | [diff] [blame] | 244 | #ifdef BB_FEATURE_CLEAN_UP |
| 245 | if (his_front) { |
| 246 | struct history *n; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 247 | |
Eric Andersen | b040d4f | 2000-07-25 18:01:20 +0000 | [diff] [blame] | 248 | //while(his_front!=his_end) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 249 | while (his_front != his_end) { |
Eric Andersen | b040d4f | 2000-07-25 18:01:20 +0000 | [diff] [blame] | 250 | n = his_front->n; |
| 251 | free(his_front->s); |
| 252 | free(his_front); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 253 | his_front = n; |
Eric Andersen | b040d4f | 2000-07-25 18:01:20 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | #endif |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 259 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 260 | /* special for recount position for scroll and remove terminal margin effect */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 261 | static void cmdedit_set_out_char(int next_char) |
| 262 | { |
| 263 | |
| 264 | int c = command_ps[cursor]; |
| 265 | |
| 266 | if (c == 0) |
| 267 | c = ' '; /* destroy end char? */ |
| 268 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 269 | if (!isprint(c)) { /* Inverse put non-printable characters */ |
| 270 | if (((unsigned char) c) >= 128) |
| 271 | c -= 128; |
| 272 | if (((unsigned char) c) < ' ') |
| 273 | c += '@'; |
| 274 | if (c == 127) |
| 275 | c = '?'; |
| 276 | printf("\033[7m%c\033[0m", c); |
| 277 | } else |
| 278 | #endif |
| 279 | putchar(c); |
| 280 | if (++cmdedit_x >= cmdedit_termw) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 281 | /* terminal is scrolled down */ |
| 282 | cmdedit_y++; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 283 | cmdedit_x = 0; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 284 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 285 | if (!next_char) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 286 | next_char = ' '; |
| 287 | /* destroy "(auto)margin" */ |
| 288 | putchar(next_char); |
| 289 | putchar('\b'); |
| 290 | } |
| 291 | cursor++; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 294 | /* Move to end line. Bonus: rewrite line from cursor */ |
| 295 | static void input_end(void) |
| 296 | { |
| 297 | while (cursor < len) |
| 298 | cmdedit_set_out_char(0); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* Go to the next line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 302 | static void goto_new_line(void) |
| 303 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 304 | input_end(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 305 | if (cmdedit_x) |
| 306 | putchar('\n'); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 310 | static inline void out1str(const char *s) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 311 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 312 | fputs(s, stdout); |
| 313 | } |
| 314 | static inline void beep(void) |
| 315 | { |
| 316 | putchar('\007'); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 319 | /* Move back one charactor */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 320 | /* special for slow terminal */ |
| 321 | static void input_backward(int num) |
| 322 | { |
| 323 | if (num > cursor) |
| 324 | num = cursor; |
| 325 | cursor -= num; /* new cursor (in command, not terminal) */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 326 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 327 | if (cmdedit_x >= num) { /* no to up line */ |
| 328 | cmdedit_x -= num; |
| 329 | if (num < 4) |
| 330 | while (num-- > 0) |
| 331 | putchar('\b'); |
| 332 | |
| 333 | else |
| 334 | printf("\033[%dD", num); |
| 335 | } else { |
| 336 | int count_y; |
| 337 | |
| 338 | if (cmdedit_x) { |
| 339 | putchar('\r'); /* back to first terminal pos. */ |
| 340 | num -= cmdedit_x; /* set previous backward */ |
| 341 | } |
| 342 | count_y = 1 + num / cmdedit_termw; |
| 343 | printf("\033[%dA", count_y); |
| 344 | cmdedit_y -= count_y; |
| 345 | /* require forward after uping */ |
| 346 | cmdedit_x = cmdedit_termw * count_y - num; |
| 347 | printf("\033[%dC", cmdedit_x); /* set term cursor */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 348 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 351 | static void put_prompt(void) |
| 352 | { |
| 353 | out1str(cmdedit_prompt); |
| 354 | cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */ |
| 355 | cursor = 0; |
| 356 | } |
| 357 | |
| 358 | #ifdef BB_FEATURE_BASH_STYLE_PROMT |
| 359 | static void |
| 360 | add_to_prompt(char **prmt_mem_ptr, int *alm, int *prmt_len, |
| 361 | const char *addb) |
| 362 | { |
| 363 | int l = strlen(addb); |
| 364 | |
| 365 | *prmt_len += l; |
| 366 | if (*alm < (*prmt_len) + 1) { |
| 367 | *alm = (*prmt_len) + 1; |
| 368 | *prmt_mem_ptr = xrealloc(*prmt_mem_ptr, *alm); |
| 369 | } |
| 370 | strcat(*prmt_mem_ptr, addb); |
| 371 | } |
| 372 | #endif |
| 373 | |
| 374 | static void parse_prompt(const char *prmt_ptr) |
| 375 | { |
| 376 | #ifdef BB_FEATURE_BASH_STYLE_PROMT |
| 377 | int alm = strlen(prmt_ptr) + 1; /* supposedly require memory */ |
| 378 | int prmt_len = 0; |
| 379 | int sub_len = 0; |
| 380 | int flg_not_length = '['; |
| 381 | char *prmt_mem_ptr = xstrdup(prmt_ptr); |
| 382 | char pwd_buf[PATH_MAX + 1]; |
| 383 | char buf[16]; |
| 384 | int c; |
| 385 | |
| 386 | pwd_buf[0] = 0; |
| 387 | *prmt_mem_ptr = 0; |
| 388 | |
| 389 | while (*prmt_ptr) { |
| 390 | c = *prmt_ptr++; |
| 391 | if (c == '\\') { |
| 392 | c = *prmt_ptr; |
| 393 | if (c == 0) |
| 394 | break; |
| 395 | prmt_ptr++; |
| 396 | switch (c) { |
| 397 | case 'u': |
| 398 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, user_buf); |
| 399 | continue; |
| 400 | case 'h': |
| 401 | if (hostname_buf[0] == 0) { |
| 402 | hostname_buf = xcalloc(256, 1); |
| 403 | if (gethostname(hostname_buf, 255) < 0) { |
| 404 | strcpy(hostname_buf, "?"); |
| 405 | } else { |
| 406 | char *s = strchr(hostname_buf, '.'); |
| 407 | |
| 408 | if (s) |
| 409 | *s = 0; |
| 410 | } |
| 411 | } |
| 412 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, |
| 413 | hostname_buf); |
| 414 | continue; |
| 415 | case '$': |
| 416 | c = my_euid == 0 ? '#' : '$'; |
| 417 | break; |
| 418 | case 'w': |
| 419 | if (pwd_buf[0] == 0) { |
| 420 | int l; |
| 421 | |
| 422 | getcwd(pwd_buf, PATH_MAX); |
| 423 | l = strlen(home_pwd_buf); |
| 424 | if (home_pwd_buf[0] != 0 && |
| 425 | strncmp(home_pwd_buf, pwd_buf, l) == 0) { |
| 426 | strcpy(pwd_buf + 1, pwd_buf + l); |
| 427 | pwd_buf[0] = '~'; |
| 428 | } |
| 429 | } |
| 430 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, pwd_buf); |
| 431 | continue; |
| 432 | case '!': |
| 433 | snprintf(buf, sizeof(buf), "%d", num_ok_lines); |
| 434 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf); |
| 435 | continue; |
| 436 | case 'e': |
| 437 | case 'E': /* \e \E = \033 */ |
| 438 | c = '\033'; |
| 439 | break; |
| 440 | case 'x': |
| 441 | case 'X': |
| 442 | case '0': |
| 443 | case '1': |
| 444 | case '2': |
| 445 | case '3': |
| 446 | case '4': |
| 447 | case '5': |
| 448 | case '6': |
| 449 | case '7':{ |
| 450 | int l; |
| 451 | int ho = 0; |
| 452 | char *eho; |
| 453 | |
| 454 | if (c == 'X') |
| 455 | c = 'x'; |
| 456 | |
| 457 | for (l = 0; l < 3;) { |
| 458 | |
| 459 | buf[l++] = *prmt_ptr; |
| 460 | buf[l] = 0; |
| 461 | ho = strtol(buf, &eho, c == 'x' ? 16 : 8); |
| 462 | if (ho > UCHAR_MAX || (eho - buf) < l) { |
| 463 | l--; |
| 464 | break; |
| 465 | } |
| 466 | prmt_ptr++; |
| 467 | } |
| 468 | buf[l] = 0; |
| 469 | ho = strtol(buf, 0, c == 'x' ? 16 : 8); |
| 470 | c = ho == 0 ? '?' : (char) ho; |
| 471 | break; |
| 472 | } |
| 473 | case '[': |
| 474 | case ']': |
| 475 | if (c == flg_not_length) { |
| 476 | flg_not_length = flg_not_length == '[' ? ']' : '['; |
| 477 | continue; |
| 478 | } |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | buf[0] = c; |
| 483 | buf[1] = 0; |
| 484 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf); |
| 485 | if (flg_not_length == ']') |
| 486 | sub_len++; |
| 487 | } |
| 488 | cmdedit_prmt_len = prmt_len - sub_len; |
| 489 | cmdedit_prompt = prmt_mem_ptr; |
| 490 | #else |
| 491 | cmdedit_prompt = prmt_ptr; |
| 492 | cmdedit_prmt_len = strlen(prmt_ptr); |
| 493 | #endif |
| 494 | put_prompt(); |
| 495 | } |
| 496 | |
| 497 | |
| 498 | /* draw promt, editor line, and clear tail */ |
| 499 | static void redraw(int y, int back_cursor) |
| 500 | { |
| 501 | if (y > 0) /* up to start y */ |
| 502 | printf("\033[%dA", y); |
| 503 | cmdedit_y = 0; /* new quasireal y */ |
| 504 | putchar('\r'); |
| 505 | put_prompt(); |
| 506 | input_end(); /* rewrite */ |
| 507 | printf("\033[J"); /* destroy tail after cursor */ |
| 508 | input_backward(back_cursor); |
| 509 | } |
| 510 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 511 | /* Delete the char in front of the cursor */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 512 | static void input_delete(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 513 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 514 | int j = cursor; |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 515 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 516 | if (j == len) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 517 | return; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 518 | |
| 519 | strcpy(command_ps + j, command_ps + j + 1); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 520 | len--; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 521 | input_end(); /* rewtite new line */ |
| 522 | cmdedit_set_out_char(0); /* destroy end char */ |
| 523 | input_backward(cursor - j); /* back to old pos cursor */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 526 | /* Delete the char in back of the cursor */ |
| 527 | static void input_backspace(void) |
| 528 | { |
| 529 | if (cursor > 0) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 530 | input_backward(1); |
| 531 | input_delete(); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
| 535 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 536 | /* Move forward one charactor */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 537 | static void input_forward(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 538 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 539 | if (cursor < len) |
| 540 | cmdedit_set_out_char(command_ps[cursor + 1]); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 544 | static void clean_up_and_die(int sig) |
| 545 | { |
| 546 | goto_new_line(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 547 | if (sig != SIGINT) |
| 548 | exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 549 | cmdedit_reset_term(); |
| 550 | } |
| 551 | |
| 552 | static void cmdedit_setwidth(int w, int redraw_flg) |
| 553 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 554 | cmdedit_termw = cmdedit_prmt_len + 2; |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 555 | if (w <= cmdedit_termw) { |
| 556 | cmdedit_termw = cmdedit_termw % w; |
| 557 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 558 | if (w > cmdedit_termw) { |
| 559 | |
| 560 | cmdedit_termw = w; |
| 561 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 562 | if (redraw_flg) { |
| 563 | /* new y for current cursor */ |
| 564 | int new_y = (cursor + cmdedit_prmt_len) / w; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 565 | |
| 566 | /* redraw */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 567 | redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor); |
| 568 | fflush(stdout); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 569 | } |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 570 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | extern void cmdedit_init(void) |
| 574 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 575 | if ((handlers_sets & SET_WCHG_HANDLERS) == 0) { |
| 576 | /* emulate usage handler to set handler and call yours work */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 577 | win_changed(-SIGWINCH); |
| 578 | handlers_sets |= SET_WCHG_HANDLERS; |
| 579 | } |
| 580 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 581 | if ((handlers_sets & SET_ATEXIT) == 0) { |
| 582 | #ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 583 | struct passwd *entry; |
| 584 | |
| 585 | my_euid = geteuid(); |
| 586 | entry = getpwuid(my_euid); |
| 587 | if (entry) { |
| 588 | user_buf = xstrdup(entry->pw_name); |
| 589 | home_pwd_buf = xstrdup(entry->pw_dir); |
| 590 | } |
| 591 | #endif |
| 592 | |
| 593 | #ifdef BB_FEATURE_SH_TAB_COMPLETION |
| 594 | |
| 595 | #ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 596 | my_euid = geteuid(); |
| 597 | #endif |
| 598 | my_uid = getuid(); |
| 599 | my_gid = getgid(); |
| 600 | #endif /* BB_FEATURE_SH_TAB_COMPLETION */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 601 | handlers_sets |= SET_ATEXIT; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 602 | atexit(cmdedit_reset_term); /* be sure to do this only once */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 603 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 604 | |
| 605 | if ((handlers_sets & SET_TERM_HANDLERS) == 0) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 606 | signal(SIGKILL, clean_up_and_die); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 607 | signal(SIGINT, clean_up_and_die); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 608 | signal(SIGQUIT, clean_up_and_die); |
| 609 | signal(SIGTERM, clean_up_and_die); |
| 610 | handlers_sets |= SET_TERM_HANDLERS; |
| 611 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 612 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 613 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 614 | |
| 615 | #ifdef BB_FEATURE_SH_TAB_COMPLETION |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 616 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 617 | static int is_execute(const struct stat *st) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 618 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 619 | if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) || |
| 620 | (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) || |
| 621 | (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) || |
| 622 | (st->st_mode & S_IXOTH)) return TRUE; |
| 623 | return FALSE; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 624 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 625 | |
Eric Andersen | 28a78ab | 2001-02-16 20:26:50 +0000 | [diff] [blame] | 626 | #ifdef BB_FEATURE_SH_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 627 | |
| 628 | static char **username_tab_completion(char *ud, int *num_matches) |
| 629 | { |
| 630 | struct passwd *entry; |
| 631 | int userlen; |
| 632 | char *temp; |
| 633 | |
| 634 | |
| 635 | ud++; /* ~user/... to user/... */ |
| 636 | userlen = strlen(ud); |
| 637 | |
| 638 | if (num_matches == 0) { /* "~/..." or "~user/..." */ |
| 639 | char *sav_ud = ud - 1; |
| 640 | char *home = 0; |
| 641 | |
| 642 | if (*ud == '/') { /* "~/..." */ |
| 643 | home = home_pwd_buf; |
| 644 | } else { |
| 645 | /* "~user/..." */ |
| 646 | temp = strchr(ud, '/'); |
| 647 | *temp = 0; /* ~user\0 */ |
| 648 | entry = getpwnam(ud); |
| 649 | *temp = '/'; /* restore ~user/... */ |
| 650 | ud = temp; |
| 651 | if (entry) |
| 652 | home = entry->pw_dir; |
| 653 | } |
| 654 | if (home) { |
| 655 | if ((userlen + strlen(home) + 1) < BUFSIZ) { |
| 656 | char temp2[BUFSIZ]; /* argument size */ |
| 657 | |
| 658 | /* /home/user/... */ |
| 659 | sprintf(temp2, "%s%s", home, ud); |
| 660 | strcpy(sav_ud, temp2); |
| 661 | } |
| 662 | } |
| 663 | return 0; /* void, result save to argument :-) */ |
| 664 | } else { |
| 665 | /* "~[^/]*" */ |
| 666 | char **matches = (char **) NULL; |
| 667 | int nm = 0; |
| 668 | |
| 669 | setpwent(); |
| 670 | |
| 671 | while ((entry = getpwent()) != NULL) { |
| 672 | /* Null usernames should result in all users as possible completions. */ |
| 673 | if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) { |
| 674 | |
| 675 | temp = xmalloc(3 + strlen(entry->pw_name)); |
| 676 | sprintf(temp, "~%s/", entry->pw_name); |
| 677 | matches = xrealloc(matches, (nm + 1) * sizeof(char *)); |
| 678 | |
| 679 | matches[nm++] = temp; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | endpwent(); |
| 684 | (*num_matches) = nm; |
| 685 | return (matches); |
| 686 | } |
| 687 | } |
Eric Andersen | 28a78ab | 2001-02-16 20:26:50 +0000 | [diff] [blame] | 688 | #endif /* BB_FEATURE_SH_USERNAME_COMPLETION */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 689 | |
| 690 | enum { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 691 | FIND_EXE_ONLY = 0, |
| 692 | FIND_DIR_ONLY = 1, |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 693 | FIND_FILE_ONLY = 2, |
| 694 | }; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 695 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 696 | static int path_parse(char ***p, int flags) |
| 697 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 698 | int npth; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 699 | char *tmp; |
| 700 | char *pth; |
| 701 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 702 | /* if not setenv PATH variable, to search cur dir "." */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 703 | if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 || |
| 704 | /* PATH=<empty> or PATH=:<empty> */ |
| 705 | *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 706 | return 1; |
| 707 | } |
| 708 | |
| 709 | tmp = pth; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 710 | npth = 0; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 711 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 712 | for (;;) { |
| 713 | npth++; /* count words is + 1 count ':' */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 714 | tmp = strchr(tmp, ':'); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 715 | if (tmp) { |
| 716 | if (*++tmp == 0) |
| 717 | break; /* :<empty> */ |
| 718 | } else |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 719 | break; |
| 720 | } |
| 721 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 722 | *p = xmalloc(npth * sizeof(char *)); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 723 | |
| 724 | tmp = pth; |
| 725 | (*p)[0] = xstrdup(tmp); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 726 | npth = 1; /* count words is + 1 count ':' */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 727 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 728 | for (;;) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 729 | tmp = strchr(tmp, ':'); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 730 | if (tmp) { |
| 731 | (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */ |
| 732 | if (*++tmp == 0) |
| 733 | break; /* :<empty> */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 734 | } else |
| 735 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 736 | (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | return npth; |
| 740 | } |
| 741 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 742 | static char *add_quote_for_spec_chars(char *found) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 743 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 744 | int l = 0; |
| 745 | char *s = xmalloc((strlen(found) + 1) * 2); |
| 746 | |
| 747 | while (*found) { |
| 748 | if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found)) |
| 749 | s[l++] = '\\'; |
| 750 | s[l++] = *found++; |
| 751 | } |
| 752 | s[l] = 0; |
| 753 | return s; |
| 754 | } |
| 755 | |
| 756 | static char **exe_n_cwd_tab_completion(char *command, int *num_matches, |
| 757 | int type) |
| 758 | { |
| 759 | |
| 760 | char **matches = 0; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 761 | DIR *dir; |
| 762 | struct dirent *next; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 763 | char dirbuf[BUFSIZ]; |
| 764 | int nm = *num_matches; |
| 765 | struct stat st; |
| 766 | char *path1[1]; |
| 767 | char **paths = path1; |
| 768 | int npaths; |
| 769 | int i; |
| 770 | char found[BUFSIZ + 4 + PATH_MAX]; |
| 771 | char *pfind = strrchr(command, '/'); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 772 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 773 | path1[0] = "."; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 774 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 775 | if (pfind == NULL) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 776 | /* no dir, if flags==EXE_ONLY - get paths, else "." */ |
| 777 | npaths = path_parse(&paths, type); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 778 | pfind = command; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 779 | } else { |
| 780 | /* with dir */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 781 | /* save for change */ |
| 782 | strcpy(dirbuf, command); |
| 783 | /* set dir only */ |
| 784 | dirbuf[(pfind - command) + 1] = 0; |
Eric Andersen | 28a78ab | 2001-02-16 20:26:50 +0000 | [diff] [blame] | 785 | #ifdef BB_FEATURE_SH_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 786 | if (dirbuf[0] == '~') /* ~/... or ~user/... */ |
| 787 | username_tab_completion(dirbuf, 0); |
| 788 | #endif |
| 789 | /* "strip" dirname in command */ |
| 790 | pfind++; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 791 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 792 | paths[0] = dirbuf; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 793 | npaths = 1; /* only 1 dir */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 794 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 795 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 796 | for (i = 0; i < npaths; i++) { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 797 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 798 | dir = opendir(paths[i]); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 799 | if (!dir) /* Don't print an error */ |
| 800 | continue; |
| 801 | |
| 802 | while ((next = readdir(dir)) != NULL) { |
| 803 | const char *str_merge = "%s/%s"; |
| 804 | char *str_found = next->d_name; |
| 805 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 806 | /* matched ? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 807 | if (strncmp(str_found, pfind, strlen(pfind))) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 808 | continue; |
| 809 | /* not see .name without .match */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 810 | if (*str_found == '.' && *pfind == 0) { |
| 811 | if (*paths[i] == '/' && paths[i][1] == 0 |
| 812 | && str_found[1] == 0) str_found = ""; /* only "/" */ |
| 813 | else |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 814 | continue; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 815 | } |
| 816 | if (paths[i][strlen(paths[i]) - 1] == '/') |
| 817 | str_merge = "%s%s"; |
| 818 | sprintf(found, str_merge, paths[i], str_found); |
| 819 | /* hmm, remover in progress? */ |
| 820 | if (stat(found, &st) < 0) |
| 821 | continue; |
| 822 | /* find with dirs ? */ |
| 823 | if (paths[i] != dirbuf) |
| 824 | strcpy(found, next->d_name); /* only name */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 825 | if (S_ISDIR(st.st_mode)) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 826 | /* name is directory */ |
| 827 | /* algorithmic only "/" ? */ |
| 828 | if (*str_found) |
| 829 | strcat(found, "/"); |
| 830 | str_found = add_quote_for_spec_chars(found); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 831 | } else { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 832 | /* not put found file if search only dirs for cd */ |
| 833 | if (type == FIND_DIR_ONLY) |
| 834 | continue; |
| 835 | str_found = add_quote_for_spec_chars(found); |
| 836 | if (type == FIND_FILE_ONLY || |
| 837 | (type == FIND_EXE_ONLY && is_execute(&st) == TRUE)) |
| 838 | strcat(str_found, " "); |
| 839 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 840 | /* Add it to the list */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 841 | matches = xrealloc(matches, (nm + 1) * sizeof(char *)); |
| 842 | |
| 843 | matches[nm++] = str_found; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 844 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 845 | closedir(dir); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 846 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 847 | if (paths != path1) { |
| 848 | free(paths[0]); /* allocated memory only in first member */ |
| 849 | free(paths); |
| 850 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 851 | *num_matches = nm; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 852 | return (matches); |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 853 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 854 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 855 | static int match_compare(const void *a, const void *b) |
| 856 | { |
| 857 | return strcmp(*(char **) a, *(char **) b); |
| 858 | } |
| 859 | |
| 860 | |
| 861 | |
| 862 | #define QUOT (UCHAR_MAX+1) |
| 863 | |
| 864 | #define collapse_pos(is, in) { \ |
| 865 | memcpy(int_buf+is, int_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); \ |
| 866 | memcpy(pos_buf+is, pos_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); } |
| 867 | |
| 868 | static int find_match(char *matchBuf, int *len_with_quotes) |
| 869 | { |
| 870 | int i, j; |
| 871 | int command_mode; |
| 872 | int c, c2; |
| 873 | int int_buf[BUFSIZ + 1]; |
| 874 | int pos_buf[BUFSIZ + 1]; |
| 875 | |
| 876 | /* set to integer dimension characters and own positions */ |
| 877 | for (i = 0;; i++) { |
| 878 | int_buf[i] = (int) ((unsigned char) matchBuf[i]); |
| 879 | if (int_buf[i] == 0) { |
| 880 | pos_buf[i] = -1; /* indicator end line */ |
| 881 | break; |
| 882 | } else |
| 883 | pos_buf[i] = i; |
| 884 | } |
| 885 | |
| 886 | /* mask \+symbol and convert '\t' to ' ' */ |
| 887 | for (i = j = 0; matchBuf[i]; i++, j++) |
| 888 | if (matchBuf[i] == '\\') { |
| 889 | collapse_pos(j, j + 1); |
| 890 | int_buf[j] |= QUOT; |
| 891 | i++; |
| 892 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 893 | if (matchBuf[i] == '\t') /* algorithm equivalent */ |
| 894 | int_buf[j] = ' ' | QUOT; |
| 895 | #endif |
| 896 | } |
| 897 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 898 | else if (matchBuf[i] == '\t') |
| 899 | int_buf[j] = ' '; |
| 900 | #endif |
| 901 | |
| 902 | /* mask "symbols" or 'symbols' */ |
| 903 | c2 = 0; |
| 904 | for (i = 0; int_buf[i]; i++) { |
| 905 | c = int_buf[i]; |
| 906 | if (c == '\'' || c == '"') { |
| 907 | if (c2 == 0) |
| 908 | c2 = c; |
| 909 | else { |
| 910 | if (c == c2) |
| 911 | c2 = 0; |
| 912 | else |
| 913 | int_buf[i] |= QUOT; |
| 914 | } |
| 915 | } else if (c2 != 0 && c != '$') |
| 916 | int_buf[i] |= QUOT; |
| 917 | } |
| 918 | |
| 919 | /* skip commands with arguments if line have commands delimiters */ |
| 920 | /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */ |
| 921 | for (i = 0; int_buf[i]; i++) { |
| 922 | c = int_buf[i]; |
| 923 | c2 = int_buf[i + 1]; |
| 924 | j = i ? int_buf[i - 1] : -1; |
| 925 | command_mode = 0; |
| 926 | if (c == ';' || c == '&' || c == '|') { |
| 927 | command_mode = 1 + (c == c2); |
| 928 | if (c == '&') { |
| 929 | if (j == '>' || j == '<') |
| 930 | command_mode = 0; |
| 931 | } else if (c == '|' && j == '>') |
| 932 | command_mode = 0; |
| 933 | } |
| 934 | if (command_mode) { |
| 935 | collapse_pos(0, i + command_mode); |
| 936 | i = -1; /* hack incremet */ |
| 937 | } |
| 938 | } |
| 939 | /* collapse `command...` */ |
| 940 | for (i = 0; int_buf[i]; i++) |
| 941 | if (int_buf[i] == '`') { |
| 942 | for (j = i + 1; int_buf[j]; j++) |
| 943 | if (int_buf[j] == '`') { |
| 944 | collapse_pos(i, j + 1); |
| 945 | j = 0; |
| 946 | break; |
| 947 | } |
| 948 | if (j) { |
| 949 | /* not found close ` - command mode, collapse all previous */ |
| 950 | collapse_pos(0, i + 1); |
| 951 | break; |
| 952 | } else |
| 953 | i--; /* hack incremet */ |
| 954 | } |
| 955 | |
| 956 | /* collapse (command...(command...)...) or {command...{command...}...} */ |
| 957 | c = 0; /* "recursive" level */ |
| 958 | c2 = 0; |
| 959 | for (i = 0; int_buf[i]; i++) |
| 960 | if (int_buf[i] == '(' || int_buf[i] == '{') { |
| 961 | if (int_buf[i] == '(') |
| 962 | c++; |
| 963 | else |
| 964 | c2++; |
| 965 | collapse_pos(0, i + 1); |
| 966 | i = -1; /* hack incremet */ |
| 967 | } |
| 968 | for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++) |
| 969 | if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) { |
| 970 | if (int_buf[i] == ')') |
| 971 | c--; |
| 972 | else |
| 973 | c2--; |
| 974 | collapse_pos(0, i + 1); |
| 975 | i = -1; /* hack incremet */ |
| 976 | } |
| 977 | |
| 978 | /* skip first not quote space */ |
| 979 | for (i = 0; int_buf[i]; i++) |
| 980 | if (int_buf[i] != ' ') |
| 981 | break; |
| 982 | if (i) |
| 983 | collapse_pos(0, i); |
| 984 | |
| 985 | /* set find mode for completion */ |
| 986 | command_mode = FIND_EXE_ONLY; |
| 987 | for (i = 0; int_buf[i]; i++) |
| 988 | if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') { |
| 989 | if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY |
| 990 | && strncmp(&matchBuf[pos_buf[0]], "cd", 2) == 0) |
| 991 | command_mode = FIND_DIR_ONLY; |
| 992 | else { |
| 993 | command_mode = FIND_FILE_ONLY; |
| 994 | break; |
| 995 | } |
| 996 | } |
| 997 | /* "strlen" */ |
| 998 | for (i = 0; int_buf[i]; i++); |
| 999 | /* find last word */ |
| 1000 | for (--i; i >= 0; i--) { |
| 1001 | c = int_buf[i]; |
| 1002 | if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') { |
| 1003 | collapse_pos(0, i + 1); |
| 1004 | break; |
| 1005 | } |
| 1006 | } |
| 1007 | /* skip first not quoted '\'' or '"' */ |
| 1008 | for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++); |
| 1009 | /* collapse quote or unquote // or /~ */ |
| 1010 | while ((int_buf[i] & ~QUOT) == '/' && ( |
| 1011 | (int_buf[i + 1] & ~QUOT) == '/' |
| 1012 | || (int_buf[i + 1] & ~QUOT) == |
| 1013 | '~')) i++; |
| 1014 | if (i) |
| 1015 | collapse_pos(0, i); |
| 1016 | |
| 1017 | /* set only match and destroy quotes */ |
| 1018 | j = 0; |
| 1019 | for (i = 0; pos_buf[i] >= 0; i++) { |
| 1020 | matchBuf[i] = matchBuf[pos_buf[i]]; |
| 1021 | j = pos_buf[i] + 1; |
| 1022 | } |
| 1023 | matchBuf[i] = 0; |
| 1024 | /* old lenght matchBuf with quotes symbols */ |
| 1025 | *len_with_quotes = j ? j - pos_buf[0] : 0; |
| 1026 | |
| 1027 | return command_mode; |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | static void input_tab(int *lastWasTab) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1032 | { |
| 1033 | /* Do TAB completion */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1034 | static int num_matches; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1035 | static char **matches; |
| 1036 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1037 | if (lastWasTab == 0) { /* free all memory */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1038 | if (matches) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1039 | while (num_matches > 0) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1040 | free(matches[--num_matches]); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1041 | free(matches); |
| 1042 | matches = (char **) NULL; |
| 1043 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1044 | return; |
| 1045 | } |
| 1046 | if (*lastWasTab == FALSE) { |
| 1047 | |
| 1048 | char *tmp; |
| 1049 | int len_found; |
| 1050 | char matchBuf[BUFSIZ]; |
| 1051 | int find_type; |
| 1052 | int recalc_pos; |
| 1053 | |
| 1054 | *lastWasTab = TRUE; /* flop trigger */ |
| 1055 | |
| 1056 | /* Make a local copy of the string -- up |
| 1057 | * to the position of the cursor */ |
| 1058 | tmp = strncpy(matchBuf, command_ps, cursor); |
| 1059 | tmp[cursor] = 0; |
| 1060 | |
| 1061 | find_type = find_match(matchBuf, &recalc_pos); |
| 1062 | |
| 1063 | /* Free up any memory already allocated */ |
| 1064 | input_tab(0); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1065 | |
Eric Andersen | 28a78ab | 2001-02-16 20:26:50 +0000 | [diff] [blame] | 1066 | #ifdef BB_FEATURE_SH_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1067 | /* If the word starts with `~' and there is no slash in the word, |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1068 | * then try completing this word as a username. */ |
| 1069 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1070 | if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1071 | matches = username_tab_completion(matchBuf, &num_matches); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1072 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1073 | /* Try to match any executable in our path and everything |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1074 | * in the current working directory that matches. */ |
| 1075 | if (!matches) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1076 | matches = |
| 1077 | exe_n_cwd_tab_completion(matchBuf, &num_matches, |
| 1078 | find_type); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1079 | |
| 1080 | /* Did we find exactly one match? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1081 | if (!matches || num_matches > 1) { |
| 1082 | char *tmp1; |
| 1083 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1084 | beep(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1085 | if (!matches) |
| 1086 | return; /* not found */ |
| 1087 | /* sort */ |
| 1088 | qsort(matches, num_matches, sizeof(char *), match_compare); |
| 1089 | |
| 1090 | /* find minimal match */ |
| 1091 | tmp = xstrdup(matches[0]); |
| 1092 | for (tmp1 = tmp; *tmp1; tmp1++) |
| 1093 | for (len_found = 1; len_found < num_matches; len_found++) |
| 1094 | if (matches[len_found][(tmp1 - tmp)] != *tmp1) { |
| 1095 | *tmp1 = 0; |
| 1096 | break; |
| 1097 | } |
| 1098 | if (*tmp == 0) { /* have unique */ |
| 1099 | free(tmp); |
| 1100 | return; |
| 1101 | } |
| 1102 | } else { /* one match */ |
| 1103 | tmp = matches[0]; |
| 1104 | /* for next completion current found */ |
| 1105 | *lastWasTab = FALSE; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1108 | len_found = strlen(tmp); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1109 | /* have space to placed match? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1110 | if ((len_found - strlen(matchBuf) + len) < BUFSIZ) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1111 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1112 | /* before word for match */ |
| 1113 | command_ps[cursor - recalc_pos] = 0; |
| 1114 | /* save tail line */ |
| 1115 | strcpy(matchBuf, command_ps + cursor); |
| 1116 | /* add match */ |
| 1117 | strcat(command_ps, tmp); |
| 1118 | /* add tail */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1119 | strcat(command_ps, matchBuf); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1120 | /* back to begin word for match */ |
| 1121 | input_backward(recalc_pos); |
| 1122 | /* new pos */ |
| 1123 | recalc_pos = cursor + len_found; |
| 1124 | /* new len */ |
| 1125 | len = strlen(command_ps); |
| 1126 | /* write out the matched command */ |
| 1127 | input_end(); |
| 1128 | input_backward(cursor - recalc_pos); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1129 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1130 | if (tmp != matches[0]) |
| 1131 | free(tmp); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1132 | } else { |
| 1133 | /* Ok -- the last char was a TAB. Since they |
| 1134 | * just hit TAB again, print a list of all the |
| 1135 | * available choices... */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1136 | if (matches && num_matches > 0) { |
| 1137 | int i, col, l; |
| 1138 | int sav_cursor = cursor; /* change goto_new_line() */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1139 | |
| 1140 | /* Go to the next line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1141 | goto_new_line(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1142 | for (i = 0, col = 0; i < num_matches; i++) { |
| 1143 | l = strlen(matches[i]); |
| 1144 | if (l < 14) |
| 1145 | l = 14; |
| 1146 | printf("%-14s ", matches[i]); |
| 1147 | if ((l += 2) > 16) |
| 1148 | while (l % 16) { |
| 1149 | putchar(' '); |
| 1150 | l++; |
| 1151 | } |
| 1152 | col += l; |
| 1153 | col -= (col / cmdedit_termw) * cmdedit_termw; |
| 1154 | if (col > 60 && matches[i + 1] != NULL) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1155 | putchar('\n'); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1156 | col = 0; |
| 1157 | } |
| 1158 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1159 | /* Go to the next line and rewrite */ |
| 1160 | putchar('\n'); |
| 1161 | redraw(0, len - sav_cursor); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1165 | #endif /* BB_FEATURE_SH_TAB_COMPLETION */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1166 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1167 | static void get_previous_history(struct history **hp, struct history *p) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1168 | { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 1169 | if ((*hp)->s) |
| 1170 | free((*hp)->s); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1171 | (*hp)->s = xstrdup(command_ps); |
| 1172 | *hp = p; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1175 | static inline void get_next_history(struct history **hp) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1176 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1177 | get_previous_history(hp, (*hp)->n); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1180 | enum { |
| 1181 | ESC = 27, |
| 1182 | DEL = 127, |
| 1183 | }; |
| 1184 | |
| 1185 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1186 | /* |
| 1187 | * This function is used to grab a character buffer |
| 1188 | * from the input file descriptor and allows you to |
| 1189 | * a string with full command editing (sortof like |
| 1190 | * a mini readline). |
| 1191 | * |
| 1192 | * The following standard commands are not implemented: |
| 1193 | * ESC-b -- Move back one word |
| 1194 | * ESC-f -- Move forward one word |
| 1195 | * ESC-d -- Delete back one word |
| 1196 | * ESC-h -- Delete forward one word |
| 1197 | * CTL-t -- Transpose two characters |
| 1198 | * |
| 1199 | * Furthermore, the "vi" command editing keys are not implemented. |
| 1200 | * |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1201 | */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1202 | extern void cmdedit_read_input(char *prompt, char command[BUFSIZ]) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1203 | { |
| 1204 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1205 | int inputFd = fileno(stdin); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1206 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1207 | int break_out = 0; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1208 | int lastWasTab = FALSE; |
| 1209 | char c = 0; |
| 1210 | struct history *hp = his_end; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1211 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1212 | /* prepare before init handlers */ |
| 1213 | cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1214 | len = 0; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1215 | command_ps = command; |
| 1216 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1217 | if (new_settings.c_cc[VMIN] == 0) { /* first call */ |
| 1218 | |
| 1219 | getTermSettings(inputFd, (void *) &initial_settings); |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1220 | memcpy(&new_settings, &initial_settings, sizeof(struct termios)); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1221 | |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1222 | new_settings.c_cc[VMIN] = 1; |
| 1223 | new_settings.c_cc[VTIME] = 0; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1224 | new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */ |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1225 | new_settings.c_lflag &= ~ICANON; /* unbuffered input */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1226 | new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL); /* Turn off echoing */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1227 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1228 | |
| 1229 | command[0] = 0; |
| 1230 | |
| 1231 | setTermSettings(inputFd, (void *) &new_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1232 | handlers_sets |= SET_RESET_TERM; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1233 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 1234 | /* Print out the command prompt */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1235 | parse_prompt(prompt); |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 1236 | /* Now initialize things */ |
| 1237 | cmdedit_init(); |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 1238 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1239 | while (1) { |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1240 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1241 | fflush(stdout); /* buffered out to fast */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1242 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1243 | if (read(inputFd, &c, 1) < 1) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1244 | return; |
Erik Andersen | f3b3d17 | 2000-04-09 18:24:05 +0000 | [diff] [blame] | 1245 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1246 | switch (c) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1247 | case '\n': |
| 1248 | case '\r': |
| 1249 | /* Enter */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1250 | goto_new_line(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1251 | break_out = 1; |
| 1252 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1253 | case 1: |
| 1254 | /* Control-a -- Beginning of line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1255 | input_backward(cursor); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1256 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1257 | case 2: |
| 1258 | /* Control-b -- Move back one character */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1259 | input_backward(1); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1260 | break; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1261 | case 3: |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1262 | /* Control-c -- stop gathering input */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1263 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1264 | /* Link into lash to reset context to 0 on ^C and such */ |
| 1265 | shell_context = 0; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1266 | |
| 1267 | /* Go to the next line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1268 | goto_new_line(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1269 | command[0] = 0; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1270 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1271 | return; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1272 | case 4: |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1273 | /* Control-d -- Delete one character, or exit |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1274 | * if the len=0 and no chars to delete */ |
| 1275 | if (len == 0) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1276 | printf("exit"); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1277 | clean_up_and_die(0); |
| 1278 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1279 | input_delete(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1280 | } |
| 1281 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1282 | case 5: |
| 1283 | /* Control-e -- End of line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1284 | input_end(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1285 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1286 | case 6: |
| 1287 | /* Control-f -- Move forward one character */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1288 | input_forward(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1289 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1290 | case '\b': |
| 1291 | case DEL: |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1292 | /* Control-h and DEL */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1293 | input_backspace(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1294 | break; |
| 1295 | case '\t': |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 1296 | #ifdef BB_FEATURE_SH_TAB_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1297 | input_tab(&lastWasTab); |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 1298 | #endif |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1299 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1300 | case 14: |
| 1301 | /* Control-n -- Get next command in history */ |
| 1302 | if (hp && hp->n && hp->n->s) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1303 | get_next_history(&hp); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1304 | goto rewrite_line; |
| 1305 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1306 | beep(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1307 | } |
| 1308 | break; |
| 1309 | case 16: |
| 1310 | /* Control-p -- Get previous command from history */ |
| 1311 | if (hp && hp->p) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1312 | get_previous_history(&hp, hp->p); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1313 | goto rewrite_line; |
| 1314 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1315 | beep(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1316 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1317 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1318 | case 21: |
| 1319 | /* Control-U -- Clear line before cursor */ |
| 1320 | if (cursor) { |
| 1321 | strcpy(command, command + cursor); |
| 1322 | redraw(cmdedit_y, len -= cursor); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1323 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1324 | break; |
| 1325 | |
| 1326 | case ESC:{ |
| 1327 | /* escape sequence follows */ |
| 1328 | if (read(inputFd, &c, 1) < 1) |
| 1329 | return; |
| 1330 | /* different vt100 emulations */ |
| 1331 | if (c == '[' || c == 'O') { |
| 1332 | if (read(inputFd, &c, 1) < 1) |
| 1333 | return; |
| 1334 | } |
| 1335 | switch (c) { |
| 1336 | #ifdef BB_FEATURE_SH_TAB_COMPLETION |
| 1337 | case '\t': /* Alt-Tab */ |
| 1338 | |
| 1339 | input_tab(&lastWasTab); |
| 1340 | break; |
| 1341 | #endif |
| 1342 | case 'A': |
| 1343 | /* Up Arrow -- Get previous command from history */ |
| 1344 | if (hp && hp->p) { |
| 1345 | get_previous_history(&hp, hp->p); |
| 1346 | goto rewrite_line; |
| 1347 | } else { |
| 1348 | beep(); |
| 1349 | } |
| 1350 | break; |
| 1351 | case 'B': |
| 1352 | /* Down Arrow -- Get next command in history */ |
| 1353 | if (hp && hp->n && hp->n->s) { |
| 1354 | get_next_history(&hp); |
| 1355 | goto rewrite_line; |
| 1356 | } else { |
| 1357 | beep(); |
| 1358 | } |
| 1359 | break; |
| 1360 | |
| 1361 | /* Rewrite the line with the selected history item */ |
| 1362 | rewrite_line: |
| 1363 | /* change command */ |
| 1364 | len = strlen(strcpy(command, hp->s)); |
| 1365 | /* redraw and go to end line */ |
| 1366 | redraw(cmdedit_y, 0); |
| 1367 | break; |
| 1368 | case 'C': |
| 1369 | /* Right Arrow -- Move forward one character */ |
| 1370 | input_forward(); |
| 1371 | break; |
| 1372 | case 'D': |
| 1373 | /* Left Arrow -- Move back one character */ |
| 1374 | input_backward(1); |
| 1375 | break; |
| 1376 | case '3': |
| 1377 | /* Delete */ |
| 1378 | input_delete(); |
| 1379 | break; |
| 1380 | case '1': |
| 1381 | case 'H': |
| 1382 | /* Home (Ctrl-A) */ |
| 1383 | input_backward(cursor); |
| 1384 | break; |
| 1385 | case '4': |
| 1386 | case 'F': |
| 1387 | /* End (Ctrl-E) */ |
| 1388 | input_end(); |
| 1389 | break; |
| 1390 | default: |
| 1391 | if (!(c >= '1' && c <= '9')) |
| 1392 | c = 0; |
| 1393 | beep(); |
| 1394 | } |
| 1395 | if (c >= '1' && c <= '9') |
| 1396 | do |
| 1397 | if (read(inputFd, &c, 1) < 1) |
| 1398 | return; |
| 1399 | while (c != '~'); |
| 1400 | break; |
| 1401 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1402 | |
| 1403 | default: /* If it's regular input, do the normal thing */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1404 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 1405 | /* Control-V -- Add non-printable symbol */ |
| 1406 | if (c == 22) { |
| 1407 | if (read(inputFd, &c, 1) < 1) |
| 1408 | return; |
| 1409 | if (c == 0) { |
| 1410 | beep(); |
| 1411 | break; |
| 1412 | } |
| 1413 | } else |
| 1414 | #endif |
| 1415 | if (!isprint(c)) /* Skip non-printable characters */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1416 | break; |
| 1417 | |
| 1418 | if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */ |
| 1419 | break; |
| 1420 | |
| 1421 | len++; |
| 1422 | |
| 1423 | if (cursor == (len - 1)) { /* Append if at the end of the line */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1424 | *(command + cursor) = c; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1425 | *(command + cursor + 1) = 0; |
| 1426 | cmdedit_set_out_char(0); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1427 | } else { /* Insert otherwise */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1428 | int sc = cursor; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1429 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1430 | memmove(command + sc + 1, command + sc, len - sc); |
| 1431 | *(command + sc) = c; |
| 1432 | sc++; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1433 | /* rewrite from cursor */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1434 | input_end(); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1435 | /* to prev x pos + 1 */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1436 | input_backward(cursor - sc); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1439 | break; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1440 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1441 | if (break_out) /* Enter is the command terminator, no more input. */ |
| 1442 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1443 | |
| 1444 | if (c != '\t') |
| 1445 | lastWasTab = FALSE; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1448 | setTermSettings(inputFd, (void *) &initial_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1449 | handlers_sets &= ~SET_RESET_TERM; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1450 | |
| 1451 | /* Handle command history log */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1452 | if (len) { /* no put empty line */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1453 | |
| 1454 | struct history *h = his_end; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1455 | char *ss; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1456 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1457 | ss = xstrdup(command); /* duplicate */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1458 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1459 | if (h == 0) { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 1460 | /* No previous history -- this memory is never freed */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1461 | h = his_front = xmalloc(sizeof(struct history)); |
| 1462 | h->n = xmalloc(sizeof(struct history)); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1463 | |
| 1464 | h->p = NULL; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1465 | h->s = ss; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1466 | h->n->p = h; |
| 1467 | h->n->n = NULL; |
| 1468 | h->n->s = NULL; |
| 1469 | his_end = h->n; |
| 1470 | history_counter++; |
| 1471 | } else { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 1472 | /* Add a new history command -- this memory is never freed */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1473 | h->n = xmalloc(sizeof(struct history)); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1474 | |
| 1475 | h->n->p = h; |
| 1476 | h->n->n = NULL; |
| 1477 | h->n->s = NULL; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1478 | h->s = ss; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1479 | his_end = h->n; |
| 1480 | |
| 1481 | /* After max history, remove the oldest command */ |
| 1482 | if (history_counter >= MAX_HISTORY) { |
| 1483 | |
| 1484 | struct history *p = his_front->n; |
| 1485 | |
| 1486 | p->p = NULL; |
| 1487 | free(his_front->s); |
| 1488 | free(his_front); |
| 1489 | his_front = p; |
| 1490 | } else { |
| 1491 | history_counter++; |
| 1492 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1493 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1494 | #if defined(BB_FEATURE_BASH_STYLE_PROMT) |
| 1495 | num_ok_lines++; |
| 1496 | #endif |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1497 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1498 | command[len++] = '\n'; /* set '\n' */ |
| 1499 | command[len] = 0; |
| 1500 | #if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_SH_TAB_COMPLETION) |
| 1501 | input_tab(0); /* strong free */ |
| 1502 | #endif |
| 1503 | #if defined(BB_FEATURE_BASH_STYLE_PROMT) |
| 1504 | free(cmdedit_prompt); |
| 1505 | #endif |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1506 | return; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 1509 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1510 | /* Undo the effects of cmdedit_init(). */ |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1511 | extern void cmdedit_terminate(void) |
| 1512 | { |
| 1513 | cmdedit_reset_term(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1514 | if ((handlers_sets & SET_TERM_HANDLERS) != 0) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1515 | signal(SIGKILL, SIG_DFL); |
| 1516 | signal(SIGINT, SIG_DFL); |
| 1517 | signal(SIGQUIT, SIG_DFL); |
| 1518 | signal(SIGTERM, SIG_DFL); |
| 1519 | signal(SIGWINCH, SIG_DFL); |
| 1520 | handlers_sets &= ~SET_TERM_HANDLERS; |
| 1521 | } |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1524 | #endif /* BB_FEATURE_SH_COMMAND_EDITING */ |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1525 | |
| 1526 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1527 | #ifdef TEST |
| 1528 | |
| 1529 | unsigned int shell_context; |
| 1530 | |
| 1531 | int main(int argc, char **argv) |
| 1532 | { |
| 1533 | char buff[BUFSIZ]; |
| 1534 | char *prompt = |
| 1535 | #if defined(BB_FEATURE_BASH_STYLE_PROMT) |
| 1536 | "\\[\\033[32;1m\\]\\u@\\[\\033[33;1m\\]\\h:\ |
| 1537 | \\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \ |
| 1538 | \\!\\[\\033[36;1m\\]\\$ \\[\\033[0m\\]"; |
| 1539 | #else |
| 1540 | "% "; |
| 1541 | #endif |
| 1542 | |
| 1543 | shell_context = 1; |
| 1544 | do { |
| 1545 | cmdedit_read_input(prompt, buff); |
| 1546 | printf("*** cmdedit_read_input() returned line =%s=\n", buff); |
| 1547 | } while (shell_context); |
| 1548 | printf("*** cmdedit_read_input() detect ^C\n"); |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | #endif /* TEST */ |