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