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