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