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