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 | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 3 | * Termios command line History and Editting. |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 5 | * Copyright (c) 1986-2001 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 |
| 12 | * Erik Andersen <andersee@debian.org> (Majorly adjusted for busybox) |
| 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 | * |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | Usage and Known bugs: |
| 21 | Terminal key codes are not extensive, and more will probably |
| 22 | need to be added. This version was created on Debian GNU/Linux 2.x. |
| 23 | Delete, Backspace, Home, End, and the arrow keys were tested |
| 24 | 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] | 25 | Ctrl-E also works as End. |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 26 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 27 | Small bugs (simple effect): |
| 28 | - not true viewing if terminal size (x*y symbols) less |
| 29 | size (prompt + editor`s line + 2 symbols) |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 30 | - not true viewing if length prompt less terminal width |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 33 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <errno.h> |
| 36 | #include <unistd.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <sys/ioctl.h> |
| 40 | #include <ctype.h> |
| 41 | #include <signal.h> |
| 42 | #include <limits.h> |
| 43 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 44 | #include "busybox.h" |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 45 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 46 | #ifdef CONFIG_LOCALE_SUPPORT |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 47 | #define Isprint(c) isprint((c)) |
| 48 | #else |
| 49 | #define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') ) |
| 50 | #endif |
| 51 | |
| 52 | #ifndef TEST |
| 53 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 54 | #define D(x) |
| 55 | |
| 56 | #else |
| 57 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 58 | #define CONFIG_FEATURE_COMMAND_EDITING |
| 59 | #define CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
| 60 | #define CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION |
| 61 | #define CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
| 62 | #define CONFIG_FEATURE_CLEAN_UP |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 63 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 64 | #define D(x) x |
| 65 | |
| 66 | #endif /* TEST */ |
| 67 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 68 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | 5165fbe | 2001-02-20 06:42:29 +0000 | [diff] [blame] | 69 | #include <dirent.h> |
| 70 | #include <sys/stat.h> |
| 71 | #endif |
| 72 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 73 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 74 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 75 | #ifndef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
| 76 | #undef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 77 | #endif |
| 78 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 79 | #if defined(CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION) || defined(CONFIG_FEATURE_SH_FANCY_PROMPT) |
| 80 | #define CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 81 | #endif |
| 82 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 83 | #ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 84 | # ifndef TEST |
Eric Andersen | 1a10eec | 2001-10-24 17:19:38 +0000 | [diff] [blame] | 85 | # include "pwd.h" |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 86 | # else |
| 87 | # include <pwd.h> |
| 88 | # endif /* TEST */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 89 | #endif /* advanced FEATURES */ |
Eric Andersen | af4ac77 | 2001-02-01 22:43:49 +0000 | [diff] [blame] | 90 | |
| 91 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 92 | |
| 93 | struct history { |
| 94 | char *s; |
| 95 | struct history *p; |
| 96 | struct history *n; |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 99 | /* Maximum length of the linked list for the command line history */ |
| 100 | static const int MAX_HISTORY = 15; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 101 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 102 | /* First element in command line list */ |
| 103 | static struct history *his_front = NULL; |
| 104 | |
| 105 | /* Last element in command line list */ |
| 106 | static struct history *his_end = NULL; |
| 107 | |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 108 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 109 | #include <termios.h> |
| 110 | #define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) |
| 111 | #define getTermSettings(fd,argp) tcgetattr(fd, argp); |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 112 | |
| 113 | /* Current termio and the previous termio before starting sh */ |
Eric Andersen | 63a8622 | 2000-11-07 06:52:13 +0000 | [diff] [blame] | 114 | static struct termios initial_settings, new_settings; |
Erik Andersen | 8ea7d8c | 2000-05-20 00:40:08 +0000 | [diff] [blame] | 115 | |
| 116 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 117 | static |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 118 | volatile int cmdedit_termw = 80; /* actual terminal width */ |
| 119 | static int history_counter = 0; /* Number of commands in history list */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 120 | static |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 121 | volatile int handlers_sets = 0; /* Set next bites: */ |
| 122 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 123 | enum { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 124 | SET_ATEXIT = 1, /* when atexit() has been called |
| 125 | and get euid,uid,gid to fast compare */ |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 126 | SET_WCHG_HANDLERS = 2, /* winchg signal handler */ |
| 127 | SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 128 | }; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 129 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 130 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 131 | static int cmdedit_x; /* real x terminal position */ |
| 132 | static int cmdedit_y; /* pseudoreal y terminal position */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 133 | static int cmdedit_prmt_len; /* lenght prompt without colores string */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 134 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 135 | static int cursor; /* required global for signal handler */ |
| 136 | static int len; /* --- "" - - "" - -"- --""-- --""--- */ |
| 137 | static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 138 | static |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 139 | #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 140 | const |
| 141 | #endif |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 142 | char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 143 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 144 | #ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 145 | static char *user_buf = ""; |
| 146 | static char *home_pwd_buf = ""; |
| 147 | static int my_euid; |
| 148 | #endif |
| 149 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 150 | #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 151 | static char *hostname_buf = ""; |
| 152 | static int num_ok_lines = 1; |
| 153 | #endif |
| 154 | |
| 155 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 156 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 157 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 158 | #ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 159 | static int my_euid; |
| 160 | #endif |
| 161 | |
| 162 | static int my_uid; |
| 163 | static int my_gid; |
| 164 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 165 | #endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 166 | |
Eric Andersen | 95b5201 | 2001-08-02 09:52:40 +0000 | [diff] [blame] | 167 | /* It seems that libc5 doesn't know what a sighandler_t is... */ |
| 168 | #if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1) |
| 169 | typedef void (*sighandler_t) (int); |
| 170 | #endif |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 171 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 172 | static void cmdedit_setwidth(int w, int redraw_flg); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 173 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 174 | static void win_changed(int nsig) |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 175 | { |
| 176 | struct winsize win = { 0, 0, 0, 0 }; |
Eric Andersen | 8d79ce8 | 2001-07-22 23:00:15 +0000 | [diff] [blame] | 177 | static sighandler_t previous_SIGWINCH_handler; /* for reset */ |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 178 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 179 | /* emulate || signal call */ |
| 180 | if (nsig == -SIGWINCH || nsig == SIGWINCH) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 181 | ioctl(0, TIOCGWINSZ, &win); |
| 182 | if (win.ws_col > 0) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 183 | cmdedit_setwidth(win.ws_col, nsig == SIGWINCH); |
| 184 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 185 | } |
Eric Andersen | 4bbdd78 | 2001-01-30 22:23:17 +0000 | [diff] [blame] | 186 | /* Unix not all standart in recall signal */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 187 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 188 | if (nsig == -SIGWINCH) /* save previous handler */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 189 | previous_SIGWINCH_handler = signal(SIGWINCH, win_changed); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 190 | else if (nsig == SIGWINCH) /* signaled called handler */ |
| 191 | signal(SIGWINCH, win_changed); /* set for next call */ |
| 192 | else /* nsig == 0 */ |
| 193 | /* set previous handler */ |
| 194 | signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 195 | } |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 196 | |
| 197 | static void cmdedit_reset_term(void) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 198 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 199 | if ((handlers_sets & SET_RESET_TERM) != 0) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 200 | /* sparc and other have broken termios support: use old termio handling. */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 201 | setTermSettings(fileno(stdin), (void *) &initial_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 202 | handlers_sets &= ~SET_RESET_TERM; |
| 203 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 204 | if ((handlers_sets & SET_WCHG_HANDLERS) != 0) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 205 | /* reset SIGWINCH handler to previous (default) */ |
| 206 | win_changed(0); |
| 207 | handlers_sets &= ~SET_WCHG_HANDLERS; |
| 208 | } |
| 209 | fflush(stdout); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 210 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Eric Andersen | b040d4f | 2000-07-25 18:01:20 +0000 | [diff] [blame] | 211 | if (his_front) { |
| 212 | struct history *n; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 213 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 214 | while (his_front != his_end) { |
Eric Andersen | b040d4f | 2000-07-25 18:01:20 +0000 | [diff] [blame] | 215 | n = his_front->n; |
| 216 | free(his_front->s); |
| 217 | free(his_front); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 218 | his_front = n; |
Eric Andersen | b040d4f | 2000-07-25 18:01:20 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | #endif |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 224 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 225 | /* special for recount position for scroll and remove terminal margin effect */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 226 | static void cmdedit_set_out_char(int next_char) |
| 227 | { |
| 228 | |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 229 | int c = (int)((unsigned char) command_ps[cursor]); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 230 | |
| 231 | if (c == 0) |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 232 | c = ' '; /* destroy end char? */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 233 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 234 | if (!Isprint(c)) { /* Inverse put non-printable characters */ |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 235 | if (c >= 128) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 236 | c -= 128; |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 237 | if (c < ' ') |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 238 | c += '@'; |
| 239 | if (c == 127) |
| 240 | c = '?'; |
| 241 | printf("\033[7m%c\033[0m", c); |
| 242 | } else |
| 243 | #endif |
| 244 | putchar(c); |
| 245 | if (++cmdedit_x >= cmdedit_termw) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 246 | /* terminal is scrolled down */ |
| 247 | cmdedit_y++; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 248 | cmdedit_x = 0; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 249 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 250 | if (!next_char) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 251 | next_char = ' '; |
| 252 | /* destroy "(auto)margin" */ |
| 253 | putchar(next_char); |
| 254 | putchar('\b'); |
| 255 | } |
| 256 | cursor++; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 259 | /* Move to end line. Bonus: rewrite line from cursor */ |
| 260 | static void input_end(void) |
| 261 | { |
| 262 | while (cursor < len) |
| 263 | cmdedit_set_out_char(0); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /* Go to the next line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 267 | static void goto_new_line(void) |
| 268 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 269 | input_end(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 270 | if (cmdedit_x) |
| 271 | putchar('\n'); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 275 | static inline void out1str(const char *s) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 276 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 277 | fputs(s, stdout); |
| 278 | } |
| 279 | static inline void beep(void) |
| 280 | { |
| 281 | putchar('\007'); |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 284 | /* Move back one charactor */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 285 | /* special for slow terminal */ |
| 286 | static void input_backward(int num) |
| 287 | { |
| 288 | if (num > cursor) |
| 289 | num = cursor; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 290 | cursor -= num; /* new cursor (in command, not terminal) */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 291 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 292 | if (cmdedit_x >= num) { /* no to up line */ |
| 293 | cmdedit_x -= num; |
| 294 | if (num < 4) |
| 295 | while (num-- > 0) |
| 296 | putchar('\b'); |
| 297 | |
| 298 | else |
| 299 | printf("\033[%dD", num); |
| 300 | } else { |
| 301 | int count_y; |
| 302 | |
| 303 | if (cmdedit_x) { |
| 304 | putchar('\r'); /* back to first terminal pos. */ |
| 305 | num -= cmdedit_x; /* set previous backward */ |
| 306 | } |
| 307 | count_y = 1 + num / cmdedit_termw; |
| 308 | printf("\033[%dA", count_y); |
| 309 | cmdedit_y -= count_y; |
| 310 | /* require forward after uping */ |
| 311 | cmdedit_x = cmdedit_termw * count_y - num; |
| 312 | printf("\033[%dC", cmdedit_x); /* set term cursor */ |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 313 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 316 | static void put_prompt(void) |
| 317 | { |
| 318 | out1str(cmdedit_prompt); |
| 319 | cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */ |
| 320 | cursor = 0; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 321 | cmdedit_y = 0; /* new quasireal y */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 324 | #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 325 | static void parse_prompt(const char *prmt_ptr) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 326 | { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 327 | cmdedit_prompt = prmt_ptr; |
| 328 | cmdedit_prmt_len = strlen(prmt_ptr); |
| 329 | put_prompt(); |
| 330 | } |
| 331 | #else |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 332 | static void parse_prompt(const char *prmt_ptr) |
| 333 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 334 | int prmt_len = 0; |
| 335 | int sub_len = 0; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 336 | char flg_not_length = '['; |
| 337 | char *prmt_mem_ptr = xcalloc(1, 1); |
| 338 | char *pwd_buf = xgetcwd(0); |
| 339 | char buf2[PATH_MAX + 1]; |
| 340 | char buf[2]; |
| 341 | char c; |
| 342 | char *pbuf; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 343 | |
Eric Andersen | 5f265b7 | 2001-05-11 16:58:46 +0000 | [diff] [blame] | 344 | if (!pwd_buf) { |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 345 | pwd_buf=(char *)unknown; |
Eric Andersen | 5f265b7 | 2001-05-11 16:58:46 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 348 | while (*prmt_ptr) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 349 | pbuf = buf; |
| 350 | pbuf[1] = 0; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 351 | c = *prmt_ptr++; |
| 352 | if (c == '\\') { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 353 | const char *cp = prmt_ptr; |
| 354 | int l; |
| 355 | |
| 356 | c = process_escape_sequence(&prmt_ptr); |
| 357 | if(prmt_ptr==cp) { |
| 358 | if (*cp == 0) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 359 | break; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 360 | c = *prmt_ptr++; |
| 361 | switch (c) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 362 | #ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 363 | case 'u': |
| 364 | pbuf = user_buf; |
| 365 | break; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 366 | #endif |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 367 | case 'h': |
| 368 | pbuf = hostname_buf; |
| 369 | if (*pbuf == 0) { |
| 370 | pbuf = xcalloc(256, 1); |
| 371 | if (gethostname(pbuf, 255) < 0) { |
| 372 | strcpy(pbuf, "?"); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 373 | } else { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 374 | char *s = strchr(pbuf, '.'); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 375 | |
| 376 | if (s) |
| 377 | *s = 0; |
| 378 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 379 | hostname_buf = pbuf; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 380 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 381 | break; |
| 382 | case '$': |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 383 | c = my_euid == 0 ? '#' : '$'; |
| 384 | break; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 385 | #ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 386 | case 'w': |
| 387 | pbuf = pwd_buf; |
| 388 | l = strlen(home_pwd_buf); |
| 389 | if (home_pwd_buf[0] != 0 && |
| 390 | strncmp(home_pwd_buf, pbuf, l) == 0 && |
| 391 | (pbuf[l]=='/' || pbuf[l]=='\0') && |
| 392 | strlen(pwd_buf+l)<PATH_MAX) { |
| 393 | pbuf = buf2; |
| 394 | *pbuf = '~'; |
| 395 | strcpy(pbuf+1, pwd_buf+l); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 396 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 397 | break; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 398 | #endif |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 399 | case 'W': |
| 400 | pbuf = pwd_buf; |
| 401 | cp = strrchr(pbuf,'/'); |
| 402 | if ( (cp != NULL) && (cp != pbuf) ) |
| 403 | pbuf += (cp-pbuf)+1; |
| 404 | break; |
| 405 | case '!': |
| 406 | snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines); |
| 407 | break; |
| 408 | case 'e': case 'E': /* \e \E = \033 */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 409 | c = '\033'; |
| 410 | break; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 411 | case 'x': case 'X': |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 412 | for (l = 0; l < 3;) { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 413 | int h; |
| 414 | buf2[l++] = *prmt_ptr; |
| 415 | buf2[l] = 0; |
| 416 | h = strtol(buf2, &pbuf, 16); |
| 417 | if (h > UCHAR_MAX || (pbuf - buf2) < l) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 418 | l--; |
| 419 | break; |
| 420 | } |
| 421 | prmt_ptr++; |
| 422 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 423 | buf2[l] = 0; |
| 424 | c = (char)strtol(buf2, 0, 16); |
| 425 | if(c==0) |
| 426 | c = '?'; |
| 427 | pbuf = buf; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 428 | break; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 429 | case '[': case ']': |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 430 | if (c == flg_not_length) { |
| 431 | flg_not_length = flg_not_length == '[' ? ']' : '['; |
| 432 | continue; |
| 433 | } |
| 434 | break; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 437 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 438 | if(pbuf == buf) |
| 439 | *pbuf = c; |
| 440 | prmt_len += strlen(pbuf); |
| 441 | prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 442 | if (flg_not_length == ']') |
| 443 | sub_len++; |
| 444 | } |
Eric Andersen | 8f69784 | 2001-07-02 15:36:57 +0000 | [diff] [blame] | 445 | if(pwd_buf!=(char *)unknown) |
| 446 | free(pwd_buf); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 447 | cmdedit_prompt = prmt_mem_ptr; |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 448 | cmdedit_prmt_len = prmt_len - sub_len; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 449 | put_prompt(); |
| 450 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 451 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 452 | |
| 453 | |
| 454 | /* draw promt, editor line, and clear tail */ |
| 455 | static void redraw(int y, int back_cursor) |
| 456 | { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 457 | if (y > 0) /* up to start y */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 458 | printf("\033[%dA", y); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 459 | putchar('\r'); |
| 460 | put_prompt(); |
| 461 | input_end(); /* rewrite */ |
| 462 | printf("\033[J"); /* destroy tail after cursor */ |
| 463 | input_backward(back_cursor); |
| 464 | } |
| 465 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 466 | /* Delete the char in front of the cursor */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 467 | static void input_delete(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 468 | { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 469 | int j = cursor; |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 470 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 471 | if (j == len) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 472 | return; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 473 | |
| 474 | strcpy(command_ps + j, command_ps + j + 1); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 475 | len--; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 476 | input_end(); /* rewtite new line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 477 | cmdedit_set_out_char(0); /* destroy end char */ |
| 478 | input_backward(cursor - j); /* back to old pos cursor */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 481 | /* Delete the char in back of the cursor */ |
| 482 | static void input_backspace(void) |
| 483 | { |
| 484 | if (cursor > 0) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 485 | input_backward(1); |
| 486 | input_delete(); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
| 490 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 491 | /* Move forward one charactor */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 492 | static void input_forward(void) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 493 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 494 | if (cursor < len) |
| 495 | cmdedit_set_out_char(command_ps[cursor + 1]); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 499 | static void cmdedit_setwidth(int w, int redraw_flg) |
| 500 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 501 | cmdedit_termw = cmdedit_prmt_len + 2; |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 502 | if (w <= cmdedit_termw) { |
| 503 | cmdedit_termw = cmdedit_termw % w; |
| 504 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 505 | if (w > cmdedit_termw) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 506 | cmdedit_termw = w; |
| 507 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 508 | if (redraw_flg) { |
| 509 | /* new y for current cursor */ |
| 510 | int new_y = (cursor + cmdedit_prmt_len) / w; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 511 | |
| 512 | /* redraw */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 513 | redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor); |
| 514 | fflush(stdout); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 515 | } |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 516 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 519 | static void cmdedit_init(void) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 520 | { |
Eric Andersen | 61173a5 | 2001-03-19 17:48:55 +0000 | [diff] [blame] | 521 | cmdedit_prmt_len = 0; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 522 | if ((handlers_sets & SET_WCHG_HANDLERS) == 0) { |
| 523 | /* emulate usage handler to set handler and call yours work */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 524 | win_changed(-SIGWINCH); |
| 525 | handlers_sets |= SET_WCHG_HANDLERS; |
| 526 | } |
| 527 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 528 | if ((handlers_sets & SET_ATEXIT) == 0) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 529 | #ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 530 | struct passwd *entry; |
| 531 | |
| 532 | my_euid = geteuid(); |
| 533 | entry = getpwuid(my_euid); |
| 534 | if (entry) { |
| 535 | user_buf = xstrdup(entry->pw_name); |
| 536 | home_pwd_buf = xstrdup(entry->pw_dir); |
| 537 | } |
| 538 | #endif |
| 539 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 540 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 541 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 542 | #ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 543 | my_euid = geteuid(); |
| 544 | #endif |
| 545 | my_uid = getuid(); |
| 546 | my_gid = getgid(); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 547 | #endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 548 | handlers_sets |= SET_ATEXIT; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 549 | atexit(cmdedit_reset_term); /* be sure to do this only once */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 550 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 551 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 552 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 553 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 554 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 555 | static int is_execute(const struct stat *st) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 556 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 557 | if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) || |
| 558 | (my_uid == st->st_uid && (st->st_mode & S_IXUSR)) || |
| 559 | (my_gid == st->st_gid && (st->st_mode & S_IXGRP)) || |
| 560 | (st->st_mode & S_IXOTH)) return TRUE; |
| 561 | return FALSE; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 562 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 563 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 564 | #ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 565 | |
| 566 | static char **username_tab_completion(char *ud, int *num_matches) |
| 567 | { |
| 568 | struct passwd *entry; |
| 569 | int userlen; |
| 570 | char *temp; |
| 571 | |
| 572 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 573 | ud++; /* ~user/... to user/... */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 574 | userlen = strlen(ud); |
| 575 | |
| 576 | if (num_matches == 0) { /* "~/..." or "~user/..." */ |
| 577 | char *sav_ud = ud - 1; |
| 578 | char *home = 0; |
| 579 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 580 | if (*ud == '/') { /* "~/..." */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 581 | home = home_pwd_buf; |
| 582 | } else { |
| 583 | /* "~user/..." */ |
| 584 | temp = strchr(ud, '/'); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 585 | *temp = 0; /* ~user\0 */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 586 | entry = getpwnam(ud); |
| 587 | *temp = '/'; /* restore ~user/... */ |
| 588 | ud = temp; |
| 589 | if (entry) |
| 590 | home = entry->pw_dir; |
| 591 | } |
| 592 | if (home) { |
| 593 | if ((userlen + strlen(home) + 1) < BUFSIZ) { |
| 594 | char temp2[BUFSIZ]; /* argument size */ |
| 595 | |
| 596 | /* /home/user/... */ |
| 597 | sprintf(temp2, "%s%s", home, ud); |
| 598 | strcpy(sav_ud, temp2); |
| 599 | } |
| 600 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 601 | return 0; /* void, result save to argument :-) */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 602 | } else { |
| 603 | /* "~[^/]*" */ |
| 604 | char **matches = (char **) NULL; |
| 605 | int nm = 0; |
| 606 | |
| 607 | setpwent(); |
| 608 | |
| 609 | while ((entry = getpwent()) != NULL) { |
| 610 | /* Null usernames should result in all users as possible completions. */ |
| 611 | if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) { |
| 612 | |
| 613 | temp = xmalloc(3 + strlen(entry->pw_name)); |
| 614 | sprintf(temp, "~%s/", entry->pw_name); |
| 615 | matches = xrealloc(matches, (nm + 1) * sizeof(char *)); |
| 616 | |
| 617 | matches[nm++] = temp; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | endpwent(); |
| 622 | (*num_matches) = nm; |
| 623 | return (matches); |
| 624 | } |
| 625 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 626 | #endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 627 | |
| 628 | enum { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 629 | FIND_EXE_ONLY = 0, |
| 630 | FIND_DIR_ONLY = 1, |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 631 | FIND_FILE_ONLY = 2, |
| 632 | }; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 633 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 634 | static int path_parse(char ***p, int flags) |
| 635 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 636 | int npth; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 637 | char *tmp; |
| 638 | char *pth; |
| 639 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 640 | /* if not setenv PATH variable, to search cur dir "." */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 641 | if (flags != FIND_EXE_ONLY || (pth = getenv("PATH")) == 0 || |
| 642 | /* PATH=<empty> or PATH=:<empty> */ |
| 643 | *pth == 0 || (*pth == ':' && *(pth + 1) == 0)) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 644 | return 1; |
| 645 | } |
| 646 | |
| 647 | tmp = pth; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 648 | npth = 0; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 649 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 650 | for (;;) { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 651 | npth++; /* count words is + 1 count ':' */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 652 | tmp = strchr(tmp, ':'); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 653 | if (tmp) { |
| 654 | if (*++tmp == 0) |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 655 | break; /* :<empty> */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 656 | } else |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 657 | break; |
| 658 | } |
| 659 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 660 | *p = xmalloc(npth * sizeof(char *)); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 661 | |
| 662 | tmp = pth; |
| 663 | (*p)[0] = xstrdup(tmp); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 664 | npth = 1; /* count words is + 1 count ':' */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 665 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 666 | for (;;) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 667 | tmp = strchr(tmp, ':'); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 668 | if (tmp) { |
| 669 | (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */ |
| 670 | if (*++tmp == 0) |
| 671 | break; /* :<empty> */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 672 | } else |
| 673 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 674 | (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | return npth; |
| 678 | } |
| 679 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 680 | static char *add_quote_for_spec_chars(char *found) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 681 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 682 | int l = 0; |
| 683 | char *s = xmalloc((strlen(found) + 1) * 2); |
| 684 | |
| 685 | while (*found) { |
| 686 | if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found)) |
| 687 | s[l++] = '\\'; |
| 688 | s[l++] = *found++; |
| 689 | } |
| 690 | s[l] = 0; |
| 691 | return s; |
| 692 | } |
| 693 | |
| 694 | static char **exe_n_cwd_tab_completion(char *command, int *num_matches, |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 695 | int type) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 696 | { |
| 697 | |
| 698 | char **matches = 0; |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 699 | DIR *dir; |
| 700 | struct dirent *next; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 701 | char dirbuf[BUFSIZ]; |
| 702 | int nm = *num_matches; |
| 703 | struct stat st; |
| 704 | char *path1[1]; |
| 705 | char **paths = path1; |
| 706 | int npaths; |
| 707 | int i; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 708 | char *found; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 709 | char *pfind = strrchr(command, '/'); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 710 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 711 | path1[0] = "."; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 712 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 713 | if (pfind == NULL) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 714 | /* no dir, if flags==EXE_ONLY - get paths, else "." */ |
| 715 | npaths = path_parse(&paths, type); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 716 | pfind = command; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 717 | } else { |
| 718 | /* with dir */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 719 | /* save for change */ |
| 720 | strcpy(dirbuf, command); |
| 721 | /* set dir only */ |
| 722 | dirbuf[(pfind - command) + 1] = 0; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 723 | #ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 724 | if (dirbuf[0] == '~') /* ~/... or ~user/... */ |
| 725 | username_tab_completion(dirbuf, 0); |
| 726 | #endif |
| 727 | /* "strip" dirname in command */ |
| 728 | pfind++; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 729 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 730 | paths[0] = dirbuf; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 731 | npaths = 1; /* only 1 dir */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 732 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 733 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 734 | for (i = 0; i < npaths; i++) { |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 735 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 736 | dir = opendir(paths[i]); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 737 | if (!dir) /* Don't print an error */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 738 | continue; |
| 739 | |
| 740 | while ((next = readdir(dir)) != NULL) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 741 | char *str_found = next->d_name; |
| 742 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 743 | /* matched ? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 744 | if (strncmp(str_found, pfind, strlen(pfind))) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 745 | continue; |
| 746 | /* not see .name without .match */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 747 | if (*str_found == '.' && *pfind == 0) { |
| 748 | if (*paths[i] == '/' && paths[i][1] == 0 |
| 749 | && str_found[1] == 0) str_found = ""; /* only "/" */ |
| 750 | else |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 751 | continue; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 752 | } |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 753 | found = concat_path_file(paths[i], str_found); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 754 | /* hmm, remover in progress? */ |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 755 | if (stat(found, &st) < 0) |
| 756 | goto cont; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 757 | /* find with dirs ? */ |
| 758 | if (paths[i] != dirbuf) |
| 759 | strcpy(found, next->d_name); /* only name */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 760 | if (S_ISDIR(st.st_mode)) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 761 | /* name is directory */ |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 762 | str_found = found; |
| 763 | found = concat_path_file(found, ""); |
| 764 | free(str_found); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 765 | str_found = add_quote_for_spec_chars(found); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 766 | } else { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 767 | /* not put found file if search only dirs for cd */ |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 768 | if (type == FIND_DIR_ONLY) |
| 769 | goto cont; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 770 | str_found = add_quote_for_spec_chars(found); |
| 771 | if (type == FIND_FILE_ONLY || |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 772 | (type == FIND_EXE_ONLY && is_execute(&st))) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 773 | strcat(str_found, " "); |
| 774 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 775 | /* Add it to the list */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 776 | matches = xrealloc(matches, (nm + 1) * sizeof(char *)); |
| 777 | |
| 778 | matches[nm++] = str_found; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 779 | cont: |
| 780 | free(found); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 781 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 782 | closedir(dir); |
Erik Andersen | 1dbe340 | 2000-03-19 10:46:06 +0000 | [diff] [blame] | 783 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 784 | if (paths != path1) { |
| 785 | free(paths[0]); /* allocated memory only in first member */ |
| 786 | free(paths); |
| 787 | } |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 788 | *num_matches = nm; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 789 | return (matches); |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 790 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 791 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 792 | static int match_compare(const void *a, const void *b) |
| 793 | { |
| 794 | return strcmp(*(char **) a, *(char **) b); |
| 795 | } |
| 796 | |
| 797 | |
| 798 | |
| 799 | #define QUOT (UCHAR_MAX+1) |
| 800 | |
| 801 | #define collapse_pos(is, in) { \ |
| 802 | memcpy(int_buf+is, int_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); \ |
| 803 | memcpy(pos_buf+is, pos_buf+in, (BUFSIZ+1-is-in)*sizeof(int)); } |
| 804 | |
| 805 | static int find_match(char *matchBuf, int *len_with_quotes) |
| 806 | { |
| 807 | int i, j; |
| 808 | int command_mode; |
| 809 | int c, c2; |
| 810 | int int_buf[BUFSIZ + 1]; |
| 811 | int pos_buf[BUFSIZ + 1]; |
| 812 | |
| 813 | /* set to integer dimension characters and own positions */ |
| 814 | for (i = 0;; i++) { |
| 815 | int_buf[i] = (int) ((unsigned char) matchBuf[i]); |
| 816 | if (int_buf[i] == 0) { |
| 817 | pos_buf[i] = -1; /* indicator end line */ |
| 818 | break; |
| 819 | } else |
| 820 | pos_buf[i] = i; |
| 821 | } |
| 822 | |
| 823 | /* mask \+symbol and convert '\t' to ' ' */ |
| 824 | for (i = j = 0; matchBuf[i]; i++, j++) |
| 825 | if (matchBuf[i] == '\\') { |
| 826 | collapse_pos(j, j + 1); |
| 827 | int_buf[j] |= QUOT; |
| 828 | i++; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 829 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 830 | if (matchBuf[i] == '\t') /* algorithm equivalent */ |
| 831 | int_buf[j] = ' ' | QUOT; |
| 832 | #endif |
| 833 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 834 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 835 | else if (matchBuf[i] == '\t') |
| 836 | int_buf[j] = ' '; |
| 837 | #endif |
| 838 | |
| 839 | /* mask "symbols" or 'symbols' */ |
| 840 | c2 = 0; |
| 841 | for (i = 0; int_buf[i]; i++) { |
| 842 | c = int_buf[i]; |
| 843 | if (c == '\'' || c == '"') { |
| 844 | if (c2 == 0) |
| 845 | c2 = c; |
| 846 | else { |
| 847 | if (c == c2) |
| 848 | c2 = 0; |
| 849 | else |
| 850 | int_buf[i] |= QUOT; |
| 851 | } |
| 852 | } else if (c2 != 0 && c != '$') |
| 853 | int_buf[i] |= QUOT; |
| 854 | } |
| 855 | |
| 856 | /* skip commands with arguments if line have commands delimiters */ |
| 857 | /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */ |
| 858 | for (i = 0; int_buf[i]; i++) { |
| 859 | c = int_buf[i]; |
| 860 | c2 = int_buf[i + 1]; |
| 861 | j = i ? int_buf[i - 1] : -1; |
| 862 | command_mode = 0; |
| 863 | if (c == ';' || c == '&' || c == '|') { |
| 864 | command_mode = 1 + (c == c2); |
| 865 | if (c == '&') { |
| 866 | if (j == '>' || j == '<') |
| 867 | command_mode = 0; |
| 868 | } else if (c == '|' && j == '>') |
| 869 | command_mode = 0; |
| 870 | } |
| 871 | if (command_mode) { |
| 872 | collapse_pos(0, i + command_mode); |
| 873 | i = -1; /* hack incremet */ |
| 874 | } |
| 875 | } |
| 876 | /* collapse `command...` */ |
| 877 | for (i = 0; int_buf[i]; i++) |
| 878 | if (int_buf[i] == '`') { |
| 879 | for (j = i + 1; int_buf[j]; j++) |
| 880 | if (int_buf[j] == '`') { |
| 881 | collapse_pos(i, j + 1); |
| 882 | j = 0; |
| 883 | break; |
| 884 | } |
| 885 | if (j) { |
| 886 | /* not found close ` - command mode, collapse all previous */ |
| 887 | collapse_pos(0, i + 1); |
| 888 | break; |
| 889 | } else |
| 890 | i--; /* hack incremet */ |
| 891 | } |
| 892 | |
| 893 | /* collapse (command...(command...)...) or {command...{command...}...} */ |
| 894 | c = 0; /* "recursive" level */ |
| 895 | c2 = 0; |
| 896 | for (i = 0; int_buf[i]; i++) |
| 897 | if (int_buf[i] == '(' || int_buf[i] == '{') { |
| 898 | if (int_buf[i] == '(') |
| 899 | c++; |
| 900 | else |
| 901 | c2++; |
| 902 | collapse_pos(0, i + 1); |
| 903 | i = -1; /* hack incremet */ |
| 904 | } |
| 905 | for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++) |
| 906 | if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) { |
| 907 | if (int_buf[i] == ')') |
| 908 | c--; |
| 909 | else |
| 910 | c2--; |
| 911 | collapse_pos(0, i + 1); |
| 912 | i = -1; /* hack incremet */ |
| 913 | } |
| 914 | |
| 915 | /* skip first not quote space */ |
| 916 | for (i = 0; int_buf[i]; i++) |
| 917 | if (int_buf[i] != ' ') |
| 918 | break; |
| 919 | if (i) |
| 920 | collapse_pos(0, i); |
| 921 | |
| 922 | /* set find mode for completion */ |
| 923 | command_mode = FIND_EXE_ONLY; |
| 924 | for (i = 0; int_buf[i]; i++) |
| 925 | if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') { |
| 926 | if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 927 | && matchBuf[pos_buf[0]]=='c' |
| 928 | && matchBuf[pos_buf[1]]=='d' ) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 929 | command_mode = FIND_DIR_ONLY; |
| 930 | else { |
| 931 | command_mode = FIND_FILE_ONLY; |
| 932 | break; |
| 933 | } |
| 934 | } |
| 935 | /* "strlen" */ |
| 936 | for (i = 0; int_buf[i]; i++); |
| 937 | /* find last word */ |
| 938 | for (--i; i >= 0; i--) { |
| 939 | c = int_buf[i]; |
| 940 | if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') { |
| 941 | collapse_pos(0, i + 1); |
| 942 | break; |
| 943 | } |
| 944 | } |
| 945 | /* skip first not quoted '\'' or '"' */ |
| 946 | for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++); |
| 947 | /* collapse quote or unquote // or /~ */ |
Mark Whitley | 7e5291f | 2001-03-08 19:31:12 +0000 | [diff] [blame] | 948 | while ((int_buf[i] & ~QUOT) == '/' && |
| 949 | ((int_buf[i + 1] & ~QUOT) == '/' |
| 950 | || (int_buf[i + 1] & ~QUOT) == '~')) { |
| 951 | i++; |
| 952 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 953 | |
| 954 | /* set only match and destroy quotes */ |
| 955 | j = 0; |
Eric Andersen | 4f99053 | 2001-05-31 17:15:57 +0000 | [diff] [blame] | 956 | for (c = 0; pos_buf[i] >= 0; i++) { |
| 957 | matchBuf[c++] = matchBuf[pos_buf[i]]; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 958 | j = pos_buf[i] + 1; |
| 959 | } |
Eric Andersen | 4f99053 | 2001-05-31 17:15:57 +0000 | [diff] [blame] | 960 | matchBuf[c] = 0; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 961 | /* old lenght matchBuf with quotes symbols */ |
| 962 | *len_with_quotes = j ? j - pos_buf[0] : 0; |
| 963 | |
| 964 | return command_mode; |
| 965 | } |
| 966 | |
| 967 | |
| 968 | static void input_tab(int *lastWasTab) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 969 | { |
| 970 | /* Do TAB completion */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 971 | static int num_matches; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 972 | static char **matches; |
| 973 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 974 | if (lastWasTab == 0) { /* free all memory */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 975 | if (matches) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 976 | while (num_matches > 0) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 977 | free(matches[--num_matches]); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 978 | free(matches); |
| 979 | matches = (char **) NULL; |
| 980 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 981 | return; |
| 982 | } |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 983 | if (! *lastWasTab) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 984 | |
| 985 | char *tmp; |
| 986 | int len_found; |
| 987 | char matchBuf[BUFSIZ]; |
| 988 | int find_type; |
| 989 | int recalc_pos; |
| 990 | |
| 991 | *lastWasTab = TRUE; /* flop trigger */ |
| 992 | |
| 993 | /* Make a local copy of the string -- up |
| 994 | * to the position of the cursor */ |
| 995 | tmp = strncpy(matchBuf, command_ps, cursor); |
| 996 | tmp[cursor] = 0; |
| 997 | |
| 998 | find_type = find_match(matchBuf, &recalc_pos); |
| 999 | |
| 1000 | /* Free up any memory already allocated */ |
| 1001 | input_tab(0); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1002 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1003 | #ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1004 | /* 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] | 1005 | * then try completing this word as a username. */ |
| 1006 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1007 | if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1008 | matches = username_tab_completion(matchBuf, &num_matches); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1009 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1010 | /* Try to match any executable in our path and everything |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1011 | * in the current working directory that matches. */ |
| 1012 | if (!matches) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1013 | matches = |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 1014 | exe_n_cwd_tab_completion(matchBuf, |
| 1015 | &num_matches, find_type); |
| 1016 | /* Remove duplicate found */ |
| 1017 | if(matches) { |
| 1018 | int i, j; |
| 1019 | /* bubble */ |
| 1020 | for(i=0; i<(num_matches-1); i++) |
| 1021 | for(j=i+1; j<num_matches; j++) |
| 1022 | if(matches[i]!=0 && matches[j]!=0 && |
| 1023 | strcmp(matches[i], matches[j])==0) { |
| 1024 | free(matches[j]); |
| 1025 | matches[j]=0; |
| 1026 | } |
| 1027 | j=num_matches; |
| 1028 | num_matches = 0; |
| 1029 | for(i=0; i<j; i++) |
| 1030 | if(matches[i]) { |
| 1031 | if(!strcmp(matches[i], "./")) |
| 1032 | matches[i][1]=0; |
| 1033 | else if(!strcmp(matches[i], "../")) |
| 1034 | matches[i][2]=0; |
| 1035 | matches[num_matches++]=matches[i]; |
| 1036 | } |
| 1037 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1038 | /* Did we find exactly one match? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1039 | if (!matches || num_matches > 1) { |
| 1040 | char *tmp1; |
| 1041 | |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1042 | beep(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1043 | if (!matches) |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1044 | return; /* not found */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1045 | /* sort */ |
| 1046 | qsort(matches, num_matches, sizeof(char *), match_compare); |
| 1047 | |
| 1048 | /* find minimal match */ |
| 1049 | tmp = xstrdup(matches[0]); |
| 1050 | for (tmp1 = tmp; *tmp1; tmp1++) |
| 1051 | for (len_found = 1; len_found < num_matches; len_found++) |
| 1052 | if (matches[len_found][(tmp1 - tmp)] != *tmp1) { |
| 1053 | *tmp1 = 0; |
| 1054 | break; |
| 1055 | } |
| 1056 | if (*tmp == 0) { /* have unique */ |
| 1057 | free(tmp); |
| 1058 | return; |
| 1059 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1060 | } else { /* one match */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1061 | tmp = matches[0]; |
| 1062 | /* for next completion current found */ |
| 1063 | *lastWasTab = FALSE; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1066 | len_found = strlen(tmp); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1067 | /* have space to placed match? */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1068 | if ((len_found - strlen(matchBuf) + len) < BUFSIZ) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1069 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1070 | /* before word for match */ |
| 1071 | command_ps[cursor - recalc_pos] = 0; |
| 1072 | /* save tail line */ |
| 1073 | strcpy(matchBuf, command_ps + cursor); |
| 1074 | /* add match */ |
| 1075 | strcat(command_ps, tmp); |
| 1076 | /* add tail */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1077 | strcat(command_ps, matchBuf); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1078 | /* back to begin word for match */ |
| 1079 | input_backward(recalc_pos); |
| 1080 | /* new pos */ |
| 1081 | recalc_pos = cursor + len_found; |
| 1082 | /* new len */ |
| 1083 | len = strlen(command_ps); |
| 1084 | /* write out the matched command */ |
Eric Andersen | 4f99053 | 2001-05-31 17:15:57 +0000 | [diff] [blame] | 1085 | redraw(cmdedit_y, len - recalc_pos); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1086 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1087 | if (tmp != matches[0]) |
| 1088 | free(tmp); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1089 | } else { |
| 1090 | /* Ok -- the last char was a TAB. Since they |
| 1091 | * just hit TAB again, print a list of all the |
| 1092 | * available choices... */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1093 | if (matches && num_matches > 0) { |
| 1094 | int i, col, l; |
| 1095 | int sav_cursor = cursor; /* change goto_new_line() */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1096 | |
| 1097 | /* Go to the next line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1098 | goto_new_line(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1099 | for (i = 0, col = 0; i < num_matches; i++) { |
| 1100 | l = strlen(matches[i]); |
| 1101 | if (l < 14) |
| 1102 | l = 14; |
| 1103 | printf("%-14s ", matches[i]); |
| 1104 | if ((l += 2) > 16) |
| 1105 | while (l % 16) { |
| 1106 | putchar(' '); |
| 1107 | l++; |
| 1108 | } |
| 1109 | col += l; |
| 1110 | col -= (col / cmdedit_termw) * cmdedit_termw; |
| 1111 | if (col > 60 && matches[i + 1] != NULL) { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1112 | putchar('\n'); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1113 | col = 0; |
| 1114 | } |
| 1115 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1116 | /* Go to the next line and rewrite */ |
| 1117 | putchar('\n'); |
| 1118 | redraw(0, len - sav_cursor); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1122 | #endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1123 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1124 | static void get_previous_history(struct history **hp, struct history *p) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1125 | { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 1126 | if ((*hp)->s) |
| 1127 | free((*hp)->s); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1128 | (*hp)->s = xstrdup(command_ps); |
| 1129 | *hp = p; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1132 | static inline void get_next_history(struct history **hp) |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1133 | { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1134 | get_previous_history(hp, (*hp)->n); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1137 | enum { |
| 1138 | ESC = 27, |
| 1139 | DEL = 127, |
| 1140 | }; |
| 1141 | |
| 1142 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1143 | /* |
| 1144 | * This function is used to grab a character buffer |
| 1145 | * from the input file descriptor and allows you to |
| 1146 | * a string with full command editing (sortof like |
| 1147 | * a mini readline). |
| 1148 | * |
| 1149 | * The following standard commands are not implemented: |
| 1150 | * ESC-b -- Move back one word |
| 1151 | * ESC-f -- Move forward one word |
| 1152 | * ESC-d -- Delete back one word |
| 1153 | * ESC-h -- Delete forward one word |
| 1154 | * CTL-t -- Transpose two characters |
| 1155 | * |
| 1156 | * Furthermore, the "vi" command editing keys are not implemented. |
| 1157 | * |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1158 | */ |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1159 | |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 1160 | |
| 1161 | int cmdedit_read_input(char *prompt, char command[BUFSIZ]) |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1162 | { |
| 1163 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1164 | int break_out = 0; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1165 | int lastWasTab = FALSE; |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 1166 | unsigned char c = 0; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1167 | struct history *hp = his_end; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1168 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1169 | /* prepare before init handlers */ |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1170 | cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1171 | len = 0; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1172 | command_ps = command; |
| 1173 | |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 1174 | getTermSettings(0, (void *) &initial_settings); |
| 1175 | memcpy(&new_settings, &initial_settings, sizeof(struct termios)); |
| 1176 | new_settings.c_lflag &= ~ICANON; /* unbuffered input */ |
| 1177 | /* Turn off echoing and CTRL-C, so we can trap it */ |
| 1178 | new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 1179 | #ifndef linux |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 1180 | /* Hmm, in linux c_cc[] not parsed if set ~ICANON */ |
| 1181 | new_settings.c_cc[VMIN] = 1; |
| 1182 | new_settings.c_cc[VTIME] = 0; |
| 1183 | /* Turn off CTRL-C, so we can trap it */ |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 1184 | # ifndef _POSIX_VDISABLE |
| 1185 | # define _POSIX_VDISABLE '\0' |
| 1186 | # endif |
Eric Andersen | 3450636 | 2001-08-02 05:02:46 +0000 | [diff] [blame] | 1187 | new_settings.c_cc[VINTR] = _POSIX_VDISABLE; |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 1188 | #endif |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1189 | command[0] = 0; |
| 1190 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 1191 | setTermSettings(0, (void *) &new_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1192 | handlers_sets |= SET_RESET_TERM; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1193 | |
Eric Andersen | 6faae7d | 2001-02-16 20:09:17 +0000 | [diff] [blame] | 1194 | /* Now initialize things */ |
| 1195 | cmdedit_init(); |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 1196 | /* Print out the command prompt */ |
| 1197 | parse_prompt(prompt); |
Eric Andersen | b3dc3b8 | 2001-01-04 11:08:45 +0000 | [diff] [blame] | 1198 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1199 | while (1) { |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1200 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1201 | fflush(stdout); /* buffered out to fast */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1202 | |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1203 | if (safe_read(0, &c, 1) < 1) |
Eric Andersen | ed424db | 2001-04-23 15:28:28 +0000 | [diff] [blame] | 1204 | /* if we can't read input then exit */ |
| 1205 | goto prepare_to_die; |
Erik Andersen | f3b3d17 | 2000-04-09 18:24:05 +0000 | [diff] [blame] | 1206 | |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1207 | switch (c) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1208 | case '\n': |
| 1209 | case '\r': |
| 1210 | /* Enter */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1211 | goto_new_line(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1212 | break_out = 1; |
| 1213 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1214 | case 1: |
| 1215 | /* Control-a -- Beginning of line */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1216 | input_backward(cursor); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1217 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1218 | case 2: |
| 1219 | /* Control-b -- Move back one character */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1220 | input_backward(1); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1221 | break; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1222 | case 3: |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1223 | /* Control-c -- stop gathering input */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1224 | goto_new_line(); |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1225 | command[0] = 0; |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1226 | len = 0; |
| 1227 | lastWasTab = FALSE; |
| 1228 | put_prompt(); |
| 1229 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1230 | case 4: |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1231 | /* Control-d -- Delete one character, or exit |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1232 | * if the len=0 and no chars to delete */ |
| 1233 | if (len == 0) { |
Eric Andersen | ed424db | 2001-04-23 15:28:28 +0000 | [diff] [blame] | 1234 | prepare_to_die: |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1235 | #if !defined(CONFIG_ASH) |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1236 | printf("exit"); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1237 | goto_new_line(); |
| 1238 | /* cmdedit_reset_term() called in atexit */ |
| 1239 | exit(EXIT_SUCCESS); |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 1240 | #else |
| 1241 | break_out = -1; /* for control stoped jobs */ |
| 1242 | break; |
| 1243 | #endif |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1244 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1245 | input_delete(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1246 | } |
| 1247 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1248 | case 5: |
| 1249 | /* Control-e -- End of line */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1250 | input_end(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1251 | break; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1252 | case 6: |
| 1253 | /* Control-f -- Move forward one character */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1254 | input_forward(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1255 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1256 | case '\b': |
| 1257 | case DEL: |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1258 | /* Control-h and DEL */ |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1259 | input_backspace(); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1260 | break; |
| 1261 | case '\t': |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1262 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1263 | input_tab(&lastWasTab); |
Erik Andersen | a268573 | 2000-04-09 18:27:46 +0000 | [diff] [blame] | 1264 | #endif |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1265 | break; |
Eric Andersen | f1f2bd0 | 2001-12-21 11:20:15 +0000 | [diff] [blame^] | 1266 | case 12: |
| 1267 | /* Control-l -- clear screen |
| 1268 | * if the len=0 and no chars in edit line */ |
| 1269 | if (len == 0) { |
| 1270 | printf("\033[H\033[J"); |
| 1271 | put_prompt(); |
| 1272 | } else { |
| 1273 | beep(); |
| 1274 | } |
| 1275 | break; |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1276 | case 14: |
| 1277 | /* Control-n -- Get next command in history */ |
| 1278 | if (hp && hp->n && hp->n->s) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1279 | get_next_history(&hp); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1280 | goto rewrite_line; |
| 1281 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1282 | beep(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1283 | } |
| 1284 | break; |
| 1285 | case 16: |
| 1286 | /* Control-p -- Get previous command from history */ |
| 1287 | if (hp && hp->p) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1288 | get_previous_history(&hp, hp->p); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1289 | goto rewrite_line; |
| 1290 | } else { |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1291 | beep(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1292 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1293 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1294 | case 21: |
| 1295 | /* Control-U -- Clear line before cursor */ |
| 1296 | if (cursor) { |
| 1297 | strcpy(command, command + cursor); |
| 1298 | redraw(cmdedit_y, len -= cursor); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1299 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1300 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1301 | case ESC:{ |
| 1302 | /* escape sequence follows */ |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1303 | if (safe_read(0, &c, 1) < 1) |
| 1304 | goto prepare_to_die; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1305 | /* different vt100 emulations */ |
| 1306 | if (c == '[' || c == 'O') { |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1307 | if (safe_read(0, &c, 1) < 1) |
| 1308 | goto prepare_to_die; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1309 | } |
| 1310 | switch (c) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1311 | #ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1312 | case '\t': /* Alt-Tab */ |
| 1313 | |
| 1314 | input_tab(&lastWasTab); |
| 1315 | break; |
| 1316 | #endif |
| 1317 | case 'A': |
| 1318 | /* Up Arrow -- Get previous command from history */ |
| 1319 | if (hp && hp->p) { |
| 1320 | get_previous_history(&hp, hp->p); |
| 1321 | goto rewrite_line; |
| 1322 | } else { |
| 1323 | beep(); |
| 1324 | } |
| 1325 | break; |
| 1326 | case 'B': |
| 1327 | /* Down Arrow -- Get next command in history */ |
| 1328 | if (hp && hp->n && hp->n->s) { |
| 1329 | get_next_history(&hp); |
| 1330 | goto rewrite_line; |
| 1331 | } else { |
| 1332 | beep(); |
| 1333 | } |
| 1334 | break; |
| 1335 | |
| 1336 | /* Rewrite the line with the selected history item */ |
| 1337 | rewrite_line: |
| 1338 | /* change command */ |
| 1339 | len = strlen(strcpy(command, hp->s)); |
| 1340 | /* redraw and go to end line */ |
| 1341 | redraw(cmdedit_y, 0); |
| 1342 | break; |
| 1343 | case 'C': |
| 1344 | /* Right Arrow -- Move forward one character */ |
| 1345 | input_forward(); |
| 1346 | break; |
| 1347 | case 'D': |
| 1348 | /* Left Arrow -- Move back one character */ |
| 1349 | input_backward(1); |
| 1350 | break; |
| 1351 | case '3': |
| 1352 | /* Delete */ |
| 1353 | input_delete(); |
| 1354 | break; |
| 1355 | case '1': |
| 1356 | case 'H': |
| 1357 | /* Home (Ctrl-A) */ |
| 1358 | input_backward(cursor); |
| 1359 | break; |
| 1360 | case '4': |
| 1361 | case 'F': |
| 1362 | /* End (Ctrl-E) */ |
| 1363 | input_end(); |
| 1364 | break; |
| 1365 | default: |
| 1366 | if (!(c >= '1' && c <= '9')) |
| 1367 | c = 0; |
| 1368 | beep(); |
| 1369 | } |
| 1370 | if (c >= '1' && c <= '9') |
| 1371 | do |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1372 | if (safe_read(0, &c, 1) < 1) |
| 1373 | goto prepare_to_die; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1374 | while (c != '~'); |
| 1375 | break; |
| 1376 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1377 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1378 | default: /* If it's regular input, do the normal thing */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1379 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1380 | /* Control-V -- Add non-printable symbol */ |
| 1381 | if (c == 22) { |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1382 | if (safe_read(0, &c, 1) < 1) |
| 1383 | goto prepare_to_die; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1384 | if (c == 0) { |
| 1385 | beep(); |
| 1386 | break; |
| 1387 | } |
| 1388 | } else |
| 1389 | #endif |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 1390 | if (!Isprint(c)) /* Skip non-printable characters */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1391 | break; |
| 1392 | |
| 1393 | if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */ |
| 1394 | break; |
| 1395 | |
| 1396 | len++; |
| 1397 | |
| 1398 | if (cursor == (len - 1)) { /* Append if at the end of the line */ |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1399 | *(command + cursor) = c; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1400 | *(command + cursor + 1) = 0; |
| 1401 | cmdedit_set_out_char(0); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1402 | } else { /* Insert otherwise */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1403 | int sc = cursor; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1404 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1405 | memmove(command + sc + 1, command + sc, len - sc); |
| 1406 | *(command + sc) = c; |
| 1407 | sc++; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1408 | /* rewrite from cursor */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1409 | input_end(); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1410 | /* to prev x pos + 1 */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1411 | input_backward(cursor - sc); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1414 | break; |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1415 | } |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1416 | if (break_out) /* Enter is the command terminator, no more input. */ |
| 1417 | break; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1418 | |
| 1419 | if (c != '\t') |
| 1420 | lastWasTab = FALSE; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 1423 | setTermSettings(0, (void *) &initial_settings); |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1424 | handlers_sets &= ~SET_RESET_TERM; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1425 | |
| 1426 | /* Handle command history log */ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1427 | if (len) { /* no put empty line */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1428 | |
| 1429 | struct history *h = his_end; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1430 | char *ss; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1431 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1432 | ss = xstrdup(command); /* duplicate */ |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1433 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1434 | if (h == 0) { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 1435 | /* No previous history -- this memory is never freed */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1436 | h = his_front = xmalloc(sizeof(struct history)); |
| 1437 | h->n = xmalloc(sizeof(struct history)); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1438 | |
| 1439 | h->p = NULL; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1440 | h->s = ss; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1441 | h->n->p = h; |
| 1442 | h->n->n = NULL; |
| 1443 | h->n->s = NULL; |
| 1444 | his_end = h->n; |
| 1445 | history_counter++; |
| 1446 | } else { |
Eric Andersen | 91a4400 | 2000-07-19 17:37:57 +0000 | [diff] [blame] | 1447 | /* Add a new history command -- this memory is never freed */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 1448 | h->n = xmalloc(sizeof(struct history)); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1449 | |
| 1450 | h->n->p = h; |
| 1451 | h->n->n = NULL; |
| 1452 | h->n->s = NULL; |
Mark Whitley | 4e33875 | 2001-01-26 20:42:23 +0000 | [diff] [blame] | 1453 | h->s = ss; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 1454 | his_end = h->n; |
| 1455 | |
| 1456 | /* After max history, remove the oldest command */ |
| 1457 | if (history_counter >= MAX_HISTORY) { |
| 1458 | |
| 1459 | struct history *p = his_front->n; |
| 1460 | |
| 1461 | p->p = NULL; |
| 1462 | free(his_front->s); |
| 1463 | free(his_front); |
| 1464 | his_front = p; |
| 1465 | } else { |
| 1466 | history_counter++; |
| 1467 | } |
Erik Andersen | 13456d1 | 2000-03-16 08:09:57 +0000 | [diff] [blame] | 1468 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1469 | #if defined(CONFIG_FEATURE_SH_FANCY_PROMPT) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1470 | num_ok_lines++; |
| 1471 | #endif |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 1472 | } |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 1473 | if(break_out>0) { |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1474 | command[len++] = '\n'; /* set '\n' */ |
| 1475 | command[len] = 0; |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 1476 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1477 | #if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1478 | input_tab(0); /* strong free */ |
| 1479 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1480 | #if defined(CONFIG_FEATURE_SH_FANCY_PROMPT) |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1481 | free(cmdedit_prompt); |
| 1482 | #endif |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1483 | cmdedit_reset_term(); |
Eric Andersen | 044228d | 2001-07-17 01:12:36 +0000 | [diff] [blame] | 1484 | return len; |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1487 | |
| 1488 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1489 | #endif /* CONFIG_FEATURE_COMMAND_EDITING */ |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1490 | |
| 1491 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1492 | #ifdef TEST |
| 1493 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 1494 | const char *applet_name = "debug stuff usage"; |
| 1495 | const char *memory_exhausted = "Memory exhausted"; |
| 1496 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1497 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 1498 | #include <locale.h> |
| 1499 | #endif |
| 1500 | |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1501 | int main(int argc, char **argv) |
| 1502 | { |
| 1503 | char buff[BUFSIZ]; |
| 1504 | char *prompt = |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1505 | #if defined(CONFIG_FEATURE_SH_FANCY_PROMPT) |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1506 | "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\ |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1507 | \\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \ |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1508 | \\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]"; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1509 | #else |
| 1510 | "% "; |
| 1511 | #endif |
| 1512 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1513 | #ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT |
Eric Andersen | f9ff8a7 | 2001-03-15 20:51:09 +0000 | [diff] [blame] | 1514 | setlocale(LC_ALL, ""); |
| 1515 | #endif |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1516 | while(1) { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1517 | int l; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1518 | cmdedit_read_input(prompt, buff); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1519 | l = strlen(buff); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1520 | if(l==0) |
| 1521 | break; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1522 | if(l > 0 && buff[l-1] == '\n') |
| 1523 | buff[l-1] = 0; |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1524 | printf("*** cmdedit_read_input() returned line =%s=\n", buff); |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 1525 | } |
Eric Andersen | 5f2c79d | 2001-02-16 18:36:04 +0000 | [diff] [blame] | 1526 | printf("*** cmdedit_read_input() detect ^C\n"); |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1530 | #endif /* TEST */ |