blob: 2db85d0d48a7b2dfbc82d6a1e9617dd0921eed8f [file] [log] [blame]
Erik Andersenc7c634b2000-03-19 05:28:55 +00001/* vi: set sw=4 ts=4: */
Erik Andersen13456d12000-03-16 08:09:57 +00002/*
Eric Andersenaff114c2004-04-14 17:51:38 +00003 * Termios command line History and Editing.
Erik Andersen13456d12000-03-16 08:09:57 +00004 *
Eric Andersen81fe1232003-07-29 06:38:40 +00005 * Copyright (c) 1986-2003 may safely be consumed by a BSD or GPL license.
Eric Andersen7467c8d2001-07-12 20:26:32 +00006 * Written by: Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersen5f2c79d2001-02-16 18:36:04 +00007 *
8 * Used ideas:
9 * Adam Rogoyski <rogoyski@cs.utexas.edu>
10 * Dave Cinege <dcinege@psychosis.com>
11 * Jakub Jelinek (c) 1995
Eric Andersen81fe1232003-07-29 06:38:40 +000012 * Erik Andersen <andersen@codepoet.org> (Majorly adjusted for busybox)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000013 *
Erik Andersen13456d12000-03-16 08:09:57 +000014 * This code is 'as is' with no warranty.
Erik Andersen13456d12000-03-16 08:09:57 +000015 */
16
17/*
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000018 Usage and known bugs:
Erik Andersen13456d12000-03-16 08:09:57 +000019 Terminal key codes are not extensive, and more will probably
20 need to be added. This version was created on Debian GNU/Linux 2.x.
21 Delete, Backspace, Home, End, and the arrow keys were tested
22 to work in an Xterm and console. Ctrl-A also works as Home.
Mark Whitley4e338752001-01-26 20:42:23 +000023 Ctrl-E also works as End.
Erik Andersen13456d12000-03-16 08:09:57 +000024
Eric Andersen5f2c79d2001-02-16 18:36:04 +000025 Small bugs (simple effect):
26 - not true viewing if terminal size (x*y symbols) less
Denis Vlasenko00cdbd82007-01-21 19:21:21 +000027 size (prompt + editor's line + 2 symbols)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000028 - not true viewing if length prompt less terminal width
Erik Andersen13456d12000-03-16 08:09:57 +000029 */
30
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000032
Glenn L McGrath475820c2004-01-22 12:42:23 +000033
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000034/* FIXME: obsolete CONFIG item? */
35#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
36
37
Glenn L McGrath3b251852004-01-03 12:07:32 +000038#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000039
Denis Vlasenko38f63192007-01-22 09:03:07 +000040#define ENABLE_FEATURE_EDITING 0
41#define ENABLE_FEATURE_TAB_COMPLETION 0
42#define ENABLE_FEATURE_USERNAME_COMPLETION 0
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000043#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
44#define ENABLE_FEATURE_CLEAN_UP 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +000045
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000046#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000047
Eric Andersen5165fbe2001-02-20 06:42:29 +000048
Denis Vlasenko82b39e82007-01-21 19:18:19 +000049/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko38f63192007-01-22 09:03:07 +000050#if ENABLE_FEATURE_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000051
Denis Vlasenko82b39e82007-01-21 19:18:19 +000052#if ENABLE_LOCALE_SUPPORT
53#define Isprint(c) isprint(c)
54#else
55#define Isprint(c) ((c) >= ' ' && (c) != ((unsigned char)'\233'))
56#endif
57
Denis Vlasenko7e46cf72006-12-23 01:21:55 +000058#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
Denis Vlasenko38f63192007-01-22 09:03:07 +000059(ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000060
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000061enum { MAX_LINELEN = CONFIG_FEATURE_EDITING_MAX_LEN };
Robert Griebl350d26b2002-12-03 22:45:46 +000062
Denis Vlasenko8e1c7152007-01-22 07:21:38 +000063static line_input_t *state;
Erik Andersen1d1d9502000-04-21 01:26:49 +000064
Eric Andersen63a86222000-11-07 06:52:13 +000065static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000066
Denis Vlasenko8e1c7152007-01-22 07:21:38 +000067static volatile unsigned cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +000068
Eric Andersenc470f442003-07-28 09:56:35 +000069static int cmdedit_x; /* real x terminal position */
70static int cmdedit_y; /* pseudoreal y terminal position */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +000071static int cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen86349772000-12-18 20:25:50 +000072
Denis Vlasenko8e1c7152007-01-22 07:21:38 +000073static unsigned cursor;
74static unsigned command_len;
Denis Vlasenko82b39e82007-01-21 19:18:19 +000075static char *command_ps;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +000076static const char *cmdedit_prompt;
Eric Andersen5f2c79d2001-02-16 18:36:04 +000077
Denis Vlasenko38f63192007-01-22 09:03:07 +000078#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +000079static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +000080static int num_ok_lines = 1;
81#endif
82
Denis Vlasenko82b39e82007-01-21 19:18:19 +000083#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
Denis Vlasenko86b29ea2007-09-27 10:17:16 +000084static const char null_str[] = "";
85static char *user_buf;
86static char *home_pwd_buf = (char*)null_str;
Denis Vlasenko82b39e82007-01-21 19:18:19 +000087#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +000088
Denis Vlasenko82b39e82007-01-21 19:18:19 +000089/* Put 'command_ps[cursor]', cursor++.
90 * Advance cursor on screen. If we reached right margin, scroll text up
91 * and remove terminal margin effect by printing 'next_char' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000092static void cmdedit_set_out_char(int next_char)
93{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +000094 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +000095
Denis Vlasenko82b39e82007-01-21 19:18:19 +000096 if (c == '\0') {
97 /* erase character after end of input string */
98 c = ' ';
99 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000100#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000101 /* Display non-printable characters in reverse */
102 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000103 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000104 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000105 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000106 c += '@';
107 if (c == 127)
108 c = '?';
109 printf("\033[7m%c\033[0m", c);
110 } else
111#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000112 {
113 if (initial_settings.c_lflag & ECHO)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000114 bb_putchar(c);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000115 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000116 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000117 /* terminal is scrolled down */
118 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000119 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000120 /* destroy "(auto)margin" */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000121 bb_putchar(next_char);
122 bb_putchar('\b');
Mark Whitley4e338752001-01-26 20:42:23 +0000123 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000124// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000125 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000126}
127
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000128/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000129static void input_end(void)
130{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000131 while (cursor < command_len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000132 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000133}
134
135/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000136static void goto_new_line(void)
137{
Mark Whitley4e338752001-01-26 20:42:23 +0000138 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000139 if (cmdedit_x)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000140 bb_putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000141}
142
143
Rob Landley88621d72006-08-29 19:41:06 +0000144static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000145{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000146 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000147 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000148}
Eric Andersen81fe1232003-07-29 06:38:40 +0000149
Rob Landley88621d72006-08-29 19:41:06 +0000150static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000151{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000152 bb_putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000153}
154
Eric Andersenaff114c2004-04-14 17:51:38 +0000155/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000156/* (optimized for slow terminals) */
157static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000158{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000159 int count_y;
160
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000161 if (num > cursor)
162 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000163 if (!num)
164 return;
165 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000166
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000167 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000168 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000169 if (num <= 4) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000170 printf("\b\b\b\b" + (4-num));
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000171 return;
172 }
173 printf("\033[%uD", num);
174 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000175 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000176
177 /* Need to go one or more lines up */
178 num -= cmdedit_x;
179 count_y = 1 + (num / cmdedit_termw);
180 cmdedit_y -= count_y;
181 cmdedit_x = cmdedit_termw * count_y - num;
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000182 /* go to 1st column; go up; go to correct column */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000183 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000184}
185
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000186static void put_prompt(void)
187{
188 out1str(cmdedit_prompt);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000189 cmdedit_x = cmdedit_prmt_len;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000190 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000191// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000192 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000193}
194
Eric Andersenaff114c2004-04-14 17:51:38 +0000195/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000196static void redraw(int y, int back_cursor)
197{
Eric Andersenc470f442003-07-28 09:56:35 +0000198 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000199 printf("\033[%dA", y);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000200 bb_putchar('\r');
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000201 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000202 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000203 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000204 input_backward(back_cursor);
205}
206
Denis Vlasenko38f63192007-01-22 09:03:07 +0000207#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000208#define DELBUFSIZ 128
209static char *delbuf; /* a (malloced) place to store deleted characters */
210static char *delp;
211static char newdelflag; /* whether delbuf should be reused yet */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000212#endif
213
214/* Delete the char in front of the cursor, optionally saving it
215 * for later putback */
216static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000217{
Mark Whitley4e338752001-01-26 20:42:23 +0000218 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000219
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000220 if (j == command_len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000221 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000222
Denis Vlasenko38f63192007-01-22 09:03:07 +0000223#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000224 if (save) {
225 if (newdelflag) {
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000226 if (!delbuf)
227 delbuf = malloc(DELBUFSIZ);
228 /* safe if malloc fails */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000229 delp = delbuf;
230 newdelflag = 0;
231 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000232 if (delbuf && (delp - delbuf < DELBUFSIZ))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000233 *delp++ = command_ps[j];
234 }
235#endif
236
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000237 strcpy(command_ps + j, command_ps + j + 1);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000238 command_len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000239 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000240 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000241 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000242}
243
Denis Vlasenko38f63192007-01-22 09:03:07 +0000244#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000245static void put(void)
246{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000247 int ocursor;
248 int j = delp - delbuf;
249
Paul Fox3f11b1b2005-08-04 19:04:46 +0000250 if (j == 0)
251 return;
252 ocursor = cursor;
253 /* open hole and then fill it */
Denis Vlasenko253ce002007-01-22 08:34:44 +0000254 memmove(command_ps + cursor + j, command_ps + cursor, command_len - cursor + 1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000255 strncpy(command_ps + cursor, delbuf, j);
Denis Vlasenko253ce002007-01-22 08:34:44 +0000256 command_len += j;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000257 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000258 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000259}
260#endif
261
Mark Whitley4e338752001-01-26 20:42:23 +0000262/* Delete the char in back of the cursor */
263static void input_backspace(void)
264{
265 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000266 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000267 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000268 }
269}
270
Eric Andersenaff114c2004-04-14 17:51:38 +0000271/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000272static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000273{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000274 if (cursor < command_len)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000275 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000276}
277
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000278
Denis Vlasenko38f63192007-01-22 09:03:07 +0000279#if ENABLE_FEATURE_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000280
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000281static char **matches;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000282static unsigned num_matches;
283
284static void free_tab_completion_data(void)
285{
286 if (matches) {
287 while (num_matches)
288 free(matches[--num_matches]);
289 free(matches);
290 matches = NULL;
291 }
292}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000293
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000294static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000295{
296 int nm = num_matches;
297 int nm1 = nm + 1;
298
299 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000300 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000301 num_matches++;
302}
303
Denis Vlasenko38f63192007-01-22 09:03:07 +0000304#if ENABLE_FEATURE_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000305static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000306{
307 struct passwd *entry;
308 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000309
Eric Andersenc470f442003-07-28 09:56:35 +0000310 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000311 userlen = strlen(ud);
312
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000313 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000314 char *sav_ud = ud - 1;
Denis Vlasenko86b29ea2007-09-27 10:17:16 +0000315 char *home = NULL;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000316 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000317
Eric Andersenc470f442003-07-28 09:56:35 +0000318 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000319 home = home_pwd_buf;
320 } else {
321 /* "~user/..." */
322 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000323 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000324 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000325 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000326 ud = temp;
327 if (entry)
328 home = entry->pw_dir;
329 }
330 if (home) {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000331 if ((userlen + strlen(home) + 1) < MAX_LINELEN) {
332 char temp2[MAX_LINELEN]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000333
334 /* /home/user/... */
335 sprintf(temp2, "%s%s", home, ud);
336 strcpy(sav_ud, temp2);
337 }
338 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000339 } else {
340 /* "~[^/]*" */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000341 /* Using _r function to avoid pulling in static buffers */
Denis Vlasenko6b343dd2007-03-18 00:57:15 +0000342 char line_buff[256];
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000343 struct passwd pwd;
344 struct passwd *result;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000345
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000346 setpwent();
347 while (!getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000348 /* Null usernames should result in all users as possible completions. */
Denis Vlasenko5df955f2007-03-13 13:01:14 +0000349 if (/*!userlen || */ strncmp(ud, pwd.pw_name, userlen) == 0) {
350 add_match(xasprintf("~%s/", pwd.pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000351 }
352 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000353 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000354 }
355}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000356#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000357
358enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000359 FIND_EXE_ONLY = 0,
360 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000361 FIND_FILE_ONLY = 2,
362};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000363
Mark Whitley4e338752001-01-26 20:42:23 +0000364static int path_parse(char ***p, int flags)
365{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000366 int npth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000367 const char *pth;
Denis Vlasenko253ce002007-01-22 08:34:44 +0000368 char *tmp;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000369 char **res;
Mark Whitley4e338752001-01-26 20:42:23 +0000370
Mark Whitley4e338752001-01-26 20:42:23 +0000371 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000372 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000373 return 1;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000374
375 if (state->flags & WITH_PATH_LOOKUP)
376 pth = state->path_lookup;
377 else
378 pth = getenv("PATH");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000379 /* PATH=<empty> or PATH=:<empty> */
380 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
381 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000382
Denis Vlasenko253ce002007-01-22 08:34:44 +0000383 tmp = (char*)pth;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000384 npth = 1; /* path component count */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000385 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000386 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000387 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000388 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000389 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000390 break; /* :<empty> */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000391 npth++;
Mark Whitley4e338752001-01-26 20:42:23 +0000392 }
393
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000394 res = xmalloc(npth * sizeof(char*));
Denis Vlasenko253ce002007-01-22 08:34:44 +0000395 res[0] = tmp = xstrdup(pth);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000396 npth = 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000397 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000398 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000399 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000400 break;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000401 *tmp++ = '\0'; /* ':' -> '\0' */
402 if (*tmp == '\0')
403 break; /* :<empty> */
404 res[npth++] = tmp;
Mark Whitley4e338752001-01-26 20:42:23 +0000405 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000406 *p = res;
Mark Whitley4e338752001-01-26 20:42:23 +0000407 return npth;
408}
409
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000410static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000411{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000412 DIR *dir;
413 struct dirent *next;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000414 char dirbuf[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415 struct stat st;
416 char *path1[1];
417 char **paths = path1;
418 int npaths;
419 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000420 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000421 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000422
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000423 npaths = 1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000424 path1[0] = (char*)".";
Mark Whitley4e338752001-01-26 20:42:23 +0000425
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000426 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000427 /* no dir, if flags==EXE_ONLY - get paths, else "." */
428 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000429 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000430 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000431 /* dirbuf = ".../.../.../" */
432 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko38f63192007-01-22 09:03:07 +0000433#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000434 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000435 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000436#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000437 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000438 /* point to 'l' in "..../last_component" */
439 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000440 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000441
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000442 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000443 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000444 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000445 continue;
446
447 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000448 int len1;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000449 const char *str_found = next->d_name;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000450
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000451 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000452 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000453 continue;
454 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000455 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000456 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000457 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000458 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000459 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000460 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000461 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000462 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000463 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000464 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000465 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000466 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000467
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000468 len1 = strlen(found);
469 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000470 found[len1] = '\0';
471 found[len1+1] = '\0';
472
Mark Whitley4e338752001-01-26 20:42:23 +0000473 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000474 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000475 if (found[len1-1] != '/') {
476 found[len1] = '/';
477 }
Mark Whitley4e338752001-01-26 20:42:23 +0000478 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000479 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000480 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000481 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000482 }
Mark Whitley4e338752001-01-26 20:42:23 +0000483 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000484 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000485 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000486 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000487 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000488 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000489 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000490 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000491 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000492 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000493 free(paths);
494 }
Erik Andersen6273f652000-03-17 01:12:41 +0000495}
Erik Andersenf0657d32000-04-12 17:49:52 +0000496
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000497#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000498
499#define collapse_pos(is, in) { \
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000500 memmove(int_buf+(is), int_buf+(in), (MAX_LINELEN+1-(is)-(in))*sizeof(int)); \
501 memmove(pos_buf+(is), pos_buf+(in), (MAX_LINELEN+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000502
503static int find_match(char *matchBuf, int *len_with_quotes)
504{
505 int i, j;
506 int command_mode;
507 int c, c2;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000508 int int_buf[MAX_LINELEN + 1];
509 int pos_buf[MAX_LINELEN + 1];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000510
511 /* set to integer dimension characters and own positions */
512 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000513 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000514 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000515 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000517 }
518 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000519 }
520
521 /* mask \+symbol and convert '\t' to ' ' */
522 for (i = j = 0; matchBuf[i]; i++, j++)
523 if (matchBuf[i] == '\\') {
524 collapse_pos(j, j + 1);
525 int_buf[j] |= QUOT;
526 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000527#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000528 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000529 int_buf[j] = ' ' | QUOT;
530#endif
531 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000532#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000533 else if (matchBuf[i] == '\t')
534 int_buf[j] = ' ';
535#endif
536
537 /* mask "symbols" or 'symbols' */
538 c2 = 0;
539 for (i = 0; int_buf[i]; i++) {
540 c = int_buf[i];
541 if (c == '\'' || c == '"') {
542 if (c2 == 0)
543 c2 = c;
544 else {
545 if (c == c2)
546 c2 = 0;
547 else
548 int_buf[i] |= QUOT;
549 }
550 } else if (c2 != 0 && c != '$')
551 int_buf[i] |= QUOT;
552 }
553
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000554 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000555 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
556 for (i = 0; int_buf[i]; i++) {
557 c = int_buf[i];
558 c2 = int_buf[i + 1];
559 j = i ? int_buf[i - 1] : -1;
560 command_mode = 0;
561 if (c == ';' || c == '&' || c == '|') {
562 command_mode = 1 + (c == c2);
563 if (c == '&') {
564 if (j == '>' || j == '<')
565 command_mode = 0;
566 } else if (c == '|' && j == '>')
567 command_mode = 0;
568 }
569 if (command_mode) {
570 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000571 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000572 }
573 }
574 /* collapse `command...` */
575 for (i = 0; int_buf[i]; i++)
576 if (int_buf[i] == '`') {
577 for (j = i + 1; int_buf[j]; j++)
578 if (int_buf[j] == '`') {
579 collapse_pos(i, j + 1);
580 j = 0;
581 break;
582 }
583 if (j) {
584 /* not found close ` - command mode, collapse all previous */
585 collapse_pos(0, i + 1);
586 break;
587 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000588 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000589 }
590
591 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000592 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000593 c2 = 0;
594 for (i = 0; int_buf[i]; i++)
595 if (int_buf[i] == '(' || int_buf[i] == '{') {
596 if (int_buf[i] == '(')
597 c++;
598 else
599 c2++;
600 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000601 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000602 }
603 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
604 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
605 if (int_buf[i] == ')')
606 c--;
607 else
608 c2--;
609 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000610 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000611 }
612
613 /* skip first not quote space */
614 for (i = 0; int_buf[i]; i++)
615 if (int_buf[i] != ' ')
616 break;
617 if (i)
618 collapse_pos(0, i);
619
620 /* set find mode for completion */
621 command_mode = FIND_EXE_ONLY;
622 for (i = 0; int_buf[i]; i++)
623 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
624 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000625 && matchBuf[pos_buf[0]]=='c'
626 && matchBuf[pos_buf[1]]=='d'
627 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000628 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000629 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000630 command_mode = FIND_FILE_ONLY;
631 break;
632 }
633 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000634 for (i = 0; int_buf[i]; i++)
635 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000636 /* find last word */
637 for (--i; i >= 0; i--) {
638 c = int_buf[i];
639 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
640 collapse_pos(0, i + 1);
641 break;
642 }
643 }
644 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000645 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
646 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000647 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000648 while ((int_buf[i] & ~QUOT) == '/'
649 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
650 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000651 i++;
652 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000653
654 /* set only match and destroy quotes */
655 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000656 for (c = 0; pos_buf[i] >= 0; i++) {
657 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000658 j = pos_buf[i] + 1;
659 }
Eric Andersen4f990532001-05-31 17:15:57 +0000660 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000661 /* old lenght matchBuf with quotes symbols */
662 *len_with_quotes = j ? j - pos_buf[0] : 0;
663
664 return command_mode;
665}
666
Glenn L McGrath4d001292003-01-06 01:11:50 +0000667/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000668 * display by column (original idea from ls applet,
669 * very optimized by me :)
670 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000671static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000672{
673 int ncols, row;
674 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000675 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000676 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000677 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000678
679 /* find the longest file name- use that as the column width */
680 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000681 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000682 if (column_width < l)
683 column_width = l;
684 }
685 column_width += 2; /* min space for columns */
686 ncols = cmdedit_termw / column_width;
687
688 if (ncols > 1) {
689 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000690 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000691 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000692 } else {
693 ncols = 1;
694 }
695 for (row = 0; row < nrows; row++) {
696 int n = row;
697 int nc;
698
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000699 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000700 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000701 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000702 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000703 printf("%s\n", matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000704 }
705}
706
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000707static char *add_quote_for_spec_chars(char *found)
708{
709 int l = 0;
710 char *s = xmalloc((strlen(found) + 1) * 2);
711
712 while (*found) {
713 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
714 s[l++] = '\\';
715 s[l++] = *found++;
716 }
717 s[l] = 0;
718 return s;
719}
720
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000721static int match_compare(const void *a, const void *b)
722{
723 return strcmp(*(char**)a, *(char**)b);
724}
725
726/* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000727static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000728{
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000729 if (!(state->flags & TAB_COMPLETION))
730 return;
731
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000732 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000733 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000734 int len_found;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000735 char matchBuf[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000736 int find_type;
737 int recalc_pos;
738
Eric Andersenc470f442003-07-28 09:56:35 +0000739 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000740
741 /* Make a local copy of the string -- up
742 * to the position of the cursor */
743 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000744 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000745
746 find_type = find_match(matchBuf, &recalc_pos);
747
748 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000749 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000750
Denis Vlasenko38f63192007-01-22 09:03:07 +0000751#if ENABLE_FEATURE_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000752 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000753 * then try completing this word as a username. */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000754 if (state->flags & USERNAME_COMPLETION)
755 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
756 username_tab_completion(matchBuf, NULL);
Mark Whitley4e338752001-01-26 20:42:23 +0000757#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000758 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000759 * in the current working directory */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000760 if (!matches)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000761 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000762 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000763 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000764 int i, n = 0;
765 qsort(matches, num_matches, sizeof(char*), match_compare);
766 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000767 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000768 if (strcmp(matches[i], matches[i+1]) == 0) {
769 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000770 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000771 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000772 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000773 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000774 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000775 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000776 matches[n] = matches[i];
777 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000778 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000779 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000780 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000781 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000782 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000783 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000784 /* find minimal match */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000785 // ash: yet another failure in trying to achieve "we don't die on OOM"
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 Vlasenkoe8a07882007-06-10 15:08:44 +0000812 if ((len_found - strlen(matchBuf) + command_len) < MAX_LINELEN) {
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 Vlasenkoe8a07882007-06-10 15:08:44 +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 Vlasenkoe8a07882007-06-10 15:08:44 +0000899 if (l >= MAX_LINELEN)
900 hl[MAX_LINELEN-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;
Denis Vlasenko09221922007-04-15 13:21:01 +0000953#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +0000954 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000955 save_history(state->hist_file);
Denis Vlasenko09221922007-04-15 13:21:01 +0000956#endif
Denis Vlasenko38f63192007-01-22 09:03:07 +0000957 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000958}
959
960#else /* MAX_HISTORY == 0 */
961#define remember_in_history(a) ((void)0)
962#endif /* MAX_HISTORY */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000963
964
Erik Andersen6273f652000-03-17 01:12:41 +0000965/*
966 * This function is used to grab a character buffer
967 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +0000968 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +0000969 * a mini readline).
970 *
971 * The following standard commands are not implemented:
972 * ESC-b -- Move back one word
973 * ESC-f -- Move forward one word
974 * ESC-d -- Delete back one word
975 * ESC-h -- Delete forward one word
976 * CTL-t -- Transpose two characters
977 *
Paul Fox3f11b1b2005-08-04 19:04:46 +0000978 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000979 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +0000980 */
Eric Andersenc470f442003-07-28 09:56:35 +0000981
Denis Vlasenko38f63192007-01-22 09:03:07 +0000982#if ENABLE_FEATURE_EDITING_VI
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000983static void
Paul Fox3f11b1b2005-08-04 19:04:46 +0000984vi_Word_motion(char *command, int eat)
985{
Denis Vlasenko253ce002007-01-22 08:34:44 +0000986 while (cursor < command_len && !isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000987 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +0000988 if (eat) while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000989 input_forward();
990}
991
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000992static void
Paul Fox3f11b1b2005-08-04 19:04:46 +0000993vi_word_motion(char *command, int eat)
994{
995 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +0000996 while (cursor < command_len
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000997 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000998 input_forward();
999 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001000 while (cursor < command_len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001001 input_forward();
1002 }
1003
Denis Vlasenko253ce002007-01-22 08:34:44 +00001004 if (cursor < command_len)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001005 input_forward();
1006
Denis Vlasenko253ce002007-01-22 08:34:44 +00001007 if (eat && cursor < command_len && isspace(command[cursor]))
1008 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001009 input_forward();
1010}
1011
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001012static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001013vi_End_motion(char *command)
1014{
1015 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001016 while (cursor < command_len && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001017 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001018 while (cursor < command_len-1 && !isspace(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001019 input_forward();
1020}
1021
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001022static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001023vi_end_motion(char *command)
1024{
Denis Vlasenko253ce002007-01-22 08:34:44 +00001025 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001026 return;
1027 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001028 while (cursor < command_len-1 && isspace(command[cursor]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001029 input_forward();
Denis Vlasenko253ce002007-01-22 08:34:44 +00001030 if (cursor >= command_len-1)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001031 return;
1032 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001033 while (cursor < command_len-1
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001034 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1035 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001036 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001037 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001038 } else if (ispunct(command[cursor])) {
Denis Vlasenko253ce002007-01-22 08:34:44 +00001039 while (cursor < command_len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001040 input_forward();
1041 }
1042}
1043
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001044static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001045vi_Back_motion(char *command)
1046{
1047 while (cursor > 0 && isspace(command[cursor-1]))
1048 input_backward(1);
1049 while (cursor > 0 && !isspace(command[cursor-1]))
1050 input_backward(1);
1051}
1052
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001053static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001054vi_back_motion(char *command)
1055{
1056 if (cursor <= 0)
1057 return;
1058 input_backward(1);
1059 while (cursor > 0 && isspace(command[cursor]))
1060 input_backward(1);
1061 if (cursor <= 0)
1062 return;
1063 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001064 while (cursor > 0
1065 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1066 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001067 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001068 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001069 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001070 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001071 input_backward(1);
1072 }
1073}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001074#endif
1075
1076
1077/*
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001078 * read_line_input and its helpers
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001079 */
1080
Denis Vlasenko38f63192007-01-22 09:03:07 +00001081#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001082static void parse_prompt(const char *prmt_ptr)
1083{
1084 cmdedit_prompt = prmt_ptr;
1085 cmdedit_prmt_len = strlen(prmt_ptr);
1086 put_prompt();
1087}
1088#else
1089static void parse_prompt(const char *prmt_ptr)
1090{
1091 int prmt_len = 0;
1092 size_t cur_prmt_len = 0;
1093 char flg_not_length = '[';
1094 char *prmt_mem_ptr = xzalloc(1);
Denis Vlasenko6ca04442007-02-11 16:19:28 +00001095 char *pwd_buf = xrealloc_getcwd_or_warn(NULL);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001096 char buf2[PATH_MAX + 1];
1097 char buf[2];
1098 char c;
1099 char *pbuf;
1100
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001101 cmdedit_prmt_len = 0;
1102
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001103 if (!pwd_buf) {
1104 pwd_buf = (char *)bb_msg_unknown;
1105 }
1106
1107 while (*prmt_ptr) {
1108 pbuf = buf;
1109 pbuf[1] = 0;
1110 c = *prmt_ptr++;
1111 if (c == '\\') {
1112 const char *cp = prmt_ptr;
1113 int l;
1114
1115 c = bb_process_escape_sequence(&prmt_ptr);
1116 if (prmt_ptr == cp) {
1117 if (*cp == 0)
1118 break;
1119 c = *prmt_ptr++;
1120 switch (c) {
1121#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1122 case 'u':
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001123 pbuf = user_buf ? user_buf : (char*)"";
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001124 break;
1125#endif
1126 case 'h':
1127 pbuf = hostname_buf;
1128 if (!pbuf) {
1129 pbuf = xzalloc(256);
1130 if (gethostname(pbuf, 255) < 0) {
1131 strcpy(pbuf, "?");
1132 } else {
1133 char *s = strchr(pbuf, '.');
1134 if (s)
1135 *s = '\0';
1136 }
1137 hostname_buf = pbuf;
1138 }
1139 break;
1140 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001141 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001142 break;
1143#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1144 case 'w':
1145 pbuf = pwd_buf;
1146 l = strlen(home_pwd_buf);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001147 if (l != 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001148 && strncmp(home_pwd_buf, pbuf, l) == 0
1149 && (pbuf[l]=='/' || pbuf[l]=='\0')
1150 && strlen(pwd_buf+l)<PATH_MAX
1151 ) {
1152 pbuf = buf2;
1153 *pbuf = '~';
1154 strcpy(pbuf+1, pwd_buf+l);
1155 }
1156 break;
1157#endif
1158 case 'W':
1159 pbuf = pwd_buf;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001160 cp = strrchr(pbuf, '/');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001161 if (cp != NULL && cp != pbuf)
1162 pbuf += (cp-pbuf) + 1;
1163 break;
1164 case '!':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001165 pbuf = buf2;
1166 snprintf(buf2, sizeof(buf2), "%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001167 break;
1168 case 'e': case 'E': /* \e \E = \033 */
1169 c = '\033';
1170 break;
1171 case 'x': case 'X':
1172 for (l = 0; l < 3;) {
1173 int h;
1174 buf2[l++] = *prmt_ptr;
1175 buf2[l] = 0;
1176 h = strtol(buf2, &pbuf, 16);
1177 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
1178 l--;
1179 break;
1180 }
1181 prmt_ptr++;
1182 }
1183 buf2[l] = 0;
1184 c = (char)strtol(buf2, NULL, 16);
1185 if (c == 0)
1186 c = '?';
1187 pbuf = buf;
1188 break;
1189 case '[': case ']':
1190 if (c == flg_not_length) {
1191 flg_not_length = flg_not_length == '[' ? ']' : '[';
1192 continue;
1193 }
1194 break;
1195 }
1196 }
1197 }
1198 if (pbuf == buf)
1199 *pbuf = c;
1200 cur_prmt_len = strlen(pbuf);
1201 prmt_len += cur_prmt_len;
1202 if (flg_not_length != ']')
1203 cmdedit_prmt_len += cur_prmt_len;
1204 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
1205 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001206 if (pwd_buf != (char *)bb_msg_unknown)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001207 free(pwd_buf);
1208 cmdedit_prompt = prmt_mem_ptr;
1209 put_prompt();
1210}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001211#endif
1212
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001213#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
1214#define getTermSettings(fd, argp) tcgetattr(fd, argp);
1215
1216static sighandler_t previous_SIGWINCH_handler;
1217
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001218static void cmdedit_setwidth(unsigned w, int redraw_flg)
1219{
1220 cmdedit_termw = w;
1221 if (redraw_flg) {
1222 /* new y for current cursor */
1223 int new_y = (cursor + cmdedit_prmt_len) / w;
1224 /* redraw */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001225 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), command_len - cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001226 fflush(stdout);
1227 }
1228}
1229
1230static void win_changed(int nsig)
1231{
1232 int width;
1233 get_terminal_width_height(0, &width, NULL);
1234 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1235 if (nsig == SIGWINCH)
1236 signal(SIGWINCH, win_changed); /* rearm ourself */
1237}
1238
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001239/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001240 * The emacs and vi modes share much of the code in the big
1241 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001242 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001243 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001244 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001245 */
1246
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001247#define vbit 0x100
1248
1249/* leave out the "vi-mode"-only case labels if vi editing isn't
1250 * configured. */
Denis Vlasenko38f63192007-01-22 09:03:07 +00001251#define vi_case(caselabel) USE_FEATURE_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001252
1253/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001254#undef CTRL
1255#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001256
Denis Vlasenko6a5377a2007-09-25 18:35:28 +00001257/* Returns:
1258 * -1 on read errors or EOF, or on bare Ctrl-D.
1259 * 0 on ctrl-C,
1260 * >0 length of input string, including terminating '\n'
1261 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001262int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *st)
Erik Andersen13456d12000-03-16 08:09:57 +00001263{
Erik Andersenc7c634b2000-03-19 05:28:55 +00001264 int lastWasTab = FALSE;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001265 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001266 unsigned char c;
1267 smallint break_out = 0;
Denis Vlasenko38f63192007-01-22 09:03:07 +00001268#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001269 smallint vi_cmdmode = 0;
1270 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001271#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001272
1273// FIXME: audit & improve this
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001274 if (maxsize > MAX_LINELEN)
1275 maxsize = MAX_LINELEN;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001276
1277 /* With null flags, no other fields are ever used */
Denis Vlasenko703e2022007-01-22 14:12:08 +00001278 state = st ? st : (line_input_t*) &const_int_0;
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001279#if ENABLE_FEATURE_EDITING_SAVEHISTORY
Denis Vlasenkobf3561f2007-04-14 10:10:40 +00001280 if ((state->flags & SAVE_HISTORY) && state->hist_file)
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001281 load_history(state->hist_file);
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +00001282#endif
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001283
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001284 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001285 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001286 command_len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001287 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001288 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001289
Eric Andersen34506362001-08-02 05:02:46 +00001290 getTermSettings(0, (void *) &initial_settings);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001291 memcpy(&new_settings, &initial_settings, sizeof(new_settings));
Eric Andersen34506362001-08-02 05:02:46 +00001292 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1293 /* Turn off echoing and CTRL-C, so we can trap it */
1294 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001295 /* Hmm, in linux c_cc[] is not parsed if ICANON is off */
Eric Andersen34506362001-08-02 05:02:46 +00001296 new_settings.c_cc[VMIN] = 1;
1297 new_settings.c_cc[VTIME] = 0;
1298 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001299#ifndef _POSIX_VDISABLE
1300#define _POSIX_VDISABLE '\0'
1301#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001302 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001303 setTermSettings(0, (void *) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001304
Eric Andersen6faae7d2001-02-16 20:09:17 +00001305 /* Now initialize things */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001306 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1307 win_changed(0); /* do initial resizing */
1308#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1309 {
1310 struct passwd *entry;
1311
1312 entry = getpwuid(geteuid());
1313 if (entry) {
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001314 /* If we enter read_line_input for the Nth time,
1315 * they may be already allocated! Need to free. */
1316 free(user_buf);
1317 if (home_pwd_buf != null_str);
1318 free(home_pwd_buf);
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001319 user_buf = xstrdup(entry->pw_name);
1320 home_pwd_buf = xstrdup(entry->pw_dir);
Denis Vlasenko86b29ea2007-09-27 10:17:16 +00001321 /* They are not freed on exit (too small to bother) */
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001322 }
1323 }
1324#endif
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001325 /* Print out the command prompt */
1326 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001327
Erik Andersenc7c634b2000-03-19 05:28:55 +00001328 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001329 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001330
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001331 if (safe_read(0, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001332 /* if we can't read input then exit */
1333 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001334 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001335
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001336 ic = c;
1337
Denis Vlasenko38f63192007-01-22 09:03:07 +00001338#if ENABLE_FEATURE_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001339 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001340 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001341 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001342#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001343 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001344 case '\n':
1345 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001346 vi_case('\n'|vbit:)
1347 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001348 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001349 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001350 break_out = 1;
1351 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001352#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001353 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001354 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001355 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001356 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001357 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001358 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001359 vi_case('h'|vbit:)
1360 vi_case('\b'|vbit:)
1361 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001362 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001363 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001364 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001365#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001366 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001367 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001368 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001369 goto_new_line();
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001370 command_len = 0;
1371 break_out = -1; /* "do not append '\n'" */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001372 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001373 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001374 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001375 * if the len=0 and no chars to delete */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001376 if (command_len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001377 errno = 0;
1378 prepare_to_die:
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001379 /* to control stopped jobs */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001380 break_out = command_len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001381 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001382 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001383 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001384 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001385
1386#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001387 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001388 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001389 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001390 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001391 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001392 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001393 vi_case('l'|vbit:)
1394 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001395 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001396 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001397 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001398#endif
1399
Erik Andersenf0657d32000-04-12 17:49:52 +00001400 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001401 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001402 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001403 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001404 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001405
Erik Andersenc7c634b2000-03-19 05:28:55 +00001406 case '\t':
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001407 input_tab(&lastWasTab);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001408 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001409
1410#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001411 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001412 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001413 command[cursor] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001414 command_len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001415 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001416 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001417 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001418 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001419 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001420 printf("\033[H");
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001421 redraw(0, command_len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001422 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001423#endif
1424
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001425#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001426 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001427 vi_case(CTRL('N')|vbit:)
1428 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001429 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001430 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001431 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001432 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001433 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001434 vi_case(CTRL('P')|vbit:)
1435 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001436 /* Control-p -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001437 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001438 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001439 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001440 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001441 beep();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001442 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001443#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001444
1445#if ENABLE_FEATURE_EDITING_FANCY_KEYS
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001446 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001447 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001448 /* Control-U -- Clear line before cursor */
1449 if (cursor) {
1450 strcpy(command, command + cursor);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001451 command_len -= cursor;
1452 redraw(cmdedit_y, command_len);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001453 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001454 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001455#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001456 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001457 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001458 /* Control-W -- Remove the last word */
1459 while (cursor > 0 && isspace(command[cursor-1]))
1460 input_backspace();
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001461 while (cursor > 0 && !isspace(command[cursor-1]))
Eric Andersen27bb7902003-12-23 20:24:51 +00001462 input_backspace();
1463 break;
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001464
Denis Vlasenko38f63192007-01-22 09:03:07 +00001465#if ENABLE_FEATURE_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001466 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001467 vi_cmdmode = 0;
1468 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001469 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001470 input_backward(cursor);
1471 vi_cmdmode = 0;
1472 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001473 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001474 input_forward();
1475 vi_cmdmode = 0;
1476 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001477 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001478 input_end();
1479 vi_cmdmode = 0;
1480 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001481 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001482 input_delete(1);
1483 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001484 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001485 if (cursor > 0) {
1486 input_backward(1);
1487 input_delete(1);
1488 }
1489 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001490 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001491 vi_Word_motion(command, 1);
1492 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001493 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001494 vi_word_motion(command, 1);
1495 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001496 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001497 vi_End_motion(command);
1498 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001499 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001500 vi_end_motion(command);
1501 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001502 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001503 vi_Back_motion(command);
1504 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001505 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001506 vi_back_motion(command);
1507 break;
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 */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001511 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001512 goto clear_to_eol;
1513
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001514 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001515 vi_cmdmode = 0;
1516 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001517 case 'd'|vbit: {
1518 int nc, sc;
1519 sc = cursor;
1520 prevc = ic;
1521 if (safe_read(0, &c, 1) < 1)
1522 goto prepare_to_die;
1523 if (c == (prevc & 0xff)) {
1524 /* "cc", "dd" */
1525 input_backward(cursor);
1526 goto clear_to_eol;
1527 break;
1528 }
1529 switch (c) {
1530 case 'w':
1531 case 'W':
1532 case 'e':
1533 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001534 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001535 case 'w': /* "dw", "cw" */
1536 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001537 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001538 case 'W': /* 'dW', 'cW' */
1539 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001540 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001541 case 'e': /* 'de', 'ce' */
1542 vi_end_motion(command);
1543 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001544 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001545 case 'E': /* 'dE', 'cE' */
1546 vi_End_motion(command);
1547 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001548 break;
1549 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001550 nc = cursor;
1551 input_backward(cursor - sc);
1552 while (nc-- > cursor)
1553 input_delete(1);
1554 break;
1555 case 'b': /* "db", "cb" */
1556 case 'B': /* implemented as B */
1557 if (c == 'b')
1558 vi_back_motion(command);
1559 else
1560 vi_Back_motion(command);
1561 while (sc-- > cursor)
1562 input_delete(1);
1563 break;
1564 case ' ': /* "d ", "c " */
1565 input_delete(1);
1566 break;
1567 case '$': /* "d$", "c$" */
1568 clear_to_eol:
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001569 while (cursor < command_len)
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001570 input_delete(1);
1571 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001572 }
1573 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001574 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001575 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001576 input_forward();
1577 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001578 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001579 put();
1580 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001581 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001582 if (safe_read(0, &c, 1) < 1)
1583 goto prepare_to_die;
1584 if (c == 0)
1585 beep();
1586 else {
1587 *(command + cursor) = c;
Denis Vlasenko4daad902007-09-27 10:20:47 +00001588 bb_putchar(c);
1589 bb_putchar('\b');
Paul Fox3f11b1b2005-08-04 19:04:46 +00001590 }
1591 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001592#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001593
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001594 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001595
Denis Vlasenko38f63192007-01-22 09:03:07 +00001596#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001597 if (state->flags & VI_MODE) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001598 /* ESC: insert mode --> command mode */
1599 vi_cmdmode = 1;
1600 input_backward(1);
1601 break;
1602 }
1603#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001604 /* escape sequence follows */
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 /* different vt100 emulations */
1608 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001609 vi_case('['|vbit:)
1610 vi_case('O'|vbit:)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001611 if (safe_read(0, &c, 1) < 1)
1612 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001613 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001614 if (c >= '1' && c <= '9') {
1615 unsigned char dummy;
1616
1617 if (safe_read(0, &dummy, 1) < 1)
1618 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001619 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001620 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001621 }
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001622
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001623 switch (c) {
Denis Vlasenko38f63192007-01-22 09:03:07 +00001624#if ENABLE_FEATURE_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001625 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001626 input_tab(&lastWasTab);
1627 break;
1628#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001629#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001630 case 'A':
1631 /* Up Arrow -- Get previous command from history */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001632 if ((state->flags & DO_HISTORY) && state->cur_history > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001633 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001634 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001635 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001636 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001637 break;
1638 case 'B':
1639 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001640 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001641 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001642 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001643 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001644 /* change command */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001645 command_len = strlen(strcpy(command, state->history[state->cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001646 /* redraw and go to eol (bol, in vi */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001647 redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001648 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001649#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001650 case 'C':
1651 /* Right Arrow -- Move forward one character */
1652 input_forward();
1653 break;
1654 case 'D':
1655 /* Left Arrow -- Move back one character */
1656 input_backward(1);
1657 break;
1658 case '3':
1659 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001660 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001661 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001662 case '1': // vt100? linux vt? or what?
1663 case '7': // vt100? linux vt? or what?
1664 case 'H': /* xterm's <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001665 input_backward(cursor);
1666 break;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001667 case '4': // vt100? linux vt? or what?
1668 case '8': // vt100? linux vt? or what?
1669 case 'F': /* xterm's <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001670 input_end();
1671 break;
1672 default:
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001673 c = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001674 beep();
1675 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001676 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001677
Eric Andersenc470f442003-07-28 09:56:35 +00001678 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001679#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001680 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001681 if (c == CTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001682 if (safe_read(0, &c, 1) < 1)
1683 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001684 if (c == 0) {
1685 beep();
1686 break;
1687 }
1688 } else
1689#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001690
Denis Vlasenko38f63192007-01-22 09:03:07 +00001691#if ENABLE_FEATURE_EDITING_VI
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001692 if (vi_cmdmode) /* Don't self-insert */
1693 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001694#endif
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001695 if (!Isprint(c)) /* Skip non-printable characters */
1696 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001697
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001698 if (command_len >= (maxsize - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001699 break;
1700
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001701 command_len++;
1702 if (cursor == (command_len - 1)) { /* Append if at the end of the line */
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001703 command[cursor] = c;
1704 command[cursor+1] = '\0';
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001705 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001706 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001707 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001708
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001709 memmove(command + sc + 1, command + sc, command_len - sc);
Denis Vlasenko00cdbd82007-01-21 19:21:21 +00001710 command[sc] = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001711 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001712 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001713 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001714 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001715 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001716 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001717 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001718 }
Eric Andersenc470f442003-07-28 09:56:35 +00001719 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001720 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001721
1722 if (c != '\t')
1723 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001724 }
1725
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001726 if (command_len > 0)
1727 remember_in_history(command);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001728
Eric Andersen27bb7902003-12-23 20:24:51 +00001729 if (break_out > 0) {
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001730 command[command_len++] = '\n';
1731 command[command_len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001732 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001733
Denis Vlasenko38f63192007-01-22 09:03:07 +00001734#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001735 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001736#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001737
Denis Vlasenko38f63192007-01-22 09:03:07 +00001738#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001739 free((char*)cmdedit_prompt);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001740#endif
Denis Vlasenko6258fd32007-01-22 07:30:26 +00001741 /* restore initial_settings */
1742 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
1743 /* restore SIGWINCH handler */
1744 signal(SIGWINCH, previous_SIGWINCH_handler);
1745 fflush(stdout);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001746 return command_len;
1747}
1748
1749line_input_t *new_line_input_t(int flags)
1750{
1751 line_input_t *n = xzalloc(sizeof(*n));
1752 n->flags = flags;
1753 return n;
1754}
1755
1756#else
1757
1758#undef read_line_input
1759int read_line_input(const char* prompt, char* command, int maxsize)
1760{
1761 fputs(prompt, stdout);
1762 fflush(stdout);
1763 fgets(command, maxsize, stdin);
1764 return strlen(command);
Eric Andersen501c88b2000-07-28 15:14:45 +00001765}
1766
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001767#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001768
1769
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001770/*
1771 * Testing
1772 */
1773
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001774#ifdef TEST
1775
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001776#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001777
1778const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001779
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001780int main(int argc, char **argv)
1781{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001782 char buff[MAX_LINELEN];
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001783 char *prompt =
Denis Vlasenko38f63192007-01-22 09:03:07 +00001784#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001785 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1786 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1787 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001788#else
1789 "% ";
1790#endif
1791
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001792#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001793 setlocale(LC_ALL, "");
1794#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001795 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001796 int l;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001797 l = read_line_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001798 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001799 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001800 buff[l-1] = 0;
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001801 printf("*** read_line_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001802 }
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001803 printf("*** read_line_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001804 return 0;
1805}
1806
Eric Andersenc470f442003-07-28 09:56:35 +00001807#endif /* TEST */