blob: 34389086831d1babd40166c33f0c301d69f14bb5 [file] [log] [blame]
Erik Andersenc7c634b2000-03-19 05:28:55 +00001/* vi: set sw=4 ts=4: */
Erik Andersen13456d12000-03-16 08:09:57 +00002/*
Eric Andersenaff114c2004-04-14 17:51:38 +00003 * Termios command line History and Editing.
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
Eric Andersen81fe1232003-07-29 06:38:40 +00005 * Copyright (c) 1986-2003 may safely be consumed by a BSD or GPL license.
Eric Andersen7467c8d2001-07-12 20:26:32 +00006 * Written by: Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersen5f2c79d2001-02-16 18:36:04 +00007 *
8 * Used ideas:
9 * Adam Rogoyski <rogoyski@cs.utexas.edu>
10 * Dave Cinege <dcinege@psychosis.com>
11 * Jakub Jelinek (c) 1995
Eric Andersen81fe1232003-07-29 06:38:40 +000012 * Erik Andersen <andersen@codepoet.org> (Majorly adjusted for busybox)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000013 *
Erik Andersen13456d12000-03-16 08:09:57 +000014 * This code is 'as is' with no warranty.
Erik Andersen13456d12000-03-16 08:09:57 +000015 */
16
17/*
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000018 Usage and known bugs:
Erik Andersen13456d12000-03-16 08:09:57 +000019 Terminal key codes are not extensive, and more will probably
20 need to be added. This version was created on Debian GNU/Linux 2.x.
21 Delete, Backspace, Home, End, and the arrow keys were tested
22 to work in an Xterm and console. Ctrl-A also works as Home.
Mark Whitley4e338752001-01-26 20:42:23 +000023 Ctrl-E also works as End.
Erik Andersen13456d12000-03-16 08:09:57 +000024
Eric Andersen5f2c79d2001-02-16 18:36:04 +000025 Small bugs (simple effect):
26 - not true viewing if terminal size (x*y symbols) less
Denis Vlasenko00cdbd82007-01-21 19:21:21 +000027 size (prompt + editor's line + 2 symbols)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000028 - not true viewing if length prompt less terminal width
Erik Andersen13456d12000-03-16 08:09:57 +000029 */
30
Eric Andersencbe31da2001-02-20 06:14:08 +000031#include <sys/ioctl.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +000032#include "busybox.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000033
Glenn L McGrath475820c2004-01-22 12:42:23 +000034
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000035/* FIXME: obsolete CONFIG item? */
36#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
37
38
Glenn L McGrath3b251852004-01-03 12:07:32 +000039#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000040
Denis 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 Vlasenko38f63192007-01-22 09:03:07 +000088#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +000089static int my_uid;
90static int my_gid;
Denis Vlasenko82b39e82007-01-21 19:18:19 +000091#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000092
Denis Vlasenko82b39e82007-01-21 19:18:19 +000093/* Put 'command_ps[cursor]', cursor++.
94 * Advance cursor on screen. If we reached right margin, scroll text up
95 * and remove terminal margin effect by printing 'next_char' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000096static void cmdedit_set_out_char(int next_char)
97{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +000098 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +000099
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000100 if (c == '\0') {
101 /* erase character after end of input string */
102 c = ' ';
103 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000104#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000105 /* Display non-printable characters in reverse */
106 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000107 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000108 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000109 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000110 c += '@';
111 if (c == 127)
112 c = '?';
113 printf("\033[7m%c\033[0m", c);
114 } else
115#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000116 {
117 if (initial_settings.c_lflag & ECHO)
118 putchar(c);
119 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000120 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000121 /* terminal is scrolled down */
122 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000123 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000124 /* destroy "(auto)margin" */
125 putchar(next_char);
126 putchar('\b');
127 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000128// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000129 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000130}
131
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000132/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000133static void input_end(void)
134{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000135 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000136 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000137}
138
139/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000140static void goto_new_line(void)
141{
Mark Whitley4e338752001-01-26 20:42:23 +0000142 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000143 if (cmdedit_x)
144 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000145}
146
147
Rob Landley88621d72006-08-29 19:41:06 +0000148static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000149{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000150 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000151 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000152}
Eric Andersen81fe1232003-07-29 06:38:40 +0000153
Rob Landley88621d72006-08-29 19:41:06 +0000154static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000155{
156 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000157}
158
Eric Andersenaff114c2004-04-14 17:51:38 +0000159/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000160/* (optimized for slow terminals) */
161static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000162{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000163 int count_y;
164
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000165 if (num > cursor)
166 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000167 if (!num)
168 return;
169 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000170
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000171 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000172 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000173 if (num <= 4) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000174 printf("\b\b\b\b" + (4-num));
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000175 return;
176 }
177 printf("\033[%uD", num);
178 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000179 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000180
181 /* Need to go one or more lines up */
182 num -= cmdedit_x;
183 count_y = 1 + (num / cmdedit_termw);
184 cmdedit_y -= count_y;
185 cmdedit_x = cmdedit_termw * count_y - num;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000186 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000187 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000188}
189
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000190static void put_prompt(void)
191{
192 out1str(cmdedit_prompt);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000193 cmdedit_x = cmdedit_prmt_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000194 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000195// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000196 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000197}
198
Eric Andersenaff114c2004-04-14 17:51:38 +0000199/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000200static void redraw(int y, int back_cursor)
201{
Eric Andersenc470f442003-07-28 09:56:35 +0000202 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000203 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000204 putchar('\r');
205 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000206 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000207 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000208 input_backward(back_cursor);
209}
210
Denis Vlasenko38f63192007-01-22 09:03:07 +0000211#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000212#define DELBUFSIZ 128
213static char *delbuf; /* a (malloced) place to store deleted characters */
214static char *delp;
215static char newdelflag; /* whether delbuf should be reused yet */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000216#endif
217
218/* Delete the char in front of the cursor, optionally saving it
219 * for later putback */
220static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000221{
Mark Whitley4e338752001-01-26 20:42:23 +0000222 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000223
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000224 if (j == command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000225 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000226
Denis Vlasenko38f63192007-01-22 09:03:07 +0000227#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000228 if (save) {
229 if (newdelflag) {
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000230 if (!delbuf)
231 delbuf = malloc(DELBUFSIZ);
232 /* safe if malloc fails */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000233 delp = delbuf;
234 newdelflag = 0;
235 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000236 if (delbuf && (delp - delbuf < DELBUFSIZ))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000237 *delp++ = command_ps[j];
238 }
239#endif
240
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000241 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000242 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000243 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000244 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000245 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000246}
247
Denis Vlasenko38f63192007-01-22 09:03:07 +0000248#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000249static void put(void)
250{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000251 int ocursor;
252 int j = delp - delbuf;
253
Paul Fox3f11b1b2005-08-04 19:04:46 +0000254 if (j == 0)
255 return;
256 ocursor = cursor;
257 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000258 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000259 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000260 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000261 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000262 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000263}
264#endif
265
Mark Whitley4e338752001-01-26 20:42:23 +0000266/* Delete the char in back of the cursor */
267static void input_backspace(void)
268{
269 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000270 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000271 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000272 }
273}
274
Eric Andersenaff114c2004-04-14 17:51:38 +0000275/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000276static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000277{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000278 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000279 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000280}
281
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000282
Denis Vlasenko38f63192007-01-22 09:03:07 +0000283#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000284
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000285static char **matches;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000286static unsigned num_matches;
287
288static void free_tab_completion_data(void)
289{
290 if (matches) {
291 while (num_matches)
292 free(matches[--num_matches]);
293 free(matches);
294 matches = NULL;
295 }
296}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000297
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000298static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000299{
300 int nm = num_matches;
301 int nm1 = nm + 1;
302
303 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000304 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000305 num_matches++;
306}
307
Denis Vlasenko38f63192007-01-22 09:03:07 +0000308#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000309static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000310{
311 struct passwd *entry;
312 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000313
Eric Andersenc470f442003-07-28 09:56:35 +0000314 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000315 userlen = strlen(ud);
316
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000317 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000318 char *sav_ud = ud - 1;
319 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000320 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000321
Eric Andersenc470f442003-07-28 09:56:35 +0000322 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000323 home = home_pwd_buf;
324 } else {
325 /* "~user/..." */
326 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000327 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000328 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000329 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000330 ud = temp;
331 if (entry)
332 home = entry->pw_dir;
333 }
334 if (home) {
335 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000336 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000337
338 /* /home/user/... */
339 sprintf(temp2, "%s%s", home, ud);
340 strcpy(sav_ud, temp2);
341 }
342 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000343 } else {
344 /* "~[^/]*" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000345 setpwent();
346
347 while ((entry = getpwent()) != NULL) {
348 /* Null usernames should result in all users as possible completions. */
349 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000350 add_match(xasprintf("~%s/", entry->pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000351 }
352 }
353
354 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000355 }
356}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000357#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000358
359enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000360 FIND_EXE_ONLY = 0,
361 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000362 FIND_FILE_ONLY = 2,
363};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000364
Mark Whitley4e338752001-01-26 20:42:23 +0000365static int path_parse(char ***p, int flags)
366{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000367 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000368 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000369 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000370 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000371
Mark Whitley4e338752001-01-26 20:42:23 +0000372 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000373 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000374 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000375
376 if (state->flags & WITH_PATH_LOOKUP)
377 pth = state->path_lookup;
378 else
379 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000380 /* PATH=<empty> or PATH=:<empty> */
381 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
382 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000383
Denis Vlasenko253ce002007-01-22 08:34:44 +0000384 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000385 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000386 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000387 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000388 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000389 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000390 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000391 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000392 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000393 }
394
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000395 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000396 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000397 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000398 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000399 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000400 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000401 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000402 *tmp++ = '\0'; /* ':' -> '\0' */
403 if (*tmp == '\0')
404 break; /* :<empty> */
405 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000406 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000407 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000408 return npth;
409}
410
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000411static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000412{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000413 DIR *dir;
414 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000416 struct stat st;
417 char *path1[1];
418 char **paths = path1;
419 int npaths;
420 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000421 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000422 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000423
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000424 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000425 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000426
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000427 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000428 /* no dir, if flags==EXE_ONLY - get paths, else "." */
429 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000431 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000432 /* dirbuf = ".../.../.../" */
433 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000434#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000435 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000436 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000437#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000438 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000439 /* point to 'l' in "..../last_component" */
440 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000441 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000442
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000443 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000444 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000445 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000446 continue;
447
448 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000449 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000450 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000451
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000452 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000453 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000454 continue;
455 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000456 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000457 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000458 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000459 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000460 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000461 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000462 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000463 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000464 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000465 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000466 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000467 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000468
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000469 len1 = strlen(found);
470 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000471 found[len1] = '\0';
472 found[len1+1] = '\0';
473
Mark Whitley4e338752001-01-26 20:42:23 +0000474 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000475 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000476 if (found[len1-1] != '/') {
477 found[len1] = '/';
478 }
Mark Whitley4e338752001-01-26 20:42:23 +0000479 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000480 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000481 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000482 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000483 }
Mark Whitley4e338752001-01-26 20:42:23 +0000484 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000485 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000486 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000487 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000488 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000489 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000491 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000492 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000493 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000494 free(paths);
495 }
Erik Andersen6273f652000-03-17 01:12:41 +0000496}
Erik Andersenf0657d32000-04-12 17:49:52 +0000497
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000498#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000499
500#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000501 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
502 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000503
504static int find_match(char *matchBuf, int *len_with_quotes)
505{
506 int i, j;
507 int command_mode;
508 int c, c2;
509 int int_buf[BUFSIZ + 1];
510 int pos_buf[BUFSIZ + 1];
511
512 /* set to integer dimension characters and own positions */
513 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000514 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000515 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000516 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000517 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000518 }
519 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000520 }
521
522 /* mask \+symbol and convert '\t' to ' ' */
523 for (i = j = 0; matchBuf[i]; i++, j++)
524 if (matchBuf[i] == '\\') {
525 collapse_pos(j, j + 1);
526 int_buf[j] |= QUOT;
527 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000528#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000529 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000530 int_buf[j] = ' ' | QUOT;
531#endif
532 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000533#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000534 else if (matchBuf[i] == '\t')
535 int_buf[j] = ' ';
536#endif
537
538 /* mask "symbols" or 'symbols' */
539 c2 = 0;
540 for (i = 0; int_buf[i]; i++) {
541 c = int_buf[i];
542 if (c == '\'' || c == '"') {
543 if (c2 == 0)
544 c2 = c;
545 else {
546 if (c == c2)
547 c2 = 0;
548 else
549 int_buf[i] |= QUOT;
550 }
551 } else if (c2 != 0 && c != '$')
552 int_buf[i] |= QUOT;
553 }
554
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000555 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000556 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
557 for (i = 0; int_buf[i]; i++) {
558 c = int_buf[i];
559 c2 = int_buf[i + 1];
560 j = i ? int_buf[i - 1] : -1;
561 command_mode = 0;
562 if (c == ';' || c == '&' || c == '|') {
563 command_mode = 1 + (c == c2);
564 if (c == '&') {
565 if (j == '>' || j == '<')
566 command_mode = 0;
567 } else if (c == '|' && j == '>')
568 command_mode = 0;
569 }
570 if (command_mode) {
571 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000572 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000573 }
574 }
575 /* collapse `command...` */
576 for (i = 0; int_buf[i]; i++)
577 if (int_buf[i] == '`') {
578 for (j = i + 1; int_buf[j]; j++)
579 if (int_buf[j] == '`') {
580 collapse_pos(i, j + 1);
581 j = 0;
582 break;
583 }
584 if (j) {
585 /* not found close ` - command mode, collapse all previous */
586 collapse_pos(0, i + 1);
587 break;
588 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000589 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000590 }
591
592 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000593 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000594 c2 = 0;
595 for (i = 0; int_buf[i]; i++)
596 if (int_buf[i] == '(' || int_buf[i] == '{') {
597 if (int_buf[i] == '(')
598 c++;
599 else
600 c2++;
601 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000602 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000603 }
604 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
605 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
606 if (int_buf[i] == ')')
607 c--;
608 else
609 c2--;
610 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000611 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000612 }
613
614 /* skip first not quote space */
615 for (i = 0; int_buf[i]; i++)
616 if (int_buf[i] != ' ')
617 break;
618 if (i)
619 collapse_pos(0, i);
620
621 /* set find mode for completion */
622 command_mode = FIND_EXE_ONLY;
623 for (i = 0; int_buf[i]; i++)
624 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
625 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000626 && matchBuf[pos_buf[0]]=='c'
627 && matchBuf[pos_buf[1]]=='d'
628 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000629 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000630 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000631 command_mode = FIND_FILE_ONLY;
632 break;
633 }
634 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000635 for (i = 0; int_buf[i]; i++)
636 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000637 /* find last word */
638 for (--i; i >= 0; i--) {
639 c = int_buf[i];
640 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
641 collapse_pos(0, i + 1);
642 break;
643 }
644 }
645 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000646 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
647 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000648 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000649 while ((int_buf[i] & ~QUOT) == '/'
650 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
651 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000652 i++;
653 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000654
655 /* set only match and destroy quotes */
656 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000657 for (c = 0; pos_buf[i] >= 0; i++) {
658 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000659 j = pos_buf[i] + 1;
660 }
Eric Andersen4f990532001-05-31 17:15:57 +0000661 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000662 /* old lenght matchBuf with quotes symbols */
663 *len_with_quotes = j ? j - pos_buf[0] : 0;
664
665 return command_mode;
666}
667
Glenn L McGrath4d001292003-01-06 01:11:50 +0000668/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000669 * display by column (original idea from ls applet,
670 * very optimized by me :)
671 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000672static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000673{
674 int ncols, row;
675 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000676 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000677 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000678 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000679
680 /* find the longest file name- use that as the column width */
681 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000682 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000683 if (column_width < l)
684 column_width = l;
685 }
686 column_width += 2; /* min space for columns */
687 ncols = cmdedit_termw / column_width;
688
689 if (ncols > 1) {
690 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000691 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000692 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000693 } else {
694 ncols = 1;
695 }
696 for (row = 0; row < nrows; row++) {
697 int n = row;
698 int nc;
699
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000700 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000701 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000702 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000703 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000704 printf("%s\n", matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000705 }
706}
707
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000708static char *add_quote_for_spec_chars(char *found)
709{
710 int l = 0;
711 char *s = xmalloc((strlen(found) + 1) * 2);
712
713 while (*found) {
714 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
715 s[l++] = '\\';
716 s[l++] = *found++;
717 }
718 s[l] = 0;
719 return s;
720}
721
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000722static int match_compare(const void *a, const void *b)
723{
724 return strcmp(*(char**)a, *(char**)b);
725}
726
727/* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000728static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000729{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000730 if (!(state->flags & TAB_COMPLETION))
731 return;
732
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000733 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000734 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000735 int len_found;
736 char matchBuf[BUFSIZ];
737 int find_type;
738 int recalc_pos;
739
Eric Andersenc470f442003-07-28 09:56:35 +0000740 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000741
742 /* Make a local copy of the string -- up
743 * to the position of the cursor */
744 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000745 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000746
747 find_type = find_match(matchBuf, &recalc_pos);
748
749 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000750 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000751
Denis Vlasenko38f63192007-01-22 09:03:07 +0000752#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000753 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000754 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000755 if (state->flags & USERNAME_COMPLETION)
756 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
757 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000758#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000759 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000760 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000761 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000762 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000763 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000764 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000765 int i, n = 0;
766 qsort(matches, num_matches, sizeof(char*), match_compare);
767 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000768 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000769 if (strcmp(matches[i], matches[i+1]) == 0) {
770 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000771 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000772 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000773 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000774 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000775 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000776 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000777 matches[n] = matches[i];
778 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000779 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000780 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000781 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000782 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000783 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000784 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000785 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000786 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000787 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000788 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000789 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000790 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000791 break;
792 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000793 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000794 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000795 return;
796 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000797 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000798 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000799 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000800 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000801 /* for next completion current found */
802 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000803
804 len_found = strlen(tmp);
805 if (tmp[len_found-1] != '/') {
806 tmp[len_found] = ' ';
807 tmp[len_found+1] = '\0';
808 }
Mark Whitley4e338752001-01-26 20:42:23 +0000809 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000810 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000811 /* have space to placed match? */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000812 if ((len_found - strlen(matchBuf) + command_len) < BUFSIZ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000813 /* before word for match */
814 command_ps[cursor - recalc_pos] = 0;
815 /* save tail line */
816 strcpy(matchBuf, command_ps + cursor);
817 /* add match */
818 strcat(command_ps, tmp);
819 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000820 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000821 /* back to begin word for match */
822 input_backward(recalc_pos);
823 /* new pos */
824 recalc_pos = cursor + len_found;
825 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000826 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000827 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000828 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000829 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000830 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +0000831 } else {
832 /* Ok -- the last char was a TAB. Since they
833 * just hit TAB again, print a list of all the
834 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000835 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000836 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000837
838 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000839 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000840 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000841 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000842 }
843 }
844}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000845
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000846#else
847#define input_tab(a) ((void)0)
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000848#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000849
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000850
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000851#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000852
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000853/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000854static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000855{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000856 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
857 free(state->history[state->cur_history]);
858 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000859 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000860 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000861}
862
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000863static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000864{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000865 if (state->flags & DO_HISTORY) {
866 int ch = state->cur_history;
867 if (ch < state->cnt_history) {
868 get_previous_history(); /* save the current history line */
869 state->cur_history = ch + 1;
870 return state->cur_history;
871 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000872 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000873 beep();
874 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000875}
Robert Griebl350d26b2002-12-03 22:45:46 +0000876
Denis Vlasenko38f63192007-01-22 09:03:07 +0000877#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000878/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000879static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000880{
Robert Griebl350d26b2002-12-03 22:45:46 +0000881 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000882 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000883
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000884 /* cleanup old */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000885 for (hi = state->cnt_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000886 hi--;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000887 free(state->history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000888 }
889
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000890 fp = fopen(fromfile, "r");
891 if (fp) {
892 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000893 char * hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000894 int l;
895
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000896 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000897 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000898 l = strlen(hl);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000899 if (l >= BUFSIZ)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000900 hl[BUFSIZ-1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000901 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000902 free(hl);
903 continue;
904 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000905 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000906 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000907 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000908 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000909 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000910}
911
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000912/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000913static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000914{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000915 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000916
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000917 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000918 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000919 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000920
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000921 for (i = 0; i < state->cnt_history; i++) {
922 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000923 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000924 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000925 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000926}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000927#else
928#define load_history(a) ((void)0)
929#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000930#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +0000931
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000932static void remember_in_history(const char *str)
933{
934 int i;
935
936 if (!(state->flags & DO_HISTORY))
937 return;
938
939 i = state->cnt_history;
940 free(state->history[MAX_HISTORY]);
941 state->history[MAX_HISTORY] = NULL;
942 /* After max history, remove the oldest command */
943 if (i >= MAX_HISTORY) {
944 free(state->history[0]);
945 for (i = 0; i < MAX_HISTORY-1; i++)
946 state->history[i] = state->history[i+1];
947 }
948// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
949// (i.e. do not save dups?)
950 state->history[i++] = xstrdup(str);
951 state->cur_history = i;
952 state->cnt_history = i;
953 if (state->flags & SAVE_HISTORY)
954 save_history(state->hist_file);
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);
1093 char *pwd_buf = xgetcwd(0);
1094 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 Vlasenko8e1c7152007-01-22 07:21:38 +00001272 if (state->flags & SAVE_HISTORY)
1273 load_history(state->hist_file);
1274
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001275 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001276 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001277 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001278 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001279 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001280
Eric Andersen34506362001-08-02 05:02:46 +00001281 getTermSettings(0, (void *) &initial_settings);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001282 memcpy(&new_settings, &initial_settings, sizeof(new_settings));
Eric Andersen34506362001-08-02 05:02:46 +00001283 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1284 /* Turn off echoing and CTRL-C, so we can trap it */
1285 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001286 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001287 new_settings.c_cc[VMIN] = 1;
1288 new_settings.c_cc[VTIME] = 0;
1289 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001290#ifndef _POSIX_VDISABLE
1291#define _POSIX_VDISABLE '\0'
1292#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001293 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001294 setTermSettings(0, (void *) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001295
Eric Andersen6faae7d2001-02-16 20:09:17 +00001296 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001297 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1298 win_changed(0); /* do initial resizing */
1299#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1300 {
1301 struct passwd *entry;
1302
1303 entry = getpwuid(geteuid());
1304 if (entry) {
1305 user_buf = xstrdup(entry->pw_name);
1306 home_pwd_buf = xstrdup(entry->pw_dir);
1307 }
1308 }
1309#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001310#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001311 my_uid = getuid();
1312 my_gid = getgid();
1313#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001314 /* Print out the command prompt */
1315 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001316
Erik Andersenc7c634b2000-03-19 05:28:55 +00001317 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001318 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001319
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001320 if (safe_read(0, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001321 /* if we can't read input then exit */
1322 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001323 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001324
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001325 ic = c;
1326
Denis Vlasenko38f63192007-01-22 09:03:07 +00001327#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001328 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001329 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001330 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001331#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001332 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001333 case '\n':
1334 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001335 vi_case('\n'|vbit:)
1336 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001337 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001338 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001339 break_out = 1;
1340 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001341#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001342 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001343 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001344 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001345 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001346 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001347 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001348 vi_case('h'|vbit:)
1349 vi_case('\b'|vbit:)
1350 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001351 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001352 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001353 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001354#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001355 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001356 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001357 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001358 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001359 command_len = 0;
1360 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001361 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001362 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001363 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001364 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001365 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001366 errno = 0;
1367 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001368 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001369 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001370 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001371 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001372 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001373 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001374
1375#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001376 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001377 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001378 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001379 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001380 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001381 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001382 vi_case('l'|vbit:)
1383 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001384 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001385 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001386 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001387#endif
1388
Erik Andersenf0657d32000-04-12 17:49:52 +00001389 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001390 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001391 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001392 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001393 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001394
Erik Andersenc7c634b2000-03-19 05:28:55 +00001395 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001396 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001397 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001398
1399#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001400 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001401 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001402 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001403 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001404 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001405 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001406 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001407 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001408 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001409 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001410 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001411 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001412#endif
1413
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001414#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001415 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001416 vi_case(CTRL('N')|vbit:)
1417 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001418 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001419 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001420 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001421 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001422 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001423 vi_case(CTRL('P')|vbit:)
1424 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001425 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001426 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001427 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001428 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001429 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001430 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001431 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001432#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001433
1434#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001435 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001436 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001437 /* Control-U -- Clear line before cursor */
1438 if (cursor) {
1439 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001440 command_len -= cursor;
1441 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001442 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001443 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001444#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001445 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001446 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001447 /* Control-W -- Remove the last word */
1448 while (cursor > 0 && isspace(command[cursor-1]))
1449 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001450 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001451 input_backspace();
1452 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001453
Denis Vlasenko38f63192007-01-22 09:03:07 +00001454#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001455 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001456 vi_cmdmode = 0;
1457 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001458 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001459 input_backward(cursor);
1460 vi_cmdmode = 0;
1461 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001462 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001463 input_forward();
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_end();
1468 vi_cmdmode = 0;
1469 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001470 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001471 input_delete(1);
1472 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001473 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001474 if (cursor > 0) {
1475 input_backward(1);
1476 input_delete(1);
1477 }
1478 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001479 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001480 vi_Word_motion(command, 1);
1481 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001482 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001483 vi_word_motion(command, 1);
1484 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001485 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001486 vi_End_motion(command);
1487 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001488 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001489 vi_end_motion(command);
1490 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001491 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001492 vi_Back_motion(command);
1493 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001494 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001495 vi_back_motion(command);
1496 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001497 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001498 vi_cmdmode = 0;
1499 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001500 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001501 goto clear_to_eol;
1502
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001503 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001504 vi_cmdmode = 0;
1505 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001506 case 'd'|vbit: {
1507 int nc, sc;
1508 sc = cursor;
1509 prevc = ic;
1510 if (safe_read(0, &c, 1) < 1)
1511 goto prepare_to_die;
1512 if (c == (prevc & 0xff)) {
1513 /* "cc", "dd" */
1514 input_backward(cursor);
1515 goto clear_to_eol;
1516 break;
1517 }
1518 switch (c) {
1519 case 'w':
1520 case 'W':
1521 case 'e':
1522 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001523 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001524 case 'w': /* "dw", "cw" */
1525 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001526 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001527 case 'W': /* 'dW', 'cW' */
1528 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001529 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001530 case 'e': /* 'de', 'ce' */
1531 vi_end_motion(command);
1532 input_forward();
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;
1538 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001539 nc = cursor;
1540 input_backward(cursor - sc);
1541 while (nc-- > cursor)
1542 input_delete(1);
1543 break;
1544 case 'b': /* "db", "cb" */
1545 case 'B': /* implemented as B */
1546 if (c == 'b')
1547 vi_back_motion(command);
1548 else
1549 vi_Back_motion(command);
1550 while (sc-- > cursor)
1551 input_delete(1);
1552 break;
1553 case ' ': /* "d ", "c " */
1554 input_delete(1);
1555 break;
1556 case '$': /* "d$", "c$" */
1557 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001558 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001559 input_delete(1);
1560 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001561 }
1562 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001563 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001564 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001565 input_forward();
1566 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001567 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001568 put();
1569 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001570 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001571 if (safe_read(0, &c, 1) < 1)
1572 goto prepare_to_die;
1573 if (c == 0)
1574 beep();
1575 else {
1576 *(command + cursor) = c;
1577 putchar(c);
1578 putchar('\b');
1579 }
1580 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001581#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001582
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001583 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001584
Denis Vlasenko38f63192007-01-22 09:03:07 +00001585#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001586 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001587 /* ESC: insert mode --> command mode */
1588 vi_cmdmode = 1;
1589 input_backward(1);
1590 break;
1591 }
1592#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001593 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001594 if (safe_read(0, &c, 1) < 1)
1595 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001596 /* different vt100 emulations */
1597 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001598 vi_case('['|vbit:)
1599 vi_case('O'|vbit:)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001600 if (safe_read(0, &c, 1) < 1)
1601 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001602 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001603 if (c >= '1' && c <= '9') {
1604 unsigned char dummy;
1605
1606 if (safe_read(0, &dummy, 1) < 1)
1607 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001608 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001609 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001610 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001611
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001612 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001613#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001614 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001615 input_tab(&lastWasTab);
1616 break;
1617#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001618#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001619 case 'A':
1620 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001621 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001622 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001623 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001624 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001625 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001626 break;
1627 case 'B':
1628 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001629 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001630 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001631 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001632 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001633 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001634 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001635 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001636 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001637 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001638#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001639 case 'C':
1640 /* Right Arrow -- Move forward one character */
1641 input_forward();
1642 break;
1643 case 'D':
1644 /* Left Arrow -- Move back one character */
1645 input_backward(1);
1646 break;
1647 case '3':
1648 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001649 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001650 break;
1651 case '1':
1652 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001653 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001654 input_backward(cursor);
1655 break;
1656 case '4':
1657 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001658 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001659 input_end();
1660 break;
1661 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001662 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001663 beep();
1664 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001665 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001666
Eric Andersenc470f442003-07-28 09:56:35 +00001667 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001668#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001669 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001670 if (c == CTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001671 if (safe_read(0, &c, 1) < 1)
1672 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001673 if (c == 0) {
1674 beep();
1675 break;
1676 }
1677 } else
1678#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001679
Denis Vlasenko38f63192007-01-22 09:03:07 +00001680#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001681 if (vi_cmdmode) /* Don't self-insert */
1682 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001683#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001684 if (!Isprint(c)) /* Skip non-printable characters */
1685 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001686
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001687 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001688 break;
1689
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001690 command_len++;
1691 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001692 command[cursor] = c;
1693 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001694 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001695 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001696 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001697
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001698 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001699 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001700 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001701 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001702 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001703 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001704 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001705 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001706 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001707 }
Eric Andersenc470f442003-07-28 09:56:35 +00001708 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001709 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001710
1711 if (c != '\t')
1712 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001713 }
1714
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001715 if (command_len > 0)
1716 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001717
Eric Andersen27bb7902003-12-23 20:24:51 +00001718 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001719 command[command_len++] = '\n';
1720 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001721 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001722
Denis Vlasenko38f63192007-01-22 09:03:07 +00001723#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001724 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001725#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001726
Denis Vlasenko38f63192007-01-22 09:03:07 +00001727#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001728 free((char*)cmdedit_prompt);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001729#endif
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001730 /* restore initial_settings */
1731 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
1732 /* restore SIGWINCH handler */
1733 signal(SIGWINCH, previous_SIGWINCH_handler);
1734 fflush(stdout);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001735 return command_len;
1736}
1737
1738line_input_t *new_line_input_t(int flags)
1739{
1740 line_input_t *n = xzalloc(sizeof(*n));
1741 n->flags = flags;
1742 return n;
1743}
1744
1745#else
1746
1747#undef read_line_input
1748int read_line_input(const char* prompt, char* command, int maxsize)
1749{
1750 fputs(prompt, stdout);
1751 fflush(stdout);
1752 fgets(command, maxsize, stdin);
1753 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001754}
1755
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001756#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001757
1758
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001759/*
1760 * Testing
1761 */
1762
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001763#ifdef TEST
1764
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001765#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001766
1767const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001768
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001769int main(int argc, char **argv)
1770{
1771 char buff[BUFSIZ];
1772 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001773#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001774 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1775 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1776 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001777#else
1778 "% ";
1779#endif
1780
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001781#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001782 setlocale(LC_ALL, "");
1783#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001784 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001785 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001786 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001787 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001788 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001789 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001790 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001791 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001792 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001793 return 0;
1794}
1795
Eric Andersenc470f442003-07-28 09:56:35 +00001796#endif /* TEST */