blob: a1432af157a272155d42e4075bb6b9eca78e9284 [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"
"Robert P. J. Day"4eddb422006-07-03 00:46:47 +000033#include "cmdedit.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000034
Glenn L McGrath475820c2004-01-22 12:42:23 +000035
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000036/* FIXME: obsolete CONFIG item? */
37#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
38
39
Glenn L McGrath3b251852004-01-03 12:07:32 +000040#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000041
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000042#define ENABLE_FEATURE_COMMAND_EDITING 0
43#define ENABLE_FEATURE_COMMAND_TAB_COMPLETION 0
44#define ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION 0
45#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
46#define ENABLE_FEATURE_CLEAN_UP 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +000047
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000048#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000049
Eric Andersen5165fbe2001-02-20 06:42:29 +000050
Denis Vlasenko82b39e82007-01-21 19:18:19 +000051/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000052#if ENABLE_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000053
Denis Vlasenko82b39e82007-01-21 19:18:19 +000054
55#if ENABLE_LOCALE_SUPPORT
56#define Isprint(c) isprint(c)
57#else
58#define Isprint(c) ((c) >= ' ' && (c) != ((unsigned char)'\233'))
59#endif
60
Denis Vlasenko7e46cf72006-12-23 01:21:55 +000061#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
62(ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION || ENABLE_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000063
Denis Vlasenko82b39e82007-01-21 19:18:19 +000064/* Maximum length of command line history */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000065#if !ENABLE_FEATURE_COMMAND_HISTORY
Robert Griebl350d26b2002-12-03 22:45:46 +000066#define MAX_HISTORY 15
67#else
Denis Vlasenko9d4533e2006-11-02 22:09:37 +000068#define MAX_HISTORY (CONFIG_FEATURE_COMMAND_HISTORY + 0)
Robert Griebl350d26b2002-12-03 22:45:46 +000069#endif
70
Erik Andersen1d1d9502000-04-21 01:26:49 +000071
Denis Vlasenko82b39e82007-01-21 19:18:19 +000072/* Current termios and the previous termios before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +000073static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000074
Mark Whitley4e338752001-01-26 20:42:23 +000075static
Denis Vlasenko82b39e82007-01-21 19:18:19 +000076volatile unsigned cmdedit_termw = 80; /* actual terminal width */
Erik Andersen13456d12000-03-16 08:09:57 +000077
Mark Whitley4e338752001-01-26 20:42:23 +000078
Eric Andersenc470f442003-07-28 09:56:35 +000079static int cmdedit_x; /* real x terminal position */
80static int cmdedit_y; /* pseudoreal y terminal position */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +000081static int cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen86349772000-12-18 20:25:50 +000082
Denis Vlasenko82b39e82007-01-21 19:18:19 +000083static int cursor;
84static int len;
85static char *command_ps;
86static SKIP_FEATURE_SH_FANCY_PROMPT(const) char *cmdedit_prompt;
Eric Andersen5f2c79d2001-02-16 18:36:04 +000087
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000088#if ENABLE_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +000089static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +000090static int num_ok_lines = 1;
91#endif
92
Denis Vlasenko82b39e82007-01-21 19:18:19 +000093#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
94static char *user_buf = "";
95static char *home_pwd_buf = "";
96#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000097
Denis Vlasenko82b39e82007-01-21 19:18:19 +000098#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +000099static int my_uid;
100static int my_gid;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000101#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000102
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000103/* Put 'command_ps[cursor]', cursor++.
104 * Advance cursor on screen. If we reached right margin, scroll text up
105 * and remove terminal margin effect by printing 'next_char' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000106static void cmdedit_set_out_char(int next_char)
107{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000108 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000109
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000110 if (c == '\0') {
111 /* erase character after end of input string */
112 c = ' ';
113 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000114#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000115 /* Display non-printable characters in reverse */
116 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000117 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000118 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000119 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000120 c += '@';
121 if (c == 127)
122 c = '?';
123 printf("\033[7m%c\033[0m", c);
124 } else
125#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000126 {
127 if (initial_settings.c_lflag & ECHO)
128 putchar(c);
129 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000130 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000131 /* terminal is scrolled down */
132 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000133 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000134 /* destroy "(auto)margin" */
135 putchar(next_char);
136 putchar('\b');
137 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000138// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000139 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000140}
141
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000142/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000143static void input_end(void)
144{
145 while (cursor < len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000146 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000147}
148
149/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000150static void goto_new_line(void)
151{
Mark Whitley4e338752001-01-26 20:42:23 +0000152 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000153 if (cmdedit_x)
154 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000155}
156
157
Rob Landley88621d72006-08-29 19:41:06 +0000158static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000159{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000160 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000161 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000162}
Eric Andersen81fe1232003-07-29 06:38:40 +0000163
Rob Landley88621d72006-08-29 19:41:06 +0000164static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000165{
166 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000167}
168
Eric Andersenaff114c2004-04-14 17:51:38 +0000169/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000170/* (optimized for slow terminals) */
171static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000172{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000173 int count_y;
174
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000175 if (num > cursor)
176 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000177 if (!num)
178 return;
179 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000180
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000181 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000182 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000183 if (num <= 4) {
184 do putchar('\b'); while (--num);
185 return;
186 }
187 printf("\033[%uD", num);
188 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000189 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000190
191 /* Need to go one or more lines up */
192 num -= cmdedit_x;
193 count_y = 1 + (num / cmdedit_termw);
194 cmdedit_y -= count_y;
195 cmdedit_x = cmdedit_termw * count_y - num;
196 /* go to 1st col; go up; go to correct column */
197 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000198}
199
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000200static void put_prompt(void)
201{
202 out1str(cmdedit_prompt);
Eric Andersenc470f442003-07-28 09:56:35 +0000203 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000204 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000205// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000206 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000207}
208
Eric Andersenaff114c2004-04-14 17:51:38 +0000209/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000210static void redraw(int y, int back_cursor)
211{
Eric Andersenc470f442003-07-28 09:56:35 +0000212 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000213 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000214 putchar('\r');
215 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000216 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000217 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000218 input_backward(back_cursor);
219}
220
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000221#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000222#define DELBUFSIZ 128
223static char *delbuf; /* a (malloced) place to store deleted characters */
224static char *delp;
225static char newdelflag; /* whether delbuf should be reused yet */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000226#endif
227
228/* Delete the char in front of the cursor, optionally saving it
229 * for later putback */
230static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000231{
Mark Whitley4e338752001-01-26 20:42:23 +0000232 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000233
Mark Whitley4e338752001-01-26 20:42:23 +0000234 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000235 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000236
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000237#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000238 if (save) {
239 if (newdelflag) {
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000240 if (!delbuf)
241 delbuf = malloc(DELBUFSIZ);
242 /* safe if malloc fails */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000243 delp = delbuf;
244 newdelflag = 0;
245 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000246 if (delbuf && (delp - delbuf < DELBUFSIZ))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000247 *delp++ = command_ps[j];
248 }
249#endif
250
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000251 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000252 len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000253 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000254 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000255 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000256}
257
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000258#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000259static void put(void)
260{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000261 int ocursor;
262 int j = delp - delbuf;
263
Paul Fox3f11b1b2005-08-04 19:04:46 +0000264 if (j == 0)
265 return;
266 ocursor = cursor;
267 /* open hole and then fill it */
268 memmove(command_ps + cursor + j, command_ps + cursor, len - cursor + 1);
269 strncpy(command_ps + cursor, delbuf, j);
270 len += j;
271 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000272 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000273}
274#endif
275
Mark Whitley4e338752001-01-26 20:42:23 +0000276/* Delete the char in back of the cursor */
277static void input_backspace(void)
278{
279 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000280 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000281 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000282 }
283}
284
Eric Andersenaff114c2004-04-14 17:51:38 +0000285/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000286static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000287{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000288 if (cursor < len)
289 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000290}
291
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000292
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000293#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000294
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000295static char **matches;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000296static unsigned num_matches;
297
298static void free_tab_completion_data(void)
299{
300 if (matches) {
301 while (num_matches)
302 free(matches[--num_matches]);
303 free(matches);
304 matches = NULL;
305 }
306}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000307
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000308static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000309{
310 int nm = num_matches;
311 int nm1 = nm + 1;
312
313 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000314 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000315 num_matches++;
316}
317
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000318#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000319static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000320{
321 struct passwd *entry;
322 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000323
Eric Andersenc470f442003-07-28 09:56:35 +0000324 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000325 userlen = strlen(ud);
326
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000327 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000328 char *sav_ud = ud - 1;
329 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000330 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000331
Eric Andersenc470f442003-07-28 09:56:35 +0000332 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000333 home = home_pwd_buf;
334 } else {
335 /* "~user/..." */
336 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000337 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000338 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000339 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000340 ud = temp;
341 if (entry)
342 home = entry->pw_dir;
343 }
344 if (home) {
345 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000346 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000347
348 /* /home/user/... */
349 sprintf(temp2, "%s%s", home, ud);
350 strcpy(sav_ud, temp2);
351 }
352 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000353 } else {
354 /* "~[^/]*" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000355 setpwent();
356
357 while ((entry = getpwent()) != NULL) {
358 /* Null usernames should result in all users as possible completions. */
359 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000360 add_match(xasprintf("~%s/", entry->pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000361 }
362 }
363
364 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000365 }
366}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000367#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000368
369enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000370 FIND_EXE_ONLY = 0,
371 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000372 FIND_FILE_ONLY = 2,
373};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000374
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000375#if ENABLE_ASH
Glenn L McGrath67285962004-01-14 09:34:51 +0000376const char *cmdedit_path_lookup;
Glenn L McGrath67285962004-01-14 09:34:51 +0000377#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000378static int path_parse(char ***p, int flags)
379{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000380 int npth;
Glenn L McGrath67285962004-01-14 09:34:51 +0000381 const char *tmp;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000382#if ENABLE_ASH
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000383 const char *pth = cmdedit_path_lookup;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000384#else
385 const char *pth = getenv("PATH")
386#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000387
Mark Whitley4e338752001-01-26 20:42:23 +0000388 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000389 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000390 return 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000391 /* PATH=<empty> or PATH=:<empty> */
392 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
393 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000394
395 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000396 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000397
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000398 while (1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000399 npth++; /* count words is + 1 count ':' */
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 Vlasenko82b39e82007-01-21 19:18:19 +0000403 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000404 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000405 }
406
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000407 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000408
409 tmp = pth;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000410 (*p)[0] = xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000411 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000412
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000413 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000414 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000415 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000416 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000417 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
418 if (*++tmp == 0)
419 break; /* :<empty> */
Eric Andersenc470f442003-07-28 09:56:35 +0000420 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000421 }
422
423 return npth;
424}
425
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000426static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000427{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000428 DIR *dir;
429 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000431 struct stat st;
432 char *path1[1];
433 char **paths = path1;
434 int npaths;
435 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000436 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000437 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000438
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000439 npaths = 1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000440 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000441
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000442 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000443 /* no dir, if flags==EXE_ONLY - get paths, else "." */
444 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000445 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000446 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000447 /* dirbuf = ".../.../.../" */
448 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000449#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000450 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000451 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000452#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000453 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000454 /* point to 'l' in "..../last_component" */
455 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000456 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000457
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000458 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000459 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000460 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000461 continue;
462
463 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000464 int len1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000465 char *str_found = next->d_name;
466
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000467 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000468 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000469 continue;
470 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000471 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000472 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000473 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000474 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000475 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000476 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000477 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000478 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000479 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000480 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000481 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000482 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000483
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000484 len1 = strlen(found);
485 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000486 found[len1] = '\0';
487 found[len1+1] = '\0';
488
Mark Whitley4e338752001-01-26 20:42:23 +0000489 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000491 if (found[len1-1] != '/') {
492 found[len1] = '/';
493 }
Mark Whitley4e338752001-01-26 20:42:23 +0000494 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000495 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000496 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000497 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000498 }
Mark Whitley4e338752001-01-26 20:42:23 +0000499 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000500 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000501 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000502 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000503 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000504 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000505 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000506 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000507 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000508 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000509 free(paths);
510 }
Erik Andersen6273f652000-03-17 01:12:41 +0000511}
Erik Andersenf0657d32000-04-12 17:49:52 +0000512
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000513#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000514
515#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000516 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
517 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518
519static int find_match(char *matchBuf, int *len_with_quotes)
520{
521 int i, j;
522 int command_mode;
523 int c, c2;
524 int int_buf[BUFSIZ + 1];
525 int pos_buf[BUFSIZ + 1];
526
527 /* set to integer dimension characters and own positions */
528 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000529 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000530 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000531 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000532 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000533 }
534 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000535 }
536
537 /* mask \+symbol and convert '\t' to ' ' */
538 for (i = j = 0; matchBuf[i]; i++, j++)
539 if (matchBuf[i] == '\\') {
540 collapse_pos(j, j + 1);
541 int_buf[j] |= QUOT;
542 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000543#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000544 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000545 int_buf[j] = ' ' | QUOT;
546#endif
547 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000548#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000549 else if (matchBuf[i] == '\t')
550 int_buf[j] = ' ';
551#endif
552
553 /* mask "symbols" or 'symbols' */
554 c2 = 0;
555 for (i = 0; int_buf[i]; i++) {
556 c = int_buf[i];
557 if (c == '\'' || c == '"') {
558 if (c2 == 0)
559 c2 = c;
560 else {
561 if (c == c2)
562 c2 = 0;
563 else
564 int_buf[i] |= QUOT;
565 }
566 } else if (c2 != 0 && c != '$')
567 int_buf[i] |= QUOT;
568 }
569
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000570 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000571 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
572 for (i = 0; int_buf[i]; i++) {
573 c = int_buf[i];
574 c2 = int_buf[i + 1];
575 j = i ? int_buf[i - 1] : -1;
576 command_mode = 0;
577 if (c == ';' || c == '&' || c == '|') {
578 command_mode = 1 + (c == c2);
579 if (c == '&') {
580 if (j == '>' || j == '<')
581 command_mode = 0;
582 } else if (c == '|' && j == '>')
583 command_mode = 0;
584 }
585 if (command_mode) {
586 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000587 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000588 }
589 }
590 /* collapse `command...` */
591 for (i = 0; int_buf[i]; i++)
592 if (int_buf[i] == '`') {
593 for (j = i + 1; int_buf[j]; j++)
594 if (int_buf[j] == '`') {
595 collapse_pos(i, j + 1);
596 j = 0;
597 break;
598 }
599 if (j) {
600 /* not found close ` - command mode, collapse all previous */
601 collapse_pos(0, i + 1);
602 break;
603 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000604 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000605 }
606
607 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000608 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000609 c2 = 0;
610 for (i = 0; int_buf[i]; i++)
611 if (int_buf[i] == '(' || int_buf[i] == '{') {
612 if (int_buf[i] == '(')
613 c++;
614 else
615 c2++;
616 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000617 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000618 }
619 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
620 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
621 if (int_buf[i] == ')')
622 c--;
623 else
624 c2--;
625 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000626 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000627 }
628
629 /* skip first not quote space */
630 for (i = 0; int_buf[i]; i++)
631 if (int_buf[i] != ' ')
632 break;
633 if (i)
634 collapse_pos(0, i);
635
636 /* set find mode for completion */
637 command_mode = FIND_EXE_ONLY;
638 for (i = 0; int_buf[i]; i++)
639 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
640 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000641 && matchBuf[pos_buf[0]]=='c'
642 && matchBuf[pos_buf[1]]=='d'
643 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000644 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000645 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000646 command_mode = FIND_FILE_ONLY;
647 break;
648 }
649 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000650 for (i = 0; int_buf[i]; i++)
651 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000652 /* find last word */
653 for (--i; i >= 0; i--) {
654 c = int_buf[i];
655 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
656 collapse_pos(0, i + 1);
657 break;
658 }
659 }
660 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000661 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
662 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000663 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000664 while ((int_buf[i] & ~QUOT) == '/'
665 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
666 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000667 i++;
668 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000669
670 /* set only match and destroy quotes */
671 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000672 for (c = 0; pos_buf[i] >= 0; i++) {
673 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000674 j = pos_buf[i] + 1;
675 }
Eric Andersen4f990532001-05-31 17:15:57 +0000676 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000677 /* old lenght matchBuf with quotes symbols */
678 *len_with_quotes = j ? j - pos_buf[0] : 0;
679
680 return command_mode;
681}
682
Glenn L McGrath4d001292003-01-06 01:11:50 +0000683/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000684 * display by column (original idea from ls applet,
685 * very optimized by me :)
686 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000687static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000688{
689 int ncols, row;
690 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000691 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000692 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000693 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000694
695 /* find the longest file name- use that as the column width */
696 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000697 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000698 if (column_width < l)
699 column_width = l;
700 }
701 column_width += 2; /* min space for columns */
702 ncols = cmdedit_termw / column_width;
703
704 if (ncols > 1) {
705 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000706 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000707 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000708 } else {
709 ncols = 1;
710 }
711 for (row = 0; row < nrows; row++) {
712 int n = row;
713 int nc;
714
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000715 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000716 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000717 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000718 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000719 printf("%s\n", matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000720 }
721}
722
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000723static char *add_quote_for_spec_chars(char *found)
724{
725 int l = 0;
726 char *s = xmalloc((strlen(found) + 1) * 2);
727
728 while (*found) {
729 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
730 s[l++] = '\\';
731 s[l++] = *found++;
732 }
733 s[l] = 0;
734 return s;
735}
736
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000737static int match_compare(const void *a, const void *b)
738{
739 return strcmp(*(char**)a, *(char**)b);
740}
741
742/* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000743static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000744{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000745 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000746 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000747 int len_found;
748 char matchBuf[BUFSIZ];
749 int find_type;
750 int recalc_pos;
751
Eric Andersenc470f442003-07-28 09:56:35 +0000752 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000753
754 /* Make a local copy of the string -- up
755 * to the position of the cursor */
756 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000757 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000758
759 find_type = find_match(matchBuf, &recalc_pos);
760
761 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000762 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000763
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000764#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000765 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000766 * then try completing this word as a username. */
767
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000768 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000769 username_tab_completion(matchBuf, NULL);
770 if (!matches)
Mark Whitley4e338752001-01-26 20:42:23 +0000771#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000772 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000773 * in the current working directory */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000774 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000775 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000776 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000777 int i, n = 0;
778 qsort(matches, num_matches, sizeof(char*), match_compare);
779 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000780 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000781 if (strcmp(matches[i], matches[i+1]) == 0) {
782 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000783 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000784 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000785 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000786 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000787 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000788 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000789 matches[n] = matches[i];
790 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000791 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000792 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000793 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000794 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000795 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000796 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000797 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000798 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000799 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000800 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000801 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000802 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000803 break;
804 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000805 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000806 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000807 return;
808 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000809 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000810 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000811 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000812 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000813 /* for next completion current found */
814 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000815
816 len_found = strlen(tmp);
817 if (tmp[len_found-1] != '/') {
818 tmp[len_found] = ' ';
819 tmp[len_found+1] = '\0';
820 }
Mark Whitley4e338752001-01-26 20:42:23 +0000821 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000822 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000823 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000824 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000825 /* before word for match */
826 command_ps[cursor - recalc_pos] = 0;
827 /* save tail line */
828 strcpy(matchBuf, command_ps + cursor);
829 /* add match */
830 strcat(command_ps, tmp);
831 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000832 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000833 /* back to begin word for match */
834 input_backward(recalc_pos);
835 /* new pos */
836 recalc_pos = cursor + len_found;
837 /* new len */
838 len = strlen(command_ps);
839 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +0000840 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000841 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000842 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +0000843 } else {
844 /* Ok -- the last char was a TAB. Since they
845 * just hit TAB again, print a list of all the
846 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000847 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000848 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000849
850 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000851 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000852 showfiles();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000853 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000854 }
855 }
856}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000857
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000858#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000859
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000860
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000861#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000862
863static char *history[MAX_HISTORY+1]; /* history + current */
864/* saved history lines */
865static int n_history;
866/* current pointer to history line */
867static int cur_history;
868
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000869static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000870{
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000871 if (command_ps[0] != '\0' || history[cur_history] == NULL) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000872 free(history[cur_history]);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000873 history[cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000874 }
875 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000876}
877
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000878static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000879{
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000880 int ch = cur_history;
881
882 if (ch < n_history) {
883 get_previous_history(); /* save the current history line */
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000884 cur_history = ch + 1;
885 return cur_history;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000886 } else {
887 beep();
888 return 0;
889 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000890}
Robert Griebl350d26b2002-12-03 22:45:46 +0000891
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000892#if ENABLE_FEATURE_COMMAND_SAVEHISTORY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000893void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000894{
Robert Griebl350d26b2002-12-03 22:45:46 +0000895 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000896 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000897
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000898 /* cleanup old */
899
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000900 for (hi = n_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000901 hi--;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000902 free(history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000903 }
904
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000905 fp = fopen(fromfile, "r");
906 if (fp) {
907 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000908 char * hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000909 int l;
910
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000911 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000912 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000913 l = strlen(hl);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000914 if (l >= BUFSIZ)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000915 hl[BUFSIZ-1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000916 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000917 free(hl);
918 continue;
919 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000920 history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000921 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000922 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000923 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000924 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000925}
926
Denis Vlasenko00cdbd82007-01-21 19:21:21 +0000927void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000928{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000929 FILE *fp = fopen(tofile, "w");
Eric Andersenc470f442003-07-28 09:56:35 +0000930
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000931 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000932 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000933
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000934 for (i = 0; i < n_history; i++) {
935 fprintf(fp, "%s\n", history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000936 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000937 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000938 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000939}
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000940#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +0000941
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000942#endif /* MAX_HISTORY > 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000943
944
Erik Andersen6273f652000-03-17 01:12:41 +0000945/*
946 * This function is used to grab a character buffer
947 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +0000948 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +0000949 * a mini readline).
950 *
951 * The following standard commands are not implemented:
952 * ESC-b -- Move back one word
953 * ESC-f -- Move forward one word
954 * ESC-d -- Delete back one word
955 * ESC-h -- Delete forward one word
956 * CTL-t -- Transpose two characters
957 *
Paul Fox3f11b1b2005-08-04 19:04:46 +0000958 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000959 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +0000960 */
Eric Andersenc470f442003-07-28 09:56:35 +0000961
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000962#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000963static int vi_mode;
964
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000965void setvimode(int viflag)
Paul Fox3f11b1b2005-08-04 19:04:46 +0000966{
967 vi_mode = viflag;
968}
969
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000970static void
Paul Fox3f11b1b2005-08-04 19:04:46 +0000971vi_Word_motion(char *command, int eat)
972{
973 while (cursor < len && !isspace(command[cursor]))
974 input_forward();
975 if (eat) while (cursor < len && isspace(command[cursor]))
976 input_forward();
977}
978
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000979static void
Paul Fox3f11b1b2005-08-04 19:04:46 +0000980vi_word_motion(char *command, int eat)
981{
982 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000983 while (cursor < len
984 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000985 input_forward();
986 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000987 while (cursor < len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000988 input_forward();
989 }
990
991 if (cursor < len)
992 input_forward();
993
994 if (eat && cursor < len && isspace(command[cursor]))
995 while (cursor < len && isspace(command[cursor]))
996 input_forward();
997}
998
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000999static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001000vi_End_motion(char *command)
1001{
1002 input_forward();
1003 while (cursor < len && isspace(command[cursor]))
1004 input_forward();
1005 while (cursor < len-1 && !isspace(command[cursor+1]))
1006 input_forward();
1007}
1008
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001009static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001010vi_end_motion(char *command)
1011{
1012 if (cursor >= len-1)
1013 return;
1014 input_forward();
1015 while (cursor < len-1 && isspace(command[cursor]))
1016 input_forward();
1017 if (cursor >= len-1)
1018 return;
1019 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001020 while (cursor < len-1
1021 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1022 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001023 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001024 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001025 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001026 while (cursor < len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001027 input_forward();
1028 }
1029}
1030
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001031static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001032vi_Back_motion(char *command)
1033{
1034 while (cursor > 0 && isspace(command[cursor-1]))
1035 input_backward(1);
1036 while (cursor > 0 && !isspace(command[cursor-1]))
1037 input_backward(1);
1038}
1039
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001040static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001041vi_back_motion(char *command)
1042{
1043 if (cursor <= 0)
1044 return;
1045 input_backward(1);
1046 while (cursor > 0 && isspace(command[cursor]))
1047 input_backward(1);
1048 if (cursor <= 0)
1049 return;
1050 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001051 while (cursor > 0
1052 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1053 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001054 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001055 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001056 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001057 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001058 input_backward(1);
1059 }
1060}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001061#else
1062enum { vi_mode = 0 };
1063#endif
1064
1065
1066/*
1067 * cmdedit_read_input and its helpers
1068 */
1069
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001070#if !ENABLE_FEATURE_SH_FANCY_PROMPT
1071static void parse_prompt(const char *prmt_ptr)
1072{
1073 cmdedit_prompt = prmt_ptr;
1074 cmdedit_prmt_len = strlen(prmt_ptr);
1075 put_prompt();
1076}
1077#else
1078static void parse_prompt(const char *prmt_ptr)
1079{
1080 int prmt_len = 0;
1081 size_t cur_prmt_len = 0;
1082 char flg_not_length = '[';
1083 char *prmt_mem_ptr = xzalloc(1);
1084 char *pwd_buf = xgetcwd(0);
1085 char buf2[PATH_MAX + 1];
1086 char buf[2];
1087 char c;
1088 char *pbuf;
1089
1090 if (!pwd_buf) {
1091 pwd_buf = (char *)bb_msg_unknown;
1092 }
1093
1094 while (*prmt_ptr) {
1095 pbuf = buf;
1096 pbuf[1] = 0;
1097 c = *prmt_ptr++;
1098 if (c == '\\') {
1099 const char *cp = prmt_ptr;
1100 int l;
1101
1102 c = bb_process_escape_sequence(&prmt_ptr);
1103 if (prmt_ptr == cp) {
1104 if (*cp == 0)
1105 break;
1106 c = *prmt_ptr++;
1107 switch (c) {
1108#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1109 case 'u':
1110 pbuf = user_buf;
1111 break;
1112#endif
1113 case 'h':
1114 pbuf = hostname_buf;
1115 if (!pbuf) {
1116 pbuf = xzalloc(256);
1117 if (gethostname(pbuf, 255) < 0) {
1118 strcpy(pbuf, "?");
1119 } else {
1120 char *s = strchr(pbuf, '.');
1121 if (s)
1122 *s = '\0';
1123 }
1124 hostname_buf = pbuf;
1125 }
1126 break;
1127 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001128 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001129 break;
1130#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1131 case 'w':
1132 pbuf = pwd_buf;
1133 l = strlen(home_pwd_buf);
1134 if (home_pwd_buf[0] != 0
1135 && strncmp(home_pwd_buf, pbuf, l) == 0
1136 && (pbuf[l]=='/' || pbuf[l]=='\0')
1137 && strlen(pwd_buf+l)<PATH_MAX
1138 ) {
1139 pbuf = buf2;
1140 *pbuf = '~';
1141 strcpy(pbuf+1, pwd_buf+l);
1142 }
1143 break;
1144#endif
1145 case 'W':
1146 pbuf = pwd_buf;
1147 cp = strrchr(pbuf,'/');
1148 if (cp != NULL && cp != pbuf)
1149 pbuf += (cp-pbuf) + 1;
1150 break;
1151 case '!':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001152 pbuf = buf2;
1153 snprintf(buf2, sizeof(buf2), "%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001154 break;
1155 case 'e': case 'E': /* \e \E = \033 */
1156 c = '\033';
1157 break;
1158 case 'x': case 'X':
1159 for (l = 0; l < 3;) {
1160 int h;
1161 buf2[l++] = *prmt_ptr;
1162 buf2[l] = 0;
1163 h = strtol(buf2, &pbuf, 16);
1164 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
1165 l--;
1166 break;
1167 }
1168 prmt_ptr++;
1169 }
1170 buf2[l] = 0;
1171 c = (char)strtol(buf2, NULL, 16);
1172 if (c == 0)
1173 c = '?';
1174 pbuf = buf;
1175 break;
1176 case '[': case ']':
1177 if (c == flg_not_length) {
1178 flg_not_length = flg_not_length == '[' ? ']' : '[';
1179 continue;
1180 }
1181 break;
1182 }
1183 }
1184 }
1185 if (pbuf == buf)
1186 *pbuf = c;
1187 cur_prmt_len = strlen(pbuf);
1188 prmt_len += cur_prmt_len;
1189 if (flg_not_length != ']')
1190 cmdedit_prmt_len += cur_prmt_len;
1191 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
1192 }
1193 if (pwd_buf!=(char *)bb_msg_unknown)
1194 free(pwd_buf);
1195 cmdedit_prompt = prmt_mem_ptr;
1196 put_prompt();
1197}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001198#endif
1199
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001200#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
1201#define getTermSettings(fd, argp) tcgetattr(fd, argp);
1202
1203static sighandler_t previous_SIGWINCH_handler;
1204
1205static void cmdedit_reset_term(void)
1206{
1207 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
1208 /* restore SIGWINCH handler */
1209 signal(SIGWINCH, previous_SIGWINCH_handler);
1210 fflush(stdout);
1211}
1212
1213static void cmdedit_setwidth(unsigned w, int redraw_flg)
1214{
1215 cmdedit_termw = w;
1216 if (redraw_flg) {
1217 /* new y for current cursor */
1218 int new_y = (cursor + cmdedit_prmt_len) / w;
1219 /* redraw */
1220 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
1221 fflush(stdout);
1222 }
1223}
1224
1225static void win_changed(int nsig)
1226{
1227 int width;
1228 get_terminal_width_height(0, &width, NULL);
1229 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1230 if (nsig == SIGWINCH)
1231 signal(SIGWINCH, win_changed); /* rearm ourself */
1232}
1233
1234static void cmdedit_init(void)
1235{
1236 cmdedit_prmt_len = 0;
1237 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1238 win_changed(0); /* do initial resizing */
1239
1240#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1241 {
1242 struct passwd *entry;
1243
1244 entry = getpwuid(geteuid());
1245 if (entry) {
1246 user_buf = xstrdup(entry->pw_name);
1247 home_pwd_buf = xstrdup(entry->pw_dir);
1248 }
1249 }
1250#endif
1251
1252#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
1253 my_uid = getuid();
1254 my_gid = getgid();
1255#endif
1256// Crap. We should be able to do it without atexit.
1257 atexit(cmdedit_reset_term); /* be sure to do this only once */
1258}
1259
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001260/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001261 * The emacs and vi modes share much of the code in the big
1262 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001263 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001264 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001265 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001266 */
1267
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001268#define vbit 0x100
1269
1270/* leave out the "vi-mode"-only case labels if vi editing isn't
1271 * configured. */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001272#define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001273
1274/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001275#undef CTRL
1276#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001277
Eric Andersen044228d2001-07-17 01:12:36 +00001278
1279int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001280{
Erik Andersenc7c634b2000-03-19 05:28:55 +00001281 int lastWasTab = FALSE;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001282 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001283 unsigned char c;
1284 smallint break_out = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001285#if ENABLE_FEATURE_COMMAND_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001286 smallint vi_cmdmode = 0;
1287 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001288#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001289 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001290 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001291 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001292 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001293 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001294
Eric Andersen34506362001-08-02 05:02:46 +00001295 getTermSettings(0, (void *) &initial_settings);
1296 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1297 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1298 /* Turn off echoing and CTRL-C, so we can trap it */
1299 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001300 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1301 new_settings.c_cc[VMIN] = 1;
1302 new_settings.c_cc[VTIME] = 0;
1303 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001304#ifndef _POSIX_VDISABLE
1305#define _POSIX_VDISABLE '\0'
1306#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001307 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001308 setTermSettings(0, (void *) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001309
Eric Andersen6faae7d2001-02-16 20:09:17 +00001310 /* Now initialize things */
1311 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001312 /* Print out the command prompt */
1313 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001314
Erik Andersenc7c634b2000-03-19 05:28:55 +00001315 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001316 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001317
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001318 if (safe_read(0, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001319 /* if we can't read input then exit */
1320 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001321 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001322
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001323 ic = c;
1324
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001325#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001326 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001327 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001328 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001329#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001330 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001331 case '\n':
1332 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001333 vi_case('\n'|vbit:)
1334 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001335 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001336 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001337 break_out = 1;
1338 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001339#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001340 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001341 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001342 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001343 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001344 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001345 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001346 vi_case('h'|vbit:)
1347 vi_case('\b'|vbit:)
1348 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001349 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001350 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001351 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001352#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001353 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001354 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001355 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001356 goto_new_line();
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001357#if !ENABLE_ASH
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001358 command[0] = '\0';
Eric Andersen7467c8d2001-07-12 20:26:32 +00001359 len = 0;
1360 lastWasTab = FALSE;
1361 put_prompt();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001362#else
1363 len = 0;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001364 break_out = -1; /* to control traps */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001365#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001366 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001367 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001368 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001369 * if the len=0 and no chars to delete */
1370 if (len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001371 errno = 0;
1372 prepare_to_die:
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001373// So, our API depends on whether we have ash compiled in or not? Crap...
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001374#if !ENABLE_ASH
Mark Whitley4e338752001-01-26 20:42:23 +00001375 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001376 goto_new_line();
1377 /* cmdedit_reset_term() called in atexit */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001378// FIXME. this is definitely not good
Eric Andersen7467c8d2001-07-12 20:26:32 +00001379 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001380#else
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001381 /* to control stopped jobs */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001382 break_out = len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001383 break;
1384#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001385 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001386 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001387 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001388
1389#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001390 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001391 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001392 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001393 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001394 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001395 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001396 vi_case('l'|vbit:)
1397 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001398 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001399 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001400 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001401#endif
1402
Erik Andersenf0657d32000-04-12 17:49:52 +00001403 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001404 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001405 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001406 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001407 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001408
Erik Andersenc7c634b2000-03-19 05:28:55 +00001409 case '\t':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001410#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001411 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001412#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001413 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001414
1415#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001416 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001417 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001418 command[cursor] = 0;
Eric Andersen65a07302002-04-13 13:26:49 +00001419 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001420 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001421 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001422 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001423 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001424 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001425 printf("\033[H");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001426 redraw(0, len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001427 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001428#endif
1429
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001430#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001431 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001432 vi_case(CTRL('N')|vbit:)
1433 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001434 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001435 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001436 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001437 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001438 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001439 vi_case(CTRL('P')|vbit:)
1440 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001441 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001442 if (cur_history > 0) {
1443 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001444 goto rewrite_line;
1445 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001446 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001447 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001448 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001449#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001450
1451#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001452 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001453 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001454 /* Control-U -- Clear line before cursor */
1455 if (cursor) {
1456 strcpy(command, command + cursor);
1457 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001458 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001459 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001460#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001461 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001462 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001463 /* Control-W -- Remove the last word */
1464 while (cursor > 0 && isspace(command[cursor-1]))
1465 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001466 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001467 input_backspace();
1468 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001469
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001470#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001471 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001472 vi_cmdmode = 0;
1473 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001474 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001475 input_backward(cursor);
1476 vi_cmdmode = 0;
1477 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001478 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001479 input_forward();
1480 vi_cmdmode = 0;
1481 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001482 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001483 input_end();
1484 vi_cmdmode = 0;
1485 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001486 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001487 input_delete(1);
1488 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001489 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001490 if (cursor > 0) {
1491 input_backward(1);
1492 input_delete(1);
1493 }
1494 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001495 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001496 vi_Word_motion(command, 1);
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 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001502 vi_End_motion(command);
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 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001508 vi_Back_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 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001514 vi_cmdmode = 0;
1515 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001516 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001517 goto clear_to_eol;
1518
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001519 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001520 vi_cmdmode = 0;
1521 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001522 case 'd'|vbit: {
1523 int nc, sc;
1524 sc = cursor;
1525 prevc = ic;
1526 if (safe_read(0, &c, 1) < 1)
1527 goto prepare_to_die;
1528 if (c == (prevc & 0xff)) {
1529 /* "cc", "dd" */
1530 input_backward(cursor);
1531 goto clear_to_eol;
1532 break;
1533 }
1534 switch (c) {
1535 case 'w':
1536 case 'W':
1537 case 'e':
1538 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001539 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001540 case 'w': /* "dw", "cw" */
1541 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001542 break;
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 'e': /* 'de', 'ce' */
1547 vi_end_motion(command);
1548 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001549 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001550 case 'E': /* 'dE', 'cE' */
1551 vi_End_motion(command);
1552 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001553 break;
1554 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001555 nc = cursor;
1556 input_backward(cursor - sc);
1557 while (nc-- > cursor)
1558 input_delete(1);
1559 break;
1560 case 'b': /* "db", "cb" */
1561 case 'B': /* implemented as B */
1562 if (c == 'b')
1563 vi_back_motion(command);
1564 else
1565 vi_Back_motion(command);
1566 while (sc-- > cursor)
1567 input_delete(1);
1568 break;
1569 case ' ': /* "d ", "c " */
1570 input_delete(1);
1571 break;
1572 case '$': /* "d$", "c$" */
1573 clear_to_eol:
1574 while (cursor < len)
1575 input_delete(1);
1576 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001577 }
1578 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001579 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001580 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001581 input_forward();
1582 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001583 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001584 put();
1585 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001586 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001587 if (safe_read(0, &c, 1) < 1)
1588 goto prepare_to_die;
1589 if (c == 0)
1590 beep();
1591 else {
1592 *(command + cursor) = c;
1593 putchar(c);
1594 putchar('\b');
1595 }
1596 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001597#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001598
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001599 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001600
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001601#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001602 if (vi_mode) {
1603 /* ESC: insert mode --> command mode */
1604 vi_cmdmode = 1;
1605 input_backward(1);
1606 break;
1607 }
1608#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001609 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001610 if (safe_read(0, &c, 1) < 1)
1611 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001612 /* different vt100 emulations */
1613 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001614 vi_case('['|vbit:)
1615 vi_case('O'|vbit:)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001616 if (safe_read(0, &c, 1) < 1)
1617 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001618 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001619 if (c >= '1' && c <= '9') {
1620 unsigned char dummy;
1621
1622 if (safe_read(0, &dummy, 1) < 1)
1623 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001624 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001625 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001626 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001627
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001628 switch (c) {
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001629#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001630 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001631 input_tab(&lastWasTab);
1632 break;
1633#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001634#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001635 case 'A':
1636 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001637 if (cur_history > 0) {
1638 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001639 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001640 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001641 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001642 break;
1643 case 'B':
1644 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001645 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001646 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001647 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001648 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001649 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001650 len = strlen(strcpy(command, history[cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001651 /* redraw and go to eol (bol, in vi */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001652 redraw(cmdedit_y, vi_mode ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001653 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001654#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001655 case 'C':
1656 /* Right Arrow -- Move forward one character */
1657 input_forward();
1658 break;
1659 case 'D':
1660 /* Left Arrow -- Move back one character */
1661 input_backward(1);
1662 break;
1663 case '3':
1664 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001665 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001666 break;
1667 case '1':
1668 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001669 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001670 input_backward(cursor);
1671 break;
1672 case '4':
1673 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001674 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001675 input_end();
1676 break;
1677 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001678 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001679 beep();
1680 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001681 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001682
Eric Andersenc470f442003-07-28 09:56:35 +00001683 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001684#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001685 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001686 if (c == CTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001687 if (safe_read(0, &c, 1) < 1)
1688 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001689 if (c == 0) {
1690 beep();
1691 break;
1692 }
1693 } else
1694#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001695
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001696#if ENABLE_FEATURE_COMMAND_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001697 if (vi_cmdmode) /* Don't self-insert */
1698 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001699#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001700 if (!Isprint(c)) /* Skip non-printable characters */
1701 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001702
Eric Andersenc470f442003-07-28 09:56:35 +00001703 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001704 break;
1705
1706 len++;
Eric Andersenc470f442003-07-28 09:56:35 +00001707 if (cursor == (len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001708 command[cursor] = c;
1709 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001710 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001711 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001712 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001713
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001714 memmove(command + sc + 1, command + sc, len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001715 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001716 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001717 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001718 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001719 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001720 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001721 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001722 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001723 }
Eric Andersenc470f442003-07-28 09:56:35 +00001724 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001725 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001726
1727 if (c != '\t')
1728 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001729 }
1730
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001731#if MAX_HISTORY > 0
Erik Andersenc7c634b2000-03-19 05:28:55 +00001732 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001733 /* cleanup may be saved current command line */
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001734 if (len > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001735 int i = n_history;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001736
1737 free(history[MAX_HISTORY]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001738 history[MAX_HISTORY] = NULL;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001739 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001740 if (i >= MAX_HISTORY) {
1741 free(history[0]);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001742 for (i = 0; i < MAX_HISTORY-1; i++)
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001743 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001744 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001745// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1746// (i.e. do not save dups?)
Rob Landleyd921b2e2006-08-03 15:41:12 +00001747 history[i++] = xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001748 cur_history = i;
1749 n_history = i;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001750 USE_FEATURE_SH_FANCY_PROMPT(num_ok_lines++;)
Erik Andersen6273f652000-03-17 01:12:41 +00001751 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001752#else /* MAX_HISTORY == 0 */
1753 /* dont put empty line */
1754 USE_FEATURE_SH_FANCY_PROMPT(if (len > 0) num_ok_lines++;)
1755#endif /* MAX_HISTORY */
1756
Eric Andersen27bb7902003-12-23 20:24:51 +00001757 if (break_out > 0) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001758 command[len++] = '\n';
1759 command[len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001760 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001761
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001762#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001763 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001764#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001765
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001766#if ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001767 free(cmdedit_prompt);
1768#endif
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001769 /* restore initial_settings and SIGWINCH handler */
Eric Andersen501c88b2000-07-28 15:14:45 +00001770 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001771 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001772}
1773
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001774#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001775
1776
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001777/*
1778 * Testing
1779 */
1780
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001781#ifdef TEST
1782
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001783#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001784
1785const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001786
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001787int main(int argc, char **argv)
1788{
1789 char buff[BUFSIZ];
1790 char *prompt =
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001791#if ENABLE_FEATURE_SH_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001792 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1793 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1794 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001795#else
1796 "% ";
1797#endif
1798
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001799#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001800 setlocale(LC_ALL, "");
1801#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001802 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001803 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001804 l = cmdedit_read_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001805 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001806 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001807 buff[l-1] = 0;
1808 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001809 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001810 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001811 return 0;
1812}
1813
Eric Andersenc470f442003-07-28 09:56:35 +00001814#endif /* TEST */