blob: 554a4ebecef6539ada5c818ded1e1f50f44f32a3 [file] [log] [blame]
Erik Andersenc7c634b2000-03-19 05:28:55 +00001/* vi: set sw=4 ts=4: */
Erik Andersen13456d12000-03-16 08:09:57 +00002/*
Eric Andersenaff114c2004-04-14 17:51:38 +00003 * Termios command line History and Editing.
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
Eric Andersen81fe1232003-07-29 06:38:40 +00005 * Copyright (c) 1986-2003 may safely be consumed by a BSD or GPL license.
Eric Andersen7467c8d2001-07-12 20:26:32 +00006 * Written by: Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersen5f2c79d2001-02-16 18:36:04 +00007 *
8 * Used ideas:
9 * Adam Rogoyski <rogoyski@cs.utexas.edu>
10 * Dave Cinege <dcinege@psychosis.com>
11 * Jakub Jelinek (c) 1995
Eric Andersen81fe1232003-07-29 06:38:40 +000012 * Erik Andersen <andersen@codepoet.org> (Majorly adjusted for busybox)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000013 *
Erik Andersen13456d12000-03-16 08:09:57 +000014 * This code is 'as is' with no warranty.
Erik Andersen13456d12000-03-16 08:09:57 +000015 */
16
17/*
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000018 Usage and known bugs:
Erik Andersen13456d12000-03-16 08:09:57 +000019 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 Whitley4e338752001-01-26 20:42:23 +000023 Ctrl-E also works as End.
Erik Andersen13456d12000-03-16 08:09:57 +000024
Eric Andersen5f2c79d2001-02-16 18:36:04 +000025 Small bugs (simple effect):
26 - not true viewing if terminal size (x*y symbols) less
Denis Vlasenko00cdbd82007-01-21 19:21:21 +000027 size (prompt + editor's line + 2 symbols)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000028 - not true viewing if length prompt less terminal width
Erik Andersen13456d12000-03-16 08:09:57 +000029 */
30
Eric Andersencbe31da2001-02-20 06:14:08 +000031#include <sys/ioctl.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +000032#include "busybox.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000033
Glenn L McGrath475820c2004-01-22 12:42:23 +000034
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000035/* FIXME: obsolete CONFIG item? */
36#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
37
38
Glenn L McGrath3b251852004-01-03 12:07:32 +000039#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000040
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000041#define ENABLE_FEATURE_COMMAND_EDITING 0
42#define ENABLE_FEATURE_COMMAND_TAB_COMPLETION 0
43#define ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION 0
44#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
45#define ENABLE_FEATURE_CLEAN_UP 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +000046
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000047#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000048
Eric Andersen5165fbe2001-02-20 06:42:29 +000049
Denis Vlasenko82b39e82007-01-21 19:18:19 +000050/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000051#if ENABLE_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000052
Denis Vlasenko82b39e82007-01-21 19:18:19 +000053#if ENABLE_LOCALE_SUPPORT
54#define Isprint(c) isprint(c)
55#else
56#define Isprint(c) ((c) >= ' ' && (c) != ((unsigned char)'\233'))
57#endif
58
Denis Vlasenko7e46cf72006-12-23 01:21:55 +000059#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
60(ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION || ENABLE_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000061
Robert Griebl350d26b2002-12-03 22:45:46 +000062
Denis Vlasenko8e1c7152007-01-22 07:21:38 +000063static line_input_t *state;
Erik Andersen1d1d9502000-04-21 01:26:49 +000064
Eric Andersen63a86222000-11-07 06:52:13 +000065static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000066
Denis Vlasenko8e1c7152007-01-22 07:21:38 +000067static volatile unsigned cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +000068
Eric Andersenc470f442003-07-28 09:56:35 +000069static int cmdedit_x; /* real x terminal position */
70static int cmdedit_y; /* pseudoreal y terminal position */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +000071static int cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen86349772000-12-18 20:25:50 +000072
Denis Vlasenko8e1c7152007-01-22 07:21:38 +000073static unsigned cursor;
74static unsigned command_len;
Denis Vlasenko82b39e82007-01-21 19:18:19 +000075static char *command_ps;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +000076static const char *cmdedit_prompt;
Eric Andersen5f2c79d2001-02-16 18:36:04 +000077
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000078#if ENABLE_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +000079static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +000080static int num_ok_lines = 1;
81#endif
82
Denis Vlasenko82b39e82007-01-21 19:18:19 +000083#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
84static char *user_buf = "";
85static char *home_pwd_buf = "";
86#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000087
Denis Vlasenko82b39e82007-01-21 19:18:19 +000088#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +000089static int my_uid;
90static int my_gid;
Denis Vlasenko82b39e82007-01-21 19:18:19 +000091#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000092
Denis Vlasenko82b39e82007-01-21 19:18:19 +000093/* Put 'command_ps[cursor]', cursor++.
94 * Advance cursor on screen. If we reached right margin, scroll text up
95 * and remove terminal margin effect by printing 'next_char' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000096static void cmdedit_set_out_char(int next_char)
97{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +000098 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +000099
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000100 if (c == '\0') {
101 /* erase character after end of input string */
102 c = ' ';
103 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000104#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000105 /* Display non-printable characters in reverse */
106 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000107 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000108 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000109 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000110 c += '@';
111 if (c == 127)
112 c = '?';
113 printf("\033[7m%c\033[0m", c);
114 } else
115#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000116 {
117 if (initial_settings.c_lflag & ECHO)
118 putchar(c);
119 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000120 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000121 /* terminal is scrolled down */
122 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000123 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000124 /* destroy "(auto)margin" */
125 putchar(next_char);
126 putchar('\b');
127 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000128// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000129 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000130}
131
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000132/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000133static void input_end(void)
134{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000135 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000136 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000137}
138
139/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000140static void goto_new_line(void)
141{
Mark Whitley4e338752001-01-26 20:42:23 +0000142 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000143 if (cmdedit_x)
144 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000145}
146
147
Rob Landley88621d72006-08-29 19:41:06 +0000148static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000149{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000150 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000151 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000152}
Eric Andersen81fe1232003-07-29 06:38:40 +0000153
Rob Landley88621d72006-08-29 19:41:06 +0000154static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000155{
156 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000157}
158
Eric Andersenaff114c2004-04-14 17:51:38 +0000159/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000160/* (optimized for slow terminals) */
161static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000162{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000163 int count_y;
164
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000165 if (num > cursor)
166 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000167 if (!num)
168 return;
169 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000170
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000171 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000172 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000173 if (num <= 4) {
174 do putchar('\b'); while (--num);
175 return;
176 }
177 printf("\033[%uD", num);
178 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000179 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000180
181 /* Need to go one or more lines up */
182 num -= cmdedit_x;
183 count_y = 1 + (num / cmdedit_termw);
184 cmdedit_y -= count_y;
185 cmdedit_x = cmdedit_termw * count_y - num;
186 /* go to 1st col; go up; go to correct column */
187 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000188}
189
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000190static void put_prompt(void)
191{
192 out1str(cmdedit_prompt);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000193 cmdedit_x = cmdedit_prmt_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000194 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000195// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000196 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000197}
198
Eric Andersenaff114c2004-04-14 17:51:38 +0000199/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000200static void redraw(int y, int back_cursor)
201{
Eric Andersenc470f442003-07-28 09:56:35 +0000202 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000203 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000204 putchar('\r');
205 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000206 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000207 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000208 input_backward(back_cursor);
209}
210
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000211#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000212#define DELBUFSIZ 128
213static char *delbuf; /* a (malloced) place to store deleted characters */
214static char *delp;
215static char newdelflag; /* whether delbuf should be reused yet */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000216#endif
217
218/* Delete the char in front of the cursor, optionally saving it
219 * for later putback */
220static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000221{
Mark Whitley4e338752001-01-26 20:42:23 +0000222 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000223
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000224 if (j == command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000225 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000226
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000227#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000228 if (save) {
229 if (newdelflag) {
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000230 if (!delbuf)
231 delbuf = malloc(DELBUFSIZ);
232 /* safe if malloc fails */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000233 delp = delbuf;
234 newdelflag = 0;
235 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000236 if (delbuf && (delp - delbuf < DELBUFSIZ))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000237 *delp++ = command_ps[j];
238 }
239#endif
240
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000241 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000242 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000243 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000244 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000245 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000246}
247
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000248#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000249static void put(void)
250{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000251 int ocursor;
252 int j = delp - delbuf;
253
Paul Fox3f11b1b2005-08-04 19:04:46 +0000254 if (j == 0)
255 return;
256 ocursor = cursor;
257 /* open hole and then fill it */
258 memmove(command_ps + cursor + j, command_ps + cursor, len - cursor + 1);
259 strncpy(command_ps + cursor, delbuf, j);
260 len += j;
261 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000262 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000263}
264#endif
265
Mark Whitley4e338752001-01-26 20:42:23 +0000266/* Delete the char in back of the cursor */
267static void input_backspace(void)
268{
269 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000270 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000271 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000272 }
273}
274
Eric Andersenaff114c2004-04-14 17:51:38 +0000275/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000276static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000277{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000278 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000279 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000280}
281
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000282
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000283#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000284
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000285static char **matches;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000286static unsigned num_matches;
287
288static void free_tab_completion_data(void)
289{
290 if (matches) {
291 while (num_matches)
292 free(matches[--num_matches]);
293 free(matches);
294 matches = NULL;
295 }
296}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000297
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000298static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000299{
300 int nm = num_matches;
301 int nm1 = nm + 1;
302
303 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000304 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000305 num_matches++;
306}
307
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000308#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000309static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000310{
311 struct passwd *entry;
312 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000313
Eric Andersenc470f442003-07-28 09:56:35 +0000314 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000315 userlen = strlen(ud);
316
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000317 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000318 char *sav_ud = ud - 1;
319 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000320 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000321
Eric Andersenc470f442003-07-28 09:56:35 +0000322 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000323 home = home_pwd_buf;
324 } else {
325 /* "~user/..." */
326 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000327 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000328 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000329 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000330 ud = temp;
331 if (entry)
332 home = entry->pw_dir;
333 }
334 if (home) {
335 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000336 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000337
338 /* /home/user/... */
339 sprintf(temp2, "%s%s", home, ud);
340 strcpy(sav_ud, temp2);
341 }
342 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000343 } else {
344 /* "~[^/]*" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000345 setpwent();
346
347 while ((entry = getpwent()) != NULL) {
348 /* Null usernames should result in all users as possible completions. */
349 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000350 add_match(xasprintf("~%s/", entry->pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000351 }
352 }
353
354 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000355 }
356}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000357#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000358
359enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000360 FIND_EXE_ONLY = 0,
361 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000362 FIND_FILE_ONLY = 2,
363};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000364
Mark Whitley4e338752001-01-26 20:42:23 +0000365static int path_parse(char ***p, int flags)
366{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000367 int npth;
Glenn L McGrath67285962004-01-14 09:34:51 +0000368 const char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000369 const char *pth;
370 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000371
Mark Whitley4e338752001-01-26 20:42:23 +0000372 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000373 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000374 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000375
376 if (state->flags & WITH_PATH_LOOKUP)
377 pth = state->path_lookup;
378 else
379 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000380 /* PATH=<empty> or PATH=:<empty> */
381 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
382 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000383
384 tmp = pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000385 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000386 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000387 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000388 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000389 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000390 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000391 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000392 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000393 }
394
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000395 res = xmalloc(npth * sizeof(char*));
396 res[0] = xstrdup(pth);
Mark Whitley4e338752001-01-26 20:42:23 +0000397 tmp = pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000398 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000399 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000400 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000401 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000402 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000403 *tmp++ = '\0'; /* ':' -> '\0' */
404 if (*tmp == '\0')
405 break; /* :<empty> */
406 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000407 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000408 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000409 return npth;
410}
411
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000412static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000413{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000414 DIR *dir;
415 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000416 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000417 struct stat st;
418 char *path1[1];
419 char **paths = path1;
420 int npaths;
421 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000422 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000423 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000424
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000425 npaths = 1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000426 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000427
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000428 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000429 /* no dir, if flags==EXE_ONLY - get paths, else "." */
430 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000431 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000432 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000433 /* dirbuf = ".../.../.../" */
434 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000435#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000436 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000437 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000438#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000439 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000440 /* point to 'l' in "..../last_component" */
441 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000442 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000443
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000444 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000445 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000446 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000447 continue;
448
449 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000450 int len1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000451 char *str_found = next->d_name;
452
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000453 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000454 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000455 continue;
456 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000457 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000458 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000459 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000460 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000461 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000462 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000463 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000464 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000465 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000466 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000467 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000468 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000469
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000470 len1 = strlen(found);
471 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000472 found[len1] = '\0';
473 found[len1+1] = '\0';
474
Mark Whitley4e338752001-01-26 20:42:23 +0000475 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000476 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000477 if (found[len1-1] != '/') {
478 found[len1] = '/';
479 }
Mark Whitley4e338752001-01-26 20:42:23 +0000480 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000481 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000482 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000483 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000484 }
Mark Whitley4e338752001-01-26 20:42:23 +0000485 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000486 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000487 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000488 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000489 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000490 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000491 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000492 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000493 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000494 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000495 free(paths);
496 }
Erik Andersen6273f652000-03-17 01:12:41 +0000497}
Erik Andersenf0657d32000-04-12 17:49:52 +0000498
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000499#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000500
501#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000502 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
503 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000504
505static int find_match(char *matchBuf, int *len_with_quotes)
506{
507 int i, j;
508 int command_mode;
509 int c, c2;
510 int int_buf[BUFSIZ + 1];
511 int pos_buf[BUFSIZ + 1];
512
513 /* set to integer dimension characters and own positions */
514 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000515 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000517 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000519 }
520 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000521 }
522
523 /* mask \+symbol and convert '\t' to ' ' */
524 for (i = j = 0; matchBuf[i]; i++, j++)
525 if (matchBuf[i] == '\\') {
526 collapse_pos(j, j + 1);
527 int_buf[j] |= QUOT;
528 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000529#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000530 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000531 int_buf[j] = ' ' | QUOT;
532#endif
533 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000534#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000535 else if (matchBuf[i] == '\t')
536 int_buf[j] = ' ';
537#endif
538
539 /* mask "symbols" or 'symbols' */
540 c2 = 0;
541 for (i = 0; int_buf[i]; i++) {
542 c = int_buf[i];
543 if (c == '\'' || c == '"') {
544 if (c2 == 0)
545 c2 = c;
546 else {
547 if (c == c2)
548 c2 = 0;
549 else
550 int_buf[i] |= QUOT;
551 }
552 } else if (c2 != 0 && c != '$')
553 int_buf[i] |= QUOT;
554 }
555
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000556 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000557 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
558 for (i = 0; int_buf[i]; i++) {
559 c = int_buf[i];
560 c2 = int_buf[i + 1];
561 j = i ? int_buf[i - 1] : -1;
562 command_mode = 0;
563 if (c == ';' || c == '&' || c == '|') {
564 command_mode = 1 + (c == c2);
565 if (c == '&') {
566 if (j == '>' || j == '<')
567 command_mode = 0;
568 } else if (c == '|' && j == '>')
569 command_mode = 0;
570 }
571 if (command_mode) {
572 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000573 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000574 }
575 }
576 /* collapse `command...` */
577 for (i = 0; int_buf[i]; i++)
578 if (int_buf[i] == '`') {
579 for (j = i + 1; int_buf[j]; j++)
580 if (int_buf[j] == '`') {
581 collapse_pos(i, j + 1);
582 j = 0;
583 break;
584 }
585 if (j) {
586 /* not found close ` - command mode, collapse all previous */
587 collapse_pos(0, i + 1);
588 break;
589 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000590 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000591 }
592
593 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000594 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000595 c2 = 0;
596 for (i = 0; int_buf[i]; i++)
597 if (int_buf[i] == '(' || int_buf[i] == '{') {
598 if (int_buf[i] == '(')
599 c++;
600 else
601 c2++;
602 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000603 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000604 }
605 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
606 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
607 if (int_buf[i] == ')')
608 c--;
609 else
610 c2--;
611 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000612 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000613 }
614
615 /* skip first not quote space */
616 for (i = 0; int_buf[i]; i++)
617 if (int_buf[i] != ' ')
618 break;
619 if (i)
620 collapse_pos(0, i);
621
622 /* set find mode for completion */
623 command_mode = FIND_EXE_ONLY;
624 for (i = 0; int_buf[i]; i++)
625 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
626 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000627 && matchBuf[pos_buf[0]]=='c'
628 && matchBuf[pos_buf[1]]=='d'
629 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000630 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000631 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000632 command_mode = FIND_FILE_ONLY;
633 break;
634 }
635 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000636 for (i = 0; int_buf[i]; i++)
637 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000638 /* find last word */
639 for (--i; i >= 0; i--) {
640 c = int_buf[i];
641 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
642 collapse_pos(0, i + 1);
643 break;
644 }
645 }
646 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000647 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
648 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000649 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000650 while ((int_buf[i] & ~QUOT) == '/'
651 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
652 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000653 i++;
654 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000655
656 /* set only match and destroy quotes */
657 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000658 for (c = 0; pos_buf[i] >= 0; i++) {
659 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000660 j = pos_buf[i] + 1;
661 }
Eric Andersen4f990532001-05-31 17:15:57 +0000662 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000663 /* old lenght matchBuf with quotes symbols */
664 *len_with_quotes = j ? j - pos_buf[0] : 0;
665
666 return command_mode;
667}
668
Glenn L McGrath4d001292003-01-06 01:11:50 +0000669/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000670 * display by column (original idea from ls applet,
671 * very optimized by me :)
672 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000673static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000674{
675 int ncols, row;
676 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000677 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000678 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000679 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000680
681 /* find the longest file name- use that as the column width */
682 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000683 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000684 if (column_width < l)
685 column_width = l;
686 }
687 column_width += 2; /* min space for columns */
688 ncols = cmdedit_termw / column_width;
689
690 if (ncols > 1) {
691 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000692 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000693 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000694 } else {
695 ncols = 1;
696 }
697 for (row = 0; row < nrows; row++) {
698 int n = row;
699 int nc;
700
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000701 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000702 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000703 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000704 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000705 printf("%s\n", matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000706 }
707}
708
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000709static char *add_quote_for_spec_chars(char *found)
710{
711 int l = 0;
712 char *s = xmalloc((strlen(found) + 1) * 2);
713
714 while (*found) {
715 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
716 s[l++] = '\\';
717 s[l++] = *found++;
718 }
719 s[l] = 0;
720 return s;
721}
722
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000723static int match_compare(const void *a, const void *b)
724{
725 return strcmp(*(char**)a, *(char**)b);
726}
727
728/* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000729static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000730{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000731 if (!(state->flags & TAB_COMPLETION))
732 return;
733
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000734 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000735 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000736 int len_found;
737 char matchBuf[BUFSIZ];
738 int find_type;
739 int recalc_pos;
740
Eric Andersenc470f442003-07-28 09:56:35 +0000741 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000742
743 /* Make a local copy of the string -- up
744 * to the position of the cursor */
745 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000746 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000747
748 find_type = find_match(matchBuf, &recalc_pos);
749
750 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000751 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000752
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000753#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000754 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000755 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000756 if (state->flags & USERNAME_COMPLETION)
757 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
758 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000759#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000760 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000761 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000762 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000763 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000764 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000765 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000766 int i, n = 0;
767 qsort(matches, num_matches, sizeof(char*), match_compare);
768 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000769 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000770 if (strcmp(matches[i], matches[i+1]) == 0) {
771 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000772 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000773 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000774 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000775 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000776 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000777 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000778 matches[n] = matches[i];
779 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000780 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000781 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000782 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000783 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000784 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000785 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000786 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000787 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000788 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000789 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000790 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000791 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000792 break;
793 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000794 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000795 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000796 return;
797 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000798 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000799 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000800 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000801 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000802 /* for next completion current found */
803 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000804
805 len_found = strlen(tmp);
806 if (tmp[len_found-1] != '/') {
807 tmp[len_found] = ' ';
808 tmp[len_found+1] = '\0';
809 }
Mark Whitley4e338752001-01-26 20:42:23 +0000810 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000811 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000812 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000813 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000814 /* before word for match */
815 command_ps[cursor - recalc_pos] = 0;
816 /* save tail line */
817 strcpy(matchBuf, command_ps + cursor);
818 /* add match */
819 strcat(command_ps, tmp);
820 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000821 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000822 /* back to begin word for match */
823 input_backward(recalc_pos);
824 /* new pos */
825 recalc_pos = cursor + len_found;
826 /* new len */
827 len = strlen(command_ps);
828 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +0000829 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000830 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000831 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +0000832 } else {
833 /* Ok -- the last char was a TAB. Since they
834 * just hit TAB again, print a list of all the
835 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000836 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000837 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000838
839 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000840 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000841 showfiles();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000842 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000843 }
844 }
845}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000846
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000847#else
848#define input_tab(a) ((void)0)
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000849#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000850
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000851
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000852#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000853
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000854/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000855static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000856{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000857 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
858 free(state->history[state->cur_history]);
859 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000860 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000861 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000862}
863
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000864static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000865{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000866 if (state->flags & DO_HISTORY) {
867 int ch = state->cur_history;
868 if (ch < state->cnt_history) {
869 get_previous_history(); /* save the current history line */
870 state->cur_history = ch + 1;
871 return state->cur_history;
872 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000873 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000874 beep();
875 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000876}
Robert Griebl350d26b2002-12-03 22:45:46 +0000877
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000878#if ENABLE_FEATURE_COMMAND_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000879/* state->flags is already checked to be nonzero */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000880void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000881{
Robert Griebl350d26b2002-12-03 22:45:46 +0000882 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000883 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000884
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000885 /* cleanup old */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000886 for (hi = state->cnt_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000887 hi--;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000888 free(state->history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000889 }
890
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000891 fp = fopen(fromfile, "r");
892 if (fp) {
893 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000894 char * hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000895 int l;
896
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000897 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000898 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000899 l = strlen(hl);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000900 if (l >= BUFSIZ)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000901 hl[BUFSIZ-1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000902 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000903 free(hl);
904 continue;
905 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000906 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000907 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000908 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000909 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000910 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000911}
912
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000913/* state->flags is already checked to be nonzero */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +0000914void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000915{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000916 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000917
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000918 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000919 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000920 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000921
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000922 for (i = 0; i < state->cnt_history; i++) {
923 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000924 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000925 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000926 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000927}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000928#else
929#define load_history(a) ((void)0)
930#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000931#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +0000932
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000933static void remember_in_history(const char *str)
934{
935 int i;
936
937 if (!(state->flags & DO_HISTORY))
938 return;
939
940 i = state->cnt_history;
941 free(state->history[MAX_HISTORY]);
942 state->history[MAX_HISTORY] = NULL;
943 /* After max history, remove the oldest command */
944 if (i >= MAX_HISTORY) {
945 free(state->history[0]);
946 for (i = 0; i < MAX_HISTORY-1; i++)
947 state->history[i] = state->history[i+1];
948 }
949// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
950// (i.e. do not save dups?)
951 state->history[i++] = xstrdup(str);
952 state->cur_history = i;
953 state->cnt_history = i;
954 if (state->flags & SAVE_HISTORY)
955 save_history(state->hist_file);
956 USE_FEATURE_SH_FANCY_PROMPT(num_ok_lines++;)
957}
958
959#else /* MAX_HISTORY == 0 */
960#define remember_in_history(a) ((void)0)
961#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000962
963
Erik Andersen6273f652000-03-17 01:12:41 +0000964/*
965 * This function is used to grab a character buffer
966 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +0000967 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +0000968 * 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 Fox3f11b1b2005-08-04 19:04:46 +0000977 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000978 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +0000979 */
Eric Andersenc470f442003-07-28 09:56:35 +0000980
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000981#if ENABLE_FEATURE_COMMAND_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000982static void
Paul Fox3f11b1b2005-08-04 19:04:46 +0000983vi_Word_motion(char *command, int eat)
984{
985 while (cursor < len && !isspace(command[cursor]))
986 input_forward();
987 if (eat) while (cursor < len && isspace(command[cursor]))
988 input_forward();
989}
990
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000991static void
Paul Fox3f11b1b2005-08-04 19:04:46 +0000992vi_word_motion(char *command, int eat)
993{
994 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000995 while (cursor < len
996 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000997 input_forward();
998 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000999 while (cursor < len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001000 input_forward();
1001 }
1002
1003 if (cursor < len)
1004 input_forward();
1005
1006 if (eat && cursor < len && isspace(command[cursor]))
1007 while (cursor < len && isspace(command[cursor]))
1008 input_forward();
1009}
1010
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001011static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001012vi_End_motion(char *command)
1013{
1014 input_forward();
1015 while (cursor < len && isspace(command[cursor]))
1016 input_forward();
1017 while (cursor < len-1 && !isspace(command[cursor+1]))
1018 input_forward();
1019}
1020
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001021static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001022vi_end_motion(char *command)
1023{
1024 if (cursor >= len-1)
1025 return;
1026 input_forward();
1027 while (cursor < len-1 && isspace(command[cursor]))
1028 input_forward();
1029 if (cursor >= len-1)
1030 return;
1031 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001032 while (cursor < len-1
1033 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1034 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001035 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001036 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001037 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001038 while (cursor < len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001039 input_forward();
1040 }
1041}
1042
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001043static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001044vi_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"e4baaa22005-09-22 12:59:26 +00001052static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001053vi_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 Vlasenko0a8a7742006-12-21 22:27:10 +00001063 while (cursor > 0
1064 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1065 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001066 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001067 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001068 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001069 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001070 input_backward(1);
1071 }
1072}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001073#endif
1074
1075
1076/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001077 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001078 */
1079
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001080#if !ENABLE_FEATURE_SH_FANCY_PROMPT
1081static 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
1088static 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);
1094 char *pwd_buf = xgetcwd(0);
1095 char buf2[PATH_MAX + 1];
1096 char buf[2];
1097 char c;
1098 char *pbuf;
1099
1100 if (!pwd_buf) {
1101 pwd_buf = (char *)bb_msg_unknown;
1102 }
1103
1104 while (*prmt_ptr) {
1105 pbuf = buf;
1106 pbuf[1] = 0;
1107 c = *prmt_ptr++;
1108 if (c == '\\') {
1109 const char *cp = prmt_ptr;
1110 int l;
1111
1112 c = bb_process_escape_sequence(&prmt_ptr);
1113 if (prmt_ptr == cp) {
1114 if (*cp == 0)
1115 break;
1116 c = *prmt_ptr++;
1117 switch (c) {
1118#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1119 case 'u':
1120 pbuf = user_buf;
1121 break;
1122#endif
1123 case 'h':
1124 pbuf = hostname_buf;
1125 if (!pbuf) {
1126 pbuf = xzalloc(256);
1127 if (gethostname(pbuf, 255) < 0) {
1128 strcpy(pbuf, "?");
1129 } else {
1130 char *s = strchr(pbuf, '.');
1131 if (s)
1132 *s = '\0';
1133 }
1134 hostname_buf = pbuf;
1135 }
1136 break;
1137 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001138 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001139 break;
1140#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1141 case 'w':
1142 pbuf = pwd_buf;
1143 l = strlen(home_pwd_buf);
1144 if (home_pwd_buf[0] != 0
1145 && strncmp(home_pwd_buf, pbuf, l) == 0
1146 && (pbuf[l]=='/' || pbuf[l]=='\0')
1147 && strlen(pwd_buf+l)<PATH_MAX
1148 ) {
1149 pbuf = buf2;
1150 *pbuf = '~';
1151 strcpy(pbuf+1, pwd_buf+l);
1152 }
1153 break;
1154#endif
1155 case 'W':
1156 pbuf = pwd_buf;
1157 cp = strrchr(pbuf,'/');
1158 if (cp != NULL && cp != pbuf)
1159 pbuf += (cp-pbuf) + 1;
1160 break;
1161 case '!':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001162 pbuf = buf2;
1163 snprintf(buf2, sizeof(buf2), "%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001164 break;
1165 case 'e': case 'E': /* \e \E = \033 */
1166 c = '\033';
1167 break;
1168 case 'x': case 'X':
1169 for (l = 0; l < 3;) {
1170 int h;
1171 buf2[l++] = *prmt_ptr;
1172 buf2[l] = 0;
1173 h = strtol(buf2, &pbuf, 16);
1174 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
1175 l--;
1176 break;
1177 }
1178 prmt_ptr++;
1179 }
1180 buf2[l] = 0;
1181 c = (char)strtol(buf2, NULL, 16);
1182 if (c == 0)
1183 c = '?';
1184 pbuf = buf;
1185 break;
1186 case '[': case ']':
1187 if (c == flg_not_length) {
1188 flg_not_length = flg_not_length == '[' ? ']' : '[';
1189 continue;
1190 }
1191 break;
1192 }
1193 }
1194 }
1195 if (pbuf == buf)
1196 *pbuf = c;
1197 cur_prmt_len = strlen(pbuf);
1198 prmt_len += cur_prmt_len;
1199 if (flg_not_length != ']')
1200 cmdedit_prmt_len += cur_prmt_len;
1201 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
1202 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001203 if (pwd_buf != (char *)bb_msg_unknown)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001204 free(pwd_buf);
1205 cmdedit_prompt = prmt_mem_ptr;
1206 put_prompt();
1207}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001208#endif
1209
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001210#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
1211#define getTermSettings(fd, argp) tcgetattr(fd, argp);
1212
1213static sighandler_t previous_SIGWINCH_handler;
1214
1215static void cmdedit_reset_term(void)
1216{
1217 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
1218 /* restore SIGWINCH handler */
1219 signal(SIGWINCH, previous_SIGWINCH_handler);
1220 fflush(stdout);
1221}
1222
1223static void cmdedit_setwidth(unsigned w, int redraw_flg)
1224{
1225 cmdedit_termw = w;
1226 if (redraw_flg) {
1227 /* new y for current cursor */
1228 int new_y = (cursor + cmdedit_prmt_len) / w;
1229 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001230 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001231 fflush(stdout);
1232 }
1233}
1234
1235static void win_changed(int nsig)
1236{
1237 int width;
1238 get_terminal_width_height(0, &width, NULL);
1239 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1240 if (nsig == SIGWINCH)
1241 signal(SIGWINCH, win_changed); /* rearm ourself */
1242}
1243
1244static void cmdedit_init(void)
1245{
1246 cmdedit_prmt_len = 0;
1247 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1248 win_changed(0); /* do initial resizing */
1249
1250#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1251 {
1252 struct passwd *entry;
1253
1254 entry = getpwuid(geteuid());
1255 if (entry) {
1256 user_buf = xstrdup(entry->pw_name);
1257 home_pwd_buf = xstrdup(entry->pw_dir);
1258 }
1259 }
1260#endif
1261
1262#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
1263 my_uid = getuid();
1264 my_gid = getgid();
1265#endif
1266// Crap. We should be able to do it without atexit.
1267 atexit(cmdedit_reset_term); /* be sure to do this only once */
1268}
1269
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001270/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001271 * The emacs and vi modes share much of the code in the big
1272 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001273 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001274 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001275 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001276 */
1277
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001278#define vbit 0x100
1279
1280/* leave out the "vi-mode"-only case labels if vi editing isn't
1281 * configured. */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001282#define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001283
1284/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001285#undef CTRL
1286#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001287
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001288int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001289{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001290 static const int null_flags;
1291
Erik Andersenc7c634b2000-03-19 05:28:55 +00001292 int lastWasTab = FALSE;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001293 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001294 unsigned char c;
1295 smallint break_out = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001296#if ENABLE_FEATURE_COMMAND_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001297 smallint vi_cmdmode = 0;
1298 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001299#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001300
1301// FIXME: audit & improve this
1302 if (maxsize > BUFSIZ)
1303 maxsize = BUFSIZ;
1304
1305 /* With null flags, no other fields are ever used */
1306 state = st ? st : (line_input_t*) &null_flags;
1307 if (state->flags & SAVE_HISTORY)
1308 load_history(state->hist_file);
1309
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001310 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001311 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001312 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001313 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001314 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001315
Eric Andersen34506362001-08-02 05:02:46 +00001316 getTermSettings(0, (void *) &initial_settings);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001317 memcpy(&new_settings, &initial_settings, sizeof(new_settings));
Eric Andersen34506362001-08-02 05:02:46 +00001318 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1319 /* Turn off echoing and CTRL-C, so we can trap it */
1320 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001321 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001322 new_settings.c_cc[VMIN] = 1;
1323 new_settings.c_cc[VTIME] = 0;
1324 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001325#ifndef _POSIX_VDISABLE
1326#define _POSIX_VDISABLE '\0'
1327#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001328 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001329 setTermSettings(0, (void *) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001330
Eric Andersen6faae7d2001-02-16 20:09:17 +00001331 /* Now initialize things */
1332 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001333 /* Print out the command prompt */
1334 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001335
Erik Andersenc7c634b2000-03-19 05:28:55 +00001336 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001337 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001338
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001339 if (safe_read(0, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001340 /* if we can't read input then exit */
1341 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001342 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001343
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001344 ic = c;
1345
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001346#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001347 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001348 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001349 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001350#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001351 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001352 case '\n':
1353 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001354 vi_case('\n'|vbit:)
1355 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001356 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001357 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001358 break_out = 1;
1359 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001360#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001361 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001362 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001363 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001364 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001365 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001366 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001367 vi_case('h'|vbit:)
1368 vi_case('\b'|vbit:)
1369 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001370 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001371 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001372 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001373#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001374 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001375 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001376 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001377 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001378 command_len = 0;
1379 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001380 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001381 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001382 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001383 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001384 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001385 errno = 0;
1386 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001387 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001388 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001389 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001390 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001391 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001392 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001393
1394#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001395 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001396 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001397 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001398 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001399 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001400 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001401 vi_case('l'|vbit:)
1402 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001403 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001404 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001405 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001406#endif
1407
Erik Andersenf0657d32000-04-12 17:49:52 +00001408 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001409 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001410 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001411 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001412 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001413
Erik Andersenc7c634b2000-03-19 05:28:55 +00001414 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001415 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001416 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001417
1418#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001419 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001420 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001421 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001422 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001423 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001424 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001425 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001426 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001427 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001428 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001429 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001430 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001431#endif
1432
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001433#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001434 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001435 vi_case(CTRL('N')|vbit:)
1436 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001437 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001438 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001439 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001440 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001441 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001442 vi_case(CTRL('P')|vbit:)
1443 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001444 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001445 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001446 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001447 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001448 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001449 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001450 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001451#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001452
1453#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001454 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001455 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001456 /* Control-U -- Clear line before cursor */
1457 if (cursor) {
1458 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001459 command_len -= cursor;
1460 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001461 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001462 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001463#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001464 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001465 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001466 /* Control-W -- Remove the last word */
1467 while (cursor > 0 && isspace(command[cursor-1]))
1468 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001469 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001470 input_backspace();
1471 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001472
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001473#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001474 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001475 vi_cmdmode = 0;
1476 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001477 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001478 input_backward(cursor);
1479 vi_cmdmode = 0;
1480 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001481 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001482 input_forward();
1483 vi_cmdmode = 0;
1484 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001485 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001486 input_end();
1487 vi_cmdmode = 0;
1488 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001489 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001490 input_delete(1);
1491 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001492 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001493 if (cursor > 0) {
1494 input_backward(1);
1495 input_delete(1);
1496 }
1497 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001498 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001499 vi_Word_motion(command, 1);
1500 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001501 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001502 vi_word_motion(command, 1);
1503 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001504 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001505 vi_End_motion(command);
1506 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001507 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001508 vi_end_motion(command);
1509 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001510 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001511 vi_Back_motion(command);
1512 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001513 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001514 vi_back_motion(command);
1515 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001516 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001517 vi_cmdmode = 0;
1518 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001519 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001520 goto clear_to_eol;
1521
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001522 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001523 vi_cmdmode = 0;
1524 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001525 case 'd'|vbit: {
1526 int nc, sc;
1527 sc = cursor;
1528 prevc = ic;
1529 if (safe_read(0, &c, 1) < 1)
1530 goto prepare_to_die;
1531 if (c == (prevc & 0xff)) {
1532 /* "cc", "dd" */
1533 input_backward(cursor);
1534 goto clear_to_eol;
1535 break;
1536 }
1537 switch (c) {
1538 case 'w':
1539 case 'W':
1540 case 'e':
1541 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001542 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001543 case 'w': /* "dw", "cw" */
1544 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001545 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001546 case 'W': /* 'dW', 'cW' */
1547 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001548 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001549 case 'e': /* 'de', 'ce' */
1550 vi_end_motion(command);
1551 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001552 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001553 case 'E': /* 'dE', 'cE' */
1554 vi_End_motion(command);
1555 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001556 break;
1557 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001558 nc = cursor;
1559 input_backward(cursor - sc);
1560 while (nc-- > cursor)
1561 input_delete(1);
1562 break;
1563 case 'b': /* "db", "cb" */
1564 case 'B': /* implemented as B */
1565 if (c == 'b')
1566 vi_back_motion(command);
1567 else
1568 vi_Back_motion(command);
1569 while (sc-- > cursor)
1570 input_delete(1);
1571 break;
1572 case ' ': /* "d ", "c " */
1573 input_delete(1);
1574 break;
1575 case '$': /* "d$", "c$" */
1576 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001577 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001578 input_delete(1);
1579 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001580 }
1581 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001582 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001583 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001584 input_forward();
1585 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001586 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001587 put();
1588 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001589 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001590 if (safe_read(0, &c, 1) < 1)
1591 goto prepare_to_die;
1592 if (c == 0)
1593 beep();
1594 else {
1595 *(command + cursor) = c;
1596 putchar(c);
1597 putchar('\b');
1598 }
1599 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001600#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001601
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001602 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001603
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001604#if ENABLE_FEATURE_COMMAND_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001605 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001606 /* ESC: insert mode --> command mode */
1607 vi_cmdmode = 1;
1608 input_backward(1);
1609 break;
1610 }
1611#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001612 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001613 if (safe_read(0, &c, 1) < 1)
1614 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001615 /* different vt100 emulations */
1616 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001617 vi_case('['|vbit:)
1618 vi_case('O'|vbit:)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001619 if (safe_read(0, &c, 1) < 1)
1620 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001621 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001622 if (c >= '1' && c <= '9') {
1623 unsigned char dummy;
1624
1625 if (safe_read(0, &dummy, 1) < 1)
1626 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001627 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001628 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001629 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001630
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001631 switch (c) {
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001632#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001633 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001634 input_tab(&lastWasTab);
1635 break;
1636#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001637#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001638 case 'A':
1639 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001640 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001641 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001642 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001643 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001644 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001645 break;
1646 case 'B':
1647 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001648 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001649 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001650 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001651 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001652 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001653 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001654 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001655 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001656 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001657#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001658 case 'C':
1659 /* Right Arrow -- Move forward one character */
1660 input_forward();
1661 break;
1662 case 'D':
1663 /* Left Arrow -- Move back one character */
1664 input_backward(1);
1665 break;
1666 case '3':
1667 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001668 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001669 break;
1670 case '1':
1671 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001672 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001673 input_backward(cursor);
1674 break;
1675 case '4':
1676 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001677 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001678 input_end();
1679 break;
1680 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001681 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001682 beep();
1683 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001684 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001685
Eric Andersenc470f442003-07-28 09:56:35 +00001686 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001687#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001688 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001689 if (c == CTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001690 if (safe_read(0, &c, 1) < 1)
1691 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001692 if (c == 0) {
1693 beep();
1694 break;
1695 }
1696 } else
1697#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001698
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001699#if ENABLE_FEATURE_COMMAND_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001700 if (vi_cmdmode) /* Don't self-insert */
1701 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001702#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001703 if (!Isprint(c)) /* Skip non-printable characters */
1704 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001705
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001706 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001707 break;
1708
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001709 command_len++;
1710 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001711 command[cursor] = c;
1712 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001713 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001714 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001715 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001716
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001717 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001718 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001719 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001720 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001721 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001722 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001723 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001724 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001725 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001726 }
Eric Andersenc470f442003-07-28 09:56:35 +00001727 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001728 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001729
1730 if (c != '\t')
1731 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001732 }
1733
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001734 if (command_len > 0)
1735 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001736
Eric Andersen27bb7902003-12-23 20:24:51 +00001737 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001738 command[command_len++] = '\n';
1739 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001740 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001741
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001742#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001743 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001744#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001745
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001746#if ENABLE_FEATURE_SH_FANCY_PROMPT
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001747 free((char*)cmdedit_prompt);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001748#endif
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001749 /* restore initial_settings and SIGWINCH handler */
Eric Andersen501c88b2000-07-28 15:14:45 +00001750 cmdedit_reset_term();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001751 return command_len;
1752}
1753
1754line_input_t *new_line_input_t(int flags)
1755{
1756 line_input_t *n = xzalloc(sizeof(*n));
1757 n->flags = flags;
1758 return n;
1759}
1760
1761#else
1762
1763#undef read_line_input
1764int read_line_input(const char* prompt, char* command, int maxsize)
1765{
1766 fputs(prompt, stdout);
1767 fflush(stdout);
1768 fgets(command, maxsize, stdin);
1769 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001770}
1771
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001772#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001773
1774
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001775/*
1776 * Testing
1777 */
1778
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001779#ifdef TEST
1780
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001781#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001782
1783const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001784
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001785int main(int argc, char **argv)
1786{
1787 char buff[BUFSIZ];
1788 char *prompt =
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001789#if ENABLE_FEATURE_SH_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001790 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1791 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1792 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001793#else
1794 "% ";
1795#endif
1796
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001797#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001798 setlocale(LC_ALL, "");
1799#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001800 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001801 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001802 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001803 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001804 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001805 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001806 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001807 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001808 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001809 return 0;
1810}
1811
Eric Andersenc470f442003-07-28 09:56:35 +00001812#endif /* TEST */