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