blob: a018a53ff533246564e48efe47efed4f0d723f68 [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;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001159 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001160 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 Vlasenko6a5377a2007-09-25 18:35:28 +00001256/* Returns:
1257 * -1 on read errors or EOF, or on bare Ctrl-D.
1258 * 0 on ctrl-C,
1259 * >0 length of input string, including terminating '\n'
1260 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001261int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001262{
Erik Andersenc7c634b2000-03-19 05:28:55 +00001263 int lastWasTab = FALSE;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001264 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001265 unsigned char c;
1266 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001267#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001268 smallint vi_cmdmode = 0;
1269 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001270#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001271
1272// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001273 if (maxsize > MAX_LINELEN)
1274 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001275
1276 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001277 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001278#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001279 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001280 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001281#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001282
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001283 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001284 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001285 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001286 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001287 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001288
Eric Andersen34506362001-08-02 05:02:46 +00001289 getTermSettings(0, (void *) &initial_settings);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001290 memcpy(&new_settings, &initial_settings, sizeof(new_settings));
Eric Andersen34506362001-08-02 05:02:46 +00001291 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1292 /* Turn off echoing and CTRL-C, so we can trap it */
1293 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001294 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001295 new_settings.c_cc[VMIN] = 1;
1296 new_settings.c_cc[VTIME] = 0;
1297 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001298#ifndef _POSIX_VDISABLE
1299#define _POSIX_VDISABLE '\0'
1300#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001301 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001302 setTermSettings(0, (void *) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001303
Eric Andersen6faae7d2001-02-16 20:09:17 +00001304 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001305 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1306 win_changed(0); /* do initial resizing */
1307#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1308 {
1309 struct passwd *entry;
1310
1311 entry = getpwuid(geteuid());
1312 if (entry) {
1313 user_buf = xstrdup(entry->pw_name);
1314 home_pwd_buf = xstrdup(entry->pw_dir);
1315 }
1316 }
1317#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001318 /* Print out the command prompt */
1319 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001320
Erik Andersenc7c634b2000-03-19 05:28:55 +00001321 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001322 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001323
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001324 if (safe_read(0, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001325 /* if we can't read input then exit */
1326 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001327 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001328
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001329 ic = c;
1330
Denis Vlasenko38f63192007-01-22 09:03:07 +00001331#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001332 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001333 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001334 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001335#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001336 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001337 case '\n':
1338 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001339 vi_case('\n'|vbit:)
1340 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001341 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001342 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001343 break_out = 1;
1344 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001345#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001346 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001347 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001348 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001349 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001350 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001351 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001352 vi_case('h'|vbit:)
1353 vi_case('\b'|vbit:)
1354 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001355 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001356 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001357 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001358#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001359 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001360 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001361 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001362 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001363 command_len = 0;
1364 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001365 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001366 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001367 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001368 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001369 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001370 errno = 0;
1371 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001372 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001373 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001374 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001375 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001376 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001377 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001378
1379#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001380 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001381 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001382 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001383 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001384 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001385 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001386 vi_case('l'|vbit:)
1387 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001388 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001389 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001390 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001391#endif
1392
Erik Andersenf0657d32000-04-12 17:49:52 +00001393 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001394 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001395 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001396 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001397 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001398
Erik Andersenc7c634b2000-03-19 05:28:55 +00001399 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001400 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001401 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001402
1403#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001404 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001405 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001406 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001407 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001408 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001409 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001410 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001411 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001412 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001413 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001414 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001415 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001416#endif
1417
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001418#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001419 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001420 vi_case(CTRL('N')|vbit:)
1421 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001422 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001423 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001424 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001425 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001426 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001427 vi_case(CTRL('P')|vbit:)
1428 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001429 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001430 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001431 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001432 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001433 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001434 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001435 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001436#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001437
1438#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001439 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001440 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001441 /* Control-U -- Clear line before cursor */
1442 if (cursor) {
1443 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001444 command_len -= cursor;
1445 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001446 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001447 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001448#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001449 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001450 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001451 /* Control-W -- Remove the last word */
1452 while (cursor > 0 && isspace(command[cursor-1]))
1453 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001454 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001455 input_backspace();
1456 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001457
Denis Vlasenko38f63192007-01-22 09:03:07 +00001458#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001459 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001460 vi_cmdmode = 0;
1461 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001462 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001463 input_backward(cursor);
1464 vi_cmdmode = 0;
1465 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001466 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001467 input_forward();
1468 vi_cmdmode = 0;
1469 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001470 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001471 input_end();
1472 vi_cmdmode = 0;
1473 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001474 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001475 input_delete(1);
1476 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001477 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001478 if (cursor > 0) {
1479 input_backward(1);
1480 input_delete(1);
1481 }
1482 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001483 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001484 vi_Word_motion(command, 1);
1485 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001486 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001487 vi_word_motion(command, 1);
1488 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001489 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001490 vi_End_motion(command);
1491 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001492 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001493 vi_end_motion(command);
1494 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001495 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001496 vi_Back_motion(command);
1497 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001498 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001499 vi_back_motion(command);
1500 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001501 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001502 vi_cmdmode = 0;
1503 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001504 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001505 goto clear_to_eol;
1506
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001507 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001508 vi_cmdmode = 0;
1509 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001510 case 'd'|vbit: {
1511 int nc, sc;
1512 sc = cursor;
1513 prevc = ic;
1514 if (safe_read(0, &c, 1) < 1)
1515 goto prepare_to_die;
1516 if (c == (prevc & 0xff)) {
1517 /* "cc", "dd" */
1518 input_backward(cursor);
1519 goto clear_to_eol;
1520 break;
1521 }
1522 switch (c) {
1523 case 'w':
1524 case 'W':
1525 case 'e':
1526 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001527 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001528 case 'w': /* "dw", "cw" */
1529 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001530 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001531 case 'W': /* 'dW', 'cW' */
1532 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001533 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001534 case 'e': /* 'de', 'ce' */
1535 vi_end_motion(command);
1536 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001537 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001538 case 'E': /* 'dE', 'cE' */
1539 vi_End_motion(command);
1540 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001541 break;
1542 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001543 nc = cursor;
1544 input_backward(cursor - sc);
1545 while (nc-- > cursor)
1546 input_delete(1);
1547 break;
1548 case 'b': /* "db", "cb" */
1549 case 'B': /* implemented as B */
1550 if (c == 'b')
1551 vi_back_motion(command);
1552 else
1553 vi_Back_motion(command);
1554 while (sc-- > cursor)
1555 input_delete(1);
1556 break;
1557 case ' ': /* "d ", "c " */
1558 input_delete(1);
1559 break;
1560 case '$': /* "d$", "c$" */
1561 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001562 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001563 input_delete(1);
1564 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001565 }
1566 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001567 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001568 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001569 input_forward();
1570 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001571 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001572 put();
1573 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001574 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001575 if (safe_read(0, &c, 1) < 1)
1576 goto prepare_to_die;
1577 if (c == 0)
1578 beep();
1579 else {
1580 *(command + cursor) = c;
1581 putchar(c);
1582 putchar('\b');
1583 }
1584 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001585#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001586
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001587 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001588
Denis Vlasenko38f63192007-01-22 09:03:07 +00001589#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001590 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001591 /* ESC: insert mode --> command mode */
1592 vi_cmdmode = 1;
1593 input_backward(1);
1594 break;
1595 }
1596#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001597 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001598 if (safe_read(0, &c, 1) < 1)
1599 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001600 /* different vt100 emulations */
1601 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001602 vi_case('['|vbit:)
1603 vi_case('O'|vbit:)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001604 if (safe_read(0, &c, 1) < 1)
1605 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001606 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001607 if (c >= '1' && c <= '9') {
1608 unsigned char dummy;
1609
1610 if (safe_read(0, &dummy, 1) < 1)
1611 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001612 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001613 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001614 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001615
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001616 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001617#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001618 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001619 input_tab(&lastWasTab);
1620 break;
1621#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001622#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001623 case 'A':
1624 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001625 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001626 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001627 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001628 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001629 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001630 break;
1631 case 'B':
1632 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001633 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001634 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001635 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001636 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001637 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001638 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001639 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001640 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001641 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001642#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001643 case 'C':
1644 /* Right Arrow -- Move forward one character */
1645 input_forward();
1646 break;
1647 case 'D':
1648 /* Left Arrow -- Move back one character */
1649 input_backward(1);
1650 break;
1651 case '3':
1652 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001653 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001654 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001655 case '1': // vt100? linux vt? or what?
1656 case '7': // vt100? linux vt? or what?
1657 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001658 input_backward(cursor);
1659 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001660 case '4': // vt100? linux vt? or what?
1661 case '8': // vt100? linux vt? or what?
1662 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001663 input_end();
1664 break;
1665 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001666 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001667 beep();
1668 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001669 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001670
Eric Andersenc470f442003-07-28 09:56:35 +00001671 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001672#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001673 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001674 if (c == CTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001675 if (safe_read(0, &c, 1) < 1)
1676 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001677 if (c == 0) {
1678 beep();
1679 break;
1680 }
1681 } else
1682#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001683
Denis Vlasenko38f63192007-01-22 09:03:07 +00001684#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001685 if (vi_cmdmode) /* Don't self-insert */
1686 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001687#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001688 if (!Isprint(c)) /* Skip non-printable characters */
1689 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001690
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001691 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001692 break;
1693
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001694 command_len++;
1695 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001696 command[cursor] = c;
1697 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001698 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001699 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001700 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001701
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001702 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001703 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001704 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001705 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001706 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001707 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001708 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001709 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001710 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001711 }
Eric Andersenc470f442003-07-28 09:56:35 +00001712 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001713 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001714
1715 if (c != '\t')
1716 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001717 }
1718
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001719 if (command_len > 0)
1720 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001721
Eric Andersen27bb7902003-12-23 20:24:51 +00001722 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001723 command[command_len++] = '\n';
1724 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001725 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001726
Denis Vlasenko38f63192007-01-22 09:03:07 +00001727#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001728 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001729#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001730
Denis Vlasenko38f63192007-01-22 09:03:07 +00001731#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001732 free((char*)cmdedit_prompt);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001733#endif
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001734 /* restore initial_settings */
1735 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
1736 /* restore SIGWINCH handler */
1737 signal(SIGWINCH, previous_SIGWINCH_handler);
1738 fflush(stdout);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001739 return command_len;
1740}
1741
1742line_input_t *new_line_input_t(int flags)
1743{
1744 line_input_t *n = xzalloc(sizeof(*n));
1745 n->flags = flags;
1746 return n;
1747}
1748
1749#else
1750
1751#undef read_line_input
1752int read_line_input(const char* prompt, char* command, int maxsize)
1753{
1754 fputs(prompt, stdout);
1755 fflush(stdout);
1756 fgets(command, maxsize, stdin);
1757 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001758}
1759
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001760#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001761
1762
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001763/*
1764 * Testing
1765 */
1766
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001767#ifdef TEST
1768
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001769#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001770
1771const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001772
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001773int main(int argc, char **argv)
1774{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001775 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001776 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001777#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001778 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1779 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1780 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001781#else
1782 "% ";
1783#endif
1784
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001785#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001786 setlocale(LC_ALL, "");
1787#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001788 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001789 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001790 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001791 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001792 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001793 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001794 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001795 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001796 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001797 return 0;
1798}
1799
Eric Andersenc470f442003-07-28 09:56:35 +00001800#endif /* TEST */