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