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