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