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