blob: b950d7f26827732d18abadd5497d2332bc0b6eb9 [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 Vlasenko9a7d38f2007-05-31 22:42:12 +000031//#include <sys/ioctl.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000032#include "libbb.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000033
Glenn L McGrath475820c2004-01-22 12:42:23 +000034
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000035/* FIXME: obsolete CONFIG item? */
36#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
37
38
Glenn L McGrath3b251852004-01-03 12:07:32 +000039#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000040
Denis Vlasenko38f63192007-01-22 09:03:07 +000041#define ENABLE_FEATURE_EDITING 0
42#define ENABLE_FEATURE_TAB_COMPLETION 0
43#define ENABLE_FEATURE_USERNAME_COMPLETION 0
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000044#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
45#define ENABLE_FEATURE_CLEAN_UP 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +000046
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000047#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000048
Eric Andersen5165fbe2001-02-20 06:42:29 +000049
Denis Vlasenko82b39e82007-01-21 19:18:19 +000050/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko38f63192007-01-22 09:03:07 +000051#if ENABLE_FEATURE_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000052
Denis Vlasenko82b39e82007-01-21 19:18:19 +000053#if ENABLE_LOCALE_SUPPORT
54#define Isprint(c) isprint(c)
55#else
56#define Isprint(c) ((c) >= ' ' && (c) != ((unsigned char)'\233'))
57#endif
58
Denis Vlasenko7e46cf72006-12-23 01:21:55 +000059#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
Denis Vlasenko38f63192007-01-22 09:03:07 +000060(ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000061
Robert Griebl350d26b2002-12-03 22:45:46 +000062
Denis Vlasenko8e1c7152007-01-22 07:21:38 +000063static line_input_t *state;
Erik Andersen1d1d9502000-04-21 01:26:49 +000064
Eric Andersen63a86222000-11-07 06:52:13 +000065static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000066
Denis Vlasenko8e1c7152007-01-22 07:21:38 +000067static volatile unsigned cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +000068
Eric Andersenc470f442003-07-28 09:56:35 +000069static int cmdedit_x; /* real x terminal position */
70static int cmdedit_y; /* pseudoreal y terminal position */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +000071static int cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen86349772000-12-18 20:25:50 +000072
Denis Vlasenko8e1c7152007-01-22 07:21:38 +000073static unsigned cursor;
74static unsigned command_len;
Denis Vlasenko82b39e82007-01-21 19:18:19 +000075static char *command_ps;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +000076static const char *cmdedit_prompt;
Eric Andersen5f2c79d2001-02-16 18:36:04 +000077
Denis 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) {
330 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000331 char temp2[BUFSIZ]; /* 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;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000413 char dirbuf[BUFSIZ];
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) { \
Paul Fox574fee42005-07-19 20:41:06 +0000499 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
500 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+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;
507 int int_buf[BUFSIZ + 1];
508 int pos_buf[BUFSIZ + 1];
509
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;
734 char matchBuf[BUFSIZ];
735 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 */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000784 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000785 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000786 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000787 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000788 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000789 break;
790 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000791 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000792 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000793 return;
794 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000795 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000796 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000797 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000798 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000799 /* for next completion current found */
800 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000801
802 len_found = strlen(tmp);
803 if (tmp[len_found-1] != '/') {
804 tmp[len_found] = ' ';
805 tmp[len_found+1] = '\0';
806 }
Mark Whitley4e338752001-01-26 20:42:23 +0000807 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000808 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000809 /* have space to placed match? */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000810 if ((len_found - strlen(matchBuf) + command_len) < BUFSIZ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000811 /* before word for match */
812 command_ps[cursor - recalc_pos] = 0;
813 /* save tail line */
814 strcpy(matchBuf, command_ps + cursor);
815 /* add match */
816 strcat(command_ps, tmp);
817 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000818 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000819 /* back to begin word for match */
820 input_backward(recalc_pos);
821 /* new pos */
822 recalc_pos = cursor + len_found;
823 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000824 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000825 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000826 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000827 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000828 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +0000829 } else {
830 /* Ok -- the last char was a TAB. Since they
831 * just hit TAB again, print a list of all the
832 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000833 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000834 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000835
836 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000837 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000838 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000839 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000840 }
841 }
842}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000843
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000844#else
845#define input_tab(a) ((void)0)
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000846#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000847
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000848
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000849#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000850
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000851/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000852static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000853{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000854 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
855 free(state->history[state->cur_history]);
856 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000857 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000858 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000859}
860
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000861static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000862{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000863 if (state->flags & DO_HISTORY) {
864 int ch = state->cur_history;
865 if (ch < state->cnt_history) {
866 get_previous_history(); /* save the current history line */
867 state->cur_history = ch + 1;
868 return state->cur_history;
869 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000870 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000871 beep();
872 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000873}
Robert Griebl350d26b2002-12-03 22:45:46 +0000874
Denis Vlasenko38f63192007-01-22 09:03:07 +0000875#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000876/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000877static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000878{
Robert Griebl350d26b2002-12-03 22:45:46 +0000879 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000880 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000881
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000882 /* cleanup old */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000883 for (hi = state->cnt_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000884 hi--;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000885 free(state->history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000886 }
887
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000888 fp = fopen(fromfile, "r");
889 if (fp) {
890 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000891 char * hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000892 int l;
893
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000894 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000895 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000896 l = strlen(hl);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000897 if (l >= BUFSIZ)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000898 hl[BUFSIZ-1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000899 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000900 free(hl);
901 continue;
902 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000903 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000904 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000905 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000906 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000907 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000908}
909
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000910/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000911static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000912{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000913 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000914
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000915 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000916 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000917 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000918
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000919 for (i = 0; i < state->cnt_history; i++) {
920 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000921 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000922 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000923 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000924}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000925#else
926#define load_history(a) ((void)0)
927#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000928#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +0000929
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000930static void remember_in_history(const char *str)
931{
932 int i;
933
934 if (!(state->flags & DO_HISTORY))
935 return;
936
937 i = state->cnt_history;
938 free(state->history[MAX_HISTORY]);
939 state->history[MAX_HISTORY] = NULL;
940 /* After max history, remove the oldest command */
941 if (i >= MAX_HISTORY) {
942 free(state->history[0]);
943 for (i = 0; i < MAX_HISTORY-1; i++)
944 state->history[i] = state->history[i+1];
945 }
946// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
947// (i.e. do not save dups?)
948 state->history[i++] = xstrdup(str);
949 state->cur_history = i;
950 state->cnt_history = i;
Denis Vlasenko09221922007-04-15 13:21:01 +0000951#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +0000952 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000953 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +0000954#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +0000955 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000956}
957
958#else /* MAX_HISTORY == 0 */
959#define remember_in_history(a) ((void)0)
960#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000961
962
Erik Andersen6273f652000-03-17 01:12:41 +0000963/*
964 * This function is used to grab a character buffer
965 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +0000966 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +0000967 * a mini readline).
968 *
969 * The following standard commands are not implemented:
970 * ESC-b -- Move back one word
971 * ESC-f -- Move forward one word
972 * ESC-d -- Delete back one word
973 * ESC-h -- Delete forward one word
974 * CTL-t -- Transpose two characters
975 *
Paul Fox3f11b1b2005-08-04 19:04:46 +0000976 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000977 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +0000978 */
Eric Andersenc470f442003-07-28 09:56:35 +0000979
Denis Vlasenko38f63192007-01-22 09:03:07 +0000980#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000981static void
Paul Fox3f11b1b2005-08-04 19:04:46 +0000982vi_Word_motion(char *command, int eat)
983{
Denis Vlasenko253ce002007-01-22 08:34:44 +0000984 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000985 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000986 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000987 input_forward();
988}
989
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000990static void
Paul Fox3f11b1b2005-08-04 19:04:46 +0000991vi_word_motion(char *command, int eat)
992{
993 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +0000994 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000995 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000996 input_forward();
997 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +0000998 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000999 input_forward();
1000 }
1001
Denis Vlasenko253ce002007-01-22 08:34:44 +00001002 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001003 input_forward();
1004
Denis Vlasenko253ce002007-01-22 08:34:44 +00001005 if (eat && cursor < command_len && isspace(command[cursor]))
1006 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001007 input_forward();
1008}
1009
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001010static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001011vi_End_motion(char *command)
1012{
1013 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001014 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001015 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001016 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001017 input_forward();
1018}
1019
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001020static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001021vi_end_motion(char *command)
1022{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001023 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001024 return;
1025 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001026 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001027 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001028 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001029 return;
1030 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001031 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001032 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1033 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001034 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001035 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001036 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001037 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001038 input_forward();
1039 }
1040}
1041
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001042static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001043vi_Back_motion(char *command)
1044{
1045 while (cursor > 0 && isspace(command[cursor-1]))
1046 input_backward(1);
1047 while (cursor > 0 && !isspace(command[cursor-1]))
1048 input_backward(1);
1049}
1050
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001051static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001052vi_back_motion(char *command)
1053{
1054 if (cursor <= 0)
1055 return;
1056 input_backward(1);
1057 while (cursor > 0 && isspace(command[cursor]))
1058 input_backward(1);
1059 if (cursor <= 0)
1060 return;
1061 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001062 while (cursor > 0
1063 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1064 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001065 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001066 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001067 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001068 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001069 input_backward(1);
1070 }
1071}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001072#endif
1073
1074
1075/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001076 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001077 */
1078
Denis Vlasenko38f63192007-01-22 09:03:07 +00001079#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001080static void parse_prompt(const char *prmt_ptr)
1081{
1082 cmdedit_prompt = prmt_ptr;
1083 cmdedit_prmt_len = strlen(prmt_ptr);
1084 put_prompt();
1085}
1086#else
1087static void parse_prompt(const char *prmt_ptr)
1088{
1089 int prmt_len = 0;
1090 size_t cur_prmt_len = 0;
1091 char flg_not_length = '[';
1092 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko6ca04442007-02-11 16:19:28 +00001093 char *pwd_buf = xrealloc_getcwd_or_warn(NULL);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001094 char buf2[PATH_MAX + 1];
1095 char buf[2];
1096 char c;
1097 char *pbuf;
1098
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001099 cmdedit_prmt_len = 0;
1100
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001101 if (!pwd_buf) {
1102 pwd_buf = (char *)bb_msg_unknown;
1103 }
1104
1105 while (*prmt_ptr) {
1106 pbuf = buf;
1107 pbuf[1] = 0;
1108 c = *prmt_ptr++;
1109 if (c == '\\') {
1110 const char *cp = prmt_ptr;
1111 int l;
1112
1113 c = bb_process_escape_sequence(&prmt_ptr);
1114 if (prmt_ptr == cp) {
1115 if (*cp == 0)
1116 break;
1117 c = *prmt_ptr++;
1118 switch (c) {
1119#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1120 case 'u':
1121 pbuf = user_buf;
1122 break;
1123#endif
1124 case 'h':
1125 pbuf = hostname_buf;
1126 if (!pbuf) {
1127 pbuf = xzalloc(256);
1128 if (gethostname(pbuf, 255) < 0) {
1129 strcpy(pbuf, "?");
1130 } else {
1131 char *s = strchr(pbuf, '.');
1132 if (s)
1133 *s = '\0';
1134 }
1135 hostname_buf = pbuf;
1136 }
1137 break;
1138 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001139 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001140 break;
1141#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1142 case 'w':
1143 pbuf = pwd_buf;
1144 l = strlen(home_pwd_buf);
1145 if (home_pwd_buf[0] != 0
1146 && strncmp(home_pwd_buf, pbuf, l) == 0
1147 && (pbuf[l]=='/' || pbuf[l]=='\0')
1148 && strlen(pwd_buf+l)<PATH_MAX
1149 ) {
1150 pbuf = buf2;
1151 *pbuf = '~';
1152 strcpy(pbuf+1, pwd_buf+l);
1153 }
1154 break;
1155#endif
1156 case 'W':
1157 pbuf = pwd_buf;
1158 cp = strrchr(pbuf,'/');
1159 if (cp != NULL && cp != pbuf)
1160 pbuf += (cp-pbuf) + 1;
1161 break;
1162 case '!':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001163 pbuf = buf2;
1164 snprintf(buf2, sizeof(buf2), "%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001165 break;
1166 case 'e': case 'E': /* \e \E = \033 */
1167 c = '\033';
1168 break;
1169 case 'x': case 'X':
1170 for (l = 0; l < 3;) {
1171 int h;
1172 buf2[l++] = *prmt_ptr;
1173 buf2[l] = 0;
1174 h = strtol(buf2, &pbuf, 16);
1175 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
1176 l--;
1177 break;
1178 }
1179 prmt_ptr++;
1180 }
1181 buf2[l] = 0;
1182 c = (char)strtol(buf2, NULL, 16);
1183 if (c == 0)
1184 c = '?';
1185 pbuf = buf;
1186 break;
1187 case '[': case ']':
1188 if (c == flg_not_length) {
1189 flg_not_length = flg_not_length == '[' ? ']' : '[';
1190 continue;
1191 }
1192 break;
1193 }
1194 }
1195 }
1196 if (pbuf == buf)
1197 *pbuf = c;
1198 cur_prmt_len = strlen(pbuf);
1199 prmt_len += cur_prmt_len;
1200 if (flg_not_length != ']')
1201 cmdedit_prmt_len += cur_prmt_len;
1202 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
1203 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001204 if (pwd_buf != (char *)bb_msg_unknown)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001205 free(pwd_buf);
1206 cmdedit_prompt = prmt_mem_ptr;
1207 put_prompt();
1208}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001209#endif
1210
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001211#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
1212#define getTermSettings(fd, argp) tcgetattr(fd, argp);
1213
1214static sighandler_t previous_SIGWINCH_handler;
1215
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001216static void cmdedit_setwidth(unsigned w, int redraw_flg)
1217{
1218 cmdedit_termw = w;
1219 if (redraw_flg) {
1220 /* new y for current cursor */
1221 int new_y = (cursor + cmdedit_prmt_len) / w;
1222 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001223 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001224 fflush(stdout);
1225 }
1226}
1227
1228static void win_changed(int nsig)
1229{
1230 int width;
1231 get_terminal_width_height(0, &width, NULL);
1232 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1233 if (nsig == SIGWINCH)
1234 signal(SIGWINCH, win_changed); /* rearm ourself */
1235}
1236
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001237/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001238 * The emacs and vi modes share much of the code in the big
1239 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001240 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001241 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001242 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001243 */
1244
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001245#define vbit 0x100
1246
1247/* leave out the "vi-mode"-only case labels if vi editing isn't
1248 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001249#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001250
1251/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001252#undef CTRL
1253#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001254
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001255int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001256{
Erik Andersenc7c634b2000-03-19 05:28:55 +00001257 int lastWasTab = FALSE;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001258 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001259 unsigned char c;
1260 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001261#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001262 smallint vi_cmdmode = 0;
1263 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001264#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001265
1266// FIXME: audit & improve this
1267 if (maxsize > BUFSIZ)
1268 maxsize = BUFSIZ;
1269
1270 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001271 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001272#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001273 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001274 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001275#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001276
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001277 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001278 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001279 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001280 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001281 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001282
Eric Andersen34506362001-08-02 05:02:46 +00001283 getTermSettings(0, (void *) &initial_settings);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001284 memcpy(&new_settings, &initial_settings, sizeof(new_settings));
Eric Andersen34506362001-08-02 05:02:46 +00001285 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1286 /* Turn off echoing and CTRL-C, so we can trap it */
1287 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001288 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001289 new_settings.c_cc[VMIN] = 1;
1290 new_settings.c_cc[VTIME] = 0;
1291 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001292#ifndef _POSIX_VDISABLE
1293#define _POSIX_VDISABLE '\0'
1294#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001295 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001296 setTermSettings(0, (void *) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001297
Eric Andersen6faae7d2001-02-16 20:09:17 +00001298 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001299 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1300 win_changed(0); /* do initial resizing */
1301#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1302 {
1303 struct passwd *entry;
1304
1305 entry = getpwuid(geteuid());
1306 if (entry) {
1307 user_buf = xstrdup(entry->pw_name);
1308 home_pwd_buf = xstrdup(entry->pw_dir);
1309 }
1310 }
1311#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001312 /* Print out the command prompt */
1313 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001314
Erik Andersenc7c634b2000-03-19 05:28:55 +00001315 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001316 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001317
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001318 if (safe_read(0, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001319 /* if we can't read input then exit */
1320 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001321 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001322
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001323 ic = c;
1324
Denis Vlasenko38f63192007-01-22 09:03:07 +00001325#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001326 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001327 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001328 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001329#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001330 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001331 case '\n':
1332 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001333 vi_case('\n'|vbit:)
1334 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001335 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001336 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001337 break_out = 1;
1338 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001339#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001340 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001341 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001342 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001343 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001344 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001345 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001346 vi_case('h'|vbit:)
1347 vi_case('\b'|vbit:)
1348 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001349 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001350 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001351 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001352#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001353 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001354 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001355 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001356 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001357 command_len = 0;
1358 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001359 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001360 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001361 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001362 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001363 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001364 errno = 0;
1365 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001366 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001367 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001368 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001369 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001370 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001371 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001372
1373#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001374 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001375 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001376 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001377 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001378 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001379 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001380 vi_case('l'|vbit:)
1381 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001382 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001383 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001384 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001385#endif
1386
Erik Andersenf0657d32000-04-12 17:49:52 +00001387 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001388 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001389 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001390 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001391 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001392
Erik Andersenc7c634b2000-03-19 05:28:55 +00001393 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001394 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001395 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001396
1397#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001398 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001399 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001400 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001401 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001402 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001403 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001404 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001405 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001406 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001407 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001408 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001409 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001410#endif
1411
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001412#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001413 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001414 vi_case(CTRL('N')|vbit:)
1415 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001416 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001417 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001418 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001419 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001420 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001421 vi_case(CTRL('P')|vbit:)
1422 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001423 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001424 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001425 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001426 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001427 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001428 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001429 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001430#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001431
1432#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001433 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001434 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001435 /* Control-U -- Clear line before cursor */
1436 if (cursor) {
1437 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001438 command_len -= cursor;
1439 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001440 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001441 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001442#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001443 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001444 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001445 /* Control-W -- Remove the last word */
1446 while (cursor > 0 && isspace(command[cursor-1]))
1447 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001448 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001449 input_backspace();
1450 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001451
Denis Vlasenko38f63192007-01-22 09:03:07 +00001452#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001453 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001454 vi_cmdmode = 0;
1455 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001456 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001457 input_backward(cursor);
1458 vi_cmdmode = 0;
1459 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001460 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001461 input_forward();
1462 vi_cmdmode = 0;
1463 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001464 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001465 input_end();
1466 vi_cmdmode = 0;
1467 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001468 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001469 input_delete(1);
1470 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001471 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001472 if (cursor > 0) {
1473 input_backward(1);
1474 input_delete(1);
1475 }
1476 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001477 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001478 vi_Word_motion(command, 1);
1479 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001480 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001481 vi_word_motion(command, 1);
1482 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001483 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001484 vi_End_motion(command);
1485 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001486 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001487 vi_end_motion(command);
1488 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001489 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001490 vi_Back_motion(command);
1491 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001492 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001493 vi_back_motion(command);
1494 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001495 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001496 vi_cmdmode = 0;
1497 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001498 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001499 goto clear_to_eol;
1500
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 */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001504 case 'd'|vbit: {
1505 int nc, sc;
1506 sc = cursor;
1507 prevc = ic;
1508 if (safe_read(0, &c, 1) < 1)
1509 goto prepare_to_die;
1510 if (c == (prevc & 0xff)) {
1511 /* "cc", "dd" */
1512 input_backward(cursor);
1513 goto clear_to_eol;
1514 break;
1515 }
1516 switch (c) {
1517 case 'w':
1518 case 'W':
1519 case 'e':
1520 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001521 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001522 case 'w': /* "dw", "cw" */
1523 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001524 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001525 case 'W': /* 'dW', 'cW' */
1526 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001527 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001528 case 'e': /* 'de', 'ce' */
1529 vi_end_motion(command);
1530 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001531 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001532 case 'E': /* 'dE', 'cE' */
1533 vi_End_motion(command);
1534 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001535 break;
1536 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001537 nc = cursor;
1538 input_backward(cursor - sc);
1539 while (nc-- > cursor)
1540 input_delete(1);
1541 break;
1542 case 'b': /* "db", "cb" */
1543 case 'B': /* implemented as B */
1544 if (c == 'b')
1545 vi_back_motion(command);
1546 else
1547 vi_Back_motion(command);
1548 while (sc-- > cursor)
1549 input_delete(1);
1550 break;
1551 case ' ': /* "d ", "c " */
1552 input_delete(1);
1553 break;
1554 case '$': /* "d$", "c$" */
1555 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001556 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001557 input_delete(1);
1558 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001559 }
1560 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001561 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001562 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001563 input_forward();
1564 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001565 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001566 put();
1567 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001568 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001569 if (safe_read(0, &c, 1) < 1)
1570 goto prepare_to_die;
1571 if (c == 0)
1572 beep();
1573 else {
1574 *(command + cursor) = c;
1575 putchar(c);
1576 putchar('\b');
1577 }
1578 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001579#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001580
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001581 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001582
Denis Vlasenko38f63192007-01-22 09:03:07 +00001583#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001584 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001585 /* ESC: insert mode --> command mode */
1586 vi_cmdmode = 1;
1587 input_backward(1);
1588 break;
1589 }
1590#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001591 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001592 if (safe_read(0, &c, 1) < 1)
1593 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001594 /* different vt100 emulations */
1595 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001596 vi_case('['|vbit:)
1597 vi_case('O'|vbit:)
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 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001601 if (c >= '1' && c <= '9') {
1602 unsigned char dummy;
1603
1604 if (safe_read(0, &dummy, 1) < 1)
1605 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001606 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001607 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001608 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001609
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001610 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001611#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001612 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001613 input_tab(&lastWasTab);
1614 break;
1615#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001616#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001617 case 'A':
1618 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001619 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001620 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001621 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001622 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001623 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001624 break;
1625 case 'B':
1626 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001627 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001628 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001629 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001630 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001631 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001632 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001633 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001634 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001635 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001636#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001637 case 'C':
1638 /* Right Arrow -- Move forward one character */
1639 input_forward();
1640 break;
1641 case 'D':
1642 /* Left Arrow -- Move back one character */
1643 input_backward(1);
1644 break;
1645 case '3':
1646 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001647 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001648 break;
1649 case '1':
1650 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001651 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001652 input_backward(cursor);
1653 break;
1654 case '4':
1655 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001656 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001657 input_end();
1658 break;
1659 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001660 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001661 beep();
1662 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001663 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001664
Eric Andersenc470f442003-07-28 09:56:35 +00001665 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001666#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001667 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001668 if (c == CTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001669 if (safe_read(0, &c, 1) < 1)
1670 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001671 if (c == 0) {
1672 beep();
1673 break;
1674 }
1675 } else
1676#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001677
Denis Vlasenko38f63192007-01-22 09:03:07 +00001678#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001679 if (vi_cmdmode) /* Don't self-insert */
1680 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001681#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001682 if (!Isprint(c)) /* Skip non-printable characters */
1683 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001684
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001685 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001686 break;
1687
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001688 command_len++;
1689 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001690 command[cursor] = c;
1691 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001692 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001693 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001694 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001695
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001696 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001697 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001698 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001699 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001700 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001701 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001702 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001703 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001704 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001705 }
Eric Andersenc470f442003-07-28 09:56:35 +00001706 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001707 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001708
1709 if (c != '\t')
1710 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001711 }
1712
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001713 if (command_len > 0)
1714 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001715
Eric Andersen27bb7902003-12-23 20:24:51 +00001716 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001717 command[command_len++] = '\n';
1718 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001719 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001720
Denis Vlasenko38f63192007-01-22 09:03:07 +00001721#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001722 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001723#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001724
Denis Vlasenko38f63192007-01-22 09:03:07 +00001725#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001726 free((char*)cmdedit_prompt);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001727#endif
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001728 /* restore initial_settings */
1729 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
1730 /* restore SIGWINCH handler */
1731 signal(SIGWINCH, previous_SIGWINCH_handler);
1732 fflush(stdout);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001733 return command_len;
1734}
1735
1736line_input_t *new_line_input_t(int flags)
1737{
1738 line_input_t *n = xzalloc(sizeof(*n));
1739 n->flags = flags;
1740 return n;
1741}
1742
1743#else
1744
1745#undef read_line_input
1746int read_line_input(const char* prompt, char* command, int maxsize)
1747{
1748 fputs(prompt, stdout);
1749 fflush(stdout);
1750 fgets(command, maxsize, stdin);
1751 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001752}
1753
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001754#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001755
1756
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001757/*
1758 * Testing
1759 */
1760
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001761#ifdef TEST
1762
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001763#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001764
1765const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001766
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001767int main(int argc, char **argv)
1768{
1769 char buff[BUFSIZ];
1770 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001771#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001772 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1773 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1774 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001775#else
1776 "% ";
1777#endif
1778
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001779#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001780 setlocale(LC_ALL, "");
1781#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001782 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001783 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001784 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001785 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001786 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001787 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001788 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001789 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001790 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001791 return 0;
1792}
1793
Eric Andersenc470f442003-07-28 09:56:35 +00001794#endif /* TEST */