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