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