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