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 | 78ff819 | 2008-06-28 21:03:43 +0000 | [diff] [blame] | 18 | * Usage and known bugs: |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame] | 19 | * Terminal key codes are not extensive, more needs to be added. |
| 20 | * This version was created on Debian GNU/Linux 2.x. |
Denis Vlasenko | 78ff819 | 2008-06-28 21:03:43 +0000 | [diff] [blame] | 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. |
| 23 | * Ctrl-E also works as End. |
| 24 | * |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame] | 25 | * The following readline-like commands are not implemented: |
| 26 | * ESC-b -- Move back one word |
| 27 | * ESC-f -- Move forward one word |
| 28 | * ESC-d -- Delete forward one word |
| 29 | * CTL-t -- Transpose two characters |
| 30 | * |
Denis Vlasenko | 78ff819 | 2008-06-28 21:03:43 +0000 | [diff] [blame] | 31 | * lineedit does not know that the terminal escape sequences do not |
| 32 | * take up space on the screen. The redisplay code assumes, unless |
| 33 | * told otherwise, that each character in the prompt is a printable |
| 34 | * character that takes up one character position on the screen. |
| 35 | * You need to tell lineedit that some sequences of characters |
| 36 | * in the prompt take up no screen space. Compatibly with readline, |
| 37 | * use the \[ escape to begin a sequence of non-printing characters, |
| 38 | * and the \] escape to signal the end of such a sequence. Example: |
| 39 | * |
| 40 | * PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] ' |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 41 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 42 | #include "libbb.h" |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 43 | #include "unicode.h" |
Glenn L McGrath | 475820c | 2004-01-22 12:42:23 +0000 | [diff] [blame] | 44 | |
Denis Vlasenko | 7f1dc21 | 2006-12-19 01:10:25 +0000 | [diff] [blame] | 45 | /* FIXME: obsolete CONFIG item? */ |
| 46 | #define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0 |
| 47 | |
Glenn L McGrath | 3b25185 | 2004-01-03 12:07:32 +0000 | [diff] [blame] | 48 | #ifdef TEST |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 49 | # define ENABLE_FEATURE_EDITING 0 |
| 50 | # define ENABLE_FEATURE_TAB_COMPLETION 0 |
| 51 | # define ENABLE_FEATURE_USERNAME_COMPLETION 0 |
| 52 | # define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0 |
| 53 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 54 | |
Eric Andersen | 5165fbe | 2001-02-20 06:42:29 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 56 | /* Entire file (except TESTing part) sits inside this #if */ |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 57 | #if ENABLE_FEATURE_EDITING |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 7e46cf7 | 2006-12-23 01:21:55 +0000 | [diff] [blame] | 60 | #define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \ |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 61 | (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 62 | #define IF_FEATURE_GETUSERNAME_AND_HOMEDIR(...) |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 63 | #if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 64 | #undef IF_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 65 | #define IF_FEATURE_GETUSERNAME_AND_HOMEDIR(...) __VA_ARGS__ |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 66 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 67 | |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 68 | |
| 69 | #undef CHAR_T |
| 70 | #if ENABLE_FEATURE_ASSUME_UNICODE |
| 71 | # define BB_NUL L'\0' |
| 72 | # define CHAR_T wchar_t |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame] | 73 | static bool BB_isspace(CHAR_T c) { return ((unsigned)c < 256 && isspace(c)); } |
| 74 | static bool BB_isalnum(CHAR_T c) { return ((unsigned)c < 256 && isalnum(c)); } |
| 75 | static bool BB_ispunct(CHAR_T c) { return ((unsigned)c < 256 && ispunct(c)); } |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 76 | # undef isspace |
| 77 | # undef isalnum |
| 78 | # undef ispunct |
| 79 | # undef isprint |
| 80 | # define isspace isspace_must_not_be_used |
| 81 | # define isalnum isalnum_must_not_be_used |
| 82 | # define ispunct ispunct_must_not_be_used |
| 83 | # define isprint isprint_must_not_be_used |
| 84 | #else |
| 85 | # define BB_NUL '\0' |
| 86 | # define CHAR_T char |
| 87 | # define BB_isspace(c) isspace(c) |
| 88 | # define BB_isalnum(c) isalnum(c) |
| 89 | # define BB_ispunct(c) ispunct(c) |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 90 | #endif |
| 91 | |
| 92 | |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 93 | enum { |
| 94 | /* We use int16_t for positions, need to limit line len */ |
| 95 | MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN < 0x7ff0 |
Denis Vlasenko | 6b40443 | 2008-01-07 16:13:14 +0000 | [diff] [blame] | 96 | ? CONFIG_FEATURE_EDITING_MAX_LEN |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 97 | : 0x7ff0 |
| 98 | }; |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 99 | |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 100 | #if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 101 | static const char null_str[] ALIGN1 = ""; |
| 102 | #endif |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 103 | |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 104 | /* We try to minimize both static and stack usage. */ |
Denis Vlasenko | 5d89fba | 2008-04-22 00:08:27 +0000 | [diff] [blame] | 105 | struct lineedit_statics { |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 106 | line_input_t *state; |
Erik Andersen | 8ea7d8c | 2000-05-20 00:40:08 +0000 | [diff] [blame] | 107 | |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 108 | volatile unsigned cmdedit_termw; /* = 80; */ /* actual terminal width */ |
| 109 | sighandler_t previous_SIGWINCH_handler; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 110 | |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 111 | unsigned cmdedit_x; /* real x (col) terminal position */ |
| 112 | unsigned cmdedit_y; /* pseudoreal y (row) terminal position */ |
Denis Vlasenko | b267ed9 | 2008-05-25 21:52:03 +0000 | [diff] [blame] | 113 | unsigned cmdedit_prmt_len; /* length of prompt (without colors etc) */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 114 | |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 115 | unsigned cursor; |
Denys Vlasenko | 2f3f09c | 2009-09-29 00:00:12 +0200 | [diff] [blame] | 116 | int command_len; /* must be signed */ |
| 117 | /* signed maxsize: we want x in "if (x > S.maxsize)" |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 118 | * to _not_ be promoted to unsigned */ |
| 119 | int maxsize; |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 120 | CHAR_T *command_ps; |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 121 | |
| 122 | const char *cmdedit_prompt; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 123 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 124 | int num_ok_lines; /* = 1; */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 125 | #endif |
| 126 | |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 127 | #if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 128 | char *user_buf; |
| 129 | char *home_pwd_buf; /* = (char*)null_str; */ |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 130 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 131 | |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 132 | #if ENABLE_FEATURE_TAB_COMPLETION |
| 133 | char **matches; |
| 134 | unsigned num_matches; |
| 135 | #endif |
| 136 | |
| 137 | #if ENABLE_FEATURE_EDITING_VI |
| 138 | #define DELBUFSIZ 128 |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 139 | CHAR_T *delptr; |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 140 | smallint newdelflag; /* whether delbuf should be reused yet */ |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 141 | CHAR_T delbuf[DELBUFSIZ]; /* a place to store deleted characters */ |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 142 | #endif |
Denys Vlasenko | eb62d7c | 2009-10-27 10:34:06 +0100 | [diff] [blame] | 143 | #if ENABLE_FEATURE_EDITING_ASK_TERMINAL |
Denys Vlasenko | d83bbf4 | 2009-10-27 10:47:49 +0100 | [diff] [blame] | 144 | smallint sent_ESC_br6n; |
Denys Vlasenko | eb62d7c | 2009-10-27 10:34:06 +0100 | [diff] [blame] | 145 | #endif |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 146 | |
| 147 | /* Formerly these were big buffers on stack: */ |
| 148 | #if ENABLE_FEATURE_TAB_COMPLETION |
| 149 | char exe_n_cwd_tab_completion__dirbuf[MAX_LINELEN]; |
| 150 | char input_tab__matchBuf[MAX_LINELEN]; |
| 151 | int16_t find_match__int_buf[MAX_LINELEN + 1]; /* need to have 9 bits at least */ |
| 152 | int16_t find_match__pos_buf[MAX_LINELEN + 1]; |
| 153 | #endif |
| 154 | }; |
| 155 | |
Denis Vlasenko | 5d89fba | 2008-04-22 00:08:27 +0000 | [diff] [blame] | 156 | /* See lineedit_ptr_hack.c */ |
| 157 | extern struct lineedit_statics *const lineedit_ptr_to_statics; |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 158 | |
Denis Vlasenko | 5d89fba | 2008-04-22 00:08:27 +0000 | [diff] [blame] | 159 | #define S (*lineedit_ptr_to_statics) |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 160 | #define state (S.state ) |
| 161 | #define cmdedit_termw (S.cmdedit_termw ) |
| 162 | #define previous_SIGWINCH_handler (S.previous_SIGWINCH_handler) |
| 163 | #define cmdedit_x (S.cmdedit_x ) |
| 164 | #define cmdedit_y (S.cmdedit_y ) |
| 165 | #define cmdedit_prmt_len (S.cmdedit_prmt_len) |
| 166 | #define cursor (S.cursor ) |
| 167 | #define command_len (S.command_len ) |
| 168 | #define command_ps (S.command_ps ) |
| 169 | #define cmdedit_prompt (S.cmdedit_prompt ) |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 170 | #define num_ok_lines (S.num_ok_lines ) |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 171 | #define user_buf (S.user_buf ) |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 172 | #define home_pwd_buf (S.home_pwd_buf ) |
| 173 | #define matches (S.matches ) |
| 174 | #define num_matches (S.num_matches ) |
| 175 | #define delptr (S.delptr ) |
| 176 | #define newdelflag (S.newdelflag ) |
| 177 | #define delbuf (S.delbuf ) |
| 178 | |
| 179 | #define INIT_S() do { \ |
Denis Vlasenko | 5d89fba | 2008-04-22 00:08:27 +0000 | [diff] [blame] | 180 | (*(struct lineedit_statics**)&lineedit_ptr_to_statics) = xzalloc(sizeof(S)); \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 181 | barrier(); \ |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 182 | cmdedit_termw = 80; \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 183 | IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \ |
| 184 | IF_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \ |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 185 | } while (0) |
| 186 | static void deinit_S(void) |
| 187 | { |
| 188 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 189 | /* This one is allocated only if FANCY_PROMPT is on |
| 190 | * (otherwise it points to verbatim prompt (NOT malloced) */ |
| 191 | free((char*)cmdedit_prompt); |
| 192 | #endif |
| 193 | #if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 194 | free(user_buf); |
| 195 | if (home_pwd_buf != null_str) |
| 196 | free(home_pwd_buf); |
| 197 | #endif |
Denis Vlasenko | 5d89fba | 2008-04-22 00:08:27 +0000 | [diff] [blame] | 198 | free(lineedit_ptr_to_statics); |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 199 | } |
| 200 | #define DEINIT_S() deinit_S() |
| 201 | |
Denis Vlasenko | 2c84495 | 2008-04-25 18:44:35 +0000 | [diff] [blame] | 202 | |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 203 | #if ENABLE_FEATURE_ASSUME_UNICODE |
| 204 | static size_t load_string(const char *src, int maxsize) |
| 205 | { |
| 206 | ssize_t len = mbstowcs(command_ps, src, maxsize - 1); |
| 207 | if (len < 0) |
| 208 | len = 0; |
| 209 | command_ps[len] = L'\0'; |
| 210 | return len; |
| 211 | } |
| 212 | static size_t save_string(char *dst, int maxsize) |
| 213 | { |
| 214 | ssize_t len = wcstombs(dst, command_ps, maxsize - 1); |
| 215 | if (len < 0) |
| 216 | len = 0; |
| 217 | dst[len] = '\0'; |
| 218 | return len; |
| 219 | } |
| 220 | /* I thought just fputwc(c, stdout) would work. But no... */ |
| 221 | static void BB_PUTCHAR(wchar_t c) |
| 222 | { |
| 223 | char buf[MB_CUR_MAX + 1]; |
| 224 | mbstate_t mbst = { 0 }; |
| 225 | ssize_t len = wcrtomb(buf, c, &mbst); |
| 226 | |
| 227 | if (len > 0) { |
| 228 | buf[len] = '\0'; |
| 229 | fputs(buf, stdout); |
| 230 | } |
| 231 | } |
| 232 | #else |
| 233 | static size_t load_string(const char *src, int maxsize) |
| 234 | { |
| 235 | safe_strncpy(command_ps, src, maxsize); |
| 236 | return strlen(command_ps); |
| 237 | } |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 238 | # if ENABLE_FEATURE_TAB_COMPLETION |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 239 | static void save_string(char *dst, int maxsize) |
| 240 | { |
| 241 | safe_strncpy(dst, command_ps, maxsize); |
| 242 | } |
Denys Vlasenko | 7dd0ce4 | 2009-07-15 18:27:47 +0200 | [diff] [blame] | 243 | # endif |
| 244 | # define BB_PUTCHAR(c) bb_putchar(c) |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 245 | #endif |
| 246 | |
| 247 | |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 248 | /* Put 'command_ps[cursor]', cursor++. |
| 249 | * Advance cursor on screen. If we reached right margin, scroll text up |
| 250 | * and remove terminal margin effect by printing 'next_char' */ |
Denis Vlasenko | 2c84495 | 2008-04-25 18:44:35 +0000 | [diff] [blame] | 251 | #define HACK_FOR_WRONG_WIDTH 1 |
| 252 | #if HACK_FOR_WRONG_WIDTH |
| 253 | static void cmdedit_set_out_char(void) |
| 254 | #define cmdedit_set_out_char(next_char) cmdedit_set_out_char() |
| 255 | #else |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 256 | static void cmdedit_set_out_char(int next_char) |
Denis Vlasenko | 2c84495 | 2008-04-25 18:44:35 +0000 | [diff] [blame] | 257 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 258 | { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 259 | CHAR_T c = command_ps[cursor]; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 260 | |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 261 | if (c == BB_NUL) { |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 262 | /* erase character after end of input string */ |
| 263 | c = ' '; |
| 264 | } |
Denis Vlasenko | 7f1dc21 | 2006-12-19 01:10:25 +0000 | [diff] [blame] | 265 | #if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 266 | /* Display non-printable characters in reverse */ |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 267 | if (!BB_isprint(c)) { |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 268 | if (c >= 128) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 269 | c -= 128; |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 270 | if (c < ' ') |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 271 | c += '@'; |
| 272 | if (c == 127) |
| 273 | c = '?'; |
| 274 | printf("\033[7m%c\033[0m", c); |
| 275 | } else |
| 276 | #endif |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 277 | { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 278 | BB_PUTCHAR(c); |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 279 | } |
Denis Vlasenko | b267ed9 | 2008-05-25 21:52:03 +0000 | [diff] [blame] | 280 | if (++cmdedit_x >= cmdedit_termw) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 281 | /* terminal is scrolled down */ |
| 282 | cmdedit_y++; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 283 | cmdedit_x = 0; |
Denis Vlasenko | 2c84495 | 2008-04-25 18:44:35 +0000 | [diff] [blame] | 284 | #if HACK_FOR_WRONG_WIDTH |
| 285 | /* This works better if our idea of term width is wrong |
| 286 | * and it is actually wider (often happens on serial lines). |
| 287 | * Printing CR,LF *forces* cursor to next line. |
| 288 | * OTOH if terminal width is correct AND terminal does NOT |
| 289 | * have automargin (IOW: it is moving cursor to next line |
| 290 | * by itself (which is wrong for VT-10x terminals)), |
| 291 | * this will break things: there will be one extra empty line */ |
| 292 | puts("\r"); /* + implicit '\n' */ |
| 293 | #else |
| 294 | /* Works ok only if cmdedit_termw is correct */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 295 | /* destroy "(auto)margin" */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 296 | bb_putchar(next_char); |
| 297 | bb_putchar('\b'); |
Denis Vlasenko | 2c84495 | 2008-04-25 18:44:35 +0000 | [diff] [blame] | 298 | #endif |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 299 | } |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 300 | // Huh? What if command_ps[cursor] == BB_NUL (we are at the end already?) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 301 | cursor++; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 304 | /* Move to end of line (by printing all chars till the end) */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 305 | static void input_end(void) |
| 306 | { |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 307 | while (cursor < command_len) |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 308 | cmdedit_set_out_char(' '); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* Go to the next line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 312 | static void goto_new_line(void) |
| 313 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 314 | input_end(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 315 | if (cmdedit_x) |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 316 | bb_putchar('\n'); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 320 | static void out1str(const char *s) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 321 | { |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 322 | if (s) |
Robert Griebl | b230159 | 2002-07-30 23:13:51 +0000 | [diff] [blame] | 323 | fputs(s, stdout); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 324 | } |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 325 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 326 | static void beep(void) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 327 | { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 328 | bb_putchar('\007'); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 331 | /* Move back one character */ |
Denis Vlasenko | 47bdb3a | 2007-01-21 19:18:59 +0000 | [diff] [blame] | 332 | /* (optimized for slow terminals) */ |
| 333 | static void input_backward(unsigned num) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 334 | { |
Denis Vlasenko | 47bdb3a | 2007-01-21 19:18:59 +0000 | [diff] [blame] | 335 | int count_y; |
| 336 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 337 | if (num > cursor) |
| 338 | num = cursor; |
Denis Vlasenko | 47bdb3a | 2007-01-21 19:18:59 +0000 | [diff] [blame] | 339 | if (!num) |
| 340 | return; |
| 341 | cursor -= num; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 342 | |
Denis Vlasenko | b267ed9 | 2008-05-25 21:52:03 +0000 | [diff] [blame] | 343 | if (cmdedit_x >= num) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 344 | cmdedit_x -= num; |
Denis Vlasenko | 47bdb3a | 2007-01-21 19:18:59 +0000 | [diff] [blame] | 345 | if (num <= 4) { |
Denis Vlasenko | 6f04391 | 2008-02-18 22:28:03 +0000 | [diff] [blame] | 346 | /* This is longer by 5 bytes on x86. |
Denis Vlasenko | b267ed9 | 2008-05-25 21:52:03 +0000 | [diff] [blame] | 347 | * Also gets miscompiled for ARM users |
| 348 | * (busybox.net/bugs/view.php?id=2274). |
Denis Vlasenko | 6f04391 | 2008-02-18 22:28:03 +0000 | [diff] [blame] | 349 | * printf(("\b\b\b\b" + 4) - num); |
| 350 | * return; |
| 351 | */ |
| 352 | do { |
| 353 | bb_putchar('\b'); |
| 354 | } while (--num); |
Denis Vlasenko | 47bdb3a | 2007-01-21 19:18:59 +0000 | [diff] [blame] | 355 | return; |
| 356 | } |
| 357 | printf("\033[%uD", num); |
| 358 | return; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 359 | } |
Denis Vlasenko | 47bdb3a | 2007-01-21 19:18:59 +0000 | [diff] [blame] | 360 | |
| 361 | /* Need to go one or more lines up */ |
| 362 | num -= cmdedit_x; |
Denis Vlasenko | b267ed9 | 2008-05-25 21:52:03 +0000 | [diff] [blame] | 363 | { |
| 364 | unsigned w = cmdedit_termw; /* volatile var */ |
| 365 | count_y = 1 + (num / w); |
| 366 | cmdedit_y -= count_y; |
| 367 | cmdedit_x = w * count_y - num; |
| 368 | } |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 369 | /* go to 1st column; go up; go to correct column */ |
Denis Vlasenko | 47bdb3a | 2007-01-21 19:18:59 +0000 | [diff] [blame] | 370 | printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 373 | static void put_prompt(void) |
| 374 | { |
| 375 | out1str(cmdedit_prompt); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 376 | fflush_all(); |
Denys Vlasenko | eb62d7c | 2009-10-27 10:34:06 +0100 | [diff] [blame] | 377 | #if ENABLE_FEATURE_EDITING_ASK_TERMINAL |
| 378 | { |
Denys Vlasenko | c396fe6 | 2009-05-17 19:28:14 +0200 | [diff] [blame] | 379 | /* Ask terminal where is the cursor now. |
| 380 | * lineedit_read_key handles response and corrects |
| 381 | * our idea of current cursor position. |
| 382 | * Testcase: run "echo -n long_line_long_line_long_line", |
| 383 | * then type in a long, wrapping command and try to |
| 384 | * delete it using backspace key. |
| 385 | * Note: we print it _after_ prompt, because |
| 386 | * prompt may contain CR. Example: PS1='\[\r\n\]\w ' |
| 387 | */ |
Denys Vlasenko | 727e1b5 | 2009-10-26 15:23:32 +0100 | [diff] [blame] | 388 | /* Problem: if there is buffered input on stdin, |
| 389 | * the response will be delivered later, |
| 390 | * possibly to an unsuspecting application. |
| 391 | * Testcase: "sleep 1; busybox ash" + press and hold [Enter]. |
| 392 | * Result: |
| 393 | * ~/srcdevel/bbox/fix/busybox.t4 # |
| 394 | * ~/srcdevel/bbox/fix/busybox.t4 # |
| 395 | * ^[[59;34~/srcdevel/bbox/fix/busybox.t4 # <-- garbage |
| 396 | * ~/srcdevel/bbox/fix/busybox.t4 # |
| 397 | * |
| 398 | * Checking for input with poll only makes the race narrower, |
| 399 | * I still can trigger it. Strace: |
| 400 | * |
| 401 | * write(1, "~/srcdevel/bbox/fix/busybox.t4 # ", 33) = 33 |
| 402 | * poll([{fd=0, events=POLLIN}], 1, 0) = 0 (Timeout) <-- no input exists |
| 403 | * write(1, "\33[6n", 4) = 4 <-- send the ESC sequence, quick! |
| 404 | * poll([{fd=0, events=POLLIN}], 1, 4294967295) = 1 ([{fd=0, revents=POLLIN}]) |
| 405 | * read(0, "\n", 1) = 1 <-- oh crap, user's input got in first |
| 406 | */ |
| 407 | struct pollfd pfd; |
| 408 | |
| 409 | pfd.fd = STDIN_FILENO; |
| 410 | pfd.events = POLLIN; |
| 411 | if (safe_poll(&pfd, 1, 0) == 0) { |
Denys Vlasenko | d83bbf4 | 2009-10-27 10:47:49 +0100 | [diff] [blame] | 412 | S.sent_ESC_br6n = 1; |
Denys Vlasenko | 727e1b5 | 2009-10-26 15:23:32 +0100 | [diff] [blame] | 413 | out1str("\033" "[6n"); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 414 | fflush_all(); /* make terminal see it ASAP! */ |
Denys Vlasenko | 727e1b5 | 2009-10-26 15:23:32 +0100 | [diff] [blame] | 415 | } |
Denys Vlasenko | c396fe6 | 2009-05-17 19:28:14 +0200 | [diff] [blame] | 416 | } |
Denys Vlasenko | eb62d7c | 2009-10-27 10:34:06 +0100 | [diff] [blame] | 417 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 418 | cursor = 0; |
Denis Vlasenko | b267ed9 | 2008-05-25 21:52:03 +0000 | [diff] [blame] | 419 | { |
| 420 | unsigned w = cmdedit_termw; /* volatile var */ |
| 421 | cmdedit_y = cmdedit_prmt_len / w; /* new quasireal y */ |
| 422 | cmdedit_x = cmdedit_prmt_len % w; |
| 423 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 426 | /* draw prompt, editor line, and clear tail */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 427 | static void redraw(int y, int back_cursor) |
| 428 | { |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 429 | if (y > 0) /* up to start y */ |
| 430 | printf("\033[%uA", y); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 431 | bb_putchar('\r'); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 432 | put_prompt(); |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 433 | input_end(); /* rewrite */ |
| 434 | printf("\033[J"); /* erase after cursor */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 435 | input_backward(back_cursor); |
| 436 | } |
| 437 | |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 438 | /* Delete the char in front of the cursor, optionally saving it |
| 439 | * for later putback */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 440 | #if !ENABLE_FEATURE_EDITING_VI |
| 441 | static void input_delete(void) |
| 442 | #define input_delete(save) input_delete() |
| 443 | #else |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 444 | static void input_delete(int save) |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 445 | #endif |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 446 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 447 | int j = cursor; |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 448 | |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 449 | if (j == (int)command_len) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 450 | return; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 451 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 452 | #if ENABLE_FEATURE_EDITING_VI |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 453 | if (save) { |
| 454 | if (newdelflag) { |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 455 | delptr = delbuf; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 456 | newdelflag = 0; |
| 457 | } |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 458 | if ((delptr - delbuf) < DELBUFSIZ) |
| 459 | *delptr++ = command_ps[j]; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 460 | } |
| 461 | #endif |
| 462 | |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 463 | memmove(command_ps + j, command_ps + j + 1, |
Denys Vlasenko | 9531f7d | 2009-07-16 14:33:16 +0200 | [diff] [blame] | 464 | /* (command_len + 1 [because of NUL]) - (j + 1) |
| 465 | * simplified into (command_len - j) */ |
| 466 | (command_len - j) * sizeof(command_ps[0])); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 467 | command_len--; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 468 | input_end(); /* rewrite new line */ |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 469 | cmdedit_set_out_char(' '); /* erase char */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 470 | input_backward(cursor - j); /* back to old pos cursor */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 473 | #if ENABLE_FEATURE_EDITING_VI |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 474 | static void put(void) |
| 475 | { |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 476 | int ocursor; |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 477 | int j = delptr - delbuf; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 478 | |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 479 | if (j == 0) |
| 480 | return; |
| 481 | ocursor = cursor; |
| 482 | /* open hole and then fill it */ |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 483 | memmove(command_ps + cursor + j, command_ps + cursor, |
| 484 | (command_len - cursor + 1) * sizeof(command_ps[0])); |
| 485 | memcpy(command_ps + cursor, delbuf, j * sizeof(command_ps[0])); |
Denis Vlasenko | 253ce00 | 2007-01-22 08:34:44 +0000 | [diff] [blame] | 486 | command_len += j; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 487 | input_end(); /* rewrite new line */ |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 488 | input_backward(cursor - ocursor - j + 1); /* at end of new text */ |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 489 | } |
| 490 | #endif |
| 491 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 492 | /* Delete the char in back of the cursor */ |
| 493 | static void input_backspace(void) |
| 494 | { |
| 495 | if (cursor > 0) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 496 | input_backward(1); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 497 | input_delete(0); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 501 | /* Move forward one character */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 502 | static void input_forward(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 503 | { |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 504 | if (cursor < command_len) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 505 | cmdedit_set_out_char(command_ps[cursor + 1]); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 508 | #if ENABLE_FEATURE_TAB_COMPLETION |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 509 | |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 510 | static void free_tab_completion_data(void) |
| 511 | { |
| 512 | if (matches) { |
| 513 | while (num_matches) |
| 514 | free(matches[--num_matches]); |
| 515 | free(matches); |
| 516 | matches = NULL; |
| 517 | } |
| 518 | } |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 519 | |
Denis Vlasenko | d56b47f | 2006-12-21 22:24:46 +0000 | [diff] [blame] | 520 | static void add_match(char *matched) |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 521 | { |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 522 | matches = xrealloc_vector(matches, 4, num_matches); |
| 523 | matches[num_matches] = matched; |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 524 | num_matches++; |
| 525 | } |
| 526 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 527 | #if ENABLE_FEATURE_USERNAME_COMPLETION |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 528 | static void username_tab_completion(char *ud, char *with_shash_flg) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 529 | { |
| 530 | struct passwd *entry; |
| 531 | int userlen; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 532 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 533 | ud++; /* ~user/... to user/... */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 534 | userlen = strlen(ud); |
| 535 | |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 536 | if (with_shash_flg) { /* "~/..." or "~user/..." */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 537 | char *sav_ud = ud - 1; |
Denis Vlasenko | 86b29ea | 2007-09-27 10:17:16 +0000 | [diff] [blame] | 538 | char *home = NULL; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 539 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 540 | if (*ud == '/') { /* "~/..." */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 541 | home = home_pwd_buf; |
| 542 | } else { |
| 543 | /* "~user/..." */ |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 544 | char *temp; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 545 | temp = strchr(ud, '/'); |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 546 | *temp = '\0'; /* ~user\0 */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 547 | entry = getpwnam(ud); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 548 | *temp = '/'; /* restore ~user/... */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 549 | ud = temp; |
| 550 | if (entry) |
| 551 | home = entry->pw_dir; |
| 552 | } |
| 553 | if (home) { |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 554 | if ((userlen + strlen(home) + 1) < MAX_LINELEN) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 555 | /* /home/user/... */ |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 556 | sprintf(sav_ud, "%s%s", home, ud); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 559 | } else { |
| 560 | /* "~[^/]*" */ |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 561 | /* Using _r function to avoid pulling in static buffers */ |
Denis Vlasenko | 6b343dd | 2007-03-18 00:57:15 +0000 | [diff] [blame] | 562 | char line_buff[256]; |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 563 | struct passwd pwd; |
| 564 | struct passwd *result; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 565 | |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 566 | setpwent(); |
| 567 | while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 568 | /* Null usernames should result in all users as possible completions. */ |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 569 | if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) { |
| 570 | add_match(xasprintf("~%s/", pwd.pw_name)); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 573 | endpwent(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
Denis Vlasenko | 7f1dc21 | 2006-12-19 01:10:25 +0000 | [diff] [blame] | 576 | #endif /* FEATURE_COMMAND_USERNAME_COMPLETION */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 577 | |
| 578 | enum { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 579 | FIND_EXE_ONLY = 0, |
| 580 | FIND_DIR_ONLY = 1, |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 581 | FIND_FILE_ONLY = 2, |
| 582 | }; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 583 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 584 | static int path_parse(char ***p, int flags) |
| 585 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 586 | int npth; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 587 | const char *pth; |
Denis Vlasenko | 253ce00 | 2007-01-22 08:34:44 +0000 | [diff] [blame] | 588 | char *tmp; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 589 | char **res; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 590 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 591 | /* if not setenv PATH variable, to search cur dir "." */ |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 592 | if (flags != FIND_EXE_ONLY) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 593 | return 1; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 594 | |
| 595 | if (state->flags & WITH_PATH_LOOKUP) |
| 596 | pth = state->path_lookup; |
| 597 | else |
| 598 | pth = getenv("PATH"); |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 599 | /* PATH=<empty> or PATH=:<empty> */ |
| 600 | if (!pth || !pth[0] || LONE_CHAR(pth, ':')) |
| 601 | return 1; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 602 | |
Denis Vlasenko | 253ce00 | 2007-01-22 08:34:44 +0000 | [diff] [blame] | 603 | tmp = (char*)pth; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 604 | npth = 1; /* path component count */ |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 605 | while (1) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 606 | tmp = strchr(tmp, ':'); |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 607 | if (!tmp) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 608 | break; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 609 | if (*++tmp == '\0') |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 610 | break; /* :<empty> */ |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 611 | npth++; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 614 | res = xmalloc(npth * sizeof(char*)); |
Denis Vlasenko | 253ce00 | 2007-01-22 08:34:44 +0000 | [diff] [blame] | 615 | res[0] = tmp = xstrdup(pth); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 616 | npth = 1; |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 617 | while (1) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 618 | tmp = strchr(tmp, ':'); |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 619 | if (!tmp) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 620 | break; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 621 | *tmp++ = '\0'; /* ':' -> '\0' */ |
| 622 | if (*tmp == '\0') |
| 623 | break; /* :<empty> */ |
| 624 | res[npth++] = tmp; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 625 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 626 | *p = res; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 627 | return npth; |
| 628 | } |
| 629 | |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 630 | static void exe_n_cwd_tab_completion(char *command, int type) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 631 | { |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 632 | DIR *dir; |
| 633 | struct dirent *next; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 634 | struct stat st; |
| 635 | char *path1[1]; |
| 636 | char **paths = path1; |
| 637 | int npaths; |
| 638 | int i; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 639 | char *found; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 640 | char *pfind = strrchr(command, '/'); |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 641 | /* char dirbuf[MAX_LINELEN]; */ |
| 642 | #define dirbuf (S.exe_n_cwd_tab_completion__dirbuf) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 643 | |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 644 | npaths = 1; |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 645 | path1[0] = (char*)"."; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 646 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 647 | if (pfind == NULL) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 648 | /* no dir, if flags==EXE_ONLY - get paths, else "." */ |
| 649 | npaths = path_parse(&paths, type); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 650 | pfind = command; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 651 | } else { |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 652 | /* dirbuf = ".../.../.../" */ |
| 653 | safe_strncpy(dirbuf, command, (pfind - command) + 2); |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 654 | #if ENABLE_FEATURE_USERNAME_COMPLETION |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 655 | if (dirbuf[0] == '~') /* ~/... or ~user/... */ |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 656 | username_tab_completion(dirbuf, dirbuf); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 657 | #endif |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 658 | paths[0] = dirbuf; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 659 | /* point to 'l' in "..../last_component" */ |
| 660 | pfind++; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 661 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 662 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 663 | for (i = 0; i < npaths; i++) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 664 | dir = opendir(paths[i]); |
Denis Vlasenko | b520271 | 2008-04-24 04:42:52 +0000 | [diff] [blame] | 665 | if (!dir) |
| 666 | continue; /* don't print an error */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 667 | |
| 668 | while ((next = readdir(dir)) != NULL) { |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 669 | int len1; |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 670 | const char *str_found = next->d_name; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 671 | |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 672 | /* matched? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 673 | if (strncmp(str_found, pfind, strlen(pfind))) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 674 | continue; |
| 675 | /* not see .name without .match */ |
Denis Vlasenko | b520271 | 2008-04-24 04:42:52 +0000 | [diff] [blame] | 676 | if (*str_found == '.' && *pfind == '\0') { |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 677 | if (NOT_LONE_CHAR(paths[i], '/') || str_found[1]) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 678 | continue; |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 679 | str_found = ""; /* only "/" */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 680 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 681 | found = concat_path_file(paths[i], str_found); |
Denis Vlasenko | b520271 | 2008-04-24 04:42:52 +0000 | [diff] [blame] | 682 | /* hmm, remove in progress? */ |
| 683 | /* NB: stat() first so that we see is it a directory; |
| 684 | * but if that fails, use lstat() so that |
| 685 | * we still match dangling links */ |
| 686 | if (stat(found, &st) && lstat(found, &st)) |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 687 | goto cont; |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 688 | /* find with dirs? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 689 | if (paths[i] != dirbuf) |
Denis Vlasenko | b520271 | 2008-04-24 04:42:52 +0000 | [diff] [blame] | 690 | strcpy(found, next->d_name); /* only name */ |
Denis Vlasenko | d56b47f | 2006-12-21 22:24:46 +0000 | [diff] [blame] | 691 | |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 692 | len1 = strlen(found); |
| 693 | found = xrealloc(found, len1 + 2); |
Denis Vlasenko | d56b47f | 2006-12-21 22:24:46 +0000 | [diff] [blame] | 694 | found[len1] = '\0'; |
| 695 | found[len1+1] = '\0'; |
| 696 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 697 | if (S_ISDIR(st.st_mode)) { |
Denis Vlasenko | b520271 | 2008-04-24 04:42:52 +0000 | [diff] [blame] | 698 | /* name is a directory */ |
Denis Vlasenko | d56b47f | 2006-12-21 22:24:46 +0000 | [diff] [blame] | 699 | if (found[len1-1] != '/') { |
| 700 | found[len1] = '/'; |
| 701 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 702 | } else { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 703 | /* not put found file if search only dirs for cd */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 704 | if (type == FIND_DIR_ONLY) |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 705 | goto cont; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 706 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 707 | /* Add it to the list */ |
Denis Vlasenko | d56b47f | 2006-12-21 22:24:46 +0000 | [diff] [blame] | 708 | add_match(found); |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 709 | continue; |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 710 | cont: |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 711 | free(found); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 712 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 713 | closedir(dir); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 714 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 715 | if (paths != path1) { |
Denis Vlasenko | b520271 | 2008-04-24 04:42:52 +0000 | [diff] [blame] | 716 | free(paths[0]); /* allocated memory is only in first member */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 717 | free(paths); |
| 718 | } |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 719 | #undef dirbuf |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 720 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 721 | |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 722 | /* QUOT is used on elements of int_buf[], which are bytes, |
| 723 | * not Unicode chars. Therefore it works correctly even in Unicode mode. |
| 724 | */ |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 725 | #define QUOT (UCHAR_MAX+1) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 726 | |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 727 | #define int_buf (S.find_match__int_buf) |
| 728 | #define pos_buf (S.find_match__pos_buf) |
Denys Vlasenko | 5c2e81b | 2009-07-16 14:14:34 +0200 | [diff] [blame] | 729 | /* is must be <= in */ |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 730 | static void collapse_pos(int is, int in) |
| 731 | { |
Denys Vlasenko | 5c2e81b | 2009-07-16 14:14:34 +0200 | [diff] [blame] | 732 | memmove(int_buf+is, int_buf+in, (MAX_LINELEN+1-in)*sizeof(int_buf[0])); |
| 733 | memmove(pos_buf+is, pos_buf+in, (MAX_LINELEN+1-in)*sizeof(pos_buf[0])); |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 734 | } |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 735 | static NOINLINE int find_match(char *matchBuf, int *len_with_quotes) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 736 | { |
| 737 | int i, j; |
| 738 | int command_mode; |
| 739 | int c, c2; |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 740 | /* Were local, but it uses too much stack */ |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 741 | /* int16_t int_buf[MAX_LINELEN + 1]; */ |
| 742 | /* int16_t pos_buf[MAX_LINELEN + 1]; */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 743 | |
| 744 | /* set to integer dimension characters and own positions */ |
| 745 | for (i = 0;; i++) { |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 746 | int_buf[i] = (unsigned char)matchBuf[i]; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 747 | if (int_buf[i] == 0) { |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 748 | pos_buf[i] = -1; /* end-fo-line indicator */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 749 | break; |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 750 | } |
| 751 | pos_buf[i] = i; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /* mask \+symbol and convert '\t' to ' ' */ |
| 755 | for (i = j = 0; matchBuf[i]; i++, j++) |
| 756 | if (matchBuf[i] == '\\') { |
| 757 | collapse_pos(j, j + 1); |
| 758 | int_buf[j] |= QUOT; |
| 759 | i++; |
Denis Vlasenko | 7f1dc21 | 2006-12-19 01:10:25 +0000 | [diff] [blame] | 760 | #if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 761 | if (matchBuf[i] == '\t') /* algorithm equivalent */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 762 | int_buf[j] = ' ' | QUOT; |
| 763 | #endif |
| 764 | } |
Denis Vlasenko | 7f1dc21 | 2006-12-19 01:10:25 +0000 | [diff] [blame] | 765 | #if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 766 | else if (matchBuf[i] == '\t') |
| 767 | int_buf[j] = ' '; |
| 768 | #endif |
| 769 | |
| 770 | /* mask "symbols" or 'symbols' */ |
| 771 | c2 = 0; |
| 772 | for (i = 0; int_buf[i]; i++) { |
| 773 | c = int_buf[i]; |
| 774 | if (c == '\'' || c == '"') { |
| 775 | if (c2 == 0) |
| 776 | c2 = c; |
| 777 | else { |
| 778 | if (c == c2) |
| 779 | c2 = 0; |
| 780 | else |
| 781 | int_buf[i] |= QUOT; |
| 782 | } |
| 783 | } else if (c2 != 0 && c != '$') |
| 784 | int_buf[i] |= QUOT; |
| 785 | } |
| 786 | |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 787 | /* skip commands with arguments if line has commands delimiters */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 788 | /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */ |
| 789 | for (i = 0; int_buf[i]; i++) { |
| 790 | c = int_buf[i]; |
| 791 | c2 = int_buf[i + 1]; |
| 792 | j = i ? int_buf[i - 1] : -1; |
| 793 | command_mode = 0; |
| 794 | if (c == ';' || c == '&' || c == '|') { |
| 795 | command_mode = 1 + (c == c2); |
| 796 | if (c == '&') { |
| 797 | if (j == '>' || j == '<') |
| 798 | command_mode = 0; |
| 799 | } else if (c == '|' && j == '>') |
| 800 | command_mode = 0; |
| 801 | } |
| 802 | if (command_mode) { |
| 803 | collapse_pos(0, i + command_mode); |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 804 | i = -1; /* hack incremet */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | /* collapse `command...` */ |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 808 | for (i = 0; int_buf[i]; i++) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 809 | if (int_buf[i] == '`') { |
| 810 | for (j = i + 1; int_buf[j]; j++) |
| 811 | if (int_buf[j] == '`') { |
| 812 | collapse_pos(i, j + 1); |
| 813 | j = 0; |
| 814 | break; |
| 815 | } |
| 816 | if (j) { |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 817 | /* not found closing ` - command mode, collapse all previous */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 818 | collapse_pos(0, i + 1); |
| 819 | break; |
| 820 | } else |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 821 | i--; /* hack incremet */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 822 | } |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 823 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 824 | |
| 825 | /* collapse (command...(command...)...) or {command...{command...}...} */ |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 826 | c = 0; /* "recursive" level */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 827 | c2 = 0; |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 828 | for (i = 0; int_buf[i]; i++) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 829 | if (int_buf[i] == '(' || int_buf[i] == '{') { |
| 830 | if (int_buf[i] == '(') |
| 831 | c++; |
| 832 | else |
| 833 | c2++; |
| 834 | collapse_pos(0, i + 1); |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 835 | i = -1; /* hack incremet */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 836 | } |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 837 | } |
| 838 | for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 839 | if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) { |
| 840 | if (int_buf[i] == ')') |
| 841 | c--; |
| 842 | else |
| 843 | c2--; |
| 844 | collapse_pos(0, i + 1); |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 845 | i = -1; /* hack incremet */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 846 | } |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 847 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 848 | |
| 849 | /* skip first not quote space */ |
| 850 | for (i = 0; int_buf[i]; i++) |
| 851 | if (int_buf[i] != ' ') |
| 852 | break; |
| 853 | if (i) |
| 854 | collapse_pos(0, i); |
| 855 | |
| 856 | /* set find mode for completion */ |
| 857 | command_mode = FIND_EXE_ONLY; |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 858 | for (i = 0; int_buf[i]; i++) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 859 | if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') { |
| 860 | if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 861 | && matchBuf[pos_buf[0]] == 'c' |
| 862 | && matchBuf[pos_buf[1]] == 'd' |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 863 | ) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 864 | command_mode = FIND_DIR_ONLY; |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 865 | } else { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 866 | command_mode = FIND_FILE_ONLY; |
| 867 | break; |
| 868 | } |
| 869 | } |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 870 | } |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 871 | for (i = 0; int_buf[i]; i++) |
| 872 | /* "strlen" */; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 873 | /* find last word */ |
| 874 | for (--i; i >= 0; i--) { |
| 875 | c = int_buf[i]; |
| 876 | if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') { |
| 877 | collapse_pos(0, i + 1); |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | /* skip first not quoted '\'' or '"' */ |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 882 | for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++) |
| 883 | /*skip*/; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 884 | /* collapse quote or unquote // or /~ */ |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 885 | while ((int_buf[i] & ~QUOT) == '/' |
| 886 | && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~') |
| 887 | ) { |
Mark Whitley | 7e5291f | 2001-03-08 19:31:12 +0000 | [diff] [blame] | 888 | i++; |
| 889 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 890 | |
| 891 | /* set only match and destroy quotes */ |
| 892 | j = 0; |
Eric Andersen | 4f99053 | 2001-05-31 17:15:57 +0000 | [diff] [blame] | 893 | for (c = 0; pos_buf[i] >= 0; i++) { |
| 894 | matchBuf[c++] = matchBuf[pos_buf[i]]; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 895 | j = pos_buf[i] + 1; |
| 896 | } |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 897 | matchBuf[c] = '\0'; |
Denis Vlasenko | f74194e | 2007-10-18 12:54:39 +0000 | [diff] [blame] | 898 | /* old length matchBuf with quotes symbols */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 899 | *len_with_quotes = j ? j - pos_buf[0] : 0; |
| 900 | |
| 901 | return command_mode; |
Denys Vlasenko | 5c2e81b | 2009-07-16 14:14:34 +0200 | [diff] [blame] | 902 | } |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 903 | #undef int_buf |
| 904 | #undef pos_buf |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 905 | |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 906 | /* |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 907 | * display by column (original idea from ls applet, |
| 908 | * very optimized by me :) |
| 909 | */ |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 910 | static void showfiles(void) |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 911 | { |
| 912 | int ncols, row; |
| 913 | int column_width = 0; |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 914 | int nfiles = num_matches; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 915 | int nrows = nfiles; |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 916 | int l; |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 917 | |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 918 | /* find the longest file name - use that as the column width */ |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 919 | for (row = 0; row < nrows; row++) { |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 920 | l = bb_mbstrlen(matches[row]); |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 921 | if (column_width < l) |
| 922 | column_width = l; |
| 923 | } |
| 924 | column_width += 2; /* min space for columns */ |
| 925 | ncols = cmdedit_termw / column_width; |
| 926 | |
| 927 | if (ncols > 1) { |
| 928 | nrows /= ncols; |
Denis Vlasenko | 7f1dc21 | 2006-12-19 01:10:25 +0000 | [diff] [blame] | 929 | if (nfiles % ncols) |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 930 | nrows++; /* round up fractionals */ |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 931 | } else { |
| 932 | ncols = 1; |
| 933 | } |
| 934 | for (row = 0; row < nrows; row++) { |
| 935 | int n = row; |
| 936 | int nc; |
| 937 | |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 938 | for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) { |
Denis Vlasenko | d56b47f | 2006-12-21 22:24:46 +0000 | [diff] [blame] | 939 | printf("%s%-*s", matches[n], |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 940 | (int)(column_width - bb_mbstrlen(matches[n])), "" |
| 941 | ); |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 942 | } |
Denis Vlasenko | feb7ae7 | 2007-10-01 12:05:12 +0000 | [diff] [blame] | 943 | puts(matches[n]); |
Glenn L McGrath | 4d00129 | 2003-01-06 01:11:50 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 947 | static char *add_quote_for_spec_chars(char *found) |
| 948 | { |
| 949 | int l = 0; |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 950 | char *s = xzalloc((strlen(found) + 1) * 2); |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 951 | |
| 952 | while (*found) { |
Denys Vlasenko | 5c2e81b | 2009-07-16 14:14:34 +0200 | [diff] [blame] | 953 | if (strchr(" `\"#$%^&*()=+{}[]:;'|\\<>", *found)) |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 954 | s[l++] = '\\'; |
| 955 | s[l++] = *found++; |
| 956 | } |
Denys Vlasenko | 53fd1bf | 2009-07-16 02:19:39 +0200 | [diff] [blame] | 957 | /* s[l] = '\0'; - already is */ |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 958 | return s; |
| 959 | } |
| 960 | |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 961 | /* Do TAB completion */ |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 962 | static void input_tab(smallint *lastWasTab) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 963 | { |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 964 | if (!(state->flags & TAB_COMPLETION)) |
| 965 | return; |
| 966 | |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 967 | if (!*lastWasTab) { |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 968 | char *tmp, *tmp1; |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 969 | size_t len_found; |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 970 | /* char matchBuf[MAX_LINELEN]; */ |
| 971 | #define matchBuf (S.input_tab__matchBuf) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 972 | int find_type; |
| 973 | int recalc_pos; |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 974 | #if ENABLE_FEATURE_ASSUME_UNICODE |
| 975 | /* cursor pos in command converted to multibyte form */ |
| 976 | int cursor_mb; |
| 977 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 978 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 979 | *lastWasTab = TRUE; /* flop trigger */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 980 | |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 981 | /* Make a local copy of the string -- |
| 982 | * up to the position of the cursor */ |
| 983 | save_string(matchBuf, cursor + 1); |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 984 | #if ENABLE_FEATURE_ASSUME_UNICODE |
| 985 | cursor_mb = strlen(matchBuf); |
| 986 | #endif |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 987 | tmp = matchBuf; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 988 | |
| 989 | find_type = find_match(matchBuf, &recalc_pos); |
| 990 | |
| 991 | /* Free up any memory already allocated */ |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 992 | free_tab_completion_data(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 993 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 994 | #if ENABLE_FEATURE_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 995 | /* 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] | 996 | * then try completing this word as a username. */ |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 997 | if (state->flags & USERNAME_COMPLETION) |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 998 | if (matchBuf[0] == '~' && strchr(matchBuf, '/') == NULL) |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 999 | username_tab_completion(matchBuf, NULL); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1000 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1001 | /* Try to match any executable in our path and everything |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 1002 | * in the current working directory */ |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1003 | if (!matches) |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 1004 | exe_n_cwd_tab_completion(matchBuf, find_type); |
Denis Vlasenko | f58906b | 2006-12-19 19:30:37 +0000 | [diff] [blame] | 1005 | /* Sort, then remove any duplicates found */ |
Denis Vlasenko | 7f1dc21 | 2006-12-19 01:10:25 +0000 | [diff] [blame] | 1006 | if (matches) { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1007 | unsigned i; |
| 1008 | int n = 0; |
Denis Vlasenko | fb29038 | 2008-03-02 12:51:26 +0000 | [diff] [blame] | 1009 | qsort_string_vector(matches, num_matches); |
Denis Vlasenko | f58906b | 2006-12-19 19:30:37 +0000 | [diff] [blame] | 1010 | for (i = 0; i < num_matches - 1; ++i) { |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 1011 | if (matches[i] && matches[i+1]) { /* paranoia */ |
Denis Vlasenko | f58906b | 2006-12-19 19:30:37 +0000 | [diff] [blame] | 1012 | if (strcmp(matches[i], matches[i+1]) == 0) { |
| 1013 | free(matches[i]); |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 1014 | matches[i] = NULL; /* paranoia */ |
Denis Vlasenko | f58906b | 2006-12-19 19:30:37 +0000 | [diff] [blame] | 1015 | } else { |
Denis Vlasenko | f58906b | 2006-12-19 19:30:37 +0000 | [diff] [blame] | 1016 | matches[n++] = matches[i]; |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 1017 | } |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 1018 | } |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 1019 | } |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 1020 | matches[n] = matches[i]; |
| 1021 | num_matches = n + 1; |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 1022 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1023 | /* Did we find exactly one match? */ |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 1024 | if (!matches || num_matches > 1) { /* no */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1025 | beep(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1026 | if (!matches) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1027 | return; /* not found */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1028 | /* find minimal match */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1029 | tmp1 = xstrdup(matches[0]); |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 1030 | for (tmp = tmp1; *tmp; tmp++) { |
| 1031 | for (len_found = 1; len_found < num_matches; len_found++) { |
| 1032 | if (matches[len_found][tmp - tmp1] != *tmp) { |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 1033 | *tmp = '\0'; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1034 | break; |
| 1035 | } |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 1036 | } |
| 1037 | } |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 1038 | if (*tmp1 == '\0') { /* have unique */ |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 1039 | free(tmp1); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1040 | return; |
| 1041 | } |
Denis Vlasenko | d56b47f | 2006-12-21 22:24:46 +0000 | [diff] [blame] | 1042 | tmp = add_quote_for_spec_chars(tmp1); |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 1043 | free(tmp1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1044 | } else { /* one match */ |
Denis Vlasenko | d56b47f | 2006-12-21 22:24:46 +0000 | [diff] [blame] | 1045 | tmp = add_quote_for_spec_chars(matches[0]); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1046 | /* for next completion current found */ |
| 1047 | *lastWasTab = FALSE; |
Denis Vlasenko | d56b47f | 2006-12-21 22:24:46 +0000 | [diff] [blame] | 1048 | |
| 1049 | len_found = strlen(tmp); |
| 1050 | if (tmp[len_found-1] != '/') { |
| 1051 | tmp[len_found] = ' '; |
| 1052 | tmp[len_found+1] = '\0'; |
| 1053 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1054 | } |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 1055 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1056 | len_found = strlen(tmp); |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1057 | #if !ENABLE_FEATURE_ASSUME_UNICODE |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 1058 | /* have space to place the match? */ |
| 1059 | /* The result consists of three parts with these lengths: */ |
| 1060 | /* (cursor - recalc_pos) + len_found + (command_len - cursor) */ |
| 1061 | /* it simplifies into: */ |
| 1062 | if ((int)(len_found + command_len - recalc_pos) < S.maxsize) { |
| 1063 | /* save tail */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1064 | strcpy(matchBuf, command_ps + cursor); |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 1065 | /* add match and tail */ |
| 1066 | sprintf(&command_ps[cursor - recalc_pos], "%s%s", tmp, matchBuf); |
Denis Vlasenko | 253ce00 | 2007-01-22 08:34:44 +0000 | [diff] [blame] | 1067 | command_len = strlen(command_ps); |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 1068 | /* new pos */ |
| 1069 | recalc_pos = cursor - recalc_pos + len_found; |
| 1070 | /* write out the matched command */ |
Denis Vlasenko | 253ce00 | 2007-01-22 08:34:44 +0000 | [diff] [blame] | 1071 | redraw(cmdedit_y, command_len - recalc_pos); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1072 | } |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 1073 | #else |
| 1074 | { |
| 1075 | char command[MAX_LINELEN]; |
| 1076 | int len = save_string(command, sizeof(command)); |
| 1077 | /* have space to place the match? */ |
| 1078 | /* (cursor_mb - recalc_pos) + len_found + (len - cursor_mb) */ |
| 1079 | if ((int)(len_found + len - recalc_pos) < MAX_LINELEN) { |
| 1080 | /* save tail */ |
| 1081 | strcpy(matchBuf, command + cursor_mb); |
| 1082 | /* where do we want to have cursor after all? */ |
| 1083 | strcpy(&command[cursor_mb - recalc_pos], tmp); |
| 1084 | len = load_string(command, S.maxsize); |
| 1085 | /* add match and tail */ |
| 1086 | sprintf(&command[cursor_mb - recalc_pos], "%s%s", tmp, matchBuf); |
| 1087 | command_len = load_string(command, S.maxsize); |
| 1088 | /* write out the matched command */ |
| 1089 | redraw(cmdedit_y, command_len - len); |
| 1090 | } |
| 1091 | } |
| 1092 | #endif |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 1093 | free(tmp); |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1094 | #undef matchBuf |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1095 | } else { |
| 1096 | /* Ok -- the last char was a TAB. Since they |
| 1097 | * just hit TAB again, print a list of all the |
| 1098 | * available choices... */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1099 | if (matches && num_matches > 0) { |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 1100 | /* changed by goto_new_line() */ |
| 1101 | int sav_cursor = cursor; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1102 | |
| 1103 | /* Go to the next line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1104 | goto_new_line(); |
"Vladimir N. Oleynik" | fdb871c | 2006-01-25 11:53:47 +0000 | [diff] [blame] | 1105 | showfiles(); |
Denis Vlasenko | 253ce00 | 2007-01-22 08:34:44 +0000 | [diff] [blame] | 1106 | redraw(0, command_len - sav_cursor); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | } |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 1110 | |
Denis Vlasenko | 7f1dc21 | 2006-12-19 01:10:25 +0000 | [diff] [blame] | 1111 | #endif /* FEATURE_COMMAND_TAB_COMPLETION */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1112 | |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1113 | |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1114 | line_input_t* FAST_FUNC new_line_input_t(int flags) |
| 1115 | { |
| 1116 | line_input_t *n = xzalloc(sizeof(*n)); |
| 1117 | n->flags = flags; |
| 1118 | return n; |
| 1119 | } |
| 1120 | |
| 1121 | |
Denis Vlasenko | 9d4533e | 2006-11-02 22:09:37 +0000 | [diff] [blame] | 1122 | #if MAX_HISTORY > 0 |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1123 | |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1124 | static void save_command_ps_at_cur_history(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1125 | { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1126 | if (command_ps[0] != BB_NUL) { |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1127 | int cur = state->cur_history; |
| 1128 | free(state->history[cur]); |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1129 | |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1130 | # if ENABLE_FEATURE_ASSUME_UNICODE |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1131 | { |
| 1132 | char tbuf[MAX_LINELEN]; |
| 1133 | save_string(tbuf, sizeof(tbuf)); |
| 1134 | state->history[cur] = xstrdup(tbuf); |
| 1135 | } |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1136 | # else |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1137 | state->history[cur] = xstrdup(command_ps); |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1138 | # endif |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1139 | } |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | /* state->flags is already checked to be nonzero */ |
| 1143 | static int get_previous_history(void) |
| 1144 | { |
| 1145 | if ((state->flags & DO_HISTORY) && state->cur_history) { |
| 1146 | save_command_ps_at_cur_history(); |
| 1147 | state->cur_history--; |
| 1148 | return 1; |
| 1149 | } |
| 1150 | beep(); |
| 1151 | return 0; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1154 | static int get_next_history(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1155 | { |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1156 | if (state->flags & DO_HISTORY) { |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1157 | if (state->cur_history < state->cnt_history) { |
| 1158 | save_command_ps_at_cur_history(); /* save the current history line */ |
| 1159 | return ++state->cur_history; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1160 | } |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1161 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1162 | beep(); |
| 1163 | return 0; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1164 | } |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1165 | |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1166 | # if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1167 | /* We try to ensure that concurrent additions to the history |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1168 | * do not overwrite each other. |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1169 | * Otherwise shell users get unhappy. |
| 1170 | * |
| 1171 | * History file is trimmed lazily, when it grows several times longer |
| 1172 | * than configured MAX_HISTORY lines. |
| 1173 | */ |
| 1174 | |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1175 | static void free_line_input_t(line_input_t *n) |
| 1176 | { |
| 1177 | int i = n->cnt_history; |
| 1178 | while (i > 0) |
| 1179 | free(n->history[--i]); |
| 1180 | free(n); |
| 1181 | } |
| 1182 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1183 | /* state->flags is already checked to be nonzero */ |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1184 | static void load_history(line_input_t *st_parm) |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 1185 | { |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1186 | char *temp_h[MAX_HISTORY]; |
| 1187 | char *line; |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1188 | FILE *fp; |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1189 | unsigned idx, i, line_len; |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1190 | |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 1191 | /* NB: do not trash old history if file can't be opened */ |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1192 | |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1193 | fp = fopen_for_read(st_parm->hist_file); |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 1194 | if (fp) { |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 1195 | /* clean up old history */ |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1196 | for (idx = st_parm->cnt_history; idx > 0;) { |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1197 | idx--; |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1198 | free(st_parm->history[idx]); |
| 1199 | st_parm->history[idx] = NULL; |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1202 | /* fill temp_h[], retaining only last MAX_HISTORY lines */ |
| 1203 | memset(temp_h, 0, sizeof(temp_h)); |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1204 | st_parm->cnt_history_in_file = idx = 0; |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1205 | while ((line = xmalloc_fgetline(fp)) != NULL) { |
| 1206 | if (line[0] == '\0') { |
| 1207 | free(line); |
Glenn L McGrath | fdbbb04 | 2002-12-09 11:10:40 +0000 | [diff] [blame] | 1208 | continue; |
| 1209 | } |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1210 | free(temp_h[idx]); |
| 1211 | temp_h[idx] = line; |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1212 | st_parm->cnt_history_in_file++; |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1213 | idx++; |
| 1214 | if (idx == MAX_HISTORY) |
| 1215 | idx = 0; |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1216 | } |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 1217 | fclose(fp); |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1218 | |
| 1219 | /* find first non-NULL temp_h[], if any */ |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1220 | if (st_parm->cnt_history_in_file) { |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1221 | while (temp_h[idx] == NULL) { |
| 1222 | idx++; |
| 1223 | if (idx == MAX_HISTORY) |
| 1224 | idx = 0; |
| 1225 | } |
| 1226 | } |
| 1227 | |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1228 | /* copy temp_h[] to st_parm->history[] */ |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1229 | for (i = 0; i < MAX_HISTORY;) { |
| 1230 | line = temp_h[idx]; |
| 1231 | if (!line) |
| 1232 | break; |
| 1233 | idx++; |
| 1234 | if (idx == MAX_HISTORY) |
| 1235 | idx = 0; |
| 1236 | line_len = strlen(line); |
| 1237 | if (line_len >= MAX_LINELEN) |
| 1238 | line[MAX_LINELEN-1] = '\0'; |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1239 | st_parm->history[i++] = line; |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1240 | } |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1241 | st_parm->cnt_history = i; |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1242 | } |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1245 | /* state->flags is already checked to be nonzero */ |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1246 | static void save_history(char *str) |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1247 | { |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1248 | int fd; |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1249 | int len, len2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1250 | |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1251 | fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0666); |
| 1252 | if (fd < 0) |
| 1253 | return; |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1254 | xlseek(fd, 0, SEEK_END); /* paranoia */ |
| 1255 | len = strlen(str); |
| 1256 | str[len] = '\n'; /* we (try to) do atomic write */ |
| 1257 | len2 = full_write(fd, str, len + 1); |
| 1258 | str[len] = '\0'; |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1259 | close(fd); |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1260 | if (len2 != len + 1) |
| 1261 | return; /* "wtf?" */ |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1262 | |
| 1263 | /* did we write so much that history file needs trimming? */ |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1264 | state->cnt_history_in_file++; |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1265 | if (state->cnt_history_in_file > MAX_HISTORY * 4) { |
| 1266 | FILE *fp; |
| 1267 | char *new_name; |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1268 | line_input_t *st_temp; |
| 1269 | int i; |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1270 | |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1271 | /* we may have concurrently written entries from others. |
| 1272 | * load them */ |
| 1273 | st_temp = new_line_input_t(state->flags); |
| 1274 | st_temp->hist_file = state->hist_file; |
| 1275 | load_history(st_temp); |
| 1276 | |
| 1277 | /* write out temp file and replace hist_file atomically */ |
| 1278 | new_name = xasprintf("%s.%u.new", state->hist_file, (int) getpid()); |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1279 | fp = fopen_for_write(new_name); |
| 1280 | if (fp) { |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1281 | for (i = 0; i < st_temp->cnt_history; i++) |
| 1282 | fprintf(fp, "%s\n", st_temp->history[i]); |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1283 | fclose(fp); |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1284 | if (rename(new_name, state->hist_file) == 0) |
| 1285 | state->cnt_history_in_file = st_temp->cnt_history; |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1286 | } |
| 1287 | free(new_name); |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1288 | free_line_input_t(st_temp); |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1289 | } |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1290 | } |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1291 | # else |
| 1292 | # define load_history(a) ((void)0) |
| 1293 | # define save_history(a) ((void)0) |
| 1294 | # endif /* FEATURE_COMMAND_SAVEHISTORY */ |
Robert Griebl | 350d26b | 2002-12-03 22:45:46 +0000 | [diff] [blame] | 1295 | |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1296 | static void remember_in_history(char *str) |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1297 | { |
| 1298 | int i; |
| 1299 | |
| 1300 | if (!(state->flags & DO_HISTORY)) |
| 1301 | return; |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1302 | if (str[0] == '\0') |
| 1303 | return; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1304 | i = state->cnt_history; |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1305 | /* Don't save dupes */ |
| 1306 | if (i && strcmp(state->history[i-1], str) == 0) |
| 1307 | return; |
| 1308 | |
| 1309 | free(state->history[MAX_HISTORY]); /* redundant, paranoia */ |
| 1310 | state->history[MAX_HISTORY] = NULL; /* redundant, paranoia */ |
| 1311 | |
| 1312 | /* If history[] is full, remove the oldest command */ |
| 1313 | /* we need to keep history[MAX_HISTORY] empty, hence >=, not > */ |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1314 | if (i >= MAX_HISTORY) { |
| 1315 | free(state->history[0]); |
| 1316 | for (i = 0; i < MAX_HISTORY-1; i++) |
| 1317 | state->history[i] = state->history[i+1]; |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1318 | /* i == MAX_HISTORY-1 */ |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1319 | } |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1320 | /* i <= MAX_HISTORY-1 */ |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1321 | state->history[i++] = xstrdup(str); |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1322 | /* i <= MAX_HISTORY */ |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1323 | state->cur_history = i; |
| 1324 | state->cnt_history = i; |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1325 | # if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | bf3561f | 2007-04-14 10:10:40 +0000 | [diff] [blame] | 1326 | if ((state->flags & SAVE_HISTORY) && state->hist_file) |
Denis Vlasenko | 57abf9e | 2009-03-22 19:00:05 +0000 | [diff] [blame] | 1327 | save_history(str); |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1328 | # endif |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1329 | IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;) |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | #else /* MAX_HISTORY == 0 */ |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1333 | # define remember_in_history(a) ((void)0) |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1334 | #endif /* MAX_HISTORY */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1335 | |
| 1336 | |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame] | 1337 | #if ENABLE_FEATURE_EDITING_VI |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1338 | /* |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1339 | * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us> |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1340 | */ |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 1341 | static void |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1342 | vi_Word_motion(int eat) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1343 | { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1344 | CHAR_T *command = command_ps; |
| 1345 | |
| 1346 | while (cursor < command_len && !BB_isspace(command[cursor])) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1347 | input_forward(); |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1348 | if (eat) while (cursor < command_len && BB_isspace(command[cursor])) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1349 | input_forward(); |
| 1350 | } |
| 1351 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 1352 | static void |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1353 | vi_word_motion(int eat) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1354 | { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1355 | CHAR_T *command = command_ps; |
| 1356 | |
| 1357 | if (BB_isalnum(command[cursor]) || command[cursor] == '_') { |
Denis Vlasenko | 253ce00 | 2007-01-22 08:34:44 +0000 | [diff] [blame] | 1358 | while (cursor < command_len |
Denys Vlasenko | 1302892 | 2009-07-12 00:51:15 +0200 | [diff] [blame] | 1359 | && (BB_isalnum(command[cursor+1]) || command[cursor+1] == '_') |
| 1360 | ) { |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1361 | input_forward(); |
Denys Vlasenko | 1302892 | 2009-07-12 00:51:15 +0200 | [diff] [blame] | 1362 | } |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1363 | } else if (BB_ispunct(command[cursor])) { |
| 1364 | while (cursor < command_len && BB_ispunct(command[cursor+1])) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1365 | input_forward(); |
| 1366 | } |
| 1367 | |
Denis Vlasenko | 253ce00 | 2007-01-22 08:34:44 +0000 | [diff] [blame] | 1368 | if (cursor < command_len) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1369 | input_forward(); |
| 1370 | |
Denys Vlasenko | 1302892 | 2009-07-12 00:51:15 +0200 | [diff] [blame] | 1371 | if (eat) { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1372 | while (cursor < command_len && BB_isspace(command[cursor])) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1373 | input_forward(); |
Denys Vlasenko | 1302892 | 2009-07-12 00:51:15 +0200 | [diff] [blame] | 1374 | } |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 1377 | static void |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1378 | vi_End_motion(void) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1379 | { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1380 | CHAR_T *command = command_ps; |
| 1381 | |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1382 | input_forward(); |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1383 | while (cursor < command_len && BB_isspace(command[cursor])) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1384 | input_forward(); |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1385 | while (cursor < command_len-1 && !BB_isspace(command[cursor+1])) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1386 | input_forward(); |
| 1387 | } |
| 1388 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 1389 | static void |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1390 | vi_end_motion(void) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1391 | { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1392 | CHAR_T *command = command_ps; |
| 1393 | |
Denis Vlasenko | 253ce00 | 2007-01-22 08:34:44 +0000 | [diff] [blame] | 1394 | if (cursor >= command_len-1) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1395 | return; |
| 1396 | input_forward(); |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1397 | while (cursor < command_len-1 && BB_isspace(command[cursor])) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1398 | input_forward(); |
Denis Vlasenko | 253ce00 | 2007-01-22 08:34:44 +0000 | [diff] [blame] | 1399 | if (cursor >= command_len-1) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1400 | return; |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1401 | if (BB_isalnum(command[cursor]) || command[cursor] == '_') { |
Denis Vlasenko | 253ce00 | 2007-01-22 08:34:44 +0000 | [diff] [blame] | 1402 | while (cursor < command_len-1 |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1403 | && (BB_isalnum(command[cursor+1]) || command[cursor+1] == '_') |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 1404 | ) { |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1405 | input_forward(); |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 1406 | } |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1407 | } else if (BB_ispunct(command[cursor])) { |
| 1408 | while (cursor < command_len-1 && BB_ispunct(command[cursor+1])) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1409 | input_forward(); |
| 1410 | } |
| 1411 | } |
| 1412 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 1413 | static void |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1414 | vi_Back_motion(void) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1415 | { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1416 | CHAR_T *command = command_ps; |
| 1417 | |
| 1418 | while (cursor > 0 && BB_isspace(command[cursor-1])) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1419 | input_backward(1); |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1420 | while (cursor > 0 && !BB_isspace(command[cursor-1])) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1421 | input_backward(1); |
| 1422 | } |
| 1423 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 1424 | static void |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1425 | vi_back_motion(void) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1426 | { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1427 | CHAR_T *command = command_ps; |
| 1428 | |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1429 | if (cursor <= 0) |
| 1430 | return; |
| 1431 | input_backward(1); |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1432 | while (cursor > 0 && BB_isspace(command[cursor])) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1433 | input_backward(1); |
| 1434 | if (cursor <= 0) |
| 1435 | return; |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1436 | if (BB_isalnum(command[cursor]) || command[cursor] == '_') { |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 1437 | while (cursor > 0 |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1438 | && (BB_isalnum(command[cursor-1]) || command[cursor-1] == '_') |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 1439 | ) { |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1440 | input_backward(1); |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 1441 | } |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1442 | } else if (BB_ispunct(command[cursor])) { |
| 1443 | while (cursor > 0 && BB_ispunct(command[cursor-1])) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1444 | input_backward(1); |
| 1445 | } |
| 1446 | } |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1447 | #endif |
| 1448 | |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame] | 1449 | /* Modelled after bash 4.0 behavior of Ctrl-<arrow> */ |
| 1450 | static void ctrl_left(void) |
| 1451 | { |
| 1452 | CHAR_T *command = command_ps; |
| 1453 | |
| 1454 | while (1) { |
| 1455 | CHAR_T c; |
| 1456 | |
| 1457 | input_backward(1); |
| 1458 | if (cursor == 0) |
| 1459 | break; |
| 1460 | c = command[cursor]; |
| 1461 | if (c != ' ' && !BB_ispunct(c)) { |
| 1462 | /* we reached a "word" delimited by spaces/punct. |
| 1463 | * go to its beginning */ |
| 1464 | while (1) { |
| 1465 | c = command[cursor - 1]; |
| 1466 | if (c == ' ' || BB_ispunct(c)) |
| 1467 | break; |
| 1468 | input_backward(1); |
| 1469 | if (cursor == 0) |
| 1470 | break; |
| 1471 | } |
| 1472 | break; |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | static void ctrl_right(void) |
| 1477 | { |
| 1478 | CHAR_T *command = command_ps; |
| 1479 | |
| 1480 | while (1) { |
| 1481 | CHAR_T c; |
| 1482 | |
| 1483 | c = command[cursor]; |
| 1484 | if (c == BB_NUL) |
| 1485 | break; |
| 1486 | if (c != ' ' && !BB_ispunct(c)) { |
| 1487 | /* we reached a "word" delimited by spaces/punct. |
| 1488 | * go to its end + 1 */ |
| 1489 | while (1) { |
| 1490 | input_forward(); |
| 1491 | c = command[cursor]; |
| 1492 | if (c == BB_NUL || c == ' ' || BB_ispunct(c)) |
| 1493 | break; |
| 1494 | } |
| 1495 | break; |
| 1496 | } |
| 1497 | input_forward(); |
| 1498 | } |
| 1499 | } |
| 1500 | |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1501 | |
| 1502 | /* |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1503 | * read_line_input and its helpers |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1504 | */ |
| 1505 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1506 | #if !ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1507 | static void parse_and_put_prompt(const char *prmt_ptr) |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1508 | { |
| 1509 | cmdedit_prompt = prmt_ptr; |
| 1510 | cmdedit_prmt_len = strlen(prmt_ptr); |
| 1511 | put_prompt(); |
| 1512 | } |
| 1513 | #else |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1514 | static void parse_and_put_prompt(const char *prmt_ptr) |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1515 | { |
| 1516 | int prmt_len = 0; |
| 1517 | size_t cur_prmt_len = 0; |
| 1518 | char flg_not_length = '['; |
| 1519 | char *prmt_mem_ptr = xzalloc(1); |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1520 | char *cwd_buf = xrealloc_getcwd_or_warn(NULL); |
| 1521 | char cbuf[2]; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1522 | char c; |
| 1523 | char *pbuf; |
| 1524 | |
Denis Vlasenko | 6258fd3 | 2007-01-22 07:30:26 +0000 | [diff] [blame] | 1525 | cmdedit_prmt_len = 0; |
| 1526 | |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1527 | if (!cwd_buf) { |
| 1528 | cwd_buf = (char *)bb_msg_unknown; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1531 | cbuf[1] = '\0'; /* never changes */ |
| 1532 | |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1533 | while (*prmt_ptr) { |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1534 | char *free_me = NULL; |
| 1535 | |
| 1536 | pbuf = cbuf; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1537 | c = *prmt_ptr++; |
| 1538 | if (c == '\\') { |
| 1539 | const char *cp = prmt_ptr; |
| 1540 | int l; |
| 1541 | |
| 1542 | c = bb_process_escape_sequence(&prmt_ptr); |
| 1543 | if (prmt_ptr == cp) { |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1544 | if (*cp == '\0') |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1545 | break; |
| 1546 | c = *prmt_ptr++; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1547 | |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1548 | switch (c) { |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1549 | # if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1550 | case 'u': |
Denis Vlasenko | 86b29ea | 2007-09-27 10:17:16 +0000 | [diff] [blame] | 1551 | pbuf = user_buf ? user_buf : (char*)""; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1552 | break; |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1553 | # endif |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1554 | case 'h': |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 1555 | pbuf = free_me = safe_gethostname(); |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1556 | *strchrnul(pbuf, '.') = '\0'; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1557 | break; |
| 1558 | case '$': |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 1559 | c = (geteuid() == 0 ? '#' : '$'); |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1560 | break; |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1561 | # if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1562 | case 'w': |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1563 | /* /home/user[/something] -> ~[/something] */ |
| 1564 | pbuf = cwd_buf; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1565 | l = strlen(home_pwd_buf); |
Denis Vlasenko | 86b29ea | 2007-09-27 10:17:16 +0000 | [diff] [blame] | 1566 | if (l != 0 |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1567 | && strncmp(home_pwd_buf, cwd_buf, l) == 0 |
| 1568 | && (cwd_buf[l]=='/' || cwd_buf[l]=='\0') |
| 1569 | && strlen(cwd_buf + l) < PATH_MAX |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1570 | ) { |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1571 | pbuf = free_me = xasprintf("~%s", cwd_buf + l); |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1572 | } |
| 1573 | break; |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1574 | # endif |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1575 | case 'W': |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1576 | pbuf = cwd_buf; |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 1577 | cp = strrchr(pbuf, '/'); |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1578 | if (cp != NULL && cp != pbuf) |
| 1579 | pbuf += (cp-pbuf) + 1; |
| 1580 | break; |
| 1581 | case '!': |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1582 | pbuf = free_me = xasprintf("%d", num_ok_lines); |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1583 | break; |
| 1584 | case 'e': case 'E': /* \e \E = \033 */ |
| 1585 | c = '\033'; |
| 1586 | break; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1587 | case 'x': case 'X': { |
| 1588 | char buf2[4]; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1589 | for (l = 0; l < 3;) { |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1590 | unsigned h; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1591 | buf2[l++] = *prmt_ptr; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1592 | buf2[l] = '\0'; |
| 1593 | h = strtoul(buf2, &pbuf, 16); |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1594 | if (h > UCHAR_MAX || (pbuf - buf2) < l) { |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1595 | buf2[--l] = '\0'; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1596 | break; |
| 1597 | } |
| 1598 | prmt_ptr++; |
| 1599 | } |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1600 | c = (char)strtoul(buf2, NULL, 16); |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1601 | if (c == 0) |
| 1602 | c = '?'; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1603 | pbuf = cbuf; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1604 | break; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1605 | } |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1606 | case '[': case ']': |
| 1607 | if (c == flg_not_length) { |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1608 | flg_not_length = (flg_not_length == '[' ? ']' : '['); |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1609 | continue; |
| 1610 | } |
| 1611 | break; |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1612 | } /* switch */ |
| 1613 | } /* if */ |
| 1614 | } /* if */ |
| 1615 | cbuf[0] = c; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1616 | cur_prmt_len = strlen(pbuf); |
| 1617 | prmt_len += cur_prmt_len; |
| 1618 | if (flg_not_length != ']') |
| 1619 | cmdedit_prmt_len += cur_prmt_len; |
| 1620 | prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf); |
Denis Vlasenko | 7221c8c | 2007-12-03 10:45:14 +0000 | [diff] [blame] | 1621 | free(free_me); |
| 1622 | } /* while */ |
| 1623 | |
| 1624 | if (cwd_buf != (char *)bb_msg_unknown) |
| 1625 | free(cwd_buf); |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1626 | cmdedit_prompt = prmt_mem_ptr; |
| 1627 | put_prompt(); |
| 1628 | } |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1629 | #endif |
| 1630 | |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 1631 | static void cmdedit_setwidth(unsigned w, int redraw_flg) |
| 1632 | { |
| 1633 | cmdedit_termw = w; |
| 1634 | if (redraw_flg) { |
| 1635 | /* new y for current cursor */ |
| 1636 | int new_y = (cursor + cmdedit_prmt_len) / w; |
| 1637 | /* redraw */ |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1638 | redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1639 | fflush_all(); |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | static void win_changed(int nsig) |
| 1644 | { |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 1645 | unsigned width; |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 1646 | get_terminal_width_height(0, &width, NULL); |
| 1647 | cmdedit_setwidth(width, nsig /* - just a yes/no flag */); |
| 1648 | if (nsig == SIGWINCH) |
| 1649 | signal(SIGWINCH, win_changed); /* rearm ourself */ |
| 1650 | } |
| 1651 | |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 1652 | static int lineedit_read_key(char *read_key_buffer) |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1653 | { |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 1654 | int64_t ic; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1655 | struct pollfd pfd; |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1656 | int delay = -1; |
| 1657 | #if ENABLE_FEATURE_ASSUME_UNICODE |
| 1658 | char unicode_buf[MB_CUR_MAX + 1]; |
| 1659 | int unicode_idx = 0; |
| 1660 | #endif |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 1661 | |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1662 | pfd.fd = STDIN_FILENO; |
| 1663 | pfd.events = POLLIN; |
| 1664 | do { |
Denys Vlasenko | 1f6d230 | 2009-10-29 03:45:26 +0100 | [diff] [blame] | 1665 | #if ENABLE_FEATURE_EDITING_ASK_TERMINAL || ENABLE_FEATURE_ASSUME_UNICODE |
| 1666 | poll_again: |
| 1667 | #endif |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1668 | if (read_key_buffer[0] == 0) { |
| 1669 | /* Wait for input. Can't just call read_key, |
| 1670 | * it returns at once if stdin |
| 1671 | * is in non-blocking mode. */ |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1672 | safe_poll(&pfd, 1, delay); |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1673 | } |
| 1674 | /* Note: read_key sets errno to 0 on success: */ |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 1675 | ic = read_key(STDIN_FILENO, read_key_buffer); |
Denys Vlasenko | 04bb6b6 | 2009-10-14 12:53:04 +0200 | [diff] [blame] | 1676 | |
Denys Vlasenko | eb62d7c | 2009-10-27 10:34:06 +0100 | [diff] [blame] | 1677 | #if ENABLE_FEATURE_EDITING_ASK_TERMINAL |
| 1678 | if ((int32_t)ic == KEYCODE_CURSOR_POS |
Denys Vlasenko | d83bbf4 | 2009-10-27 10:47:49 +0100 | [diff] [blame] | 1679 | && S.sent_ESC_br6n |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 1680 | ) { |
Denys Vlasenko | d83bbf4 | 2009-10-27 10:47:49 +0100 | [diff] [blame] | 1681 | S.sent_ESC_br6n = 0; |
Denys Vlasenko | eb62d7c | 2009-10-27 10:34:06 +0100 | [diff] [blame] | 1682 | if (cursor == 0) { /* otherwise it may be bogus */ |
| 1683 | int col = ((ic >> 32) & 0x7fff) - 1; |
| 1684 | if (col > cmdedit_prmt_len) { |
| 1685 | cmdedit_x += (col - cmdedit_prmt_len); |
| 1686 | while (cmdedit_x >= cmdedit_termw) { |
| 1687 | cmdedit_x -= cmdedit_termw; |
| 1688 | cmdedit_y++; |
| 1689 | } |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 1690 | } |
| 1691 | } |
Denys Vlasenko | 1f6d230 | 2009-10-29 03:45:26 +0100 | [diff] [blame] | 1692 | goto poll_again; |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 1693 | } |
Denys Vlasenko | eb62d7c | 2009-10-27 10:34:06 +0100 | [diff] [blame] | 1694 | #endif |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1695 | |
| 1696 | #if ENABLE_FEATURE_ASSUME_UNICODE |
| 1697 | { |
| 1698 | wchar_t wc; |
| 1699 | |
| 1700 | if ((int32_t)ic < 0) /* KEYCODE_xxx */ |
| 1701 | return ic; |
| 1702 | unicode_buf[unicode_idx++] = ic; |
| 1703 | unicode_buf[unicode_idx] = '\0'; |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 1704 | if (mbstowcs(&wc, unicode_buf, 1) != 1 && unicode_idx < MB_CUR_MAX) { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1705 | delay = 50; |
Denys Vlasenko | 1f6d230 | 2009-10-29 03:45:26 +0100 | [diff] [blame] | 1706 | goto poll_again; |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1707 | } |
| 1708 | ic = wc; |
| 1709 | } |
| 1710 | #endif |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1711 | } while (errno == EAGAIN); |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1712 | |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1713 | return ic; |
| 1714 | } |
| 1715 | |
Paul Fox | 0f2dd9f | 2006-03-07 20:26:11 +0000 | [diff] [blame] | 1716 | /* leave out the "vi-mode"-only case labels if vi editing isn't |
| 1717 | * configured. */ |
Denys Vlasenko | 1302892 | 2009-07-12 00:51:15 +0200 | [diff] [blame] | 1718 | #define vi_case(caselabel) IF_FEATURE_EDITING_VI(case caselabel) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1719 | |
| 1720 | /* convert uppercase ascii to equivalent control char, for readability */ |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1721 | #undef CTRL |
| 1722 | #define CTRL(a) ((a) & ~0x40) |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1723 | |
Denys Vlasenko | 5c2e81b | 2009-07-16 14:14:34 +0200 | [diff] [blame] | 1724 | /* maxsize must be >= 2. |
| 1725 | * Returns: |
Denis Vlasenko | 80667e3 | 2008-02-02 18:35:55 +0000 | [diff] [blame] | 1726 | * -1 on read errors or EOF, or on bare Ctrl-D, |
| 1727 | * 0 on ctrl-C (the line entered is still returned in 'command'), |
Denis Vlasenko | 6a5377a | 2007-09-25 18:35:28 +0000 | [diff] [blame] | 1728 | * >0 length of input string, including terminating '\n' |
| 1729 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 1730 | int FAST_FUNC 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] | 1731 | { |
Denis Vlasenko | f31c3b6 | 2008-08-20 00:46:32 +0000 | [diff] [blame] | 1732 | int len; |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1733 | #if ENABLE_FEATURE_TAB_COMPLETION |
| 1734 | smallint lastWasTab = FALSE; |
| 1735 | #endif |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1736 | smallint break_out = 0; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1737 | #if ENABLE_FEATURE_EDITING_VI |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1738 | smallint vi_cmdmode = 0; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1739 | #endif |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1740 | struct termios initial_settings; |
| 1741 | struct termios new_settings; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1742 | char read_key_buffer[KEYCODE_BUFFER_SIZE]; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1743 | |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1744 | INIT_S(); |
| 1745 | |
| 1746 | if (tcgetattr(STDIN_FILENO, &initial_settings) < 0 |
| 1747 | || !(initial_settings.c_lflag & ECHO) |
| 1748 | ) { |
| 1749 | /* Happens when e.g. stty -echo was run before */ |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1750 | parse_and_put_prompt(prompt); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1751 | /* fflush_all(); - done by parse_and_put_prompt */ |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 1752 | if (fgets(command, maxsize, stdin) == NULL) |
| 1753 | len = -1; /* EOF or error */ |
| 1754 | else |
| 1755 | len = strlen(command); |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1756 | DEINIT_S(); |
| 1757 | return len; |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 1760 | check_unicode_in_env(); |
| 1761 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1762 | // FIXME: audit & improve this |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 1763 | if (maxsize > MAX_LINELEN) |
| 1764 | maxsize = MAX_LINELEN; |
Denys Vlasenko | 044b180 | 2009-07-12 02:50:35 +0200 | [diff] [blame] | 1765 | S.maxsize = maxsize; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1766 | |
| 1767 | /* With null flags, no other fields are ever used */ |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 1768 | state = st ? st : (line_input_t*) &const_int_0; |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1769 | #if MAX_HISTORY > 0 |
| 1770 | # if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | bf3561f | 2007-04-14 10:10:40 +0000 | [diff] [blame] | 1771 | if ((state->flags & SAVE_HISTORY) && state->hist_file) |
Denis Vlasenko | c0ea82a | 2009-03-23 06:33:37 +0000 | [diff] [blame] | 1772 | if (state->cnt_history == 0) |
| 1773 | load_history(state); |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1774 | # endif |
Denis Vlasenko | 3c385cd | 2008-11-02 00:41:05 +0000 | [diff] [blame] | 1775 | if (state->flags & DO_HISTORY) |
| 1776 | state->cur_history = state->cnt_history; |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1777 | #endif |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1778 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1779 | /* prepare before init handlers */ |
Denis Vlasenko | 47bdb3a | 2007-01-21 19:18:59 +0000 | [diff] [blame] | 1780 | cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */ |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1781 | command_len = 0; |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1782 | #if ENABLE_FEATURE_ASSUME_UNICODE |
| 1783 | command_ps = xzalloc(maxsize * sizeof(command_ps[0])); |
| 1784 | #else |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1785 | command_ps = command; |
Denis Vlasenko | 47bdb3a | 2007-01-21 19:18:59 +0000 | [diff] [blame] | 1786 | command[0] = '\0'; |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1787 | #endif |
| 1788 | #define command command_must_not_be_used |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1789 | |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1790 | new_settings = initial_settings; |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 1791 | new_settings.c_lflag &= ~ICANON; /* unbuffered input */ |
| 1792 | /* Turn off echoing and CTRL-C, so we can trap it */ |
| 1793 | new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1794 | /* Hmm, in linux c_cc[] is not parsed if ICANON is off */ |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 1795 | new_settings.c_cc[VMIN] = 1; |
| 1796 | new_settings.c_cc[VTIME] = 0; |
| 1797 | /* Turn off CTRL-C, so we can trap it */ |
Denis Vlasenko | 47bdb3a | 2007-01-21 19:18:59 +0000 | [diff] [blame] | 1798 | #ifndef _POSIX_VDISABLE |
Denys Vlasenko | db9c57e | 2009-09-27 02:48:53 +0200 | [diff] [blame] | 1799 | # define _POSIX_VDISABLE '\0' |
Denis Vlasenko | 47bdb3a | 2007-01-21 19:18:59 +0000 | [diff] [blame] | 1800 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1801 | new_settings.c_cc[VINTR] = _POSIX_VDISABLE; |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 1802 | tcsetattr_stdin_TCSANOW(&new_settings); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1803 | |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 1804 | /* Now initialize things */ |
Denis Vlasenko | 6258fd3 | 2007-01-22 07:30:26 +0000 | [diff] [blame] | 1805 | previous_SIGWINCH_handler = signal(SIGWINCH, win_changed); |
| 1806 | win_changed(0); /* do initial resizing */ |
| 1807 | #if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR |
| 1808 | { |
| 1809 | struct passwd *entry; |
| 1810 | |
| 1811 | entry = getpwuid(geteuid()); |
| 1812 | if (entry) { |
| 1813 | user_buf = xstrdup(entry->pw_name); |
| 1814 | home_pwd_buf = xstrdup(entry->pw_dir); |
| 1815 | } |
| 1816 | } |
| 1817 | #endif |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1818 | |
| 1819 | #if 0 |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1820 | for (i = 0; i <= MAX_HISTORY; i++) |
| 1821 | bb_error_msg("history[%d]:'%s'", i, state->history[i]); |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1822 | bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history); |
| 1823 | #endif |
| 1824 | |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 1825 | /* Print out the command prompt */ |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1826 | parse_and_put_prompt(prompt); |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 1827 | |
Denys Vlasenko | c396fe6 | 2009-05-17 19:28:14 +0200 | [diff] [blame] | 1828 | read_key_buffer[0] = 0; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1829 | while (1) { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1830 | /* |
| 1831 | * The emacs and vi modes share much of the code in the big |
| 1832 | * command loop. Commands entered when in vi's command mode |
| 1833 | * (aka "escape mode") get an extra bit added to distinguish |
| 1834 | * them - this keeps them from being self-inserted. This |
| 1835 | * clutters the big switch a bit, but keeps all the code |
| 1836 | * in one place. |
| 1837 | */ |
| 1838 | enum { |
| 1839 | VI_CMDMODE_BIT = 0x40000000, |
| 1840 | /* 0x80000000 bit flags KEYCODE_xxx */ |
| 1841 | }; |
Denys Vlasenko | 04bb6b6 | 2009-10-14 12:53:04 +0200 | [diff] [blame] | 1842 | int32_t ic, ic_raw; |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1843 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1844 | fflush_all(); |
Denys Vlasenko | 04bb6b6 | 2009-10-14 12:53:04 +0200 | [diff] [blame] | 1845 | ic = ic_raw = lineedit_read_key(read_key_buffer); |
Paul Fox | 0f2dd9f | 2006-03-07 20:26:11 +0000 | [diff] [blame] | 1846 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1847 | #if ENABLE_FEATURE_EDITING_VI |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1848 | newdelflag = 1; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1849 | if (vi_cmdmode) { |
| 1850 | /* btw, since KEYCODE_xxx are all < 0, this doesn't |
| 1851 | * change ic if it contains one of them: */ |
| 1852 | ic |= VI_CMDMODE_BIT; |
| 1853 | } |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1854 | #endif |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1855 | |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 1856 | switch (ic) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1857 | case '\n': |
| 1858 | case '\r': |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1859 | vi_case('\n'|VI_CMDMODE_BIT:) |
| 1860 | vi_case('\r'|VI_CMDMODE_BIT:) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1861 | /* Enter */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1862 | goto_new_line(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1863 | break_out = 1; |
| 1864 | break; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1865 | case CTRL('A'): |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1866 | vi_case('0'|VI_CMDMODE_BIT:) |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1867 | /* Control-a -- Beginning of line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1868 | input_backward(cursor); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1869 | break; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1870 | case CTRL('B'): |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1871 | vi_case('h'|VI_CMDMODE_BIT:) |
| 1872 | vi_case('\b'|VI_CMDMODE_BIT:) |
| 1873 | vi_case('\x7f'|VI_CMDMODE_BIT:) /* DEL */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1874 | /* Control-b -- Move back one character */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1875 | input_backward(1); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1876 | break; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1877 | case CTRL('E'): |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1878 | vi_case('$'|VI_CMDMODE_BIT:) |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1879 | /* Control-e -- End of line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1880 | input_end(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1881 | break; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1882 | case CTRL('F'): |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1883 | vi_case('l'|VI_CMDMODE_BIT:) |
| 1884 | vi_case(' '|VI_CMDMODE_BIT:) |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1885 | /* Control-f -- Move forward one character */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1886 | input_forward(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1887 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1888 | case '\b': |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1889 | case '\x7f': /* DEL */ |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1890 | /* Control-h and DEL */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1891 | input_backspace(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1892 | break; |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1893 | #if ENABLE_FEATURE_TAB_COMPLETION |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1894 | case '\t': |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1895 | input_tab(&lastWasTab); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1896 | break; |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 1897 | #endif |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1898 | case CTRL('K'): |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1899 | /* Control-k -- clear to end of line */ |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1900 | command_ps[cursor] = BB_NUL; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1901 | command_len = cursor; |
Eric Andersen | ae10361 | 2002-04-24 23:08:23 +0000 | [diff] [blame] | 1902 | printf("\033[J"); |
Eric Andersen | 65a0730 | 2002-04-13 13:26:49 +0000 | [diff] [blame] | 1903 | break; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1904 | case CTRL('L'): |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1905 | vi_case(CTRL('L')|VI_CMDMODE_BIT:) |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 1906 | /* Control-l -- clear screen */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1907 | printf("\033[H"); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1908 | redraw(0, command_len - cursor); |
Eric Andersen | f1f2bd0 | 2001-12-21 11:20:15 +0000 | [diff] [blame] | 1909 | break; |
Denis Vlasenko | 9d4533e | 2006-11-02 22:09:37 +0000 | [diff] [blame] | 1910 | #if MAX_HISTORY > 0 |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1911 | case CTRL('N'): |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1912 | vi_case(CTRL('N')|VI_CMDMODE_BIT:) |
| 1913 | vi_case('j'|VI_CMDMODE_BIT:) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1914 | /* Control-n -- Get next command in history */ |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1915 | if (get_next_history()) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1916 | goto rewrite_line; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1917 | break; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1918 | case CTRL('P'): |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1919 | vi_case(CTRL('P')|VI_CMDMODE_BIT:) |
| 1920 | vi_case('k'|VI_CMDMODE_BIT:) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1921 | /* Control-p -- Get previous command from history */ |
Denis Vlasenko | 682ad30 | 2008-09-27 01:28:56 +0000 | [diff] [blame] | 1922 | if (get_previous_history()) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1923 | goto rewrite_line; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1924 | break; |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 1925 | #endif |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1926 | case CTRL('U'): |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1927 | vi_case(CTRL('U')|VI_CMDMODE_BIT:) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1928 | /* Control-U -- Clear line before cursor */ |
| 1929 | if (cursor) { |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1930 | command_len -= cursor; |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1931 | memmove(command_ps, command_ps + cursor, |
| 1932 | (command_len + 1) * sizeof(command_ps[0])); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 1933 | redraw(cmdedit_y, command_len); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1934 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1935 | break; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 1936 | case CTRL('W'): |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1937 | vi_case(CTRL('W')|VI_CMDMODE_BIT:) |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 1938 | /* Control-W -- Remove the last word */ |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1939 | while (cursor > 0 && BB_isspace(command_ps[cursor-1])) |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 1940 | input_backspace(); |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1941 | while (cursor > 0 && !BB_isspace(command_ps[cursor-1])) |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 1942 | input_backspace(); |
| 1943 | break; |
Denis Vlasenko | 00cdbd8 | 2007-01-21 19:21:21 +0000 | [diff] [blame] | 1944 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1945 | #if ENABLE_FEATURE_EDITING_VI |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1946 | case 'i'|VI_CMDMODE_BIT: |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1947 | vi_cmdmode = 0; |
| 1948 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1949 | case 'I'|VI_CMDMODE_BIT: |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1950 | input_backward(cursor); |
| 1951 | vi_cmdmode = 0; |
| 1952 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1953 | case 'a'|VI_CMDMODE_BIT: |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1954 | input_forward(); |
| 1955 | vi_cmdmode = 0; |
| 1956 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1957 | case 'A'|VI_CMDMODE_BIT: |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1958 | input_end(); |
| 1959 | vi_cmdmode = 0; |
| 1960 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1961 | case 'x'|VI_CMDMODE_BIT: |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1962 | input_delete(1); |
| 1963 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1964 | case 'X'|VI_CMDMODE_BIT: |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1965 | if (cursor > 0) { |
| 1966 | input_backward(1); |
| 1967 | input_delete(1); |
| 1968 | } |
| 1969 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1970 | case 'W'|VI_CMDMODE_BIT: |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1971 | vi_Word_motion(1); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1972 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1973 | case 'w'|VI_CMDMODE_BIT: |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1974 | vi_word_motion(1); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1975 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1976 | case 'E'|VI_CMDMODE_BIT: |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1977 | vi_End_motion(); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1978 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1979 | case 'e'|VI_CMDMODE_BIT: |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1980 | vi_end_motion(); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1981 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1982 | case 'B'|VI_CMDMODE_BIT: |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1983 | vi_Back_motion(); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1984 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1985 | case 'b'|VI_CMDMODE_BIT: |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 1986 | vi_back_motion(); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1987 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1988 | case 'C'|VI_CMDMODE_BIT: |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1989 | vi_cmdmode = 0; |
| 1990 | /* fall through */ |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1991 | case 'D'|VI_CMDMODE_BIT: |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1992 | goto clear_to_eol; |
| 1993 | |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1994 | case 'c'|VI_CMDMODE_BIT: |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 1995 | vi_cmdmode = 0; |
| 1996 | /* fall through */ |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1997 | case 'd'|VI_CMDMODE_BIT: { |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 1998 | int nc, sc; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 1999 | |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 2000 | ic = lineedit_read_key(read_key_buffer); |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2001 | if (errno) /* error */ |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2002 | goto prepare_to_die; |
Denys Vlasenko | 04bb6b6 | 2009-10-14 12:53:04 +0200 | [diff] [blame] | 2003 | if (ic == ic_raw) { /* "cc", "dd" */ |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2004 | input_backward(cursor); |
| 2005 | goto clear_to_eol; |
| 2006 | break; |
| 2007 | } |
Denys Vlasenko | 04bb6b6 | 2009-10-14 12:53:04 +0200 | [diff] [blame] | 2008 | |
| 2009 | sc = cursor; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2010 | switch (ic) { |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2011 | case 'w': |
| 2012 | case 'W': |
| 2013 | case 'e': |
| 2014 | case 'E': |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2015 | switch (ic) { |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2016 | case 'w': /* "dw", "cw" */ |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2017 | vi_word_motion(vi_cmdmode); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 2018 | break; |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2019 | case 'W': /* 'dW', 'cW' */ |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2020 | vi_Word_motion(vi_cmdmode); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 2021 | break; |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2022 | case 'e': /* 'de', 'ce' */ |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2023 | vi_end_motion(); |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2024 | input_forward(); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 2025 | break; |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2026 | case 'E': /* 'dE', 'cE' */ |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2027 | vi_End_motion(); |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2028 | input_forward(); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 2029 | break; |
| 2030 | } |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2031 | nc = cursor; |
| 2032 | input_backward(cursor - sc); |
| 2033 | while (nc-- > cursor) |
| 2034 | input_delete(1); |
| 2035 | break; |
| 2036 | case 'b': /* "db", "cb" */ |
| 2037 | case 'B': /* implemented as B */ |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2038 | if (ic == 'b') |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2039 | vi_back_motion(); |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2040 | else |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2041 | vi_Back_motion(); |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2042 | while (sc-- > cursor) |
| 2043 | input_delete(1); |
| 2044 | break; |
| 2045 | case ' ': /* "d ", "c " */ |
| 2046 | input_delete(1); |
| 2047 | break; |
| 2048 | case '$': /* "d$", "c$" */ |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2049 | clear_to_eol: |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2050 | while (cursor < command_len) |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2051 | input_delete(1); |
| 2052 | break; |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 2053 | } |
| 2054 | break; |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2055 | } |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2056 | case 'p'|VI_CMDMODE_BIT: |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 2057 | input_forward(); |
| 2058 | /* fallthrough */ |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2059 | case 'P'|VI_CMDMODE_BIT: |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 2060 | put(); |
| 2061 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2062 | case 'r'|VI_CMDMODE_BIT: |
Denys Vlasenko | 04bb6b6 | 2009-10-14 12:53:04 +0200 | [diff] [blame] | 2063 | //FIXME: unicode case? |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 2064 | ic = lineedit_read_key(read_key_buffer); |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2065 | if (errno) /* error */ |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 2066 | goto prepare_to_die; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2067 | if (ic < ' ' || ic > 255) { |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 2068 | beep(); |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2069 | } else { |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2070 | command_ps[cursor] = ic; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2071 | bb_putchar(ic); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 2072 | bb_putchar('\b'); |
Paul Fox | 3f11b1b | 2005-08-04 19:04:46 +0000 | [diff] [blame] | 2073 | } |
| 2074 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2075 | case '\x1b': /* ESC */ |
| 2076 | if (state->flags & VI_MODE) { |
| 2077 | /* insert mode --> command mode */ |
| 2078 | vi_cmdmode = 1; |
| 2079 | input_backward(1); |
| 2080 | } |
| 2081 | break; |
Denis Vlasenko | 7f1dc21 | 2006-12-19 01:10:25 +0000 | [diff] [blame] | 2082 | #endif /* FEATURE_COMMAND_EDITING_VI */ |
Paul Fox | 0f2dd9f | 2006-03-07 20:26:11 +0000 | [diff] [blame] | 2083 | |
Denis Vlasenko | 9d4533e | 2006-11-02 22:09:37 +0000 | [diff] [blame] | 2084 | #if MAX_HISTORY > 0 |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2085 | case KEYCODE_UP: |
| 2086 | if (get_previous_history()) |
| 2087 | goto rewrite_line; |
| 2088 | beep(); |
| 2089 | break; |
| 2090 | case KEYCODE_DOWN: |
| 2091 | if (!get_next_history()) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2092 | break; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 2093 | rewrite_line: |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2094 | /* Rewrite the line with the selected history item */ |
| 2095 | /* change command */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 2096 | command_len = load_string(state->history[state->cur_history] ? |
| 2097 | state->history[state->cur_history] : "", maxsize); |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2098 | /* redraw and go to eol (bol, in vi) */ |
| 2099 | redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0); |
| 2100 | break; |
Glenn L McGrath | 062c74f | 2002-11-27 09:29:49 +0000 | [diff] [blame] | 2101 | #endif |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2102 | case KEYCODE_RIGHT: |
| 2103 | input_forward(); |
| 2104 | break; |
| 2105 | case KEYCODE_LEFT: |
| 2106 | input_backward(1); |
| 2107 | break; |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame] | 2108 | case KEYCODE_CTRL_LEFT: |
| 2109 | ctrl_left(); |
| 2110 | break; |
| 2111 | case KEYCODE_CTRL_RIGHT: |
| 2112 | ctrl_right(); |
| 2113 | break; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2114 | case KEYCODE_DELETE: |
| 2115 | input_delete(0); |
| 2116 | break; |
| 2117 | case KEYCODE_HOME: |
| 2118 | input_backward(cursor); |
| 2119 | break; |
| 2120 | case KEYCODE_END: |
| 2121 | input_end(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2122 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 2123 | |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2124 | default: |
Denys Vlasenko | 04bb6b6 | 2009-10-14 12:53:04 +0200 | [diff] [blame] | 2125 | if (initial_settings.c_cc[VINTR] != 0 |
| 2126 | && ic_raw == initial_settings.c_cc[VINTR] |
| 2127 | ) { |
| 2128 | /* Ctrl-C (usually) - stop gathering input */ |
| 2129 | goto_new_line(); |
| 2130 | command_len = 0; |
| 2131 | break_out = -1; /* "do not append '\n'" */ |
| 2132 | break; |
| 2133 | } |
| 2134 | if (initial_settings.c_cc[VEOF] != 0 |
| 2135 | && ic_raw == initial_settings.c_cc[VEOF] |
| 2136 | ) { |
| 2137 | /* Ctrl-D (usually) - delete one character, |
| 2138 | * or exit if len=0 and no chars to delete */ |
| 2139 | if (command_len == 0) { |
| 2140 | errno = 0; |
| 2141 | #if ENABLE_FEATURE_EDITING_VI |
| 2142 | prepare_to_die: |
| 2143 | #endif |
| 2144 | break_out = command_len = -1; |
| 2145 | break; |
| 2146 | } |
| 2147 | input_delete(0); |
| 2148 | break; |
| 2149 | } |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2150 | // /* Control-V -- force insert of next char */ |
| 2151 | // if (c == CTRL('V')) { |
| 2152 | // if (safe_read(STDIN_FILENO, &c, 1) < 1) |
| 2153 | // goto prepare_to_die; |
| 2154 | // if (c == 0) { |
| 2155 | // beep(); |
| 2156 | // break; |
| 2157 | // } |
| 2158 | // } |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2159 | if (ic < ' ' |
| 2160 | || (!ENABLE_FEATURE_ASSUME_UNICODE && ic >= 256) |
| 2161 | || (ENABLE_FEATURE_ASSUME_UNICODE && ic >= VI_CMDMODE_BIT) |
| 2162 | ) { |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2163 | /* If VI_CMDMODE_BIT is set, ic is >= 256 |
Denys Vlasenko | 04bb6b6 | 2009-10-14 12:53:04 +0200 | [diff] [blame] | 2164 | * and vi mode ignores unexpected chars. |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2165 | * Otherwise, we are here if ic is a |
| 2166 | * control char or an unhandled ESC sequence, |
| 2167 | * which is also ignored. |
| 2168 | */ |
| 2169 | break; |
| 2170 | } |
| 2171 | if ((int)command_len >= (maxsize - 2)) { |
| 2172 | /* Not enough space for the char and EOL */ |
| 2173 | break; |
Paul Fox | 84bbac5 | 2008-01-11 16:50:08 +0000 | [diff] [blame] | 2174 | } |
Denis Vlasenko | 00cdbd8 | 2007-01-21 19:21:21 +0000 | [diff] [blame] | 2175 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2176 | command_len++; |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2177 | if (cursor == (command_len - 1)) { |
| 2178 | /* We are at the end, append */ |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2179 | command_ps[cursor] = ic; |
| 2180 | command_ps[cursor + 1] = BB_NUL; |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 2181 | cmdedit_set_out_char(' '); |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2182 | } else { |
| 2183 | /* In the middle, insert */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2184 | int sc = cursor; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 2185 | |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2186 | memmove(command_ps + sc + 1, command_ps + sc, |
| 2187 | (command_len - sc) * sizeof(command_ps[0])); |
| 2188 | command_ps[sc] = ic; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2189 | sc++; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 2190 | /* rewrite from cursor */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2191 | input_end(); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 2192 | /* to prev x pos + 1 */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2193 | input_backward(cursor - sc); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 2194 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 2195 | break; |
Denys Vlasenko | 04bb6b6 | 2009-10-14 12:53:04 +0200 | [diff] [blame] | 2196 | } /* switch (ic) */ |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2197 | |
| 2198 | if (break_out) |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 2199 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2200 | |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 2201 | #if ENABLE_FEATURE_TAB_COMPLETION |
Denys Vlasenko | 04bb6b6 | 2009-10-14 12:53:04 +0200 | [diff] [blame] | 2202 | if (ic_raw != '\t') |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2203 | lastWasTab = FALSE; |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 2204 | #endif |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 2205 | } /* while (1) */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 2206 | |
Denys Vlasenko | eb62d7c | 2009-10-27 10:34:06 +0100 | [diff] [blame] | 2207 | #if ENABLE_FEATURE_EDITING_ASK_TERMINAL |
Denys Vlasenko | d83bbf4 | 2009-10-27 10:47:49 +0100 | [diff] [blame] | 2208 | if (S.sent_ESC_br6n) { |
Denys Vlasenko | eb62d7c | 2009-10-27 10:34:06 +0100 | [diff] [blame] | 2209 | /* "sleep 1; busybox ash" + hold [Enter] to trigger. |
| 2210 | * We sent "ESC [ 6 n", but got '\n' first, and |
| 2211 | * KEYCODE_CURSOR_POS response is now buffered from terminal. |
| 2212 | * It's bad already and not much can be done with it |
| 2213 | * (it _will_ be visible for the next process to read stdin), |
| 2214 | * but without this delay it even shows up on the screen |
| 2215 | * as garbage because we restore echo settings with tcsetattr |
| 2216 | * before it comes in. UGLY! |
| 2217 | */ |
| 2218 | usleep(20*1000); |
| 2219 | } |
| 2220 | #endif |
| 2221 | |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2222 | /* Stop bug catching using "command_must_not_be_used" trick */ |
| 2223 | #undef command |
| 2224 | |
| 2225 | #if ENABLE_FEATURE_ASSUME_UNICODE |
Denys Vlasenko | 2f3f09c | 2009-09-29 00:00:12 +0200 | [diff] [blame] | 2226 | command[0] = '\0'; |
| 2227 | if (command_len > 0) |
| 2228 | command_len = save_string(command, maxsize - 1); |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2229 | free(command_ps); |
| 2230 | #endif |
| 2231 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2232 | if (command_len > 0) |
| 2233 | remember_in_history(command); |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 2234 | |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 2235 | if (break_out > 0) { |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2236 | command[command_len++] = '\n'; |
| 2237 | command[command_len] = '\0'; |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 2238 | } |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 2239 | |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 2240 | #if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 5592fac | 2007-01-21 19:19:46 +0000 | [diff] [blame] | 2241 | free_tab_completion_data(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2242 | #endif |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 2243 | |
Denis Vlasenko | 6258fd3 | 2007-01-22 07:30:26 +0000 | [diff] [blame] | 2244 | /* restore initial_settings */ |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 2245 | tcsetattr_stdin_TCSANOW(&initial_settings); |
Denis Vlasenko | 6258fd3 | 2007-01-22 07:30:26 +0000 | [diff] [blame] | 2246 | /* restore SIGWINCH handler */ |
| 2247 | signal(SIGWINCH, previous_SIGWINCH_handler); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2248 | fflush_all(); |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 2249 | |
Denis Vlasenko | f31c3b6 | 2008-08-20 00:46:32 +0000 | [diff] [blame] | 2250 | len = command_len; |
Denis Vlasenko | 73cb1fd | 2007-11-10 01:35:47 +0000 | [diff] [blame] | 2251 | DEINIT_S(); |
| 2252 | |
Denis Vlasenko | f31c3b6 | 2008-08-20 00:46:32 +0000 | [diff] [blame] | 2253 | return len; /* can't return command_len, DEINIT_S() destroys it */ |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2256 | #else |
| 2257 | |
| 2258 | #undef read_line_input |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 2259 | int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize) |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2260 | { |
| 2261 | fputs(prompt, stdout); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2262 | fflush_all(); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2263 | fgets(command, maxsize, stdin); |
| 2264 | return strlen(command); |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Denys Vlasenko | 2e6d4ef | 2009-07-10 18:40:49 +0200 | [diff] [blame] | 2267 | #endif /* FEATURE_EDITING */ |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 2268 | |
| 2269 | |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 2270 | /* |
| 2271 | * Testing |
| 2272 | */ |
| 2273 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2274 | #ifdef TEST |
| 2275 | |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 2276 | #include <locale.h> |
Denis Vlasenko | 82b39e8 | 2007-01-21 19:18:19 +0000 | [diff] [blame] | 2277 | |
| 2278 | const char *applet_name = "debug stuff usage"; |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 2279 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2280 | int main(int argc, char **argv) |
| 2281 | { |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 2282 | char buff[MAX_LINELEN]; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2283 | char *prompt = |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 2284 | #if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2285 | "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:" |
| 2286 | "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] " |
| 2287 | "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]"; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2288 | #else |
| 2289 | "% "; |
| 2290 | #endif |
| 2291 | |
Denis Vlasenko | 7f1dc21 | 2006-12-19 01:10:25 +0000 | [diff] [blame] | 2292 | #if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 2293 | setlocale(LC_ALL, ""); |
| 2294 | #endif |
Denis Vlasenko | 7f1dc21 | 2006-12-19 01:10:25 +0000 | [diff] [blame] | 2295 | while (1) { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 2296 | int l; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2297 | l = read_line_input(prompt, buff); |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2298 | if (l <= 0 || buff[l-1] != '\n') |
Eric Andersen | 27bb790 | 2003-12-23 20:24:51 +0000 | [diff] [blame] | 2299 | break; |
Denis Vlasenko | 0a8a774 | 2006-12-21 22:27:10 +0000 | [diff] [blame] | 2300 | buff[l-1] = 0; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2301 | printf("*** read_line_input() returned line =%s=\n", buff); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 2302 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 2303 | printf("*** read_line_input() detect ^D\n"); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 2304 | return 0; |
| 2305 | } |
| 2306 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2307 | #endif /* TEST */ |