blob: edc0e513c5cd6a06845a8126e69b75e9e3e13f43 [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 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000345 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000346 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000347 struct passwd pwd;
348 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000349
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000350 setpwent();
351 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000352 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000353 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
354 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000355 }
356 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000357 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000358 }
359}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000360#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000361
362enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000363 FIND_EXE_ONLY = 0,
364 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000365 FIND_FILE_ONLY = 2,
366};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000367
Mark Whitley4e338752001-01-26 20:42:23 +0000368static int path_parse(char ***p, int flags)
369{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000370 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000371 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000372 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000373 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000374
Mark Whitley4e338752001-01-26 20:42:23 +0000375 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000376 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000377 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000378
379 if (state->flags & WITH_PATH_LOOKUP)
380 pth = state->path_lookup;
381 else
382 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000383 /* PATH=<empty> or PATH=:<empty> */
384 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
385 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000386
Denis Vlasenko253ce002007-01-22 08:34:44 +0000387 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000388 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000389 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000390 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000391 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000392 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000393 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000394 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000395 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000396 }
397
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000398 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000399 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000400 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000401 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000402 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000403 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000404 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000405 *tmp++ = '\0'; /* ':' -> '\0' */
406 if (*tmp == '\0')
407 break; /* :<empty> */
408 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000409 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000410 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000411 return npth;
412}
413
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000414static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000416 DIR *dir;
417 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000418 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000419 struct stat st;
420 char *path1[1];
421 char **paths = path1;
422 int npaths;
423 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000424 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000425 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000426
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000427 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000428 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000429
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000430 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000431 /* no dir, if flags==EXE_ONLY - get paths, else "." */
432 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000433 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000434 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000435 /* dirbuf = ".../.../.../" */
436 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000437#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000438 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000439 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000440#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000441 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000442 /* point to 'l' in "..../last_component" */
443 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000444 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000445
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000446 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000447 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000448 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000449 continue;
450
451 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000452 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000453 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000454
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000455 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000456 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000457 continue;
458 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000459 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000460 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000461 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000462 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000463 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000464 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000465 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000466 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000467 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000468 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000469 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000470 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000471
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000472 len1 = strlen(found);
473 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000474 found[len1] = '\0';
475 found[len1+1] = '\0';
476
Mark Whitley4e338752001-01-26 20:42:23 +0000477 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000478 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000479 if (found[len1-1] != '/') {
480 found[len1] = '/';
481 }
Mark Whitley4e338752001-01-26 20:42:23 +0000482 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000483 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000484 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000485 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000486 }
Mark Whitley4e338752001-01-26 20:42:23 +0000487 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000488 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000489 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000490 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000491 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000492 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000493 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000494 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000495 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000496 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000497 free(paths);
498 }
Erik Andersen6273f652000-03-17 01:12:41 +0000499}
Erik Andersenf0657d32000-04-12 17:49:52 +0000500
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000501#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000502
503#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000504 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
505 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000506
507static int find_match(char *matchBuf, int *len_with_quotes)
508{
509 int i, j;
510 int command_mode;
511 int c, c2;
512 int int_buf[BUFSIZ + 1];
513 int pos_buf[BUFSIZ + 1];
514
515 /* set to integer dimension characters and own positions */
516 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000517 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000519 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000520 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000521 }
522 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000523 }
524
525 /* mask \+symbol and convert '\t' to ' ' */
526 for (i = j = 0; matchBuf[i]; i++, j++)
527 if (matchBuf[i] == '\\') {
528 collapse_pos(j, j + 1);
529 int_buf[j] |= QUOT;
530 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000531#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000532 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000533 int_buf[j] = ' ' | QUOT;
534#endif
535 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000536#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000537 else if (matchBuf[i] == '\t')
538 int_buf[j] = ' ';
539#endif
540
541 /* mask "symbols" or 'symbols' */
542 c2 = 0;
543 for (i = 0; int_buf[i]; i++) {
544 c = int_buf[i];
545 if (c == '\'' || c == '"') {
546 if (c2 == 0)
547 c2 = c;
548 else {
549 if (c == c2)
550 c2 = 0;
551 else
552 int_buf[i] |= QUOT;
553 }
554 } else if (c2 != 0 && c != '$')
555 int_buf[i] |= QUOT;
556 }
557
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000558 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000559 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
560 for (i = 0; int_buf[i]; i++) {
561 c = int_buf[i];
562 c2 = int_buf[i + 1];
563 j = i ? int_buf[i - 1] : -1;
564 command_mode = 0;
565 if (c == ';' || c == '&' || c == '|') {
566 command_mode = 1 + (c == c2);
567 if (c == '&') {
568 if (j == '>' || j == '<')
569 command_mode = 0;
570 } else if (c == '|' && j == '>')
571 command_mode = 0;
572 }
573 if (command_mode) {
574 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000575 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000576 }
577 }
578 /* collapse `command...` */
579 for (i = 0; int_buf[i]; i++)
580 if (int_buf[i] == '`') {
581 for (j = i + 1; int_buf[j]; j++)
582 if (int_buf[j] == '`') {
583 collapse_pos(i, j + 1);
584 j = 0;
585 break;
586 }
587 if (j) {
588 /* not found close ` - command mode, collapse all previous */
589 collapse_pos(0, i + 1);
590 break;
591 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000592 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000593 }
594
595 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000596 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000597 c2 = 0;
598 for (i = 0; int_buf[i]; i++)
599 if (int_buf[i] == '(' || int_buf[i] == '{') {
600 if (int_buf[i] == '(')
601 c++;
602 else
603 c2++;
604 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000605 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000606 }
607 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
608 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
609 if (int_buf[i] == ')')
610 c--;
611 else
612 c2--;
613 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000614 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000615 }
616
617 /* skip first not quote space */
618 for (i = 0; int_buf[i]; i++)
619 if (int_buf[i] != ' ')
620 break;
621 if (i)
622 collapse_pos(0, i);
623
624 /* set find mode for completion */
625 command_mode = FIND_EXE_ONLY;
626 for (i = 0; int_buf[i]; i++)
627 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
628 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000629 && matchBuf[pos_buf[0]]=='c'
630 && matchBuf[pos_buf[1]]=='d'
631 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000632 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000633 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000634 command_mode = FIND_FILE_ONLY;
635 break;
636 }
637 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000638 for (i = 0; int_buf[i]; i++)
639 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000640 /* find last word */
641 for (--i; i >= 0; i--) {
642 c = int_buf[i];
643 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
644 collapse_pos(0, i + 1);
645 break;
646 }
647 }
648 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000649 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
650 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000651 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000652 while ((int_buf[i] & ~QUOT) == '/'
653 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
654 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000655 i++;
656 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000657
658 /* set only match and destroy quotes */
659 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000660 for (c = 0; pos_buf[i] >= 0; i++) {
661 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000662 j = pos_buf[i] + 1;
663 }
Eric Andersen4f990532001-05-31 17:15:57 +0000664 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000665 /* old lenght matchBuf with quotes symbols */
666 *len_with_quotes = j ? j - pos_buf[0] : 0;
667
668 return command_mode;
669}
670
Glenn L McGrath4d001292003-01-06 01:11:50 +0000671/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000672 * display by column (original idea from ls applet,
673 * very optimized by me :)
674 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000675static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000676{
677 int ncols, row;
678 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000679 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000680 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000681 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000682
683 /* find the longest file name- use that as the column width */
684 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000685 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000686 if (column_width < l)
687 column_width = l;
688 }
689 column_width += 2; /* min space for columns */
690 ncols = cmdedit_termw / column_width;
691
692 if (ncols > 1) {
693 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000694 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000695 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000696 } else {
697 ncols = 1;
698 }
699 for (row = 0; row < nrows; row++) {
700 int n = row;
701 int nc;
702
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000703 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000704 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000705 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000706 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000707 printf("%s\n", matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000708 }
709}
710
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000711static char *add_quote_for_spec_chars(char *found)
712{
713 int l = 0;
714 char *s = xmalloc((strlen(found) + 1) * 2);
715
716 while (*found) {
717 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
718 s[l++] = '\\';
719 s[l++] = *found++;
720 }
721 s[l] = 0;
722 return s;
723}
724
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000725static int match_compare(const void *a, const void *b)
726{
727 return strcmp(*(char**)a, *(char**)b);
728}
729
730/* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000731static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000732{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000733 if (!(state->flags & TAB_COMPLETION))
734 return;
735
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000736 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000737 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000738 int len_found;
739 char matchBuf[BUFSIZ];
740 int find_type;
741 int recalc_pos;
742
Eric Andersenc470f442003-07-28 09:56:35 +0000743 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000744
745 /* Make a local copy of the string -- up
746 * to the position of the cursor */
747 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000748 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000749
750 find_type = find_match(matchBuf, &recalc_pos);
751
752 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000753 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000754
Denis Vlasenko38f63192007-01-22 09:03:07 +0000755#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000756 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000757 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000758 if (state->flags & USERNAME_COMPLETION)
759 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
760 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000761#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000762 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000763 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000764 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000765 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000766 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000767 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000768 int i, n = 0;
769 qsort(matches, num_matches, sizeof(char*), match_compare);
770 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000771 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000772 if (strcmp(matches[i], matches[i+1]) == 0) {
773 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000774 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000775 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000776 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000777 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000778 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000779 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000780 matches[n] = matches[i];
781 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000782 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000783 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000784 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000785 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000786 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000787 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000788 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000789 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000790 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000791 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000792 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000793 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000794 break;
795 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000796 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000797 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000798 return;
799 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000800 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000801 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000802 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000803 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000804 /* for next completion current found */
805 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000806
807 len_found = strlen(tmp);
808 if (tmp[len_found-1] != '/') {
809 tmp[len_found] = ' ';
810 tmp[len_found+1] = '\0';
811 }
Mark Whitley4e338752001-01-26 20:42:23 +0000812 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000813 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000814 /* have space to placed match? */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000815 if ((len_found - strlen(matchBuf) + command_len) < BUFSIZ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000816 /* before word for match */
817 command_ps[cursor - recalc_pos] = 0;
818 /* save tail line */
819 strcpy(matchBuf, command_ps + cursor);
820 /* add match */
821 strcat(command_ps, tmp);
822 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000823 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000824 /* back to begin word for match */
825 input_backward(recalc_pos);
826 /* new pos */
827 recalc_pos = cursor + len_found;
828 /* new len */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000829 command_len = strlen(command_ps);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000830 /* write out the matched command */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000831 redraw(cmdedit_y, command_len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000832 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000833 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +0000834 } else {
835 /* Ok -- the last char was a TAB. Since they
836 * just hit TAB again, print a list of all the
837 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000838 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000839 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000840
841 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000842 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000843 showfiles();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000844 redraw(0, command_len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000845 }
846 }
847}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000848
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000849#else
850#define input_tab(a) ((void)0)
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000851#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000852
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000853
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000854#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000855
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000856/* state->flags is already checked to be nonzero */
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000857static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000858{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000859 if (command_ps[0] != '\0' || state->history[state->cur_history] == NULL) {
860 free(state->history[state->cur_history]);
861 state->history[state->cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000862 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000863 state->cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000864}
865
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000866static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000867{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000868 if (state->flags & DO_HISTORY) {
869 int ch = state->cur_history;
870 if (ch < state->cnt_history) {
871 get_previous_history(); /* save the current history line */
872 state->cur_history = ch + 1;
873 return state->cur_history;
874 }
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000875 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000876 beep();
877 return 0;
Erik Andersenf0657d32000-04-12 17:49:52 +0000878}
Robert Griebl350d26b2002-12-03 22:45:46 +0000879
Denis Vlasenko38f63192007-01-22 09:03:07 +0000880#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000881/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000882static void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000883{
Robert Griebl350d26b2002-12-03 22:45:46 +0000884 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000885 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000886
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000887 /* cleanup old */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000888 for (hi = state->cnt_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000889 hi--;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000890 free(state->history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000891 }
892
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000893 fp = fopen(fromfile, "r");
894 if (fp) {
895 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000896 char * hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000897 int l;
898
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000899 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000900 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000901 l = strlen(hl);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000902 if (l >= BUFSIZ)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000903 hl[BUFSIZ-1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000904 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000905 free(hl);
906 continue;
907 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000908 state->history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000909 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000910 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000911 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000912 state->cur_history = state->cnt_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000913}
914
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000915/* state->flags is already checked to be nonzero */
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000916static void save_history(const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000917{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000918 FILE *fp;
Eric Andersenc470f442003-07-28 09:56:35 +0000919
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000920 fp = fopen(tofile, "w");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000921 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000922 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000923
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000924 for (i = 0; i < state->cnt_history; i++) {
925 fprintf(fp, "%s\n", state->history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000926 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000927 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000928 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000929}
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000930#else
931#define load_history(a) ((void)0)
932#define save_history(a) ((void)0)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000933#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +0000934
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000935static void remember_in_history(const char *str)
936{
937 int i;
938
939 if (!(state->flags & DO_HISTORY))
940 return;
941
942 i = state->cnt_history;
943 free(state->history[MAX_HISTORY]);
944 state->history[MAX_HISTORY] = NULL;
945 /* After max history, remove the oldest command */
946 if (i >= MAX_HISTORY) {
947 free(state->history[0]);
948 for (i = 0; i < MAX_HISTORY-1; i++)
949 state->history[i] = state->history[i+1];
950 }
951// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
952// (i.e. do not save dups?)
953 state->history[i++] = xstrdup(str);
954 state->cur_history = i;
955 state->cnt_history = i;
956 if (state->flags & SAVE_HISTORY)
957 save_history(state->hist_file);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000958 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000959}
960
961#else /* MAX_HISTORY == 0 */
962#define remember_in_history(a) ((void)0)
963#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000964
965
Erik Andersen6273f652000-03-17 01:12:41 +0000966/*
967 * This function is used to grab a character buffer
968 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +0000969 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +0000970 * a mini readline).
971 *
972 * The following standard commands are not implemented:
973 * ESC-b -- Move back one word
974 * ESC-f -- Move forward one word
975 * ESC-d -- Delete back one word
976 * ESC-h -- Delete forward one word
977 * CTL-t -- Transpose two characters
978 *
Paul Fox3f11b1b2005-08-04 19:04:46 +0000979 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000980 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +0000981 */
Eric Andersenc470f442003-07-28 09:56:35 +0000982
Denis Vlasenko38f63192007-01-22 09:03:07 +0000983#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000984static void
Paul Fox3f11b1b2005-08-04 19:04:46 +0000985vi_Word_motion(char *command, int eat)
986{
Denis Vlasenko253ce002007-01-22 08:34:44 +0000987 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000988 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000989 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000990 input_forward();
991}
992
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000993static void
Paul Fox3f11b1b2005-08-04 19:04:46 +0000994vi_word_motion(char *command, int eat)
995{
996 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +0000997 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000998 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000999 input_forward();
1000 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001001 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001002 input_forward();
1003 }
1004
Denis Vlasenko253ce002007-01-22 08:34:44 +00001005 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001006 input_forward();
1007
Denis Vlasenko253ce002007-01-22 08:34:44 +00001008 if (eat && cursor < command_len && isspace(command[cursor]))
1009 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001010 input_forward();
1011}
1012
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001013static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001014vi_End_motion(char *command)
1015{
1016 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001017 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001018 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001019 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001020 input_forward();
1021}
1022
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001023static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001024vi_end_motion(char *command)
1025{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001026 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001027 return;
1028 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001029 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001030 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001031 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001032 return;
1033 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001034 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001035 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1036 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001037 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001038 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001039 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001040 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001041 input_forward();
1042 }
1043}
1044
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001045static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001046vi_Back_motion(char *command)
1047{
1048 while (cursor > 0 && isspace(command[cursor-1]))
1049 input_backward(1);
1050 while (cursor > 0 && !isspace(command[cursor-1]))
1051 input_backward(1);
1052}
1053
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001054static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001055vi_back_motion(char *command)
1056{
1057 if (cursor <= 0)
1058 return;
1059 input_backward(1);
1060 while (cursor > 0 && isspace(command[cursor]))
1061 input_backward(1);
1062 if (cursor <= 0)
1063 return;
1064 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001065 while (cursor > 0
1066 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1067 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001068 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001069 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001070 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001071 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001072 input_backward(1);
1073 }
1074}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001075#endif
1076
1077
1078/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001079 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001080 */
1081
Denis Vlasenko38f63192007-01-22 09:03:07 +00001082#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001083static void parse_prompt(const char *prmt_ptr)
1084{
1085 cmdedit_prompt = prmt_ptr;
1086 cmdedit_prmt_len = strlen(prmt_ptr);
1087 put_prompt();
1088}
1089#else
1090static void parse_prompt(const char *prmt_ptr)
1091{
1092 int prmt_len = 0;
1093 size_t cur_prmt_len = 0;
1094 char flg_not_length = '[';
1095 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko6ca04442007-02-11 16:19:28 +00001096 char *pwd_buf = xrealloc_getcwd_or_warn(NULL);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001097 char buf2[PATH_MAX + 1];
1098 char buf[2];
1099 char c;
1100 char *pbuf;
1101
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001102 cmdedit_prmt_len = 0;
1103
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001104 if (!pwd_buf) {
1105 pwd_buf = (char *)bb_msg_unknown;
1106 }
1107
1108 while (*prmt_ptr) {
1109 pbuf = buf;
1110 pbuf[1] = 0;
1111 c = *prmt_ptr++;
1112 if (c == '\\') {
1113 const char *cp = prmt_ptr;
1114 int l;
1115
1116 c = bb_process_escape_sequence(&prmt_ptr);
1117 if (prmt_ptr == cp) {
1118 if (*cp == 0)
1119 break;
1120 c = *prmt_ptr++;
1121 switch (c) {
1122#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1123 case 'u':
1124 pbuf = user_buf;
1125 break;
1126#endif
1127 case 'h':
1128 pbuf = hostname_buf;
1129 if (!pbuf) {
1130 pbuf = xzalloc(256);
1131 if (gethostname(pbuf, 255) < 0) {
1132 strcpy(pbuf, "?");
1133 } else {
1134 char *s = strchr(pbuf, '.');
1135 if (s)
1136 *s = '\0';
1137 }
1138 hostname_buf = pbuf;
1139 }
1140 break;
1141 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001142 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001143 break;
1144#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1145 case 'w':
1146 pbuf = pwd_buf;
1147 l = strlen(home_pwd_buf);
1148 if (home_pwd_buf[0] != 0
1149 && strncmp(home_pwd_buf, pbuf, l) == 0
1150 && (pbuf[l]=='/' || pbuf[l]=='\0')
1151 && strlen(pwd_buf+l)<PATH_MAX
1152 ) {
1153 pbuf = buf2;
1154 *pbuf = '~';
1155 strcpy(pbuf+1, pwd_buf+l);
1156 }
1157 break;
1158#endif
1159 case 'W':
1160 pbuf = pwd_buf;
1161 cp = strrchr(pbuf,'/');
1162 if (cp != NULL && cp != pbuf)
1163 pbuf += (cp-pbuf) + 1;
1164 break;
1165 case '!':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001166 pbuf = buf2;
1167 snprintf(buf2, sizeof(buf2), "%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001168 break;
1169 case 'e': case 'E': /* \e \E = \033 */
1170 c = '\033';
1171 break;
1172 case 'x': case 'X':
1173 for (l = 0; l < 3;) {
1174 int h;
1175 buf2[l++] = *prmt_ptr;
1176 buf2[l] = 0;
1177 h = strtol(buf2, &pbuf, 16);
1178 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
1179 l--;
1180 break;
1181 }
1182 prmt_ptr++;
1183 }
1184 buf2[l] = 0;
1185 c = (char)strtol(buf2, NULL, 16);
1186 if (c == 0)
1187 c = '?';
1188 pbuf = buf;
1189 break;
1190 case '[': case ']':
1191 if (c == flg_not_length) {
1192 flg_not_length = flg_not_length == '[' ? ']' : '[';
1193 continue;
1194 }
1195 break;
1196 }
1197 }
1198 }
1199 if (pbuf == buf)
1200 *pbuf = c;
1201 cur_prmt_len = strlen(pbuf);
1202 prmt_len += cur_prmt_len;
1203 if (flg_not_length != ']')
1204 cmdedit_prmt_len += cur_prmt_len;
1205 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
1206 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001207 if (pwd_buf != (char *)bb_msg_unknown)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001208 free(pwd_buf);
1209 cmdedit_prompt = prmt_mem_ptr;
1210 put_prompt();
1211}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001212#endif
1213
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001214#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
1215#define getTermSettings(fd, argp) tcgetattr(fd, argp);
1216
1217static sighandler_t previous_SIGWINCH_handler;
1218
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001219static void cmdedit_setwidth(unsigned w, int redraw_flg)
1220{
1221 cmdedit_termw = w;
1222 if (redraw_flg) {
1223 /* new y for current cursor */
1224 int new_y = (cursor + cmdedit_prmt_len) / w;
1225 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001226 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001227 fflush(stdout);
1228 }
1229}
1230
1231static void win_changed(int nsig)
1232{
1233 int width;
1234 get_terminal_width_height(0, &width, NULL);
1235 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1236 if (nsig == SIGWINCH)
1237 signal(SIGWINCH, win_changed); /* rearm ourself */
1238}
1239
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001240/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001241 * The emacs and vi modes share much of the code in the big
1242 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001243 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001244 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001245 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001246 */
1247
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001248#define vbit 0x100
1249
1250/* leave out the "vi-mode"-only case labels if vi editing isn't
1251 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001252#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001253
1254/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001255#undef CTRL
1256#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001257
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001258int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001259{
Erik Andersenc7c634b2000-03-19 05:28:55 +00001260 int lastWasTab = FALSE;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001261 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001262 unsigned char c;
1263 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001264#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001265 smallint vi_cmdmode = 0;
1266 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001267#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001268
1269// FIXME: audit & improve this
1270 if (maxsize > BUFSIZ)
1271 maxsize = BUFSIZ;
1272
1273 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001274 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001275#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001276 if (state->flags & SAVE_HISTORY)
1277 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001278#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001279
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001280 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001281 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001282 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001283 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001284 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001285
Eric Andersen34506362001-08-02 05:02:46 +00001286 getTermSettings(0, (void *) &initial_settings);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001287 memcpy(&new_settings, &initial_settings, sizeof(new_settings));
Eric Andersen34506362001-08-02 05:02:46 +00001288 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1289 /* Turn off echoing and CTRL-C, so we can trap it */
1290 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001291 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001292 new_settings.c_cc[VMIN] = 1;
1293 new_settings.c_cc[VTIME] = 0;
1294 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001295#ifndef _POSIX_VDISABLE
1296#define _POSIX_VDISABLE '\0'
1297#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001298 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001299 setTermSettings(0, (void *) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001300
Eric Andersen6faae7d2001-02-16 20:09:17 +00001301 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001302 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1303 win_changed(0); /* do initial resizing */
1304#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1305 {
1306 struct passwd *entry;
1307
1308 entry = getpwuid(geteuid());
1309 if (entry) {
1310 user_buf = xstrdup(entry->pw_name);
1311 home_pwd_buf = xstrdup(entry->pw_dir);
1312 }
1313 }
1314#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +00001315#if ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001316 my_uid = getuid();
1317 my_gid = getgid();
1318#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001319 /* Print out the command prompt */
1320 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001321
Erik Andersenc7c634b2000-03-19 05:28:55 +00001322 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001323 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001324
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001325 if (safe_read(0, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001326 /* if we can't read input then exit */
1327 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001328 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001329
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001330 ic = c;
1331
Denis Vlasenko38f63192007-01-22 09:03:07 +00001332#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001333 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001334 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001335 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001336#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001337 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001338 case '\n':
1339 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001340 vi_case('\n'|vbit:)
1341 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001342 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001343 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001344 break_out = 1;
1345 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001346#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001347 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001348 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001349 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001350 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001351 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001352 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001353 vi_case('h'|vbit:)
1354 vi_case('\b'|vbit:)
1355 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001356 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001357 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001358 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001359#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001360 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001361 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001362 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001363 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001364 command_len = 0;
1365 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001366 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001367 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001368 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001369 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001370 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001371 errno = 0;
1372 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001373 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001374 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001375 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001376 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001377 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001378 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001379
1380#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001381 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001382 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001383 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001384 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001385 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001386 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001387 vi_case('l'|vbit:)
1388 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001389 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001390 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001391 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001392#endif
1393
Erik Andersenf0657d32000-04-12 17:49:52 +00001394 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001395 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001396 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001397 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001398 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001399
Erik Andersenc7c634b2000-03-19 05:28:55 +00001400 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001401 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001402 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001403
1404#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001405 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001406 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001407 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001408 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001409 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001410 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001411 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001412 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001413 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001414 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001415 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001416 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001417#endif
1418
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001419#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001420 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001421 vi_case(CTRL('N')|vbit:)
1422 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001423 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001424 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001425 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001426 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001427 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001428 vi_case(CTRL('P')|vbit:)
1429 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001430 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001431 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001432 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001433 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001434 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001435 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001436 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001437#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001438
1439#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001440 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001441 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001442 /* Control-U -- Clear line before cursor */
1443 if (cursor) {
1444 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001445 command_len -= cursor;
1446 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001447 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001448 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001449#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001450 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001451 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001452 /* Control-W -- Remove the last word */
1453 while (cursor > 0 && isspace(command[cursor-1]))
1454 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001455 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001456 input_backspace();
1457 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001458
Denis Vlasenko38f63192007-01-22 09:03:07 +00001459#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001460 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001461 vi_cmdmode = 0;
1462 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001463 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001464 input_backward(cursor);
1465 vi_cmdmode = 0;
1466 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001467 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001468 input_forward();
1469 vi_cmdmode = 0;
1470 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001471 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001472 input_end();
1473 vi_cmdmode = 0;
1474 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001475 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001476 input_delete(1);
1477 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001478 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001479 if (cursor > 0) {
1480 input_backward(1);
1481 input_delete(1);
1482 }
1483 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001484 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001485 vi_Word_motion(command, 1);
1486 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001487 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001488 vi_word_motion(command, 1);
1489 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001490 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001491 vi_End_motion(command);
1492 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001493 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001494 vi_end_motion(command);
1495 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001496 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001497 vi_Back_motion(command);
1498 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001499 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001500 vi_back_motion(command);
1501 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001502 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001503 vi_cmdmode = 0;
1504 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001505 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001506 goto clear_to_eol;
1507
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001508 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001509 vi_cmdmode = 0;
1510 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001511 case 'd'|vbit: {
1512 int nc, sc;
1513 sc = cursor;
1514 prevc = ic;
1515 if (safe_read(0, &c, 1) < 1)
1516 goto prepare_to_die;
1517 if (c == (prevc & 0xff)) {
1518 /* "cc", "dd" */
1519 input_backward(cursor);
1520 goto clear_to_eol;
1521 break;
1522 }
1523 switch (c) {
1524 case 'w':
1525 case 'W':
1526 case 'e':
1527 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001528 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001529 case 'w': /* "dw", "cw" */
1530 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001531 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001532 case 'W': /* 'dW', 'cW' */
1533 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001534 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001535 case 'e': /* 'de', 'ce' */
1536 vi_end_motion(command);
1537 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001538 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001539 case 'E': /* 'dE', 'cE' */
1540 vi_End_motion(command);
1541 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001542 break;
1543 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001544 nc = cursor;
1545 input_backward(cursor - sc);
1546 while (nc-- > cursor)
1547 input_delete(1);
1548 break;
1549 case 'b': /* "db", "cb" */
1550 case 'B': /* implemented as B */
1551 if (c == 'b')
1552 vi_back_motion(command);
1553 else
1554 vi_Back_motion(command);
1555 while (sc-- > cursor)
1556 input_delete(1);
1557 break;
1558 case ' ': /* "d ", "c " */
1559 input_delete(1);
1560 break;
1561 case '$': /* "d$", "c$" */
1562 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001563 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001564 input_delete(1);
1565 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001566 }
1567 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001568 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001569 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001570 input_forward();
1571 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001572 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001573 put();
1574 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001575 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001576 if (safe_read(0, &c, 1) < 1)
1577 goto prepare_to_die;
1578 if (c == 0)
1579 beep();
1580 else {
1581 *(command + cursor) = c;
1582 putchar(c);
1583 putchar('\b');
1584 }
1585 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001586#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001587
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001588 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001589
Denis Vlasenko38f63192007-01-22 09:03:07 +00001590#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001591 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001592 /* ESC: insert mode --> command mode */
1593 vi_cmdmode = 1;
1594 input_backward(1);
1595 break;
1596 }
1597#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001598 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001599 if (safe_read(0, &c, 1) < 1)
1600 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001601 /* different vt100 emulations */
1602 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001603 vi_case('['|vbit:)
1604 vi_case('O'|vbit:)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001605 if (safe_read(0, &c, 1) < 1)
1606 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001607 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001608 if (c >= '1' && c <= '9') {
1609 unsigned char dummy;
1610
1611 if (safe_read(0, &dummy, 1) < 1)
1612 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001613 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001614 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001615 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001616
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001617 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001618#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001619 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001620 input_tab(&lastWasTab);
1621 break;
1622#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001623#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001624 case 'A':
1625 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001626 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001627 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001628 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001629 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001630 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001631 break;
1632 case 'B':
1633 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001634 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001635 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001636 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001637 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001638 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001639 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001640 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001641 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001642 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001643#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001644 case 'C':
1645 /* Right Arrow -- Move forward one character */
1646 input_forward();
1647 break;
1648 case 'D':
1649 /* Left Arrow -- Move back one character */
1650 input_backward(1);
1651 break;
1652 case '3':
1653 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001654 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001655 break;
1656 case '1':
1657 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001658 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001659 input_backward(cursor);
1660 break;
1661 case '4':
1662 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001663 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001664 input_end();
1665 break;
1666 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001667 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001668 beep();
1669 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001670 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001671
Eric Andersenc470f442003-07-28 09:56:35 +00001672 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001673#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001674 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001675 if (c == CTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001676 if (safe_read(0, &c, 1) < 1)
1677 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001678 if (c == 0) {
1679 beep();
1680 break;
1681 }
1682 } else
1683#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001684
Denis Vlasenko38f63192007-01-22 09:03:07 +00001685#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001686 if (vi_cmdmode) /* Don't self-insert */
1687 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001688#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001689 if (!Isprint(c)) /* Skip non-printable characters */
1690 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001691
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001692 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001693 break;
1694
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001695 command_len++;
1696 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001697 command[cursor] = c;
1698 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001699 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001700 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001701 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001702
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001703 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001704 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001705 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001706 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001707 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001708 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001709 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001710 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001711 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001712 }
Eric Andersenc470f442003-07-28 09:56:35 +00001713 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001714 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001715
1716 if (c != '\t')
1717 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001718 }
1719
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001720 if (command_len > 0)
1721 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001722
Eric Andersen27bb7902003-12-23 20:24:51 +00001723 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001724 command[command_len++] = '\n';
1725 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001726 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001727
Denis Vlasenko38f63192007-01-22 09:03:07 +00001728#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001729 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001730#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001731
Denis Vlasenko38f63192007-01-22 09:03:07 +00001732#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001733 free((char*)cmdedit_prompt);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001734#endif
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001735 /* restore initial_settings */
1736 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
1737 /* restore SIGWINCH handler */
1738 signal(SIGWINCH, previous_SIGWINCH_handler);
1739 fflush(stdout);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001740 return command_len;
1741}
1742
1743line_input_t *new_line_input_t(int flags)
1744{
1745 line_input_t *n = xzalloc(sizeof(*n));
1746 n->flags = flags;
1747 return n;
1748}
1749
1750#else
1751
1752#undef read_line_input
1753int read_line_input(const char* prompt, char* command, int maxsize)
1754{
1755 fputs(prompt, stdout);
1756 fflush(stdout);
1757 fgets(command, maxsize, stdin);
1758 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001759}
1760
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001761#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001762
1763
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001764/*
1765 * Testing
1766 */
1767
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001768#ifdef TEST
1769
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001770#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001771
1772const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001773
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001774int main(int argc, char **argv)
1775{
1776 char buff[BUFSIZ];
1777 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001778#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001779 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1780 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1781 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001782#else
1783 "% ";
1784#endif
1785
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001786#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001787 setlocale(LC_ALL, "");
1788#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001789 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001790 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001791 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001792 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001793 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001794 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001795 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001796 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001797 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001798 return 0;
1799}
1800
Eric Andersenc470f442003-07-28 09:56:35 +00001801#endif /* TEST */