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