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) |
| 116 | #endif |
| 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 | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 444 | case '!': |
| 445 | snprintf(buf, sizeof(buf), "%d", num_ok_lines); |
| 446 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf); |
| 447 | continue; |
| 448 | case 'e': |
| 449 | case 'E': /* \e \E = \033 */ |
| 450 | c = '\033'; |
| 451 | break; |
| 452 | case 'x': |
| 453 | case 'X': |
| 454 | case '0': |
| 455 | case '1': |
| 456 | case '2': |
| 457 | case '3': |
| 458 | case '4': |
| 459 | case '5': |
| 460 | case '6': |
| 461 | case '7':{ |
| 462 | int l; |
| 463 | int ho = 0; |
| 464 | char *eho; |
| 465 | |
| 466 | if (c == 'X') |
| 467 | c = 'x'; |
| 468 | |
| 469 | for (l = 0; l < 3;) { |
| 470 | |
| 471 | buf[l++] = *prmt_ptr; |
| 472 | buf[l] = 0; |
| 473 | ho = strtol(buf, &eho, c == 'x' ? 16 : 8); |
| 474 | if (ho > UCHAR_MAX || (eho - buf) < l) { |
| 475 | l--; |
| 476 | break; |
| 477 | } |
| 478 | prmt_ptr++; |
| 479 | } |
| 480 | buf[l] = 0; |
| 481 | ho = strtol(buf, 0, c == 'x' ? 16 : 8); |
| 482 | c = ho == 0 ? '?' : (char) ho; |
| 483 | break; |
| 484 | } |
| 485 | case '[': |
| 486 | case ']': |
| 487 | if (c == flg_not_length) { |
| 488 | flg_not_length = flg_not_length == '[' ? ']' : '['; |
| 489 | continue; |
| 490 | } |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | buf[0] = c; |
| 495 | buf[1] = 0; |
| 496 | add_to_prompt(&prmt_mem_ptr, &alm, &prmt_len, buf); |
| 497 | if (flg_not_length == ']') |
| 498 | sub_len++; |
| 499 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 500 | cmdedit_prompt = prmt_mem_ptr; |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 501 | cmdedit_prmt_len = prmt_len - sub_len; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 502 | put_prompt(); |
| 503 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 504 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 505 | |
| 506 | |
| 507 | /* draw promt, editor line, and clear tail */ |
| 508 | static void redraw(int y, int back_cursor) |
| 509 | { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 510 | if (y > 0) /* up to start y */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 511 | printf("\033[%dA", y); |
| 512 | cmdedit_y = 0; /* new quasireal y */ |
| 513 | putchar('\r'); |
| 514 | put_prompt(); |
| 515 | input_end(); /* rewrite */ |
| 516 | printf("\033[J"); /* destroy tail after cursor */ |
| 517 | input_backward(back_cursor); |
| 518 | } |
| 519 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 520 | /* Delete the char in front of the cursor */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 521 | static void input_delete(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 522 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 523 | int j = cursor; |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 524 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 525 | if (j == len) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 526 | return; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 527 | |
| 528 | strcpy(command_ps + j, command_ps + j + 1); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 529 | len--; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 530 | input_end(); /* rewtite new line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 531 | cmdedit_set_out_char(0); /* destroy end char */ |
| 532 | input_backward(cursor - j); /* back to old pos cursor */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 535 | /* Delete the char in back of the cursor */ |
| 536 | static void input_backspace(void) |
| 537 | { |
| 538 | if (cursor > 0) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 539 | input_backward(1); |
| 540 | input_delete(); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 545 | /* Move forward one charactor */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 546 | static void input_forward(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 547 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 548 | if (cursor < len) |
| 549 | cmdedit_set_out_char(command_ps[cursor + 1]); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 553 | static void clean_up_and_die(int sig) |
| 554 | { |
| 555 | goto_new_line(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 556 | if (sig != SIGINT) |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 557 | exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 558 | cmdedit_reset_term(); |
| 559 | } |
| 560 | |
| 561 | static void cmdedit_setwidth(int w, int redraw_flg) |
| 562 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 563 | cmdedit_termw = cmdedit_prmt_len + 2; |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 564 | if (w <= cmdedit_termw) { |
| 565 | cmdedit_termw = cmdedit_termw % w; |
| 566 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 567 | if (w > cmdedit_termw) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 568 | cmdedit_termw = w; |
| 569 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 570 | if (redraw_flg) { |
| 571 | /* new y for current cursor */ |
| 572 | int new_y = (cursor + cmdedit_prmt_len) / w; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 573 | |
| 574 | /* redraw */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 575 | redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor); |
| 576 | fflush(stdout); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 577 | } |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 578 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | extern void cmdedit_init(void) |
| 582 | { |
Eric Andersen | 61173a5 | 2001-03-19 17:48:55 +0000 | [diff] [blame] | 583 | cmdedit_prmt_len = 0; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 584 | if ((handlers_sets & SET_WCHG_HANDLERS) == 0) { |
| 585 | /* emulate usage handler to set handler and call yours work */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 586 | win_changed(-SIGWINCH); |
| 587 | handlers_sets |= SET_WCHG_HANDLERS; |
| 588 | } |
| 589 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 590 | if ((handlers_sets & SET_ATEXIT) == 0) { |
| 591 | #ifdef BB_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 592 | struct passwd *entry; |
| 593 | |
| 594 | my_euid = geteuid(); |
| 595 | entry = getpwuid(my_euid); |
| 596 | if (entry) { |
| 597 | user_buf = xstrdup(entry->pw_name); |
| 598 | home_pwd_buf = xstrdup(entry->pw_dir); |
| 599 | } |
| 600 | #endif |
| 601 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 602 | #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 603 | |
| 604 | #ifndef BB_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 605 | my_euid = geteuid(); |
| 606 | #endif |
| 607 | my_uid = getuid(); |
| 608 | my_gid = getgid(); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 609 | #endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 610 | handlers_sets |= SET_ATEXIT; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 611 | atexit(cmdedit_reset_term); /* be sure to do this only once */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 612 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 613 | |
| 614 | if ((handlers_sets & SET_TERM_HANDLERS) == 0) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 615 | signal(SIGKILL, clean_up_and_die); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 616 | signal(SIGINT, clean_up_and_die); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 617 | signal(SIGQUIT, clean_up_and_die); |
| 618 | signal(SIGTERM, clean_up_and_die); |
| 619 | handlers_sets |= SET_TERM_HANDLERS; |
| 620 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 621 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 622 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 623 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 624 | #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION |
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 | static int is_execute(const struct stat *st) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 627 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 628 | if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) || |
| 629 | (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) || |
| 630 | (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) || |
| 631 | (st->st_mode & S_IXOTH)) return TRUE; |
| 632 | return FALSE; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 633 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 634 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 635 | #ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 636 | |
| 637 | static char **username_tab_completion(char *ud, int *num_matches) |
| 638 | { |
| 639 | struct passwd *entry; |
| 640 | int userlen; |
| 641 | char *temp; |
| 642 | |
| 643 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 644 | ud++; /* ~user/... to user/... */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 645 | userlen = strlen(ud); |
| 646 | |
| 647 | if (num_matches == 0) { /* "~/..." or "~user/..." */ |
| 648 | char *sav_ud = ud - 1; |
| 649 | char *home = 0; |
| 650 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 651 | if (*ud == '/') { /* "~/..." */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 652 | home = home_pwd_buf; |
| 653 | } else { |
| 654 | /* "~user/..." */ |
| 655 | temp = strchr(ud, '/'); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 656 | *temp = 0; /* ~user\0 */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 657 | entry = getpwnam(ud); |
| 658 | *temp = '/'; /* restore ~user/... */ |
| 659 | ud = temp; |
| 660 | if (entry) |
| 661 | home = entry->pw_dir; |
| 662 | } |
| 663 | if (home) { |
| 664 | if ((userlen + strlen(home) + 1) < BUFSIZ) { |
| 665 | char temp2[BUFSIZ]; /* argument size */ |
| 666 | |
| 667 | /* /home/user/... */ |
| 668 | sprintf(temp2, "%s%s", home, ud); |
| 669 | strcpy(sav_ud, temp2); |
| 670 | } |
| 671 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 672 | return 0; /* void, result save to argument :-) */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 673 | } else { |
| 674 | /* "~[^/]*" */ |
| 675 | char **matches = (char **) NULL; |
| 676 | int nm = 0; |
| 677 | |
| 678 | setpwent(); |
| 679 | |
| 680 | while ((entry = getpwent()) != NULL) { |
| 681 | /* Null usernames should result in all users as possible completions. */ |
| 682 | if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) { |
| 683 | |
| 684 | temp = xmalloc(3 + strlen(entry->pw_name)); |
| 685 | sprintf(temp, "~%s/", entry->pw_name); |
| 686 | matches = xrealloc(matches, (nm + 1) * sizeof(char *)); |
| 687 | |
| 688 | matches[nm++] = temp; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | endpwent(); |
| 693 | (*num_matches) = nm; |
| 694 | return (matches); |
| 695 | } |
| 696 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 697 | #endif /* BB_FEATURE_COMMAND_USERNAME_COMPLETION */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 698 | |
| 699 | enum { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 700 | FIND_EXE_ONLY = 0, |
| 701 | FIND_DIR_ONLY = 1, |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 702 | FIND_FILE_ONLY = 2, |
| 703 | }; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 704 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 705 | static int path_parse(char ***p, int flags) |
| 706 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 707 | int npth; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 708 | char *tmp; |
| 709 | char *pth; |
| 710 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 711 | /* if not setenv PATH variable, to search cur dir "." */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 712 | if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 || |
| 713 | /* PATH=<empty> or PATH=:<empty> */ |
| 714 | *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 715 | return 1; |
| 716 | } |
| 717 | |
| 718 | tmp = pth; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 719 | npth = 0; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 720 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 721 | for (;;) { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 722 | npth++; /* count words is + 1 count ':' */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 723 | tmp = strchr(tmp, ':'); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 724 | if (tmp) { |
| 725 | if (*++tmp == 0) |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 726 | break; /* :<empty> */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 727 | } else |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 728 | break; |
| 729 | } |
| 730 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 731 | *p = xmalloc(npth * sizeof(char *)); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 732 | |
| 733 | tmp = pth; |
| 734 | (*p)[0] = xstrdup(tmp); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 735 | npth = 1; /* count words is + 1 count ':' */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 736 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 737 | for (;;) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 738 | tmp = strchr(tmp, ':'); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 739 | if (tmp) { |
| 740 | (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */ |
| 741 | if (*++tmp == 0) |
| 742 | break; /* :<empty> */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 743 | } else |
| 744 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 745 | (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | return npth; |
| 749 | } |
| 750 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 751 | static char *add_quote_for_spec_chars(char *found) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 752 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 753 | int l = 0; |
| 754 | char *s = xmalloc((strlen(found) + 1) * 2); |
| 755 | |
| 756 | while (*found) { |
| 757 | if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found)) |
| 758 | s[l++] = '\\'; |
| 759 | s[l++] = *found++; |
| 760 | } |
| 761 | s[l] = 0; |
| 762 | return s; |
| 763 | } |
| 764 | |
| 765 | static char **exe_n_cwd_tab_completion(char *command, int *num_matches, |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 766 | int type) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 767 | { |
| 768 | |
| 769 | char **matches = 0; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 770 | DIR *dir; |
| 771 | struct dirent *next; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 772 | char dirbuf[BUFSIZ]; |
| 773 | int nm = *num_matches; |
| 774 | struct stat st; |
| 775 | char *path1[1]; |
| 776 | char **paths = path1; |
| 777 | int npaths; |
| 778 | int i; |
| 779 | char found[BUFSIZ + 4 + PATH_MAX]; |
| 780 | char *pfind = strrchr(command, '/'); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 781 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 782 | path1[0] = "."; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 783 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 784 | if (pfind == NULL) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 785 | /* no dir, if flags==EXE_ONLY - get paths, else "." */ |
| 786 | npaths = path_parse(&paths, type); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 787 | pfind = command; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 788 | } else { |
| 789 | /* with dir */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 790 | /* save for change */ |
| 791 | strcpy(dirbuf, command); |
| 792 | /* set dir only */ |
| 793 | dirbuf[(pfind - command) + 1] = 0; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 794 | #ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 795 | if (dirbuf[0] == '~') /* ~/... or ~user/... */ |
| 796 | username_tab_completion(dirbuf, 0); |
| 797 | #endif |
| 798 | /* "strip" dirname in command */ |
| 799 | pfind++; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 800 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 801 | paths[0] = dirbuf; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 802 | npaths = 1; /* only 1 dir */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 803 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 804 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 805 | for (i = 0; i < npaths; i++) { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 806 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 807 | dir = opendir(paths[i]); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 808 | if (!dir) /* Don't print an error */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 809 | continue; |
| 810 | |
| 811 | while ((next = readdir(dir)) != NULL) { |
| 812 | const char *str_merge = "%s/%s"; |
| 813 | char *str_found = next->d_name; |
| 814 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 815 | /* matched ? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 816 | if (strncmp(str_found, pfind, strlen(pfind))) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 817 | continue; |
| 818 | /* not see .name without .match */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 819 | if (*str_found == '.' && *pfind == 0) { |
| 820 | if (*paths[i] == '/' && paths[i][1] == 0 |
| 821 | && str_found[1] == 0) str_found = ""; /* only "/" */ |
| 822 | else |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 823 | continue; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 824 | } |
| 825 | if (paths[i][strlen(paths[i]) - 1] == '/') |
| 826 | str_merge = "%s%s"; |
| 827 | sprintf(found, str_merge, paths[i], str_found); |
| 828 | /* hmm, remover in progress? */ |
| 829 | if (stat(found, &st) < 0) |
| 830 | continue; |
| 831 | /* find with dirs ? */ |
| 832 | if (paths[i] != dirbuf) |
| 833 | strcpy(found, next->d_name); /* only name */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 834 | if (S_ISDIR(st.st_mode)) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 835 | /* name is directory */ |
| 836 | /* algorithmic only "/" ? */ |
| 837 | if (*str_found) |
| 838 | strcat(found, "/"); |
| 839 | str_found = add_quote_for_spec_chars(found); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 840 | } else { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 841 | /* not put found file if search only dirs for cd */ |
| 842 | if (type == FIND_DIR_ONLY) |
| 843 | continue; |
| 844 | str_found = add_quote_for_spec_chars(found); |
| 845 | if (type == FIND_FILE_ONLY || |
| 846 | (type == FIND_EXE_ONLY && is_execute(&st) == TRUE)) |
| 847 | strcat(str_found, " "); |
| 848 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 849 | /* Add it to the list */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 850 | matches = xrealloc(matches, (nm + 1) * sizeof(char *)); |
| 851 | |
| 852 | matches[nm++] = str_found; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 853 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 854 | closedir(dir); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 855 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 856 | if (paths != path1) { |
| 857 | free(paths[0]); /* allocated memory only in first member */ |
| 858 | free(paths); |
| 859 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 860 | *num_matches = nm; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 861 | return (matches); |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 862 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 863 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 864 | static int match_compare(const void *a, const void *b) |
| 865 | { |
| 866 | return strcmp(*(char **) a, *(char **) b); |
| 867 | } |
| 868 | |
| 869 | |
| 870 | |
| 871 | #define QUOT (UCHAR_MAX+1) |
| 872 | |
| 873 | #define collapse_pos(is, in) { \ |
| 874 | memcpy(int_buf+is, int_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); \ |
| 875 | memcpy(pos_buf+is, pos_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); } |
| 876 | |
| 877 | static int find_match(char *matchBuf, int *len_with_quotes) |
| 878 | { |
| 879 | int i, j; |
| 880 | int command_mode; |
| 881 | int c, c2; |
| 882 | int int_buf[BUFSIZ + 1]; |
| 883 | int pos_buf[BUFSIZ + 1]; |
| 884 | |
| 885 | /* set to integer dimension characters and own positions */ |
| 886 | for (i = 0;; i++) { |
| 887 | int_buf[i] = (int) ((unsigned char) matchBuf[i]); |
| 888 | if (int_buf[i] == 0) { |
| 889 | pos_buf[i] = -1; /* indicator end line */ |
| 890 | break; |
| 891 | } else |
| 892 | pos_buf[i] = i; |
| 893 | } |
| 894 | |
| 895 | /* mask \+symbol and convert '\t' to ' ' */ |
| 896 | for (i = j = 0; matchBuf[i]; i++, j++) |
| 897 | if (matchBuf[i] == '\\') { |
| 898 | collapse_pos(j, j + 1); |
| 899 | int_buf[j] |= QUOT; |
| 900 | i++; |
| 901 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 902 | if (matchBuf[i] == '\t') /* algorithm equivalent */ |
| 903 | int_buf[j] = ' ' | QUOT; |
| 904 | #endif |
| 905 | } |
| 906 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 907 | else if (matchBuf[i] == '\t') |
| 908 | int_buf[j] = ' '; |
| 909 | #endif |
| 910 | |
| 911 | /* mask "symbols" or 'symbols' */ |
| 912 | c2 = 0; |
| 913 | for (i = 0; int_buf[i]; i++) { |
| 914 | c = int_buf[i]; |
| 915 | if (c == '\'' || c == '"') { |
| 916 | if (c2 == 0) |
| 917 | c2 = c; |
| 918 | else { |
| 919 | if (c == c2) |
| 920 | c2 = 0; |
| 921 | else |
| 922 | int_buf[i] |= QUOT; |
| 923 | } |
| 924 | } else if (c2 != 0 && c != '$') |
| 925 | int_buf[i] |= QUOT; |
| 926 | } |
| 927 | |
| 928 | /* skip commands with arguments if line have commands delimiters */ |
| 929 | /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */ |
| 930 | for (i = 0; int_buf[i]; i++) { |
| 931 | c = int_buf[i]; |
| 932 | c2 = int_buf[i + 1]; |
| 933 | j = i ? int_buf[i - 1] : -1; |
| 934 | command_mode = 0; |
| 935 | if (c == ';' || c == '&' || c == '|') { |
| 936 | command_mode = 1 + (c == c2); |
| 937 | if (c == '&') { |
| 938 | if (j == '>' || j == '<') |
| 939 | command_mode = 0; |
| 940 | } else if (c == '|' && j == '>') |
| 941 | command_mode = 0; |
| 942 | } |
| 943 | if (command_mode) { |
| 944 | collapse_pos(0, i + command_mode); |
| 945 | i = -1; /* hack incremet */ |
| 946 | } |
| 947 | } |
| 948 | /* collapse `command...` */ |
| 949 | for (i = 0; int_buf[i]; i++) |
| 950 | if (int_buf[i] == '`') { |
| 951 | for (j = i + 1; int_buf[j]; j++) |
| 952 | if (int_buf[j] == '`') { |
| 953 | collapse_pos(i, j + 1); |
| 954 | j = 0; |
| 955 | break; |
| 956 | } |
| 957 | if (j) { |
| 958 | /* not found close ` - command mode, collapse all previous */ |
| 959 | collapse_pos(0, i + 1); |
| 960 | break; |
| 961 | } else |
| 962 | i--; /* hack incremet */ |
| 963 | } |
| 964 | |
| 965 | /* collapse (command...(command...)...) or {command...{command...}...} */ |
| 966 | c = 0; /* "recursive" level */ |
| 967 | c2 = 0; |
| 968 | for (i = 0; int_buf[i]; i++) |
| 969 | if (int_buf[i] == '(' || int_buf[i] == '{') { |
| 970 | if (int_buf[i] == '(') |
| 971 | c++; |
| 972 | else |
| 973 | c2++; |
| 974 | collapse_pos(0, i + 1); |
| 975 | i = -1; /* hack incremet */ |
| 976 | } |
| 977 | for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++) |
| 978 | if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) { |
| 979 | if (int_buf[i] == ')') |
| 980 | c--; |
| 981 | else |
| 982 | c2--; |
| 983 | collapse_pos(0, i + 1); |
| 984 | i = -1; /* hack incremet */ |
| 985 | } |
| 986 | |
| 987 | /* skip first not quote space */ |
| 988 | for (i = 0; int_buf[i]; i++) |
| 989 | if (int_buf[i] != ' ') |
| 990 | break; |
| 991 | if (i) |
| 992 | collapse_pos(0, i); |
| 993 | |
| 994 | /* set find mode for completion */ |
| 995 | command_mode = FIND_EXE_ONLY; |
| 996 | for (i = 0; int_buf[i]; i++) |
| 997 | if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') { |
| 998 | if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 999 | && matchBuf[pos_buf[0]]=='c' |
| 1000 | && matchBuf[pos_buf[1]]=='d' ) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1001 | command_mode = FIND_DIR_ONLY; |
| 1002 | else { |
| 1003 | command_mode = FIND_FILE_ONLY; |
| 1004 | break; |
| 1005 | } |
| 1006 | } |
| 1007 | /* "strlen" */ |
| 1008 | for (i = 0; int_buf[i]; i++); |
| 1009 | /* find last word */ |
| 1010 | for (--i; i >= 0; i--) { |
| 1011 | c = int_buf[i]; |
| 1012 | if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') { |
| 1013 | collapse_pos(0, i + 1); |
| 1014 | break; |
| 1015 | } |
| 1016 | } |
| 1017 | /* skip first not quoted '\'' or '"' */ |
| 1018 | for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++); |
| 1019 | /* collapse quote or unquote // or /~ */ |
Mark Whitley | 7e5291f | 2001-03-08 19:31:12 +0000 | [diff] [blame] | 1020 | while ((int_buf[i] & ~QUOT) == '/' && |
| 1021 | ((int_buf[i + 1] & ~QUOT) == '/' |
| 1022 | || (int_buf[i + 1] & ~QUOT) == '~')) { |
| 1023 | i++; |
| 1024 | } |
| 1025 | if (i) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1026 | collapse_pos(0, i); |
Mark Whitley | 7e5291f | 2001-03-08 19:31:12 +0000 | [diff] [blame] | 1027 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1028 | |
| 1029 | /* set only match and destroy quotes */ |
| 1030 | j = 0; |
| 1031 | for (i = 0; pos_buf[i] >= 0; i++) { |
| 1032 | matchBuf[i] = matchBuf[pos_buf[i]]; |
| 1033 | j = pos_buf[i] + 1; |
| 1034 | } |
| 1035 | matchBuf[i] = 0; |
| 1036 | /* old lenght matchBuf with quotes symbols */ |
| 1037 | *len_with_quotes = j ? j - pos_buf[0] : 0; |
| 1038 | |
| 1039 | return command_mode; |
| 1040 | } |
| 1041 | |
| 1042 | |
| 1043 | static void input_tab(int *lastWasTab) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1044 | { |
| 1045 | /* Do TAB completion */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1046 | static int num_matches; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1047 | static char **matches; |
| 1048 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1049 | if (lastWasTab == 0) { /* free all memory */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1050 | if (matches) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1051 | while (num_matches > 0) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1052 | free(matches[--num_matches]); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1053 | free(matches); |
| 1054 | matches = (char **) NULL; |
| 1055 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1056 | return; |
| 1057 | } |
| 1058 | if (*lastWasTab == FALSE) { |
| 1059 | |
| 1060 | char *tmp; |
| 1061 | int len_found; |
| 1062 | char matchBuf[BUFSIZ]; |
| 1063 | int find_type; |
| 1064 | int recalc_pos; |
| 1065 | |
| 1066 | *lastWasTab = TRUE; /* flop trigger */ |
| 1067 | |
| 1068 | /* Make a local copy of the string -- up |
| 1069 | * to the position of the cursor */ |
| 1070 | tmp = strncpy(matchBuf, command_ps, cursor); |
| 1071 | tmp[cursor] = 0; |
| 1072 | |
| 1073 | find_type = find_match(matchBuf, &recalc_pos); |
| 1074 | |
| 1075 | /* Free up any memory already allocated */ |
| 1076 | input_tab(0); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1077 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1078 | #ifdef BB_FEATURE_COMMAND_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1079 | /* 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] | 1080 | * then try completing this word as a username. */ |
| 1081 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1082 | if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1083 | matches = username_tab_completion(matchBuf, &num_matches); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1084 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1085 | /* Try to match any executable in our path and everything |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1086 | * in the current working directory that matches. */ |
| 1087 | if (!matches) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1088 | matches = |
| 1089 | exe_n_cwd_tab_completion(matchBuf, &num_matches, |
| 1090 | find_type); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1091 | |
| 1092 | /* Did we find exactly one match? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1093 | if (!matches || num_matches > 1) { |
| 1094 | char *tmp1; |
| 1095 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1096 | beep(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1097 | if (!matches) |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1098 | return; /* not found */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1099 | /* sort */ |
| 1100 | qsort(matches, num_matches, sizeof(char *), match_compare); |
| 1101 | |
| 1102 | /* find minimal match */ |
| 1103 | tmp = xstrdup(matches[0]); |
| 1104 | for (tmp1 = tmp; *tmp1; tmp1++) |
| 1105 | for (len_found = 1; len_found < num_matches; len_found++) |
| 1106 | if (matches[len_found][(tmp1 - tmp)] != *tmp1) { |
| 1107 | *tmp1 = 0; |
| 1108 | break; |
| 1109 | } |
| 1110 | if (*tmp == 0) { /* have unique */ |
| 1111 | free(tmp); |
| 1112 | return; |
| 1113 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1114 | } else { /* one match */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1115 | tmp = matches[0]; |
| 1116 | /* for next completion current found */ |
| 1117 | *lastWasTab = FALSE; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1120 | len_found = strlen(tmp); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1121 | /* have space to placed match? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1122 | if ((len_found - strlen(matchBuf) + len) < BUFSIZ) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1123 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1124 | /* before word for match */ |
| 1125 | command_ps[cursor - recalc_pos] = 0; |
| 1126 | /* save tail line */ |
| 1127 | strcpy(matchBuf, command_ps + cursor); |
| 1128 | /* add match */ |
| 1129 | strcat(command_ps, tmp); |
| 1130 | /* add tail */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1131 | strcat(command_ps, matchBuf); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1132 | /* back to begin word for match */ |
| 1133 | input_backward(recalc_pos); |
| 1134 | /* new pos */ |
| 1135 | recalc_pos = cursor + len_found; |
| 1136 | /* new len */ |
| 1137 | len = strlen(command_ps); |
| 1138 | /* write out the matched command */ |
| 1139 | input_end(); |
| 1140 | input_backward(cursor - recalc_pos); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1141 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1142 | if (tmp != matches[0]) |
| 1143 | free(tmp); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1144 | } else { |
| 1145 | /* Ok -- the last char was a TAB. Since they |
| 1146 | * just hit TAB again, print a list of all the |
| 1147 | * available choices... */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1148 | if (matches && num_matches > 0) { |
| 1149 | int i, col, l; |
| 1150 | int sav_cursor = cursor; /* change goto_new_line() */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1151 | |
| 1152 | /* Go to the next line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1153 | goto_new_line(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1154 | for (i = 0, col = 0; i < num_matches; i++) { |
| 1155 | l = strlen(matches[i]); |
| 1156 | if (l < 14) |
| 1157 | l = 14; |
| 1158 | printf("%-14s ", matches[i]); |
| 1159 | if ((l += 2) > 16) |
| 1160 | while (l % 16) { |
| 1161 | putchar(' '); |
| 1162 | l++; |
| 1163 | } |
| 1164 | col += l; |
| 1165 | col -= (col / cmdedit_termw) * cmdedit_termw; |
| 1166 | if (col > 60 && matches[i + 1] != NULL) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1167 | putchar('\n'); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1168 | col = 0; |
| 1169 | } |
| 1170 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1171 | /* Go to the next line and rewrite */ |
| 1172 | putchar('\n'); |
| 1173 | redraw(0, len - sav_cursor); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1177 | #endif /* BB_FEATURE_COMMAND_TAB_COMPLETION */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1178 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1179 | static void get_previous_history(struct history **hp, struct history *p) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1180 | { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 1181 | if ((*hp)->s) |
| 1182 | free((*hp)->s); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1183 | (*hp)->s = xstrdup(command_ps); |
| 1184 | *hp = p; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1187 | static inline void get_next_history(struct history **hp) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1188 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1189 | get_previous_history(hp, (*hp)->n); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1192 | enum { |
| 1193 | ESC = 27, |
| 1194 | DEL = 127, |
| 1195 | }; |
| 1196 | |
| 1197 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1198 | /* |
| 1199 | * This function is used to grab a character buffer |
| 1200 | * from the input file descriptor and allows you to |
| 1201 | * a string with full command editing (sortof like |
| 1202 | * a mini readline). |
| 1203 | * |
| 1204 | * The following standard commands are not implemented: |
| 1205 | * ESC-b -- Move back one word |
| 1206 | * ESC-f -- Move forward one word |
| 1207 | * ESC-d -- Delete back one word |
| 1208 | * ESC-h -- Delete forward one word |
| 1209 | * CTL-t -- Transpose two characters |
| 1210 | * |
| 1211 | * Furthermore, the "vi" command editing keys are not implemented. |
| 1212 | * |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1213 | */ |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1214 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1215 | extern void cmdedit_read_input(char *prompt, char command[BUFSIZ]) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1216 | { |
| 1217 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1218 | int inputFd = fileno(stdin); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1219 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1220 | int break_out = 0; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1221 | int lastWasTab = FALSE; |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 1222 | unsigned char c = 0; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1223 | struct history *hp = his_end; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1224 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1225 | /* prepare before init handlers */ |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1226 | cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1227 | len = 0; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1228 | command_ps = command; |
| 1229 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1230 | if (new_settings.c_cc[VMIN] == 0) { /* first call */ |
| 1231 | |
| 1232 | getTermSettings(inputFd, (void *) &initial_settings); |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1233 | memcpy(&new_settings, &initial_settings, sizeof(struct termios)); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1234 | |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1235 | new_settings.c_cc[VMIN] = 1; |
| 1236 | new_settings.c_cc[VTIME] = 0; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1237 | /* Turn off CTRL-C, so we can trap it */ |
| 1238 | new_settings.c_cc[VINTR] = _POSIX_VDISABLE; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1239 | new_settings.c_lflag &= ~ICANON; /* unbuffered input */ |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1240 | /* Turn off echoing */ |
| 1241 | new_settings.c_lflag &= ~(ECHO | ECHOCTL | ECHONL); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1242 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1243 | |
| 1244 | command[0] = 0; |
| 1245 | |
| 1246 | setTermSettings(inputFd, (void *) &new_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1247 | handlers_sets |= SET_RESET_TERM; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1248 | |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 1249 | /* Now initialize things */ |
| 1250 | cmdedit_init(); |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 1251 | /* Print out the command prompt */ |
| 1252 | parse_prompt(prompt); |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 1253 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1254 | while (1) { |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1255 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1256 | fflush(stdout); /* buffered out to fast */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1257 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1258 | if (read(inputFd, &c, 1) < 1) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1259 | return; |
Erik Andersen | f3b3d17 | 2000-04-09 18:24:05 +0000 | [diff] [blame] | 1260 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1261 | switch (c) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1262 | case '\n': |
| 1263 | case '\r': |
| 1264 | /* Enter */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1265 | goto_new_line(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1266 | break_out = 1; |
| 1267 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1268 | case 1: |
| 1269 | /* Control-a -- Beginning of line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1270 | input_backward(cursor); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1271 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1272 | case 2: |
| 1273 | /* Control-b -- Move back one character */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1274 | input_backward(1); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1275 | break; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1276 | case 3: |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1277 | /* Control-c -- stop gathering input */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1278 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1279 | /* Link into lash to reset context to 0 on ^C and such */ |
| 1280 | shell_context = 0; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1281 | |
| 1282 | /* Go to the next line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1283 | goto_new_line(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1284 | command[0] = 0; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1285 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1286 | return; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1287 | case 4: |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1288 | /* Control-d -- Delete one character, or exit |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1289 | * if the len=0 and no chars to delete */ |
| 1290 | if (len == 0) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1291 | printf("exit"); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1292 | clean_up_and_die(0); |
| 1293 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1294 | input_delete(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1295 | } |
| 1296 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1297 | case 5: |
| 1298 | /* Control-e -- End of line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1299 | input_end(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1300 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1301 | case 6: |
| 1302 | /* Control-f -- Move forward one character */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1303 | input_forward(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1304 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1305 | case '\b': |
| 1306 | case DEL: |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1307 | /* Control-h and DEL */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1308 | input_backspace(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1309 | break; |
| 1310 | case '\t': |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1311 | #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1312 | input_tab(&lastWasTab); |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 1313 | #endif |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1314 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1315 | case 14: |
| 1316 | /* Control-n -- Get next command in history */ |
| 1317 | if (hp && hp->n && hp->n->s) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1318 | get_next_history(&hp); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1319 | goto rewrite_line; |
| 1320 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1321 | beep(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1322 | } |
| 1323 | break; |
| 1324 | case 16: |
| 1325 | /* Control-p -- Get previous command from history */ |
| 1326 | if (hp && hp->p) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1327 | get_previous_history(&hp, hp->p); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1328 | goto rewrite_line; |
| 1329 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1330 | beep(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1331 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1332 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1333 | case 21: |
| 1334 | /* Control-U -- Clear line before cursor */ |
| 1335 | if (cursor) { |
| 1336 | strcpy(command, command + cursor); |
| 1337 | redraw(cmdedit_y, len -= cursor); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1338 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1339 | break; |
| 1340 | |
| 1341 | case ESC:{ |
| 1342 | /* escape sequence follows */ |
| 1343 | if (read(inputFd, &c, 1) < 1) |
| 1344 | return; |
| 1345 | /* different vt100 emulations */ |
| 1346 | if (c == '[' || c == 'O') { |
| 1347 | if (read(inputFd, &c, 1) < 1) |
| 1348 | return; |
| 1349 | } |
| 1350 | switch (c) { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1351 | #ifdef BB_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1352 | case '\t': /* Alt-Tab */ |
| 1353 | |
| 1354 | input_tab(&lastWasTab); |
| 1355 | break; |
| 1356 | #endif |
| 1357 | case 'A': |
| 1358 | /* Up Arrow -- Get previous command from history */ |
| 1359 | if (hp && hp->p) { |
| 1360 | get_previous_history(&hp, hp->p); |
| 1361 | goto rewrite_line; |
| 1362 | } else { |
| 1363 | beep(); |
| 1364 | } |
| 1365 | break; |
| 1366 | case 'B': |
| 1367 | /* Down Arrow -- Get next command in history */ |
| 1368 | if (hp && hp->n && hp->n->s) { |
| 1369 | get_next_history(&hp); |
| 1370 | goto rewrite_line; |
| 1371 | } else { |
| 1372 | beep(); |
| 1373 | } |
| 1374 | break; |
| 1375 | |
| 1376 | /* Rewrite the line with the selected history item */ |
| 1377 | rewrite_line: |
| 1378 | /* change command */ |
| 1379 | len = strlen(strcpy(command, hp->s)); |
| 1380 | /* redraw and go to end line */ |
| 1381 | redraw(cmdedit_y, 0); |
| 1382 | break; |
| 1383 | case 'C': |
| 1384 | /* Right Arrow -- Move forward one character */ |
| 1385 | input_forward(); |
| 1386 | break; |
| 1387 | case 'D': |
| 1388 | /* Left Arrow -- Move back one character */ |
| 1389 | input_backward(1); |
| 1390 | break; |
| 1391 | case '3': |
| 1392 | /* Delete */ |
| 1393 | input_delete(); |
| 1394 | break; |
| 1395 | case '1': |
| 1396 | case 'H': |
| 1397 | /* Home (Ctrl-A) */ |
| 1398 | input_backward(cursor); |
| 1399 | break; |
| 1400 | case '4': |
| 1401 | case 'F': |
| 1402 | /* End (Ctrl-E) */ |
| 1403 | input_end(); |
| 1404 | break; |
| 1405 | default: |
| 1406 | if (!(c >= '1' && c <= '9')) |
| 1407 | c = 0; |
| 1408 | beep(); |
| 1409 | } |
| 1410 | if (c >= '1' && c <= '9') |
| 1411 | do |
| 1412 | if (read(inputFd, &c, 1) < 1) |
| 1413 | return; |
| 1414 | while (c != '~'); |
| 1415 | break; |
| 1416 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1417 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1418 | default: /* If it's regular input, do the normal thing */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1419 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 1420 | /* Control-V -- Add non-printable symbol */ |
| 1421 | if (c == 22) { |
| 1422 | if (read(inputFd, &c, 1) < 1) |
| 1423 | return; |
| 1424 | if (c == 0) { |
| 1425 | beep(); |
| 1426 | break; |
| 1427 | } |
| 1428 | } else |
| 1429 | #endif |
| 1430 | if (!isprint(c)) /* Skip non-printable characters */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1431 | break; |
| 1432 | |
| 1433 | if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */ |
| 1434 | break; |
| 1435 | |
| 1436 | len++; |
| 1437 | |
| 1438 | if (cursor == (len - 1)) { /* Append if at the end of the line */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1439 | *(command + cursor) = c; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1440 | *(command + cursor + 1) = 0; |
| 1441 | cmdedit_set_out_char(0); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1442 | } else { /* Insert otherwise */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1443 | int sc = cursor; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1444 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1445 | memmove(command + sc + 1, command + sc, len - sc); |
| 1446 | *(command + sc) = c; |
| 1447 | sc++; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1448 | /* rewrite from cursor */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1449 | input_end(); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1450 | /* to prev x pos + 1 */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1451 | input_backward(cursor - sc); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1454 | break; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1455 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1456 | if (break_out) /* Enter is the command terminator, no more input. */ |
| 1457 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1458 | |
| 1459 | if (c != '\t') |
| 1460 | lastWasTab = FALSE; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1463 | setTermSettings(inputFd, (void *) &initial_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1464 | handlers_sets &= ~SET_RESET_TERM; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1465 | |
| 1466 | /* Handle command history log */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1467 | if (len) { /* no put empty line */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1468 | |
| 1469 | struct history *h = his_end; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1470 | char *ss; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1471 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1472 | ss = xstrdup(command); /* duplicate */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1473 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1474 | if (h == 0) { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 1475 | /* No previous history -- this memory is never freed */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1476 | h = his_front = xmalloc(sizeof(struct history)); |
| 1477 | h->n = xmalloc(sizeof(struct history)); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1478 | |
| 1479 | h->p = NULL; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1480 | h->s = ss; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1481 | h->n->p = h; |
| 1482 | h->n->n = NULL; |
| 1483 | h->n->s = NULL; |
| 1484 | his_end = h->n; |
| 1485 | history_counter++; |
| 1486 | } else { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 1487 | /* Add a new history command -- this memory is never freed */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1488 | h->n = xmalloc(sizeof(struct history)); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1489 | |
| 1490 | h->n->p = h; |
| 1491 | h->n->n = NULL; |
| 1492 | h->n->s = 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 | his_end = h->n; |
| 1495 | |
| 1496 | /* After max history, remove the oldest command */ |
| 1497 | if (history_counter >= MAX_HISTORY) { |
| 1498 | |
| 1499 | struct history *p = his_front->n; |
| 1500 | |
| 1501 | p->p = NULL; |
| 1502 | free(his_front->s); |
| 1503 | free(his_front); |
| 1504 | his_front = p; |
| 1505 | } else { |
| 1506 | history_counter++; |
| 1507 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1508 | } |
Mark Whitley | f594986 | 2001-03-14 00:29:14 +0000 | [diff] [blame] | 1509 | #if !defined(BB_FEATURE_SH_SIMPLE_PROMPT) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1510 | num_ok_lines++; |
| 1511 | #endif |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1512 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1513 | command[len++] = '\n'; /* set '\n' */ |
| 1514 | command[len] = 0; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1515 | #if defined(BB_FEATURE_CLEAN_UP) && defined(BB_FEATURE_COMMAND_TAB_COMPLETION) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1516 | input_tab(0); /* strong free */ |
| 1517 | #endif |
Mark Whitley | f594986 | 2001-03-14 00:29:14 +0000 | [diff] [blame] | 1518 | #if !defined(BB_FEATURE_SH_SIMPLE_PROMPT) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1519 | free(cmdedit_prompt); |
| 1520 | #endif |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1521 | return; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 1524 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1525 | /* Undo the effects of cmdedit_init(). */ |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1526 | extern void cmdedit_terminate(void) |
| 1527 | { |
| 1528 | cmdedit_reset_term(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1529 | if ((handlers_sets & SET_TERM_HANDLERS) != 0) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1530 | signal(SIGKILL, SIG_DFL); |
| 1531 | signal(SIGINT, SIG_DFL); |
| 1532 | signal(SIGQUIT, SIG_DFL); |
| 1533 | signal(SIGTERM, SIG_DFL); |
| 1534 | signal(SIGWINCH, SIG_DFL); |
| 1535 | handlers_sets &= ~SET_TERM_HANDLERS; |
| 1536 | } |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1539 | #endif /* BB_FEATURE_COMMAND_EDITING */ |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1540 | |
| 1541 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1542 | #ifdef TEST |
| 1543 | |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 1544 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 1545 | #include <locale.h> |
| 1546 | #endif |
| 1547 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1548 | unsigned int shell_context; |
| 1549 | |
| 1550 | int main(int argc, char **argv) |
| 1551 | { |
| 1552 | char buff[BUFSIZ]; |
| 1553 | char *prompt = |
Mark Whitley | f594986 | 2001-03-14 00:29:14 +0000 | [diff] [blame] | 1554 | #if !defined(BB_FEATURE_SH_SIMPLE_PROMPT) |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1555 | "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1556 | \\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \ |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1557 | \\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]"; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1558 | #else |
| 1559 | "% "; |
| 1560 | #endif |
| 1561 | |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 1562 | #ifdef BB_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 1563 | setlocale(LC_ALL, ""); |
| 1564 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1565 | shell_context = 1; |
| 1566 | do { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1567 | int l; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1568 | cmdedit_read_input(prompt, buff); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1569 | l = strlen(buff); |
| 1570 | if(l > 0 && buff[l-1] == '\n') |
| 1571 | buff[l-1] = 0; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1572 | printf("*** cmdedit_read_input() returned line =%s=\n", buff); |
| 1573 | } while (shell_context); |
| 1574 | printf("*** cmdedit_read_input() detect ^C\n"); |
| 1575 | return 0; |
| 1576 | } |
| 1577 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1578 | #endif /* TEST */ |