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