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