blob: 1f2e6a54e83ff09be2d94cc54a0adee006b96523 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000032
Glenn L McGrath475820c2004-01-22 12:42:23 +000033
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000034/* FIXME: obsolete CONFIG item? */
35#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
36
37
Glenn L McGrath3b251852004-01-03 12:07:32 +000038#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000039
Denis Vlasenko38f63192007-01-22 09:03:07 +000040#define ENABLE_FEATURE_EDITING 0
41#define ENABLE_FEATURE_TAB_COMPLETION 0
42#define ENABLE_FEATURE_USERNAME_COMPLETION 0
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000043#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
44#define ENABLE_FEATURE_CLEAN_UP 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +000045
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000046#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000047
Eric Andersen5165fbe2001-02-20 06:42:29 +000048
Denis Vlasenko82b39e82007-01-21 19:18:19 +000049/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko38f63192007-01-22 09:03:07 +000050#if ENABLE_FEATURE_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000051
Denis Vlasenko82b39e82007-01-21 19:18:19 +000052#if ENABLE_LOCALE_SUPPORT
53#define Isprint(c) isprint(c)
54#else
55#define Isprint(c) ((c) >= ' ' && (c) != ((unsigned char)'\233'))
56#endif
57
Denis Vlasenko7e46cf72006-12-23 01:21:55 +000058#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
Denis Vlasenko38f63192007-01-22 09:03:07 +000059(ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000060
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000061enum { MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN };
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 Vlasenko38f63192007-01-22 09:03:07 +000078#if ENABLE_FEATURE_EDITING_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
Denis Vlasenkoab2aea42007-01-29 22:51:58 +000084static char *user_buf = (char*)"";
85static char *home_pwd_buf = (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +000086#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000087
Denis Vlasenko82b39e82007-01-21 19:18:19 +000088/* Put 'command_ps[cursor]', cursor++.
89 * Advance cursor on screen. If we reached right margin, scroll text up
90 * and remove terminal margin effect by printing 'next_char' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000091static void cmdedit_set_out_char(int next_char)
92{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +000093 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +000094
Denis Vlasenko82b39e82007-01-21 19:18:19 +000095 if (c == '\0') {
96 /* erase character after end of input string */
97 c = ' ';
98 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000099#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000100 /* Display non-printable characters in reverse */
101 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000102 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000103 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000104 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000105 c += '@';
106 if (c == 127)
107 c = '?';
108 printf("\033[7m%c\033[0m", c);
109 } else
110#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000111 {
112 if (initial_settings.c_lflag & ECHO)
113 putchar(c);
114 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000115 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000116 /* terminal is scrolled down */
117 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000118 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000119 /* destroy "(auto)margin" */
120 putchar(next_char);
121 putchar('\b');
122 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000123// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000124 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000125}
126
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000127/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000128static void input_end(void)
129{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000130 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000131 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000132}
133
134/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000135static void goto_new_line(void)
136{
Mark Whitley4e338752001-01-26 20:42:23 +0000137 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000138 if (cmdedit_x)
139 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000140}
141
142
Rob Landley88621d72006-08-29 19:41:06 +0000143static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000144{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000145 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000146 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000147}
Eric Andersen81fe1232003-07-29 06:38:40 +0000148
Rob Landley88621d72006-08-29 19:41:06 +0000149static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000150{
151 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000152}
153
Eric Andersenaff114c2004-04-14 17:51:38 +0000154/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000155/* (optimized for slow terminals) */
156static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000157{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000158 int count_y;
159
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000160 if (num > cursor)
161 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000162 if (!num)
163 return;
164 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000165
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000166 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000167 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000168 if (num <= 4) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000169 printf("\b\b\b\b" + (4-num));
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000170 return;
171 }
172 printf("\033[%uD", num);
173 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000174 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000175
176 /* Need to go one or more lines up */
177 num -= cmdedit_x;
178 count_y = 1 + (num / cmdedit_termw);
179 cmdedit_y -= count_y;
180 cmdedit_x = cmdedit_termw * count_y - num;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000181 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000182 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000183}
184
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000185static void put_prompt(void)
186{
187 out1str(cmdedit_prompt);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000188 cmdedit_x = cmdedit_prmt_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000189 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000190// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000191 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000192}
193
Eric Andersenaff114c2004-04-14 17:51:38 +0000194/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000195static void redraw(int y, int back_cursor)
196{
Eric Andersenc470f442003-07-28 09:56:35 +0000197 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000198 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000199 putchar('\r');
200 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000201 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000202 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000203 input_backward(back_cursor);
204}
205
Denis Vlasenko38f63192007-01-22 09:03:07 +0000206#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000207#define DELBUFSIZ 128
208static char *delbuf; /* a (malloced) place to store deleted characters */
209static char *delp;
210static char newdelflag; /* whether delbuf should be reused yet */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000211#endif
212
213/* Delete the char in front of the cursor, optionally saving it
214 * for later putback */
215static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000216{
Mark Whitley4e338752001-01-26 20:42:23 +0000217 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000218
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000219 if (j == command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000220 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000221
Denis Vlasenko38f63192007-01-22 09:03:07 +0000222#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000223 if (save) {
224 if (newdelflag) {
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000225 if (!delbuf)
226 delbuf = malloc(DELBUFSIZ);
227 /* safe if malloc fails */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000228 delp = delbuf;
229 newdelflag = 0;
230 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000231 if (delbuf && (delp - delbuf < DELBUFSIZ))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000232 *delp++ = command_ps[j];
233 }
234#endif
235
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000236 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000237 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000238 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000239 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000240 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000241}
242
Denis Vlasenko38f63192007-01-22 09:03:07 +0000243#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000244static void put(void)
245{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000246 int ocursor;
247 int j = delp - delbuf;
248
Paul Fox3f11b1b2005-08-04 19:04:46 +0000249 if (j == 0)
250 return;
251 ocursor = cursor;
252 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000253 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000254 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000255 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000256 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000257 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000258}
259#endif
260
Mark Whitley4e338752001-01-26 20:42:23 +0000261/* Delete the char in back of the cursor */
262static void input_backspace(void)
263{
264 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000265 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000266 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000267 }
268}
269
Eric Andersenaff114c2004-04-14 17:51:38 +0000270/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000271static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000272{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000273 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000274 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000275}
276
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000277
Denis Vlasenko38f63192007-01-22 09:03:07 +0000278#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000279
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000280static char **matches;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000281static unsigned num_matches;
282
283static void free_tab_completion_data(void)
284{
285 if (matches) {
286 while (num_matches)
287 free(matches[--num_matches]);
288 free(matches);
289 matches = NULL;
290 }
291}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000292
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000293static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000294{
295 int nm = num_matches;
296 int nm1 = nm + 1;
297
298 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000299 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000300 num_matches++;
301}
302
Denis Vlasenko38f63192007-01-22 09:03:07 +0000303#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000304static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000305{
306 struct passwd *entry;
307 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000308
Eric Andersenc470f442003-07-28 09:56:35 +0000309 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000310 userlen = strlen(ud);
311
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000312 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000313 char *sav_ud = ud - 1;
314 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000315 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000316
Eric Andersenc470f442003-07-28 09:56:35 +0000317 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000318 home = home_pwd_buf;
319 } else {
320 /* "~user/..." */
321 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000322 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000323 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000324 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000325 ud = temp;
326 if (entry)
327 home = entry->pw_dir;
328 }
329 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000330 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
331 char temp2[MAX_LINELEN]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000332
333 /* /home/user/... */
334 sprintf(temp2, "%s%s", home, ud);
335 strcpy(sav_ud, temp2);
336 }
337 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000338 } else {
339 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000340 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000341 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000342 struct passwd pwd;
343 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000344
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000345 setpwent();
346 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000347 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000348 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
349 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000350 }
351 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000352 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000353 }
354}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000355#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000356
357enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000358 FIND_EXE_ONLY = 0,
359 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000360 FIND_FILE_ONLY = 2,
361};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000362
Mark Whitley4e338752001-01-26 20:42:23 +0000363static int path_parse(char ***p, int flags)
364{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000365 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000366 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000367 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000368 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000369
Mark Whitley4e338752001-01-26 20:42:23 +0000370 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000371 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000372 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000373
374 if (state->flags & WITH_PATH_LOOKUP)
375 pth = state->path_lookup;
376 else
377 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000378 /* PATH=<empty> or PATH=:<empty> */
379 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
380 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000381
Denis Vlasenko253ce002007-01-22 08:34:44 +0000382 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000383 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000384 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000385 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000386 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000387 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000388 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000389 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000390 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000391 }
392
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000393 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000394 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000395 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000396 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000397 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000398 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000399 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000400 *tmp++ = '\0'; /* ':' -> '\0' */
401 if (*tmp == '\0')
402 break; /* :<empty> */
403 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000404 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000405 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000406 return npth;
407}
408
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000409static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000410{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000411 DIR *dir;
412 struct dirent *next;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000413 char dirbuf[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000414 struct stat st;
415 char *path1[1];
416 char **paths = path1;
417 int npaths;
418 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000419 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000420 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000421
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000422 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000423 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000424
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000425 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000426 /* no dir, if flags==EXE_ONLY - get paths, else "." */
427 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000428 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000429 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000430 /* dirbuf = ".../.../.../" */
431 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000432#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000433 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000434 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000435#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000436 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000437 /* point to 'l' in "..../last_component" */
438 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000439 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000440
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000441 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000442 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000443 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000444 continue;
445
446 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000447 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000448 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000449
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000450 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000451 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000452 continue;
453 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000454 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000455 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000456 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000457 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000458 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000459 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000460 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000461 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000462 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000463 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000464 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000465 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000466
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000467 len1 = strlen(found);
468 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000469 found[len1] = '\0';
470 found[len1+1] = '\0';
471
Mark Whitley4e338752001-01-26 20:42:23 +0000472 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000473 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000474 if (found[len1-1] != '/') {
475 found[len1] = '/';
476 }
Mark Whitley4e338752001-01-26 20:42:23 +0000477 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000478 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000479 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000480 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000481 }
Mark Whitley4e338752001-01-26 20:42:23 +0000482 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000483 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000484 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000485 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000486 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000487 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000488 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000489 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000491 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000492 free(paths);
493 }
Erik Andersen6273f652000-03-17 01:12:41 +0000494}
Erik Andersenf0657d32000-04-12 17:49:52 +0000495
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000496#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000497
498#define collapse_pos(is, in) { \
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000499 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in))*sizeof(int)); \
500 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000501
502static int find_match(char *matchBuf, int *len_with_quotes)
503{
504 int i, j;
505 int command_mode;
506 int c, c2;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000507 int int_buf[MAX_LINELEN + 1];
508 int pos_buf[MAX_LINELEN + 1];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000509
510 /* set to integer dimension characters and own positions */
511 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000512 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000513 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000514 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000515 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000516 }
517 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518 }
519
520 /* mask \+symbol and convert '\t' to ' ' */
521 for (i = j = 0; matchBuf[i]; i++, j++)
522 if (matchBuf[i] == '\\') {
523 collapse_pos(j, j + 1);
524 int_buf[j] |= QUOT;
525 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000526#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000527 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000528 int_buf[j] = ' ' | QUOT;
529#endif
530 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000531#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000532 else if (matchBuf[i] == '\t')
533 int_buf[j] = ' ';
534#endif
535
536 /* mask "symbols" or 'symbols' */
537 c2 = 0;
538 for (i = 0; int_buf[i]; i++) {
539 c = int_buf[i];
540 if (c == '\'' || c == '"') {
541 if (c2 == 0)
542 c2 = c;
543 else {
544 if (c == c2)
545 c2 = 0;
546 else
547 int_buf[i] |= QUOT;
548 }
549 } else if (c2 != 0 && c != '$')
550 int_buf[i] |= QUOT;
551 }
552
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000553 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
555 for (i = 0; int_buf[i]; i++) {
556 c = int_buf[i];
557 c2 = int_buf[i + 1];
558 j = i ? int_buf[i - 1] : -1;
559 command_mode = 0;
560 if (c == ';' || c == '&' || c == '|') {
561 command_mode = 1 + (c == c2);
562 if (c == '&') {
563 if (j == '>' || j == '<')
564 command_mode = 0;
565 } else if (c == '|' && j == '>')
566 command_mode = 0;
567 }
568 if (command_mode) {
569 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000570 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000571 }
572 }
573 /* collapse `command...` */
574 for (i = 0; int_buf[i]; i++)
575 if (int_buf[i] == '`') {
576 for (j = i + 1; int_buf[j]; j++)
577 if (int_buf[j] == '`') {
578 collapse_pos(i, j + 1);
579 j = 0;
580 break;
581 }
582 if (j) {
583 /* not found close ` - command mode, collapse all previous */
584 collapse_pos(0, i + 1);
585 break;
586 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000587 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000588 }
589
590 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000591 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000592 c2 = 0;
593 for (i = 0; int_buf[i]; i++)
594 if (int_buf[i] == '(' || int_buf[i] == '{') {
595 if (int_buf[i] == '(')
596 c++;
597 else
598 c2++;
599 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000600 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000601 }
602 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
603 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
604 if (int_buf[i] == ')')
605 c--;
606 else
607 c2--;
608 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000609 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000610 }
611
612 /* skip first not quote space */
613 for (i = 0; int_buf[i]; i++)
614 if (int_buf[i] != ' ')
615 break;
616 if (i)
617 collapse_pos(0, i);
618
619 /* set find mode for completion */
620 command_mode = FIND_EXE_ONLY;
621 for (i = 0; int_buf[i]; i++)
622 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
623 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000624 && matchBuf[pos_buf[0]]=='c'
625 && matchBuf[pos_buf[1]]=='d'
626 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000627 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000628 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000629 command_mode = FIND_FILE_ONLY;
630 break;
631 }
632 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000633 for (i = 0; int_buf[i]; i++)
634 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000635 /* find last word */
636 for (--i; i >= 0; i--) {
637 c = int_buf[i];
638 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
639 collapse_pos(0, i + 1);
640 break;
641 }
642 }
643 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000644 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
645 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000646 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000647 while ((int_buf[i] & ~QUOT) == '/'
648 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
649 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000650 i++;
651 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000652
653 /* set only match and destroy quotes */
654 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000655 for (c = 0; pos_buf[i] >= 0; i++) {
656 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000657 j = pos_buf[i] + 1;
658 }
Eric Andersen4f990532001-05-31 17:15:57 +0000659 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000660 /* old lenght matchBuf with quotes symbols */
661 *len_with_quotes = j ? j - pos_buf[0] : 0;
662
663 return command_mode;
664}
665
Glenn L McGrath4d001292003-01-06 01:11:50 +0000666/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000667 * display by column (original idea from ls applet,
668 * very optimized by me :)
669 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000670static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000671{
672 int ncols, row;
673 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000674 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000675 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000676 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000677
678 /* find the longest file name- use that as the column width */
679 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000680 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000681 if (column_width < l)
682 column_width = l;
683 }
684 column_width += 2; /* min space for columns */
685 ncols = cmdedit_termw / column_width;
686
687 if (ncols > 1) {
688 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000689 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000690 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000691 } else {
692 ncols = 1;
693 }
694 for (row = 0; row < nrows; row++) {
695 int n = row;
696 int nc;
697
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000698 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000699 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000700 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000701 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000702 printf("%s\n", matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000703 }
704}
705
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000706static char *add_quote_for_spec_chars(char *found)
707{
708 int l = 0;
709 char *s = xmalloc((strlen(found) + 1) * 2);
710
711 while (*found) {
712 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
713 s[l++] = '\\';
714 s[l++] = *found++;
715 }
716 s[l] = 0;
717 return s;
718}
719
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000720static int match_compare(const void *a, const void *b)
721{
722 return strcmp(*(char**)a, *(char**)b);
723}
724
725/* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000726static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000727{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000728 if (!(state->flags & TAB_COMPLETION))
729 return;
730
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000731 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000732 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000733 int len_found;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000734 char matchBuf[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000735 int find_type;
736 int recalc_pos;
737
Eric Andersenc470f442003-07-28 09:56:35 +0000738 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000739
740 /* Make a local copy of the string -- up
741 * to the position of the cursor */
742 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000743 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000744
745 find_type = find_match(matchBuf, &recalc_pos);
746
747 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000748 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000749
Denis Vlasenko38f63192007-01-22 09:03:07 +0000750#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000751 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000752 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000753 if (state->flags & USERNAME_COMPLETION)
754 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
755 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000756#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000757 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000758 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000759 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000760 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000761 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000762 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000763 int i, n = 0;
764 qsort(matches, num_matches, sizeof(char*), match_compare);
765 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000766 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000767 if (strcmp(matches[i], matches[i+1]) == 0) {
768 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000769 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000770 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000771 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000772 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000773 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000774 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000775 matches[n] = matches[i];
776 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000777 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000778 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000779 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000780 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000781 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000782 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000783 /* find minimal match */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000784 // ash: yet another failure in trying to achieve "we don't die on OOM"
Rob Landleyd921b2e2006-08-03 15:41:12 +0000785 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000786 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000787 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000788 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000789 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000790 break;
791 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000792 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000793 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000794 return;
795 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000796 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000797 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000798 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000799 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000800 /* for next completion current found */
801 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000802
803 len_found = strlen(tmp);
804 if (tmp[len_found-1] != '/') {
805 tmp[len_found] = ' ';
806 tmp[len_found+1] = '\0';
807 }
Mark Whitley4e338752001-01-26 20:42:23 +0000808 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000809 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000810 /* have space to placed match? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000811 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000812 /* before word for match */
813 command_ps[cursor - recalc_pos] = 0;
814 /* save tail line */
815 strcpy(matchBuf, command_ps + cursor);
816 /* add match */
817 strcat(command_ps, tmp);
818 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000819 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000820 /* back to begin word for match */
821 input_backward(recalc_pos);
822 /* new pos */
823 recalc_pos = cursor + len_found;
824 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000825 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000826 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000827 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000828 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000829 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +0000830 } else {
831 /* Ok -- the last char was a TAB. Since they
832 * just hit TAB again, print a list of all the
833 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000834 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000835 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000836
837 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000838 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000839 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000840 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000841 }
842 }
843}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000844
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000845#else
846#define input_tab(a) ((void)0)
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000847#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000848
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000849
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000850#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000851
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000852/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000853static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000854{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000855 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
856 free(state->history[state->cur_history]);
857 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000858 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000859 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000860}
861
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000862static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000863{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000864 if (state->flags & DO_HISTORY) {
865 int ch = state->cur_history;
866 if (ch < state->cnt_history) {
867 get_previous_history(); /* save the current history line */
868 state->cur_history = ch + 1;
869 return state->cur_history;
870 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000871 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000872 beep();
873 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000874}
Robert Griebl350d26b2002-12-03 22:45:46 +0000875
Denis Vlasenko38f63192007-01-22 09:03:07 +0000876#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000877/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000878static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000879{
Robert Griebl350d26b2002-12-03 22:45:46 +0000880 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000881 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000882
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000883 /* cleanup old */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000884 for (hi = state->cnt_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000885 hi--;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000886 free(state->history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000887 }
888
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000889 fp = fopen(fromfile, "r");
890 if (fp) {
891 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000892 char *hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000893 int l;
894
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000895 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000896 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000897 l = strlen(hl);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000898 if (l >= MAX_LINELEN)
899 hl[MAX_LINELEN-1] = '\0';
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000900 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000901 free(hl);
902 continue;
903 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000904 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000905 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000906 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000907 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000908 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000909}
910
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000911/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000912static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000913{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000914 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000915
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000916 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000917 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000918 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000919
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000920 for (i = 0; i < state->cnt_history; i++) {
921 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000922 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000923 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000924 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000925}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000926#else
927#define load_history(a) ((void)0)
928#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000929#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +0000930
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000931static void remember_in_history(const char *str)
932{
933 int i;
934
935 if (!(state->flags & DO_HISTORY))
936 return;
937
938 i = state->cnt_history;
939 free(state->history[MAX_HISTORY]);
940 state->history[MAX_HISTORY] = NULL;
941 /* After max history, remove the oldest command */
942 if (i >= MAX_HISTORY) {
943 free(state->history[0]);
944 for (i = 0; i < MAX_HISTORY-1; i++)
945 state->history[i] = state->history[i+1];
946 }
947// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
948// (i.e. do not save dups?)
949 state->history[i++] = xstrdup(str);
950 state->cur_history = i;
951 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +0000952#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +0000953 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000954 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +0000955#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +0000956 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000957}
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 Vlasenko38f63192007-01-22 09:03:07 +0000981#if ENABLE_FEATURE_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{
Denis Vlasenko253ce002007-01-22 08:34:44 +0000985 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000986 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000987 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000988 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 Vlasenko253ce002007-01-22 08:34:44 +0000995 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000996 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000997 input_forward();
998 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +0000999 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001000 input_forward();
1001 }
1002
Denis Vlasenko253ce002007-01-22 08:34:44 +00001003 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001004 input_forward();
1005
Denis Vlasenko253ce002007-01-22 08:34:44 +00001006 if (eat && cursor < command_len && isspace(command[cursor]))
1007 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001008 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();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001015 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001016 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001017 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001018 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{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001024 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001025 return;
1026 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001027 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001028 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001029 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001030 return;
1031 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001032 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001033 && (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 Vlasenko253ce002007-01-22 08:34:44 +00001038 while (cursor < command_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 Vlasenko38f63192007-01-22 09:03:07 +00001080#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001081static 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);
Denis Vlasenko6ca04442007-02-11 16:19:28 +00001094 char *pwd_buf = xrealloc_getcwd_or_warn(NULL);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001095 char buf2[PATH_MAX + 1];
1096 char buf[2];
1097 char c;
1098 char *pbuf;
1099
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001100 cmdedit_prmt_len = 0;
1101
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001102 if (!pwd_buf) {
1103 pwd_buf = (char *)bb_msg_unknown;
1104 }
1105
1106 while (*prmt_ptr) {
1107 pbuf = buf;
1108 pbuf[1] = 0;
1109 c = *prmt_ptr++;
1110 if (c == '\\') {
1111 const char *cp = prmt_ptr;
1112 int l;
1113
1114 c = bb_process_escape_sequence(&prmt_ptr);
1115 if (prmt_ptr == cp) {
1116 if (*cp == 0)
1117 break;
1118 c = *prmt_ptr++;
1119 switch (c) {
1120#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1121 case 'u':
1122 pbuf = user_buf;
1123 break;
1124#endif
1125 case 'h':
1126 pbuf = hostname_buf;
1127 if (!pbuf) {
1128 pbuf = xzalloc(256);
1129 if (gethostname(pbuf, 255) < 0) {
1130 strcpy(pbuf, "?");
1131 } else {
1132 char *s = strchr(pbuf, '.');
1133 if (s)
1134 *s = '\0';
1135 }
1136 hostname_buf = pbuf;
1137 }
1138 break;
1139 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001140 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001141 break;
1142#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1143 case 'w':
1144 pbuf = pwd_buf;
1145 l = strlen(home_pwd_buf);
1146 if (home_pwd_buf[0] != 0
1147 && strncmp(home_pwd_buf, pbuf, l) == 0
1148 && (pbuf[l]=='/' || pbuf[l]=='\0')
1149 && strlen(pwd_buf+l)<PATH_MAX
1150 ) {
1151 pbuf = buf2;
1152 *pbuf = '~';
1153 strcpy(pbuf+1, pwd_buf+l);
1154 }
1155 break;
1156#endif
1157 case 'W':
1158 pbuf = pwd_buf;
1159 cp = strrchr(pbuf,'/');
1160 if (cp != NULL && cp != pbuf)
1161 pbuf += (cp-pbuf) + 1;
1162 break;
1163 case '!':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001164 pbuf = buf2;
1165 snprintf(buf2, sizeof(buf2), "%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001166 break;
1167 case 'e': case 'E': /* \e \E = \033 */
1168 c = '\033';
1169 break;
1170 case 'x': case 'X':
1171 for (l = 0; l < 3;) {
1172 int h;
1173 buf2[l++] = *prmt_ptr;
1174 buf2[l] = 0;
1175 h = strtol(buf2, &pbuf, 16);
1176 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
1177 l--;
1178 break;
1179 }
1180 prmt_ptr++;
1181 }
1182 buf2[l] = 0;
1183 c = (char)strtol(buf2, NULL, 16);
1184 if (c == 0)
1185 c = '?';
1186 pbuf = buf;
1187 break;
1188 case '[': case ']':
1189 if (c == flg_not_length) {
1190 flg_not_length = flg_not_length == '[' ? ']' : '[';
1191 continue;
1192 }
1193 break;
1194 }
1195 }
1196 }
1197 if (pbuf == buf)
1198 *pbuf = c;
1199 cur_prmt_len = strlen(pbuf);
1200 prmt_len += cur_prmt_len;
1201 if (flg_not_length != ']')
1202 cmdedit_prmt_len += cur_prmt_len;
1203 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
1204 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001205 if (pwd_buf != (char *)bb_msg_unknown)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001206 free(pwd_buf);
1207 cmdedit_prompt = prmt_mem_ptr;
1208 put_prompt();
1209}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001210#endif
1211
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001212#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
1213#define getTermSettings(fd, argp) tcgetattr(fd, argp);
1214
1215static sighandler_t previous_SIGWINCH_handler;
1216
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001217static void cmdedit_setwidth(unsigned w, int redraw_flg)
1218{
1219 cmdedit_termw = w;
1220 if (redraw_flg) {
1221 /* new y for current cursor */
1222 int new_y = (cursor + cmdedit_prmt_len) / w;
1223 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001224 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001225 fflush(stdout);
1226 }
1227}
1228
1229static void win_changed(int nsig)
1230{
1231 int width;
1232 get_terminal_width_height(0, &width, NULL);
1233 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1234 if (nsig == SIGWINCH)
1235 signal(SIGWINCH, win_changed); /* rearm ourself */
1236}
1237
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001238/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001239 * The emacs and vi modes share much of the code in the big
1240 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001241 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001242 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001243 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001244 */
1245
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001246#define vbit 0x100
1247
1248/* leave out the "vi-mode"-only case labels if vi editing isn't
1249 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001250#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001251
1252/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001253#undef CTRL
1254#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001255
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001256int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001257{
Erik Andersenc7c634b2000-03-19 05:28:55 +00001258 int lastWasTab = FALSE;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001259 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001260 unsigned char c;
1261 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001262#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001263 smallint vi_cmdmode = 0;
1264 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001265#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001266
1267// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001268 if (maxsize > MAX_LINELEN)
1269 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001270
1271 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001272 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001273#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001274 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001275 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001276#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001277
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001278 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001279 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001280 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001281 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001282 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001283
Eric Andersen34506362001-08-02 05:02:46 +00001284 getTermSettings(0, (void *) &initial_settings);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001285 memcpy(&new_settings, &initial_settings, sizeof(new_settings));
Eric Andersen34506362001-08-02 05:02:46 +00001286 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1287 /* Turn off echoing and CTRL-C, so we can trap it */
1288 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001289 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001290 new_settings.c_cc[VMIN] = 1;
1291 new_settings.c_cc[VTIME] = 0;
1292 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001293#ifndef _POSIX_VDISABLE
1294#define _POSIX_VDISABLE '\0'
1295#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001296 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001297 setTermSettings(0, (void *) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001298
Eric Andersen6faae7d2001-02-16 20:09:17 +00001299 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001300 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1301 win_changed(0); /* do initial resizing */
1302#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1303 {
1304 struct passwd *entry;
1305
1306 entry = getpwuid(geteuid());
1307 if (entry) {
1308 user_buf = xstrdup(entry->pw_name);
1309 home_pwd_buf = xstrdup(entry->pw_dir);
1310 }
1311 }
1312#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001313 /* Print out the command prompt */
1314 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001315
Erik Andersenc7c634b2000-03-19 05:28:55 +00001316 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001317 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001318
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001319 if (safe_read(0, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001320 /* if we can't read input then exit */
1321 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001322 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001323
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001324 ic = c;
1325
Denis Vlasenko38f63192007-01-22 09:03:07 +00001326#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001327 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001328 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001329 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001330#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001331 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001332 case '\n':
1333 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001334 vi_case('\n'|vbit:)
1335 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001336 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001337 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001338 break_out = 1;
1339 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001340#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001341 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001342 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001343 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001344 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001345 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001346 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001347 vi_case('h'|vbit:)
1348 vi_case('\b'|vbit:)
1349 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001350 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001351 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001352 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001353#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001354 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001355 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001356 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001357 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001358 command_len = 0;
1359 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001360 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001361 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001362 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001363 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001364 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001365 errno = 0;
1366 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001367 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001368 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001369 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001370 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001371 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001372 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001373
1374#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001375 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001376 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001377 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001378 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001379 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001380 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001381 vi_case('l'|vbit:)
1382 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001383 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001384 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001385 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001386#endif
1387
Erik Andersenf0657d32000-04-12 17:49:52 +00001388 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001389 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001390 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001391 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001392 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001393
Erik Andersenc7c634b2000-03-19 05:28:55 +00001394 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001395 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001396 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001397
1398#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001399 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001400 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001401 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001402 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001403 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001404 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001405 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001406 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001407 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001408 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001409 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001410 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001411#endif
1412
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001413#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001414 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001415 vi_case(CTRL('N')|vbit:)
1416 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001417 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001418 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001419 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001420 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001421 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001422 vi_case(CTRL('P')|vbit:)
1423 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001424 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001425 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001426 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001427 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001428 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001429 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001430 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001431#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001432
1433#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001434 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001435 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001436 /* Control-U -- Clear line before cursor */
1437 if (cursor) {
1438 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001439 command_len -= cursor;
1440 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001441 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001442 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001443#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001444 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001445 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001446 /* Control-W -- Remove the last word */
1447 while (cursor > 0 && isspace(command[cursor-1]))
1448 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001449 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001450 input_backspace();
1451 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001452
Denis Vlasenko38f63192007-01-22 09:03:07 +00001453#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001454 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001455 vi_cmdmode = 0;
1456 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001457 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001458 input_backward(cursor);
1459 vi_cmdmode = 0;
1460 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001461 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001462 input_forward();
1463 vi_cmdmode = 0;
1464 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001465 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001466 input_end();
1467 vi_cmdmode = 0;
1468 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001469 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001470 input_delete(1);
1471 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001472 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001473 if (cursor > 0) {
1474 input_backward(1);
1475 input_delete(1);
1476 }
1477 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001478 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001479 vi_Word_motion(command, 1);
1480 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001481 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001482 vi_word_motion(command, 1);
1483 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001484 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001485 vi_End_motion(command);
1486 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001487 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001488 vi_end_motion(command);
1489 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001490 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001491 vi_Back_motion(command);
1492 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001493 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001494 vi_back_motion(command);
1495 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001496 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001497 vi_cmdmode = 0;
1498 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001499 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001500 goto clear_to_eol;
1501
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001502 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001503 vi_cmdmode = 0;
1504 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001505 case 'd'|vbit: {
1506 int nc, sc;
1507 sc = cursor;
1508 prevc = ic;
1509 if (safe_read(0, &c, 1) < 1)
1510 goto prepare_to_die;
1511 if (c == (prevc & 0xff)) {
1512 /* "cc", "dd" */
1513 input_backward(cursor);
1514 goto clear_to_eol;
1515 break;
1516 }
1517 switch (c) {
1518 case 'w':
1519 case 'W':
1520 case 'e':
1521 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001522 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001523 case 'w': /* "dw", "cw" */
1524 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001525 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001526 case 'W': /* 'dW', 'cW' */
1527 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001528 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001529 case 'e': /* 'de', 'ce' */
1530 vi_end_motion(command);
1531 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001532 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001533 case 'E': /* 'dE', 'cE' */
1534 vi_End_motion(command);
1535 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001536 break;
1537 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001538 nc = cursor;
1539 input_backward(cursor - sc);
1540 while (nc-- > cursor)
1541 input_delete(1);
1542 break;
1543 case 'b': /* "db", "cb" */
1544 case 'B': /* implemented as B */
1545 if (c == 'b')
1546 vi_back_motion(command);
1547 else
1548 vi_Back_motion(command);
1549 while (sc-- > cursor)
1550 input_delete(1);
1551 break;
1552 case ' ': /* "d ", "c " */
1553 input_delete(1);
1554 break;
1555 case '$': /* "d$", "c$" */
1556 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001557 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001558 input_delete(1);
1559 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001560 }
1561 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001562 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001563 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001564 input_forward();
1565 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001566 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001567 put();
1568 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001569 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001570 if (safe_read(0, &c, 1) < 1)
1571 goto prepare_to_die;
1572 if (c == 0)
1573 beep();
1574 else {
1575 *(command + cursor) = c;
1576 putchar(c);
1577 putchar('\b');
1578 }
1579 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001580#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001581
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001582 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001583
Denis Vlasenko38f63192007-01-22 09:03:07 +00001584#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001585 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001586 /* ESC: insert mode --> command mode */
1587 vi_cmdmode = 1;
1588 input_backward(1);
1589 break;
1590 }
1591#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001592 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001593 if (safe_read(0, &c, 1) < 1)
1594 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001595 /* different vt100 emulations */
1596 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001597 vi_case('['|vbit:)
1598 vi_case('O'|vbit:)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001599 if (safe_read(0, &c, 1) < 1)
1600 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001601 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001602 if (c >= '1' && c <= '9') {
1603 unsigned char dummy;
1604
1605 if (safe_read(0, &dummy, 1) < 1)
1606 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001607 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001608 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001609 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001610
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001611 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001612#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001613 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001614 input_tab(&lastWasTab);
1615 break;
1616#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001617#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001618 case 'A':
1619 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001620 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001621 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001622 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001623 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001624 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001625 break;
1626 case 'B':
1627 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001628 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001629 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001630 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001631 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001632 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001633 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001634 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001635 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001636 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001637#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001638 case 'C':
1639 /* Right Arrow -- Move forward one character */
1640 input_forward();
1641 break;
1642 case 'D':
1643 /* Left Arrow -- Move back one character */
1644 input_backward(1);
1645 break;
1646 case '3':
1647 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001648 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001649 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001650 case '1': // vt100? linux vt? or what?
1651 case '7': // vt100? linux vt? or what?
1652 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001653 input_backward(cursor);
1654 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001655 case '4': // vt100? linux vt? or what?
1656 case '8': // vt100? linux vt? or what?
1657 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001658 input_end();
1659 break;
1660 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001661 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001662 beep();
1663 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001664 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001665
Eric Andersenc470f442003-07-28 09:56:35 +00001666 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001667#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001668 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001669 if (c == CTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001670 if (safe_read(0, &c, 1) < 1)
1671 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001672 if (c == 0) {
1673 beep();
1674 break;
1675 }
1676 } else
1677#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001678
Denis Vlasenko38f63192007-01-22 09:03:07 +00001679#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001680 if (vi_cmdmode) /* Don't self-insert */
1681 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001682#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001683 if (!Isprint(c)) /* Skip non-printable characters */
1684 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001685
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001686 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001687 break;
1688
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001689 command_len++;
1690 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001691 command[cursor] = c;
1692 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001693 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001694 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001695 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001696
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001697 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001698 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001699 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001700 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001701 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001702 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001703 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001704 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001705 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001706 }
Eric Andersenc470f442003-07-28 09:56:35 +00001707 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001708 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001709
1710 if (c != '\t')
1711 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001712 }
1713
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001714 if (command_len > 0)
1715 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001716
Eric Andersen27bb7902003-12-23 20:24:51 +00001717 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001718 command[command_len++] = '\n';
1719 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001720 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001721
Denis Vlasenko38f63192007-01-22 09:03:07 +00001722#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001723 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001724#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001725
Denis Vlasenko38f63192007-01-22 09:03:07 +00001726#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001727 free((char*)cmdedit_prompt);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001728#endif
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001729 /* restore initial_settings */
1730 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
1731 /* restore SIGWINCH handler */
1732 signal(SIGWINCH, previous_SIGWINCH_handler);
1733 fflush(stdout);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001734 return command_len;
1735}
1736
1737line_input_t *new_line_input_t(int flags)
1738{
1739 line_input_t *n = xzalloc(sizeof(*n));
1740 n->flags = flags;
1741 return n;
1742}
1743
1744#else
1745
1746#undef read_line_input
1747int read_line_input(const char* prompt, char* command, int maxsize)
1748{
1749 fputs(prompt, stdout);
1750 fflush(stdout);
1751 fgets(command, maxsize, stdin);
1752 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001753}
1754
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001755#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001756
1757
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001758/*
1759 * Testing
1760 */
1761
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001762#ifdef TEST
1763
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001764#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001765
1766const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001767
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001768int main(int argc, char **argv)
1769{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001770 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001771 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001772#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001773 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1774 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1775 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001776#else
1777 "% ";
1778#endif
1779
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001780#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001781 setlocale(LC_ALL, "");
1782#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001783 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001784 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001785 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001786 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001787 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001788 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001789 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001790 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001791 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001792 return 0;
1793}
1794
Eric Andersenc470f442003-07-28 09:56:35 +00001795#endif /* TEST */