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 | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 3 | * Termios command line History and Editing. |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 5 | * Copyright (c) 1986-2003 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 |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 12 | * Erik Andersen <andersen@codepoet.org> (Majorly adjusted for busybox) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | 6728596 | 2004-01-14 09:34:51 +0000 | [diff] [blame] | 46 | #include "../shell/cmdedit.h" |
| 47 | |
Glenn L McGrath | 475820c | 2004-01-22 12:42:23 +0000 | [diff] [blame] | 48 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 49 | #ifdef CONFIG_LOCALE_SUPPORT |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 50 | #define Isprint(c) isprint((c)) |
| 51 | #else |
| 52 | #define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') ) |
| 53 | #endif |
| 54 | |
Glenn L McGrath | 3b25185 | 2004-01-03 12:07:32 +0000 | [diff] [blame] | 55 | #ifdef TEST |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 56 | |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 57 | /* pretect redefined for test */ |
| 58 | #undef CONFIG_FEATURE_COMMAND_EDITING |
| 59 | #undef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
| 60 | #undef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION |
| 61 | #undef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 62 | #undef CONFIG_FEATURE_CLEAN_UP |
| 63 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 64 | #define CONFIG_FEATURE_COMMAND_EDITING |
| 65 | #define CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
| 66 | #define CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION |
| 67 | #define CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 68 | #define CONFIG_FEATURE_CLEAN_UP |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 69 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 70 | #endif /* TEST */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 71 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 72 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | 5165fbe | 2001-02-20 06:42:29 +0000 | [diff] [blame] | 73 | #include <dirent.h> |
| 74 | #include <sys/stat.h> |
| 75 | #endif |
| 76 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 77 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 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 | 475820c | 2004-01-22 12:42:23 +0000 | [diff] [blame] | 84 | #include "pwd_.h" |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 85 | #endif /* advanced FEATURES */ |
Eric Andersen | af4ac77 | 2001-02-01 22:43:49 +0000 | [diff] [blame] | 86 | |
| 87 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 88 | /* Maximum length of the linked list for the command line history */ |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 89 | #ifndef CONFIG_FEATURE_COMMAND_HISTORY |
| 90 | #define MAX_HISTORY 15 |
| 91 | #else |
| 92 | #define MAX_HISTORY CONFIG_FEATURE_COMMAND_HISTORY |
| 93 | #endif |
| 94 | |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 95 | #if MAX_HISTORY < 1 |
| 96 | #warning cmdedit: You set MAX_HISTORY < 1. The history algorithm switched off. |
| 97 | #else |
| 98 | static char *history[MAX_HISTORY+1]; /* history + current */ |
| 99 | /* saved history lines */ |
| 100 | static int n_history; |
| 101 | /* current pointer to history line */ |
| 102 | static int cur_history; |
| 103 | #endif |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 104 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 105 | #include <termios.h> |
| 106 | #define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) |
| 107 | #define getTermSettings(fd,argp) tcgetattr(fd, argp); |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 108 | |
| 109 | /* Current termio and the previous termio before starting sh */ |
Eric Andersen | 63a8622 | 2000-11-07 06:52:13 +0000 | [diff] [blame] | 110 | static struct termios initial_settings, new_settings; |
Erik Andersen | 8ea7d8c | 2000-05-20 00:40:08 +0000 | [diff] [blame] | 111 | |
| 112 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 113 | static |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 114 | volatile int cmdedit_termw = 80; /* actual terminal width */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 115 | static |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 116 | volatile int handlers_sets = 0; /* Set next bites: */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 117 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 118 | enum { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 119 | SET_ATEXIT = 1, /* when atexit() has been called |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 120 | and get euid,uid,gid to fast compare */ |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 121 | SET_WCHG_HANDLERS = 2, /* winchg signal handler */ |
| 122 | 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] | 123 | }; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 124 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 125 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 126 | static int cmdedit_x; /* real x terminal position */ |
| 127 | static int cmdedit_y; /* pseudoreal y terminal position */ |
| 128 | static int cmdedit_prmt_len; /* lenght prompt without colores string */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 129 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 130 | static int cursor; /* required global for signal handler */ |
| 131 | static int len; /* --- "" - - "" - -"- --""-- --""--- */ |
| 132 | static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 133 | static |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 134 | #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 135 | const |
| 136 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 137 | char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 138 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 139 | #ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 140 | static char *user_buf = ""; |
| 141 | static char *home_pwd_buf = ""; |
| 142 | static int my_euid; |
| 143 | #endif |
| 144 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 145 | #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 146 | static char *hostname_buf; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 147 | static int num_ok_lines = 1; |
| 148 | #endif |
| 149 | |
| 150 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 151 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 152 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 153 | #ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 154 | static int my_euid; |
| 155 | #endif |
| 156 | |
| 157 | static int my_uid; |
| 158 | static int my_gid; |
| 159 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 160 | #endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */ |
| 161 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 162 | static void cmdedit_setwidth(int w, int redraw_flg); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 163 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 164 | static void win_changed(int nsig) |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 165 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 166 | static sighandler_t previous_SIGWINCH_handler; /* for reset */ |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 167 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 168 | /* emulate || signal call */ |
| 169 | if (nsig == -SIGWINCH || nsig == SIGWINCH) { |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 170 | int width = 0; |
| 171 | get_terminal_width_height(0, &width, NULL); |
| 172 | cmdedit_setwidth(width, nsig == SIGWINCH); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 173 | } |
Eric Andersen | 4bbdd78 | 2001-01-30 22:23:17 +0000 | [diff] [blame] | 174 | /* Unix not all standart in recall signal */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 175 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 176 | if (nsig == -SIGWINCH) /* save previous handler */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 177 | previous_SIGWINCH_handler = signal(SIGWINCH, win_changed); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 178 | else if (nsig == SIGWINCH) /* signaled called handler */ |
| 179 | signal(SIGWINCH, win_changed); /* set for next call */ |
| 180 | else /* nsig == 0 */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 181 | /* set previous handler */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 182 | signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 183 | } |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 184 | |
| 185 | static void cmdedit_reset_term(void) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 186 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 187 | if ((handlers_sets & SET_RESET_TERM) != 0) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 188 | /* sparc and other have broken termios support: use old termio handling. */ |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 189 | setTermSettings(STDIN_FILENO, (void *) &initial_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 190 | handlers_sets &= ~SET_RESET_TERM; |
| 191 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 192 | if ((handlers_sets & SET_WCHG_HANDLERS) != 0) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 193 | /* reset SIGWINCH handler to previous (default) */ |
| 194 | win_changed(0); |
| 195 | handlers_sets &= ~SET_WCHG_HANDLERS; |
| 196 | } |
| 197 | fflush(stdout); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 200 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 201 | /* special for recount position for scroll and remove terminal margin effect */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 202 | static void cmdedit_set_out_char(int next_char) |
| 203 | { |
| 204 | |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 205 | int c = (int)((unsigned char) command_ps[cursor]); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 206 | |
| 207 | if (c == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 208 | c = ' '; /* destroy end char? */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 209 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 210 | if (!Isprint(c)) { /* Inverse put non-printable characters */ |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 211 | if (c >= 128) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 212 | c -= 128; |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 213 | if (c < ' ') |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 214 | c += '@'; |
| 215 | if (c == 127) |
| 216 | c = '?'; |
| 217 | printf("\033[7m%c\033[0m", c); |
| 218 | } else |
| 219 | #endif |
| 220 | putchar(c); |
| 221 | if (++cmdedit_x >= cmdedit_termw) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 222 | /* terminal is scrolled down */ |
| 223 | cmdedit_y++; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 224 | cmdedit_x = 0; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 225 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 226 | if (!next_char) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 227 | next_char = ' '; |
| 228 | /* destroy "(auto)margin" */ |
| 229 | putchar(next_char); |
| 230 | putchar('\b'); |
| 231 | } |
| 232 | cursor++; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 235 | /* Move to end line. Bonus: rewrite line from cursor */ |
| 236 | static void input_end(void) |
| 237 | { |
| 238 | while (cursor < len) |
| 239 | cmdedit_set_out_char(0); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* Go to the next line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 243 | static void goto_new_line(void) |
| 244 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 245 | input_end(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 246 | if (cmdedit_x) |
| 247 | putchar('\n'); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 251 | static inline void out1str(const char *s) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 252 | { |
Robert Griebl | b230159 | 2002-07-30 23:13:51 +0000 | [diff] [blame] | 253 | if ( s ) |
| 254 | fputs(s, stdout); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 255 | } |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 256 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 257 | static inline void beep(void) |
| 258 | { |
| 259 | putchar('\007'); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 262 | /* Move back one character */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 263 | /* special for slow terminal */ |
| 264 | static void input_backward(int num) |
| 265 | { |
| 266 | if (num > cursor) |
| 267 | num = cursor; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 268 | cursor -= num; /* new cursor (in command, not terminal) */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 269 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 270 | if (cmdedit_x >= num) { /* no to up line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 271 | cmdedit_x -= num; |
| 272 | if (num < 4) |
| 273 | while (num-- > 0) |
| 274 | putchar('\b'); |
| 275 | |
| 276 | else |
| 277 | printf("\033[%dD", num); |
| 278 | } else { |
| 279 | int count_y; |
| 280 | |
| 281 | if (cmdedit_x) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 282 | putchar('\r'); /* back to first terminal pos. */ |
| 283 | num -= cmdedit_x; /* set previous backward */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 284 | } |
| 285 | count_y = 1 + num / cmdedit_termw; |
| 286 | printf("\033[%dA", count_y); |
| 287 | cmdedit_y -= count_y; |
| 288 | /* require forward after uping */ |
| 289 | cmdedit_x = cmdedit_termw * count_y - num; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 290 | printf("\033[%dC", cmdedit_x); /* set term cursor */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 291 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 294 | static void put_prompt(void) |
| 295 | { |
| 296 | out1str(cmdedit_prompt); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 297 | cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 298 | cursor = 0; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 299 | cmdedit_y = 0; /* new quasireal y */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 302 | #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 303 | static void parse_prompt(const char *prmt_ptr) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 304 | { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 305 | cmdedit_prompt = prmt_ptr; |
| 306 | cmdedit_prmt_len = strlen(prmt_ptr); |
| 307 | put_prompt(); |
| 308 | } |
| 309 | #else |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 310 | static void parse_prompt(const char *prmt_ptr) |
| 311 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 312 | int prmt_len = 0; |
"Vladimir N. Oleynik" | f087480 | 2005-09-05 15:46:26 +0000 | [diff] [blame] | 313 | size_t cur_prmt_len = 0; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 314 | char flg_not_length = '['; |
| 315 | char *prmt_mem_ptr = xcalloc(1, 1); |
| 316 | char *pwd_buf = xgetcwd(0); |
| 317 | char buf2[PATH_MAX + 1]; |
| 318 | char buf[2]; |
| 319 | char c; |
| 320 | char *pbuf; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 321 | |
Eric Andersen | 5f265b7 | 2001-05-11 16:58:46 +0000 | [diff] [blame] | 322 | if (!pwd_buf) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 323 | pwd_buf=(char *)bb_msg_unknown; |
Eric Andersen | 5f265b7 | 2001-05-11 16:58:46 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 326 | while (*prmt_ptr) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 327 | pbuf = buf; |
| 328 | pbuf[1] = 0; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 329 | c = *prmt_ptr++; |
| 330 | if (c == '\\') { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 331 | const char *cp = prmt_ptr; |
| 332 | int l; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 333 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 334 | c = bb_process_escape_sequence(&prmt_ptr); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 335 | if(prmt_ptr==cp) { |
| 336 | if (*cp == 0) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 337 | break; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 338 | c = *prmt_ptr++; |
| 339 | switch (c) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 340 | #ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 341 | case 'u': |
| 342 | pbuf = user_buf; |
| 343 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 344 | #endif |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 345 | case 'h': |
| 346 | pbuf = hostname_buf; |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 347 | if (pbuf == 0) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 348 | pbuf = xcalloc(256, 1); |
| 349 | if (gethostname(pbuf, 255) < 0) { |
| 350 | strcpy(pbuf, "?"); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 351 | } else { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 352 | char *s = strchr(pbuf, '.'); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 353 | |
| 354 | if (s) |
| 355 | *s = 0; |
| 356 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 357 | hostname_buf = pbuf; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 358 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 359 | break; |
| 360 | case '$': |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 361 | c = my_euid == 0 ? '#' : '$'; |
| 362 | break; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 363 | #ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 364 | case 'w': |
| 365 | pbuf = pwd_buf; |
| 366 | l = strlen(home_pwd_buf); |
| 367 | if (home_pwd_buf[0] != 0 && |
| 368 | strncmp(home_pwd_buf, pbuf, l) == 0 && |
| 369 | (pbuf[l]=='/' || pbuf[l]=='\0') && |
| 370 | strlen(pwd_buf+l)<PATH_MAX) { |
| 371 | pbuf = buf2; |
| 372 | *pbuf = '~'; |
| 373 | strcpy(pbuf+1, pwd_buf+l); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 374 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 375 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 376 | #endif |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 377 | case 'W': |
| 378 | pbuf = pwd_buf; |
| 379 | cp = strrchr(pbuf,'/'); |
| 380 | if ( (cp != NULL) && (cp != pbuf) ) |
| 381 | pbuf += (cp-pbuf)+1; |
| 382 | break; |
| 383 | case '!': |
| 384 | snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines); |
| 385 | break; |
| 386 | case 'e': case 'E': /* \e \E = \033 */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 387 | c = '\033'; |
| 388 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 389 | case 'x': case 'X': |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 390 | for (l = 0; l < 3;) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 391 | int h; |
| 392 | buf2[l++] = *prmt_ptr; |
| 393 | buf2[l] = 0; |
| 394 | h = strtol(buf2, &pbuf, 16); |
| 395 | if (h > UCHAR_MAX || (pbuf - buf2) < l) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 396 | l--; |
| 397 | break; |
| 398 | } |
| 399 | prmt_ptr++; |
| 400 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 401 | buf2[l] = 0; |
| 402 | c = (char)strtol(buf2, 0, 16); |
| 403 | if(c==0) |
| 404 | c = '?'; |
| 405 | pbuf = buf; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 406 | break; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 407 | case '[': case ']': |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 408 | if (c == flg_not_length) { |
| 409 | flg_not_length = flg_not_length == '[' ? ']' : '['; |
| 410 | continue; |
| 411 | } |
| 412 | break; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 413 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 414 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 415 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 416 | if(pbuf == buf) |
| 417 | *pbuf = c; |
"Vladimir N. Oleynik" | f087480 | 2005-09-05 15:46:26 +0000 | [diff] [blame] | 418 | cur_prmt_len = strlen(pbuf); |
| 419 | prmt_len += cur_prmt_len; |
| 420 | if (flg_not_length != ']') |
| 421 | cmdedit_prmt_len += cur_prmt_len; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 422 | prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 423 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 424 | if(pwd_buf!=(char *)bb_msg_unknown) |
Eric Andersen | 8f69784 | 2001-07-02 15:36:57 +0000 | [diff] [blame] | 425 | free(pwd_buf); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 426 | cmdedit_prompt = prmt_mem_ptr; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 427 | put_prompt(); |
| 428 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 429 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 430 | |
| 431 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 432 | /* draw prompt, editor line, and clear tail */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 433 | static void redraw(int y, int back_cursor) |
| 434 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 435 | if (y > 0) /* up to start y */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 436 | printf("\033[%dA", y); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 437 | putchar('\r'); |
| 438 | put_prompt(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 439 | input_end(); /* rewrite */ |
| 440 | printf("\033[J"); /* destroy tail after cursor */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 441 | input_backward(back_cursor); |
| 442 | } |
| 443 | |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 444 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI |
| 445 | static char delbuf[BUFSIZ]; /* a place to store deleted characters */ |
| 446 | static char *delp = delbuf; |
| 447 | static int newdelflag; /* whether delbuf should be reused yet */ |
| 448 | #endif |
| 449 | |
| 450 | /* Delete the char in front of the cursor, optionally saving it |
| 451 | * for later putback */ |
| 452 | static void input_delete(int save) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 453 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 454 | int j = cursor; |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 455 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 456 | if (j == len) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 457 | return; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 458 | |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 459 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI |
| 460 | if (save) { |
| 461 | if (newdelflag) { |
| 462 | delp = delbuf; |
| 463 | newdelflag = 0; |
| 464 | } |
| 465 | if (delp - delbuf < BUFSIZ) |
| 466 | *delp++ = command_ps[j]; |
| 467 | } |
| 468 | #endif |
| 469 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 470 | strcpy(command_ps + j, command_ps + j + 1); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 471 | len--; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 472 | input_end(); /* rewrite new line */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +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 | |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 477 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI |
| 478 | static void put(void) |
| 479 | { |
| 480 | int ocursor, j = delp - delbuf; |
| 481 | if (j == 0) |
| 482 | return; |
| 483 | ocursor = cursor; |
| 484 | /* open hole and then fill it */ |
| 485 | memmove(command_ps + cursor + j, command_ps + cursor, len - cursor + 1); |
| 486 | strncpy(command_ps + cursor, delbuf, j); |
| 487 | len += j; |
| 488 | input_end(); /* rewrite new line */ |
| 489 | input_backward(cursor-ocursor-j+1); /* at end of new text */ |
| 490 | } |
| 491 | #endif |
| 492 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 493 | /* Delete the char in back of the cursor */ |
| 494 | static void input_backspace(void) |
| 495 | { |
| 496 | if (cursor > 0) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 497 | input_backward(1); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 498 | input_delete(0); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
| 502 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 503 | /* Move forward one character */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 504 | static void input_forward(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 505 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 506 | if (cursor < len) |
| 507 | cmdedit_set_out_char(command_ps[cursor + 1]); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 510 | static void cmdedit_setwidth(int w, int redraw_flg) |
| 511 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 512 | cmdedit_termw = cmdedit_prmt_len + 2; |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 513 | if (w <= cmdedit_termw) { |
| 514 | cmdedit_termw = cmdedit_termw % w; |
| 515 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 516 | if (w > cmdedit_termw) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 517 | cmdedit_termw = w; |
| 518 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 519 | if (redraw_flg) { |
| 520 | /* new y for current cursor */ |
| 521 | int new_y = (cursor + cmdedit_prmt_len) / w; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 522 | |
| 523 | /* redraw */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 524 | redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor); |
| 525 | fflush(stdout); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 526 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 527 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 530 | static void cmdedit_init(void) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 531 | { |
Eric Andersen | 61173a5 | 2001-03-19 17:48:55 +0000 | [diff] [blame] | 532 | cmdedit_prmt_len = 0; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 533 | if ((handlers_sets & SET_WCHG_HANDLERS) == 0) { |
| 534 | /* emulate usage handler to set handler and call yours work */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 535 | win_changed(-SIGWINCH); |
| 536 | handlers_sets |= SET_WCHG_HANDLERS; |
| 537 | } |
| 538 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 539 | if ((handlers_sets & SET_ATEXIT) == 0) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 540 | #ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 541 | struct passwd *entry; |
| 542 | |
| 543 | my_euid = geteuid(); |
| 544 | entry = getpwuid(my_euid); |
| 545 | if (entry) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 546 | user_buf = bb_xstrdup(entry->pw_name); |
| 547 | home_pwd_buf = bb_xstrdup(entry->pw_dir); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 548 | } |
| 549 | #endif |
| 550 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 551 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 552 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 553 | #ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 554 | my_euid = geteuid(); |
| 555 | #endif |
| 556 | my_uid = getuid(); |
| 557 | my_gid = getgid(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 558 | #endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 559 | handlers_sets |= SET_ATEXIT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 560 | atexit(cmdedit_reset_term); /* be sure to do this only once */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 561 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 562 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 563 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 564 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 565 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 566 | static int is_execute(const struct stat *st) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 567 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 568 | if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) || |
| 569 | (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) || |
| 570 | (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) || |
| 571 | (st->st_mode & S_IXOTH)) return TRUE; |
| 572 | return FALSE; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 573 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 574 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 575 | #ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 576 | |
| 577 | static char **username_tab_completion(char *ud, int *num_matches) |
| 578 | { |
| 579 | struct passwd *entry; |
| 580 | int userlen; |
| 581 | char *temp; |
| 582 | |
| 583 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 584 | ud++; /* ~user/... to user/... */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 585 | userlen = strlen(ud); |
| 586 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 587 | if (num_matches == 0) { /* "~/..." or "~user/..." */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 588 | char *sav_ud = ud - 1; |
| 589 | char *home = 0; |
| 590 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 591 | if (*ud == '/') { /* "~/..." */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 592 | home = home_pwd_buf; |
| 593 | } else { |
| 594 | /* "~user/..." */ |
| 595 | temp = strchr(ud, '/'); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 596 | *temp = 0; /* ~user\0 */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 597 | entry = getpwnam(ud); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 598 | *temp = '/'; /* restore ~user/... */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 599 | ud = temp; |
| 600 | if (entry) |
| 601 | home = entry->pw_dir; |
| 602 | } |
| 603 | if (home) { |
| 604 | if ((userlen + strlen(home) + 1) < BUFSIZ) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 605 | char temp2[BUFSIZ]; /* argument size */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 606 | |
| 607 | /* /home/user/... */ |
| 608 | sprintf(temp2, "%s%s", home, ud); |
| 609 | strcpy(sav_ud, temp2); |
| 610 | } |
| 611 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 612 | return 0; /* void, result save to argument :-) */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 613 | } else { |
| 614 | /* "~[^/]*" */ |
| 615 | char **matches = (char **) NULL; |
| 616 | int nm = 0; |
| 617 | |
| 618 | setpwent(); |
| 619 | |
| 620 | while ((entry = getpwent()) != NULL) { |
| 621 | /* Null usernames should result in all users as possible completions. */ |
| 622 | if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) { |
| 623 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 624 | bb_xasprintf(&temp, "~%s/", entry->pw_name); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 625 | matches = xrealloc(matches, (nm + 1) * sizeof(char *)); |
| 626 | |
| 627 | matches[nm++] = temp; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | endpwent(); |
| 632 | (*num_matches) = nm; |
| 633 | return (matches); |
| 634 | } |
| 635 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 636 | #endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 637 | |
| 638 | enum { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 639 | FIND_EXE_ONLY = 0, |
| 640 | FIND_DIR_ONLY = 1, |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 641 | FIND_FILE_ONLY = 2, |
| 642 | }; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 643 | |
Glenn L McGrath | 6728596 | 2004-01-14 09:34:51 +0000 | [diff] [blame] | 644 | #ifdef CONFIG_ASH |
| 645 | const char *cmdedit_path_lookup; |
| 646 | #else |
| 647 | #define cmdedit_path_lookup getenv("PATH") |
| 648 | #endif |
| 649 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 650 | static int path_parse(char ***p, int flags) |
| 651 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 652 | int npth; |
Glenn L McGrath | 6728596 | 2004-01-14 09:34:51 +0000 | [diff] [blame] | 653 | const char *tmp; |
| 654 | const char *pth; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 655 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 656 | /* if not setenv PATH variable, to search cur dir "." */ |
Glenn L McGrath | 6728596 | 2004-01-14 09:34:51 +0000 | [diff] [blame] | 657 | if (flags != FIND_EXE_ONLY || (pth = cmdedit_path_lookup) == 0 || |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 658 | /* PATH=<empty> or PATH=:<empty> */ |
| 659 | *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 660 | return 1; |
| 661 | } |
| 662 | |
| 663 | tmp = pth; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 664 | npth = 0; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 665 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 666 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 667 | npth++; /* count words is + 1 count ':' */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 668 | tmp = strchr(tmp, ':'); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 669 | if (tmp) { |
| 670 | if (*++tmp == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 671 | break; /* :<empty> */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 672 | } else |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 673 | break; |
| 674 | } |
| 675 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 676 | *p = xmalloc(npth * sizeof(char *)); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 677 | |
| 678 | tmp = pth; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 679 | (*p)[0] = bb_xstrdup(tmp); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 680 | npth = 1; /* count words is + 1 count ':' */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 681 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 682 | for (;;) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 683 | tmp = strchr(tmp, ':'); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 684 | if (tmp) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 685 | (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 686 | if (*++tmp == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 687 | break; /* :<empty> */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 688 | } else |
| 689 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 690 | (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | return npth; |
| 694 | } |
| 695 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 696 | static char *add_quote_for_spec_chars(char *found) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 697 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 698 | int l = 0; |
| 699 | char *s = xmalloc((strlen(found) + 1) * 2); |
| 700 | |
| 701 | while (*found) { |
| 702 | if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found)) |
| 703 | s[l++] = '\\'; |
| 704 | s[l++] = *found++; |
| 705 | } |
| 706 | s[l] = 0; |
| 707 | return s; |
| 708 | } |
| 709 | |
| 710 | static char **exe_n_cwd_tab_completion(char *command, int *num_matches, |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 711 | int type) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 712 | { |
| 713 | |
| 714 | char **matches = 0; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 715 | DIR *dir; |
| 716 | struct dirent *next; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 717 | char dirbuf[BUFSIZ]; |
| 718 | int nm = *num_matches; |
| 719 | struct stat st; |
| 720 | char *path1[1]; |
| 721 | char **paths = path1; |
| 722 | int npaths; |
| 723 | int i; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 724 | char *found; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 725 | char *pfind = strrchr(command, '/'); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 726 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 727 | path1[0] = "."; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 728 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 729 | if (pfind == NULL) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 730 | /* no dir, if flags==EXE_ONLY - get paths, else "." */ |
| 731 | npaths = path_parse(&paths, type); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 732 | pfind = command; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 733 | } else { |
| 734 | /* with dir */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 735 | /* save for change */ |
| 736 | strcpy(dirbuf, command); |
| 737 | /* set dir only */ |
| 738 | dirbuf[(pfind - command) + 1] = 0; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 739 | #ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 740 | if (dirbuf[0] == '~') /* ~/... or ~user/... */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 741 | username_tab_completion(dirbuf, 0); |
| 742 | #endif |
| 743 | /* "strip" dirname in command */ |
| 744 | pfind++; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 745 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 746 | paths[0] = dirbuf; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 747 | npaths = 1; /* only 1 dir */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 748 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 749 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 750 | for (i = 0; i < npaths; i++) { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 751 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 752 | dir = opendir(paths[i]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 753 | if (!dir) /* Don't print an error */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 754 | continue; |
| 755 | |
| 756 | while ((next = readdir(dir)) != NULL) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 757 | char *str_found = next->d_name; |
| 758 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 759 | /* matched ? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 760 | if (strncmp(str_found, pfind, strlen(pfind))) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 761 | continue; |
| 762 | /* not see .name without .match */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 763 | if (*str_found == '.' && *pfind == 0) { |
| 764 | if (*paths[i] == '/' && paths[i][1] == 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 765 | && str_found[1] == 0) str_found = ""; /* only "/" */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 766 | else |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 767 | continue; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 768 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 769 | found = concat_path_file(paths[i], str_found); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 770 | /* hmm, remover in progress? */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 771 | if (stat(found, &st) < 0) |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 772 | goto cont; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 773 | /* find with dirs ? */ |
| 774 | if (paths[i] != dirbuf) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 775 | strcpy(found, next->d_name); /* only name */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 776 | if (S_ISDIR(st.st_mode)) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 777 | /* name is directory */ |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 778 | str_found = found; |
| 779 | found = concat_path_file(found, ""); |
| 780 | free(str_found); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 781 | str_found = add_quote_for_spec_chars(found); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 782 | } else { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 783 | /* not put found file if search only dirs for cd */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 784 | if (type == FIND_DIR_ONLY) |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 785 | goto cont; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 786 | str_found = add_quote_for_spec_chars(found); |
| 787 | if (type == FIND_FILE_ONLY || |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 788 | (type == FIND_EXE_ONLY && is_execute(&st))) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 789 | strcat(str_found, " "); |
| 790 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 791 | /* Add it to the list */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 792 | matches = xrealloc(matches, (nm + 1) * sizeof(char *)); |
| 793 | |
| 794 | matches[nm++] = str_found; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 795 | cont: |
| 796 | free(found); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 797 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 798 | closedir(dir); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 799 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 800 | if (paths != path1) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 801 | free(paths[0]); /* allocated memory only in first member */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 802 | free(paths); |
| 803 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 804 | *num_matches = nm; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 805 | return (matches); |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 806 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 807 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 808 | static int match_compare(const void *a, const void *b) |
| 809 | { |
| 810 | return strcmp(*(char **) a, *(char **) b); |
| 811 | } |
| 812 | |
| 813 | |
| 814 | |
| 815 | #define QUOT (UCHAR_MAX+1) |
| 816 | |
| 817 | #define collapse_pos(is, in) { \ |
Paul Fox | 574fee4 | 2005-07-19 20:41:06 +0000 | [diff] [blame] | 818 | memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \ |
| 819 | memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 820 | |
| 821 | static int find_match(char *matchBuf, int *len_with_quotes) |
| 822 | { |
| 823 | int i, j; |
| 824 | int command_mode; |
| 825 | int c, c2; |
| 826 | int int_buf[BUFSIZ + 1]; |
| 827 | int pos_buf[BUFSIZ + 1]; |
| 828 | |
| 829 | /* set to integer dimension characters and own positions */ |
| 830 | for (i = 0;; i++) { |
| 831 | int_buf[i] = (int) ((unsigned char) matchBuf[i]); |
| 832 | if (int_buf[i] == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 833 | pos_buf[i] = -1; /* indicator end line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 834 | break; |
| 835 | } else |
| 836 | pos_buf[i] = i; |
| 837 | } |
| 838 | |
| 839 | /* mask \+symbol and convert '\t' to ' ' */ |
| 840 | for (i = j = 0; matchBuf[i]; i++, j++) |
| 841 | if (matchBuf[i] == '\\') { |
| 842 | collapse_pos(j, j + 1); |
| 843 | int_buf[j] |= QUOT; |
| 844 | i++; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 845 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 846 | if (matchBuf[i] == '\t') /* algorithm equivalent */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 847 | int_buf[j] = ' ' | QUOT; |
| 848 | #endif |
| 849 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 850 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 851 | else if (matchBuf[i] == '\t') |
| 852 | int_buf[j] = ' '; |
| 853 | #endif |
| 854 | |
| 855 | /* mask "symbols" or 'symbols' */ |
| 856 | c2 = 0; |
| 857 | for (i = 0; int_buf[i]; i++) { |
| 858 | c = int_buf[i]; |
| 859 | if (c == '\'' || c == '"') { |
| 860 | if (c2 == 0) |
| 861 | c2 = c; |
| 862 | else { |
| 863 | if (c == c2) |
| 864 | c2 = 0; |
| 865 | else |
| 866 | int_buf[i] |= QUOT; |
| 867 | } |
| 868 | } else if (c2 != 0 && c != '$') |
| 869 | int_buf[i] |= QUOT; |
| 870 | } |
| 871 | |
| 872 | /* skip commands with arguments if line have commands delimiters */ |
| 873 | /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */ |
| 874 | for (i = 0; int_buf[i]; i++) { |
| 875 | c = int_buf[i]; |
| 876 | c2 = int_buf[i + 1]; |
| 877 | j = i ? int_buf[i - 1] : -1; |
| 878 | command_mode = 0; |
| 879 | if (c == ';' || c == '&' || c == '|') { |
| 880 | command_mode = 1 + (c == c2); |
| 881 | if (c == '&') { |
| 882 | if (j == '>' || j == '<') |
| 883 | command_mode = 0; |
| 884 | } else if (c == '|' && j == '>') |
| 885 | command_mode = 0; |
| 886 | } |
| 887 | if (command_mode) { |
| 888 | collapse_pos(0, i + command_mode); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 889 | i = -1; /* hack incremet */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | /* collapse `command...` */ |
| 893 | for (i = 0; int_buf[i]; i++) |
| 894 | if (int_buf[i] == '`') { |
| 895 | for (j = i + 1; int_buf[j]; j++) |
| 896 | if (int_buf[j] == '`') { |
| 897 | collapse_pos(i, j + 1); |
| 898 | j = 0; |
| 899 | break; |
| 900 | } |
| 901 | if (j) { |
| 902 | /* not found close ` - command mode, collapse all previous */ |
| 903 | collapse_pos(0, i + 1); |
| 904 | break; |
| 905 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 906 | i--; /* hack incremet */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /* collapse (command...(command...)...) or {command...{command...}...} */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 910 | c = 0; /* "recursive" level */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 911 | c2 = 0; |
| 912 | for (i = 0; int_buf[i]; i++) |
| 913 | if (int_buf[i] == '(' || int_buf[i] == '{') { |
| 914 | if (int_buf[i] == '(') |
| 915 | c++; |
| 916 | else |
| 917 | c2++; |
| 918 | collapse_pos(0, i + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 919 | i = -1; /* hack incremet */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 920 | } |
| 921 | for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++) |
| 922 | if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) { |
| 923 | if (int_buf[i] == ')') |
| 924 | c--; |
| 925 | else |
| 926 | c2--; |
| 927 | collapse_pos(0, i + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 928 | i = -1; /* hack incremet */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /* skip first not quote space */ |
| 932 | for (i = 0; int_buf[i]; i++) |
| 933 | if (int_buf[i] != ' ') |
| 934 | break; |
| 935 | if (i) |
| 936 | collapse_pos(0, i); |
| 937 | |
| 938 | /* set find mode for completion */ |
| 939 | command_mode = FIND_EXE_ONLY; |
| 940 | for (i = 0; int_buf[i]; i++) |
| 941 | if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') { |
| 942 | if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 943 | && matchBuf[pos_buf[0]]=='c' |
| 944 | && matchBuf[pos_buf[1]]=='d' ) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 945 | command_mode = FIND_DIR_ONLY; |
| 946 | else { |
| 947 | command_mode = FIND_FILE_ONLY; |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | /* "strlen" */ |
| 952 | for (i = 0; int_buf[i]; i++); |
| 953 | /* find last word */ |
| 954 | for (--i; i >= 0; i--) { |
| 955 | c = int_buf[i]; |
| 956 | if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') { |
| 957 | collapse_pos(0, i + 1); |
| 958 | break; |
| 959 | } |
| 960 | } |
| 961 | /* skip first not quoted '\'' or '"' */ |
| 962 | for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++); |
| 963 | /* collapse quote or unquote // or /~ */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 964 | while ((int_buf[i] & ~QUOT) == '/' && |
Mark Whitley | 7e5291f | 2001-03-08 19:31:12 +0000 | [diff] [blame] | 965 | ((int_buf[i + 1] & ~QUOT) == '/' |
| 966 | || (int_buf[i + 1] & ~QUOT) == '~')) { |
| 967 | i++; |
| 968 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 969 | |
| 970 | /* set only match and destroy quotes */ |
| 971 | j = 0; |
Eric Andersen | 4f99053 | 2001-05-31 17:15:57 +0000 | [diff] [blame] | 972 | for (c = 0; pos_buf[i] >= 0; i++) { |
| 973 | matchBuf[c++] = matchBuf[pos_buf[i]]; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 974 | j = pos_buf[i] + 1; |
| 975 | } |
Eric Andersen | 4f99053 | 2001-05-31 17:15:57 +0000 | [diff] [blame] | 976 | matchBuf[c] = 0; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 977 | /* old lenght matchBuf with quotes symbols */ |
| 978 | *len_with_quotes = j ? j - pos_buf[0] : 0; |
| 979 | |
| 980 | return command_mode; |
| 981 | } |
| 982 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 983 | /* |
| 984 | display by column original ideas from ls applet, |
| 985 | very optimize by my :) |
| 986 | */ |
| 987 | static void showfiles(char **matches, int nfiles) |
| 988 | { |
| 989 | int ncols, row; |
| 990 | int column_width = 0; |
| 991 | int nrows = nfiles; |
| 992 | |
| 993 | /* find the longest file name- use that as the column width */ |
| 994 | for (row = 0; row < nrows; row++) { |
| 995 | int l = strlen(matches[row]); |
| 996 | |
| 997 | if (column_width < l) |
| 998 | column_width = l; |
| 999 | } |
| 1000 | column_width += 2; /* min space for columns */ |
| 1001 | ncols = cmdedit_termw / column_width; |
| 1002 | |
| 1003 | if (ncols > 1) { |
| 1004 | nrows /= ncols; |
| 1005 | if(nfiles % ncols) |
| 1006 | nrows++; /* round up fractionals */ |
| 1007 | column_width = -column_width; /* for printf("%-Ns", ...); */ |
| 1008 | } else { |
| 1009 | ncols = 1; |
| 1010 | } |
| 1011 | for (row = 0; row < nrows; row++) { |
| 1012 | int n = row; |
| 1013 | int nc; |
| 1014 | |
| 1015 | for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) |
| 1016 | printf("%*s", column_width, matches[n]); |
| 1017 | printf("%s\n", matches[n]); |
| 1018 | } |
| 1019 | } |
| 1020 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1021 | |
| 1022 | static void input_tab(int *lastWasTab) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1023 | { |
| 1024 | /* Do TAB completion */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1025 | static int num_matches; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1026 | static char **matches; |
| 1027 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1028 | if (lastWasTab == 0) { /* free all memory */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1029 | if (matches) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1030 | while (num_matches > 0) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1031 | free(matches[--num_matches]); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1032 | free(matches); |
| 1033 | matches = (char **) NULL; |
| 1034 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1035 | return; |
| 1036 | } |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 1037 | if (! *lastWasTab) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1038 | |
| 1039 | char *tmp; |
| 1040 | int len_found; |
| 1041 | char matchBuf[BUFSIZ]; |
| 1042 | int find_type; |
| 1043 | int recalc_pos; |
| 1044 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1045 | *lastWasTab = TRUE; /* flop trigger */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1046 | |
| 1047 | /* Make a local copy of the string -- up |
| 1048 | * to the position of the cursor */ |
| 1049 | tmp = strncpy(matchBuf, command_ps, cursor); |
| 1050 | tmp[cursor] = 0; |
| 1051 | |
| 1052 | find_type = find_match(matchBuf, &recalc_pos); |
| 1053 | |
| 1054 | /* Free up any memory already allocated */ |
| 1055 | input_tab(0); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1056 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1057 | #ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1058 | /* 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] | 1059 | * then try completing this word as a username. */ |
| 1060 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1061 | if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1062 | matches = username_tab_completion(matchBuf, &num_matches); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1063 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1064 | /* Try to match any executable in our path and everything |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1065 | * in the current working directory that matches. */ |
| 1066 | if (!matches) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1067 | matches = |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 1068 | exe_n_cwd_tab_completion(matchBuf, |
| 1069 | &num_matches, find_type); |
| 1070 | /* Remove duplicate found */ |
| 1071 | if(matches) { |
| 1072 | int i, j; |
| 1073 | /* bubble */ |
| 1074 | for(i=0; i<(num_matches-1); i++) |
| 1075 | for(j=i+1; j<num_matches; j++) |
| 1076 | if(matches[i]!=0 && matches[j]!=0 && |
| 1077 | strcmp(matches[i], matches[j])==0) { |
| 1078 | free(matches[j]); |
| 1079 | matches[j]=0; |
| 1080 | } |
| 1081 | j=num_matches; |
| 1082 | num_matches = 0; |
| 1083 | for(i=0; i<j; i++) |
| 1084 | if(matches[i]) { |
| 1085 | if(!strcmp(matches[i], "./")) |
| 1086 | matches[i][1]=0; |
| 1087 | else if(!strcmp(matches[i], "../")) |
| 1088 | matches[i][2]=0; |
| 1089 | matches[num_matches++]=matches[i]; |
| 1090 | } |
| 1091 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1092 | /* Did we find exactly one match? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1093 | if (!matches || num_matches > 1) { |
| 1094 | char *tmp1; |
| 1095 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1096 | beep(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1097 | if (!matches) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1098 | return; /* not found */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1099 | /* sort */ |
| 1100 | qsort(matches, num_matches, sizeof(char *), match_compare); |
| 1101 | |
| 1102 | /* find minimal match */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1103 | tmp = bb_xstrdup(matches[0]); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1104 | for (tmp1 = tmp; *tmp1; tmp1++) |
| 1105 | for (len_found = 1; len_found < num_matches; len_found++) |
| 1106 | if (matches[len_found][(tmp1 - tmp)] != *tmp1) { |
| 1107 | *tmp1 = 0; |
| 1108 | break; |
| 1109 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1110 | if (*tmp == 0) { /* have unique */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1111 | free(tmp); |
| 1112 | return; |
| 1113 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1114 | } else { /* one match */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1115 | tmp = matches[0]; |
| 1116 | /* for next completion current found */ |
| 1117 | *lastWasTab = FALSE; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1120 | len_found = strlen(tmp); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1121 | /* have space to placed match? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1122 | if ((len_found - strlen(matchBuf) + len) < BUFSIZ) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1123 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1124 | /* before word for match */ |
| 1125 | command_ps[cursor - recalc_pos] = 0; |
| 1126 | /* save tail line */ |
| 1127 | strcpy(matchBuf, command_ps + cursor); |
| 1128 | /* add match */ |
| 1129 | strcat(command_ps, tmp); |
| 1130 | /* add tail */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1131 | strcat(command_ps, matchBuf); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1132 | /* back to begin word for match */ |
| 1133 | input_backward(recalc_pos); |
| 1134 | /* new pos */ |
| 1135 | recalc_pos = cursor + len_found; |
| 1136 | /* new len */ |
| 1137 | len = strlen(command_ps); |
| 1138 | /* write out the matched command */ |
Eric Andersen | 4f99053 | 2001-05-31 17:15:57 +0000 | [diff] [blame] | 1139 | redraw(cmdedit_y, len - recalc_pos); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1140 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1141 | if (tmp != matches[0]) |
| 1142 | free(tmp); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1143 | } else { |
| 1144 | /* Ok -- the last char was a TAB. Since they |
| 1145 | * just hit TAB again, print a list of all the |
| 1146 | * available choices... */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1147 | if (matches && num_matches > 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1148 | int sav_cursor = cursor; /* change goto_new_line() */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1149 | |
| 1150 | /* Go to the next line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1151 | goto_new_line(); |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 1152 | showfiles(matches, num_matches); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1153 | redraw(0, len - sav_cursor); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1157 | #endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1158 | |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1159 | #if MAX_HISTORY >= 1 |
| 1160 | static void get_previous_history(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1161 | { |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1162 | if(command_ps[0] != 0 || history[cur_history] == 0) { |
| 1163 | free(history[cur_history]); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1164 | history[cur_history] = bb_xstrdup(command_ps); |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1165 | } |
| 1166 | cur_history--; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1169 | static int get_next_history(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1170 | { |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1171 | int ch = cur_history; |
| 1172 | |
| 1173 | if (ch < n_history) { |
| 1174 | get_previous_history(); /* save the current history line */ |
| 1175 | return (cur_history = ch+1); |
| 1176 | } else { |
| 1177 | beep(); |
| 1178 | return 0; |
| 1179 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1180 | } |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1181 | |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1182 | #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 1183 | extern void load_history ( const char *fromfile ) |
| 1184 | { |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1185 | FILE *fp; |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 1186 | int hi; |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1187 | |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 1188 | /* cleanup old */ |
| 1189 | |
| 1190 | for(hi = n_history; hi > 0; ) { |
| 1191 | hi--; |
| 1192 | free ( history [hi] ); |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | if (( fp = fopen ( fromfile, "r" ))) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1196 | |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 1197 | for ( hi = 0; hi < MAX_HISTORY; ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1198 | char * hl = bb_get_chomped_line_from_file(fp); |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 1199 | int l; |
| 1200 | |
| 1201 | if(!hl) |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1202 | break; |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 1203 | l = strlen(hl); |
| 1204 | if(l >= BUFSIZ) |
| 1205 | hl[BUFSIZ-1] = 0; |
| 1206 | if(l == 0 || hl[0] == ' ') { |
| 1207 | free(hl); |
| 1208 | continue; |
| 1209 | } |
| 1210 | history [hi++] = hl; |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1211 | } |
| 1212 | fclose ( fp ); |
| 1213 | } |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 1214 | cur_history = n_history = hi; |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 1217 | extern void save_history ( const char *tofile ) |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1218 | { |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1219 | FILE *fp = fopen ( tofile, "w" ); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1220 | |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1221 | if ( fp ) { |
| 1222 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1223 | |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1224 | for ( i = 0; i < n_history; i++ ) { |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 1225 | fprintf(fp, "%s\n", history [i]); |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1226 | } |
| 1227 | fclose ( fp ); |
| 1228 | } |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1229 | } |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 1230 | #endif |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1231 | |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1232 | #endif |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1233 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1234 | enum { |
| 1235 | ESC = 27, |
| 1236 | DEL = 127, |
| 1237 | }; |
| 1238 | |
| 1239 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1240 | /* |
| 1241 | * This function is used to grab a character buffer |
| 1242 | * from the input file descriptor and allows you to |
Eric Andersen | 9b3ce77 | 2004-04-12 15:03:51 +0000 | [diff] [blame] | 1243 | * a string with full command editing (sort of like |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1244 | * a mini readline). |
| 1245 | * |
| 1246 | * The following standard commands are not implemented: |
| 1247 | * ESC-b -- Move back one word |
| 1248 | * ESC-f -- Move forward one word |
| 1249 | * ESC-d -- Delete back one word |
| 1250 | * ESC-h -- Delete forward one word |
| 1251 | * CTL-t -- Transpose two characters |
| 1252 | * |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1253 | * Minimalist vi-style command line editing available if configured. |
| 1254 | * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us> |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1255 | * |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1256 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1257 | |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1258 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI |
| 1259 | static int vi_mode; |
| 1260 | |
| 1261 | void setvimode ( int viflag ) |
| 1262 | { |
| 1263 | vi_mode = viflag; |
| 1264 | } |
| 1265 | |
| 1266 | void |
| 1267 | vi_Word_motion(char *command, int eat) |
| 1268 | { |
| 1269 | while (cursor < len && !isspace(command[cursor])) |
| 1270 | input_forward(); |
| 1271 | if (eat) while (cursor < len && isspace(command[cursor])) |
| 1272 | input_forward(); |
| 1273 | } |
| 1274 | |
| 1275 | void |
| 1276 | vi_word_motion(char *command, int eat) |
| 1277 | { |
| 1278 | if (isalnum(command[cursor]) || command[cursor] == '_') { |
| 1279 | while (cursor < len && |
| 1280 | (isalnum(command[cursor+1]) || |
| 1281 | command[cursor+1] == '_')) |
| 1282 | input_forward(); |
| 1283 | } else if (ispunct(command[cursor])) { |
| 1284 | while (cursor < len && |
| 1285 | (ispunct(command[cursor+1]))) |
| 1286 | input_forward(); |
| 1287 | } |
| 1288 | |
| 1289 | if (cursor < len) |
| 1290 | input_forward(); |
| 1291 | |
| 1292 | if (eat && cursor < len && isspace(command[cursor])) |
| 1293 | while (cursor < len && isspace(command[cursor])) |
| 1294 | input_forward(); |
| 1295 | } |
| 1296 | |
| 1297 | void |
| 1298 | vi_End_motion(char *command) |
| 1299 | { |
| 1300 | input_forward(); |
| 1301 | while (cursor < len && isspace(command[cursor])) |
| 1302 | input_forward(); |
| 1303 | while (cursor < len-1 && !isspace(command[cursor+1])) |
| 1304 | input_forward(); |
| 1305 | } |
| 1306 | |
| 1307 | void |
| 1308 | vi_end_motion(char *command) |
| 1309 | { |
| 1310 | if (cursor >= len-1) |
| 1311 | return; |
| 1312 | input_forward(); |
| 1313 | while (cursor < len-1 && isspace(command[cursor])) |
| 1314 | input_forward(); |
| 1315 | if (cursor >= len-1) |
| 1316 | return; |
| 1317 | if (isalnum(command[cursor]) || command[cursor] == '_') { |
| 1318 | while (cursor < len-1 && |
| 1319 | (isalnum(command[cursor+1]) || |
| 1320 | command[cursor+1] == '_')) |
| 1321 | input_forward(); |
| 1322 | } else if (ispunct(command[cursor])) { |
| 1323 | while (cursor < len-1 && |
| 1324 | (ispunct(command[cursor+1]))) |
| 1325 | input_forward(); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | void |
| 1330 | vi_Back_motion(char *command) |
| 1331 | { |
| 1332 | while (cursor > 0 && isspace(command[cursor-1])) |
| 1333 | input_backward(1); |
| 1334 | while (cursor > 0 && !isspace(command[cursor-1])) |
| 1335 | input_backward(1); |
| 1336 | } |
| 1337 | |
| 1338 | void |
| 1339 | vi_back_motion(char *command) |
| 1340 | { |
| 1341 | if (cursor <= 0) |
| 1342 | return; |
| 1343 | input_backward(1); |
| 1344 | while (cursor > 0 && isspace(command[cursor])) |
| 1345 | input_backward(1); |
| 1346 | if (cursor <= 0) |
| 1347 | return; |
| 1348 | if (isalnum(command[cursor]) || command[cursor] == '_') { |
| 1349 | while (cursor > 0 && |
| 1350 | (isalnum(command[cursor-1]) || |
| 1351 | command[cursor-1] == '_')) |
| 1352 | input_backward(1); |
| 1353 | } else if (ispunct(command[cursor])) { |
| 1354 | while (cursor > 0 && |
| 1355 | (ispunct(command[cursor-1]))) |
| 1356 | input_backward(1); |
| 1357 | } |
| 1358 | } |
| 1359 | #endif |
| 1360 | |
| 1361 | /* |
| 1362 | * the normal emacs mode and vi's insert mode are the same. |
| 1363 | * commands entered when in vi command mode ("escape mode") get |
| 1364 | * an extra bit added to distinguish them. this lets them share |
| 1365 | * much of the code in the big switch and while loop. i |
| 1366 | * experimented with an ugly macro to make the case labels for |
| 1367 | * these cases go away entirely when vi mode isn't configured, in |
| 1368 | * hopes of letting the jump tables get smaller: |
| 1369 | * #define vcase(caselabel) caselabel |
| 1370 | * and then |
| 1371 | * case CNTRL('A'): |
| 1372 | * case vcase(VICMD('0'):) |
| 1373 | * but it didn't seem to make any difference in code size, |
| 1374 | * and the macro-ized code was too ugly. |
| 1375 | */ |
| 1376 | |
| 1377 | #define VI_cmdbit 0x100 |
| 1378 | #define VICMD(somecmd) ((somecmd)|VI_cmdbit) |
| 1379 | |
| 1380 | /* convert uppercase ascii to equivalent control char, for readability */ |
| 1381 | #define CNTRL(uc_char) ((uc_char) - 0x40) |
| 1382 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 1383 | |
| 1384 | int cmdedit_read_input(char *prompt, char command[BUFSIZ]) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1385 | { |
| 1386 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1387 | int break_out = 0; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1388 | int lastWasTab = FALSE; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1389 | unsigned char c; |
| 1390 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI |
| 1391 | unsigned int ic, prevc; |
| 1392 | int vi_cmdmode = 0; |
| 1393 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1394 | /* prepare before init handlers */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1395 | cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1396 | len = 0; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1397 | command_ps = command; |
| 1398 | |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 1399 | getTermSettings(0, (void *) &initial_settings); |
| 1400 | memcpy(&new_settings, &initial_settings, sizeof(struct termios)); |
| 1401 | new_settings.c_lflag &= ~ICANON; /* unbuffered input */ |
| 1402 | /* Turn off echoing and CTRL-C, so we can trap it */ |
| 1403 | new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG); |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 1404 | /* Hmm, in linux c_cc[] not parsed if set ~ICANON */ |
| 1405 | new_settings.c_cc[VMIN] = 1; |
| 1406 | new_settings.c_cc[VTIME] = 0; |
| 1407 | /* Turn off CTRL-C, so we can trap it */ |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 1408 | # ifndef _POSIX_VDISABLE |
| 1409 | # define _POSIX_VDISABLE '\0' |
| 1410 | # endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1411 | new_settings.c_cc[VINTR] = _POSIX_VDISABLE; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1412 | command[0] = 0; |
| 1413 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 1414 | setTermSettings(0, (void *) &new_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1415 | handlers_sets |= SET_RESET_TERM; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1416 | |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 1417 | /* Now initialize things */ |
| 1418 | cmdedit_init(); |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 1419 | /* Print out the command prompt */ |
| 1420 | parse_prompt(prompt); |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 1421 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1422 | while (1) { |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1423 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1424 | fflush(stdout); /* buffered out to fast */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1425 | |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1426 | if (safe_read(0, &c, 1) < 1) |
Eric Andersen | ed424db | 2001-04-23 15:28:28 +0000 | [diff] [blame] | 1427 | /* if we can't read input then exit */ |
| 1428 | goto prepare_to_die; |
Erik Andersen | f3b3d17 | 2000-04-09 18:24:05 +0000 | [diff] [blame] | 1429 | |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1430 | #ifdef CONFIG_FEATURE_COMMAND_EDITING_VI |
| 1431 | newdelflag = 1; |
| 1432 | ic = c; |
| 1433 | if (vi_cmdmode) |
| 1434 | ic |= VI_cmdbit; |
| 1435 | switch (ic) |
| 1436 | #else |
| 1437 | switch (c) |
| 1438 | #endif |
| 1439 | { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1440 | case '\n': |
| 1441 | case '\r': |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1442 | case VICMD('\n'): |
| 1443 | case VICMD('\r'): |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1444 | /* Enter */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1445 | goto_new_line(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1446 | break_out = 1; |
| 1447 | break; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1448 | case CNTRL('A'): |
| 1449 | case VICMD('0'): |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1450 | /* Control-a -- Beginning of line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1451 | input_backward(cursor); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1452 | break; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1453 | case CNTRL('B'): |
| 1454 | case VICMD('h'): |
| 1455 | case VICMD('\b'): |
| 1456 | case VICMD(DEL): |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1457 | /* Control-b -- Move back one character */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1458 | input_backward(1); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1459 | break; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1460 | case CNTRL('C'): |
| 1461 | case VICMD(CNTRL('C')): |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1462 | /* Control-c -- stop gathering input */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1463 | goto_new_line(); |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 1464 | #ifndef CONFIG_ASH |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1465 | command[0] = 0; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1466 | len = 0; |
| 1467 | lastWasTab = FALSE; |
| 1468 | put_prompt(); |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 1469 | #else |
| 1470 | len = 0; |
Glenn L McGrath | 7fc504c | 2004-02-22 11:13:28 +0000 | [diff] [blame] | 1471 | break_out = -1; /* to control traps */ |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 1472 | #endif |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1473 | break; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1474 | case CNTRL('D'): |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1475 | /* Control-d -- Delete one character, or exit |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1476 | * if the len=0 and no chars to delete */ |
| 1477 | if (len == 0) { |
Eric Andersen | cb01bb1 | 2004-08-19 18:22:13 +0000 | [diff] [blame] | 1478 | errno = 0; |
Eric Andersen | ed424db | 2001-04-23 15:28:28 +0000 | [diff] [blame] | 1479 | prepare_to_die: |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1480 | #if !defined(CONFIG_ASH) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1481 | printf("exit"); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1482 | goto_new_line(); |
| 1483 | /* cmdedit_reset_term() called in atexit */ |
| 1484 | exit(EXIT_SUCCESS); |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 1485 | #else |
Glenn L McGrath | 7fc504c | 2004-02-22 11:13:28 +0000 | [diff] [blame] | 1486 | /* to control stopped jobs */ |
| 1487 | len = break_out = -1; |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 1488 | break; |
| 1489 | #endif |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1490 | } else { |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1491 | input_delete(0); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1492 | } |
| 1493 | break; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1494 | case CNTRL('E'): |
| 1495 | case VICMD('$'): |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1496 | /* Control-e -- End of line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1497 | input_end(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1498 | break; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1499 | case CNTRL('F'): |
| 1500 | case VICMD('l'): |
| 1501 | case VICMD(' '): |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1502 | /* Control-f -- Move forward one character */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1503 | input_forward(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1504 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1505 | case '\b': |
| 1506 | case DEL: |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1507 | /* Control-h and DEL */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1508 | input_backspace(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1509 | break; |
| 1510 | case '\t': |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1511 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1512 | input_tab(&lastWasTab); |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 1513 | #endif |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1514 | break; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1515 | case CNTRL('K'): |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1516 | /* Control-k -- clear to end of line */ |
Eric Andersen | 65a0730 | 2002-04-13 13:26:49 +0000 | [diff] [blame] | 1517 | *(command + cursor) = 0; |
| 1518 | len = cursor; |
Eric Andersen | ae10361 | 2002-04-24 23:08:23 +0000 | [diff] [blame] | 1519 | printf("\033[J"); |
Eric Andersen | 65a0730 | 2002-04-13 13:26:49 +0000 | [diff] [blame] | 1520 | break; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1521 | case CNTRL('L'): |
| 1522 | case VICMD(CNTRL('L')): |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 1523 | /* Control-l -- clear screen */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1524 | printf("\033[H"); |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1525 | redraw(0, len-cursor); |
Eric Andersen | f1f2bd0 | 2001-12-21 11:20:15 +0000 | [diff] [blame] | 1526 | break; |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1527 | #if MAX_HISTORY >= 1 |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1528 | case CNTRL('N'): |
| 1529 | case VICMD(CNTRL('N')): |
| 1530 | case VICMD('j'): |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1531 | /* Control-n -- Get next command in history */ |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1532 | if (get_next_history()) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1533 | goto rewrite_line; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1534 | break; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1535 | case CNTRL('P'): |
| 1536 | case VICMD(CNTRL('P')): |
| 1537 | case VICMD('k'): |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1538 | /* Control-p -- Get previous command from history */ |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1539 | if (cur_history > 0) { |
| 1540 | get_previous_history(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1541 | goto rewrite_line; |
| 1542 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1543 | beep(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1544 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1545 | break; |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1546 | #endif |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1547 | case CNTRL('U'): |
| 1548 | case VICMD(CNTRL('U')): |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1549 | /* Control-U -- Clear line before cursor */ |
| 1550 | if (cursor) { |
| 1551 | strcpy(command, command + cursor); |
| 1552 | redraw(cmdedit_y, len -= cursor); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1553 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1554 | break; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1555 | case CNTRL('W'): |
| 1556 | case VICMD(CNTRL('W')): |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 1557 | /* Control-W -- Remove the last word */ |
| 1558 | while (cursor > 0 && isspace(command[cursor-1])) |
| 1559 | input_backspace(); |
| 1560 | while (cursor > 0 &&!isspace(command[cursor-1])) |
| 1561 | input_backspace(); |
| 1562 | break; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1563 | #if CONFIG_FEATURE_COMMAND_EDITING_VI |
| 1564 | case VICMD('i'): |
| 1565 | vi_cmdmode = 0; |
| 1566 | break; |
| 1567 | case VICMD('I'): |
| 1568 | input_backward(cursor); |
| 1569 | vi_cmdmode = 0; |
| 1570 | break; |
| 1571 | case VICMD('a'): |
| 1572 | input_forward(); |
| 1573 | vi_cmdmode = 0; |
| 1574 | break; |
| 1575 | case VICMD('A'): |
| 1576 | input_end(); |
| 1577 | vi_cmdmode = 0; |
| 1578 | break; |
| 1579 | case VICMD('x'): |
| 1580 | input_delete(1); |
| 1581 | break; |
| 1582 | case VICMD('X'): |
| 1583 | if (cursor > 0) { |
| 1584 | input_backward(1); |
| 1585 | input_delete(1); |
| 1586 | } |
| 1587 | break; |
| 1588 | case VICMD('W'): |
| 1589 | vi_Word_motion(command, 1); |
| 1590 | break; |
| 1591 | case VICMD('w'): |
| 1592 | vi_word_motion(command, 1); |
| 1593 | break; |
| 1594 | case VICMD('E'): |
| 1595 | vi_End_motion(command); |
| 1596 | break; |
| 1597 | case VICMD('e'): |
| 1598 | vi_end_motion(command); |
| 1599 | break; |
| 1600 | case VICMD('B'): |
| 1601 | vi_Back_motion(command); |
| 1602 | break; |
| 1603 | case VICMD('b'): |
| 1604 | vi_back_motion(command); |
| 1605 | break; |
| 1606 | case VICMD('C'): |
| 1607 | vi_cmdmode = 0; |
| 1608 | /* fall through */ |
| 1609 | case VICMD('D'): |
| 1610 | goto clear_to_eol; |
| 1611 | |
| 1612 | case VICMD('c'): |
| 1613 | vi_cmdmode = 0; |
| 1614 | /* fall through */ |
| 1615 | case VICMD('d'): |
| 1616 | { |
| 1617 | int nc, sc; |
| 1618 | sc = cursor; |
| 1619 | prevc = ic; |
| 1620 | if (safe_read(0, &c, 1) < 1) |
| 1621 | goto prepare_to_die; |
| 1622 | if (c == (prevc & 0xff)) { |
| 1623 | /* "cc", "dd" */ |
| 1624 | input_backward(cursor); |
| 1625 | goto clear_to_eol; |
| 1626 | break; |
| 1627 | } |
| 1628 | switch(c) { |
| 1629 | case 'w': |
| 1630 | case 'W': |
| 1631 | case 'e': |
| 1632 | case 'E': |
| 1633 | switch (c) { |
| 1634 | case 'w': /* "dw", "cw" */ |
| 1635 | vi_word_motion(command, vi_cmdmode); |
| 1636 | break; |
| 1637 | case 'W': /* 'dW', 'cW' */ |
| 1638 | vi_Word_motion(command, vi_cmdmode); |
| 1639 | break; |
| 1640 | case 'e': /* 'de', 'ce' */ |
| 1641 | vi_end_motion(command); |
| 1642 | input_forward(); |
| 1643 | break; |
| 1644 | case 'E': /* 'dE', 'cE' */ |
| 1645 | vi_End_motion(command); |
| 1646 | input_forward(); |
| 1647 | break; |
| 1648 | } |
| 1649 | nc = cursor; |
| 1650 | input_backward(cursor - sc); |
| 1651 | while (nc-- > cursor) |
| 1652 | input_delete(1); |
| 1653 | break; |
| 1654 | case 'b': /* "db", "cb" */ |
| 1655 | case 'B': /* implemented as B */ |
| 1656 | if (c == 'b') |
| 1657 | vi_back_motion(command); |
| 1658 | else |
| 1659 | vi_Back_motion(command); |
| 1660 | while (sc-- > cursor) |
| 1661 | input_delete(1); |
| 1662 | break; |
| 1663 | case ' ': /* "d ", "c " */ |
| 1664 | input_delete(1); |
| 1665 | break; |
| 1666 | case '$': /* "d$", "c$" */ |
| 1667 | clear_to_eol: |
| 1668 | while (cursor < len) |
| 1669 | input_delete(1); |
| 1670 | break; |
| 1671 | } |
| 1672 | } |
| 1673 | break; |
| 1674 | case VICMD('p'): |
| 1675 | input_forward(); |
| 1676 | /* fallthrough */ |
| 1677 | case VICMD('P'): |
| 1678 | put(); |
| 1679 | break; |
| 1680 | case VICMD('r'): |
| 1681 | if (safe_read(0, &c, 1) < 1) |
| 1682 | goto prepare_to_die; |
| 1683 | if (c == 0) |
| 1684 | beep(); |
| 1685 | else { |
| 1686 | *(command + cursor) = c; |
| 1687 | putchar(c); |
| 1688 | putchar('\b'); |
| 1689 | } |
| 1690 | break; |
| 1691 | #endif /* CONFIG_FEATURE_COMMAND_EDITING_VI */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1692 | case ESC:{ |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1693 | #if CONFIG_FEATURE_COMMAND_EDITING_VI |
| 1694 | if (vi_mode) { |
| 1695 | /* ESC: insert mode --> command mode */ |
| 1696 | vi_cmdmode = 1; |
| 1697 | input_backward(1); |
| 1698 | break; |
| 1699 | } |
| 1700 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1701 | /* escape sequence follows */ |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1702 | if (safe_read(0, &c, 1) < 1) |
| 1703 | goto prepare_to_die; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1704 | /* different vt100 emulations */ |
| 1705 | if (c == '[' || c == 'O') { |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1706 | case VICMD('['): |
| 1707 | case VICMD('O'): |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1708 | if (safe_read(0, &c, 1) < 1) |
| 1709 | goto prepare_to_die; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1710 | } |
Glenn L McGrath | 475820c | 2004-01-22 12:42:23 +0000 | [diff] [blame] | 1711 | if (c >= '1' && c <= '9') { |
| 1712 | unsigned char dummy; |
| 1713 | |
| 1714 | if (safe_read(0, &dummy, 1) < 1) |
| 1715 | goto prepare_to_die; |
| 1716 | if(dummy != '~') |
| 1717 | c = 0; |
| 1718 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1719 | switch (c) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1720 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1721 | case '\t': /* Alt-Tab */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1722 | |
| 1723 | input_tab(&lastWasTab); |
| 1724 | break; |
| 1725 | #endif |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1726 | #if MAX_HISTORY >= 1 |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1727 | case 'A': |
| 1728 | /* Up Arrow -- Get previous command from history */ |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1729 | if (cur_history > 0) { |
| 1730 | get_previous_history(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1731 | goto rewrite_line; |
| 1732 | } else { |
| 1733 | beep(); |
| 1734 | } |
| 1735 | break; |
| 1736 | case 'B': |
| 1737 | /* Down Arrow -- Get next command in history */ |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1738 | if (!get_next_history()) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1739 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1740 | /* Rewrite the line with the selected history item */ |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1741 | rewrite_line: |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1742 | /* change command */ |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1743 | len = strlen(strcpy(command, history[cur_history])); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1744 | /* redraw and go to eol (bol, in vi */ |
| 1745 | #if CONFIG_FEATURE_COMMAND_EDITING_VI |
| 1746 | redraw(cmdedit_y, vi_mode ? 9999:0); |
| 1747 | #else |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1748 | redraw(cmdedit_y, 0); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1749 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1750 | break; |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1751 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1752 | case 'C': |
| 1753 | /* Right Arrow -- Move forward one character */ |
| 1754 | input_forward(); |
| 1755 | break; |
| 1756 | case 'D': |
| 1757 | /* Left Arrow -- Move back one character */ |
| 1758 | input_backward(1); |
| 1759 | break; |
| 1760 | case '3': |
| 1761 | /* Delete */ |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1762 | input_delete(0); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1763 | break; |
| 1764 | case '1': |
| 1765 | case 'H': |
Glenn L McGrath | 7fc504c | 2004-02-22 11:13:28 +0000 | [diff] [blame] | 1766 | /* <Home> */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1767 | input_backward(cursor); |
| 1768 | break; |
| 1769 | case '4': |
| 1770 | case 'F': |
Glenn L McGrath | 7fc504c | 2004-02-22 11:13:28 +0000 | [diff] [blame] | 1771 | /* <End> */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1772 | input_end(); |
| 1773 | break; |
| 1774 | default: |
Glenn L McGrath | 475820c | 2004-01-22 12:42:23 +0000 | [diff] [blame] | 1775 | c = 0; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1776 | beep(); |
| 1777 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1778 | break; |
| 1779 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1780 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1781 | default: /* If it's regular input, do the normal thing */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1782 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1783 | /* Control-V -- Add non-printable symbol */ |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1784 | if (c == CNTRL('V')) { |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1785 | if (safe_read(0, &c, 1) < 1) |
| 1786 | goto prepare_to_die; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1787 | if (c == 0) { |
| 1788 | beep(); |
| 1789 | break; |
| 1790 | } |
| 1791 | } else |
| 1792 | #endif |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1793 | { |
| 1794 | #if CONFIG_FEATURE_COMMAND_EDITING_VI |
| 1795 | if (vi_cmdmode) /* don't self-insert */ |
| 1796 | break; |
| 1797 | #endif |
| 1798 | if (!Isprint(c)) /* Skip non-printable characters */ |
| 1799 | break; |
| 1800 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1801 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1802 | if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1803 | break; |
| 1804 | |
| 1805 | len++; |
| 1806 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1807 | if (cursor == (len - 1)) { /* Append if at the end of the line */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1808 | *(command + cursor) = c; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1809 | *(command + cursor + 1) = 0; |
| 1810 | cmdedit_set_out_char(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1811 | } else { /* Insert otherwise */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1812 | int sc = cursor; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1813 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1814 | memmove(command + sc + 1, command + sc, len - sc); |
| 1815 | *(command + sc) = c; |
| 1816 | sc++; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1817 | /* rewrite from cursor */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1818 | input_end(); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1819 | /* to prev x pos + 1 */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1820 | input_backward(cursor - sc); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1823 | break; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1824 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1825 | if (break_out) /* Enter is the command terminator, no more input. */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1826 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1827 | |
| 1828 | if (c != '\t') |
| 1829 | lastWasTab = FALSE; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 1832 | setTermSettings(0, (void *) &initial_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1833 | handlers_sets &= ~SET_RESET_TERM; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1834 | |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1835 | #if MAX_HISTORY >= 1 |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1836 | /* Handle command history log */ |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1837 | /* cleanup may be saved current command line */ |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 1838 | if (len> 0) { /* no put empty line */ |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1839 | int i = n_history; |
Glenn L McGrath | 7fc504c | 2004-02-22 11:13:28 +0000 | [diff] [blame] | 1840 | |
| 1841 | free(history[MAX_HISTORY]); |
| 1842 | history[MAX_HISTORY] = 0; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1843 | /* After max history, remove the oldest command */ |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1844 | if (i >= MAX_HISTORY) { |
| 1845 | free(history[0]); |
| 1846 | for(i = 0; i < (MAX_HISTORY-1); i++) |
| 1847 | history[i] = history[i+1]; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1848 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1849 | history[i++] = bb_xstrdup(command); |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1850 | cur_history = i; |
| 1851 | n_history = i; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1852 | #if defined(CONFIG_FEATURE_SH_FANCY_PROMPT) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1853 | num_ok_lines++; |
| 1854 | #endif |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1855 | } |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1856 | #else /* MAX_HISTORY < 1 */ |
| 1857 | #if defined(CONFIG_FEATURE_SH_FANCY_PROMPT) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 1858 | if (len > 0) { /* no put empty line */ |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1859 | num_ok_lines++; |
| 1860 | } |
| 1861 | #endif |
| 1862 | #endif /* MAX_HISTORY >= 1 */ |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 1863 | if (break_out > 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1864 | command[len++] = '\n'; /* set '\n' */ |
| 1865 | command[len] = 0; |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 1866 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1867 | #if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1868 | input_tab(0); /* strong free */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1869 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1870 | #if defined(CONFIG_FEATURE_SH_FANCY_PROMPT) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1871 | free(cmdedit_prompt); |
| 1872 | #endif |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1873 | cmdedit_reset_term(); |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 1874 | return len; |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1877 | |
| 1878 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1879 | #endif /* CONFIG_FEATURE_COMMAND_EDITING */ |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1880 | |
| 1881 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1882 | #ifdef TEST |
| 1883 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1884 | const char *bb_applet_name = "debug stuff usage"; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 1885 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1886 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 1887 | #include <locale.h> |
| 1888 | #endif |
| 1889 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1890 | int main(int argc, char **argv) |
| 1891 | { |
| 1892 | char buff[BUFSIZ]; |
| 1893 | char *prompt = |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1894 | #if defined(CONFIG_FEATURE_SH_FANCY_PROMPT) |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1895 | "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1896 | \\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \ |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1897 | \\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]"; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1898 | #else |
| 1899 | "% "; |
| 1900 | #endif |
| 1901 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1902 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 1903 | setlocale(LC_ALL, ""); |
| 1904 | #endif |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1905 | while(1) { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1906 | int l; |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 1907 | l = cmdedit_read_input(prompt, buff); |
| 1908 | if(l > 0 && buff[l-1] == '\n') { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1909 | buff[l-1] = 0; |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 1910 | printf("*** cmdedit_read_input() returned line =%s=\n", buff); |
| 1911 | } else { |
| 1912 | break; |
| 1913 | } |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1914 | } |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 1915 | printf("*** cmdedit_read_input() detect ^D\n"); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1916 | return 0; |
| 1917 | } |
| 1918 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1919 | #endif /* TEST */ |