blob: 1143ded77e59ce28bd186b65a1f63edd7ca878df [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
27 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 Vlasenko47bdb3a2007-01-21 19:18:59 +000031/*
32CONFIG_FEATURE_COMMAND_EDITING=y
33# CONFIG_FEATURE_COMMAND_EDITING_VI is not set
34CONFIG_FEATURE_COMMAND_HISTORY=15
35# CONFIG_FEATURE_COMMAND_SAVEHISTORY is not set
36# CONFIG_FEATURE_COMMAND_TAB_COMPLETION is not set
37# CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION is not set
38# CONFIG_FEATURE_SH_FANCY_PROMPT is not set
39Sizes with the above:
40# size cmdedit.o
41 text data bss dec hex filename
42 2374 4 228 2606 a2e cmdedit.o
43# nm --size-sort cmdedit.o
4400000004 b cmdedit_prmt_len
4500000004 b cmdedit_prompt
4600000004 d cmdedit_termw
4700000004 b cmdedit_x
4800000004 b cmdedit_y
4900000004 b command_ps
5000000004 b cur_history
5100000004 b cursor
5200000004 b handlers_sets
5300000004 b len
5400000004 b n_history
5500000004 b previous_SIGWINCH_handler
5600000009 t beep
5700000017 t goto_new_line
580000001a t input_end
590000001b t input_backspace
600000001e t input_forward
6100000027 t get_next_history
6200000036 t put_prompt
6300000039 t redraw
640000003c b initial_settings
650000003c b new_settings
6600000040 b history
6700000047 t input_delete
680000004d t get_previous_history
6900000059 t cmdedit_reset_term
700000006c t cmdedit_set_out_char
7100000087 t input_backward
72000000a1 t win_changed
730000053c T cmdedit_read_input
74*/
75
Eric Andersencbe31da2001-02-20 06:14:08 +000076#include <sys/ioctl.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +000077#include "busybox.h"
"Robert P. J. Day"4eddb422006-07-03 00:46:47 +000078#include "cmdedit.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000079
Glenn L McGrath475820c2004-01-22 12:42:23 +000080
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000081/* FIXME: obsolete CONFIG item? */
82#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
83
84
Glenn L McGrath3b251852004-01-03 12:07:32 +000085#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000086
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000087#define ENABLE_FEATURE_COMMAND_EDITING 0
88#define ENABLE_FEATURE_COMMAND_TAB_COMPLETION 0
89#define ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION 0
90#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
91#define ENABLE_FEATURE_CLEAN_UP 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +000092
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000093#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000094
Eric Andersen5165fbe2001-02-20 06:42:29 +000095
Denis Vlasenko82b39e82007-01-21 19:18:19 +000096/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000097#if ENABLE_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000098
Denis Vlasenko82b39e82007-01-21 19:18:19 +000099
100#if ENABLE_LOCALE_SUPPORT
101#define Isprint(c) isprint(c)
102#else
103#define Isprint(c) ((c) >= ' ' && (c) != ((unsigned char)'\233'))
104#endif
105
Denis Vlasenko7e46cf72006-12-23 01:21:55 +0000106#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
107(ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION || ENABLE_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000108
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000109/* Maximum length of command line history */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000110#if !ENABLE_FEATURE_COMMAND_HISTORY
Robert Griebl350d26b2002-12-03 22:45:46 +0000111#define MAX_HISTORY 15
112#else
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000113#define MAX_HISTORY (CONFIG_FEATURE_COMMAND_HISTORY + 0)
Robert Griebl350d26b2002-12-03 22:45:46 +0000114#endif
115
Erik Andersen1d1d9502000-04-21 01:26:49 +0000116
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000117/* Current termios and the previous termios before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +0000118static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +0000119
Mark Whitley4e338752001-01-26 20:42:23 +0000120static
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000121volatile unsigned cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +0000122static
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000123volatile int handlers_sets = 0; /* Set next bits: */
Mark Whitley4e338752001-01-26 20:42:23 +0000124enum {
Eric Andersenc470f442003-07-28 09:56:35 +0000125 SET_ATEXIT = 1, /* when atexit() has been called
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000126 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +0000127 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
128 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +0000129};
Erik Andersen13456d12000-03-16 08:09:57 +0000130
Mark Whitley4e338752001-01-26 20:42:23 +0000131
Eric Andersenc470f442003-07-28 09:56:35 +0000132static int cmdedit_x; /* real x terminal position */
133static int cmdedit_y; /* pseudoreal y terminal position */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000134static int cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen86349772000-12-18 20:25:50 +0000135
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000136static int cursor;
137static int len;
138static char *command_ps;
139static SKIP_FEATURE_SH_FANCY_PROMPT(const) char *cmdedit_prompt;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000140
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000141#if ENABLE_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000142static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000143static int num_ok_lines = 1;
144#endif
145
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000146#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
147static char *user_buf = "";
148static char *home_pwd_buf = "";
149#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000150
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000151#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR || ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000152static int my_euid;
153#endif
154
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000155#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000156static int my_uid;
157static int my_gid;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000158#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000159
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000160/* Put 'command_ps[cursor]', cursor++.
161 * Advance cursor on screen. If we reached right margin, scroll text up
162 * and remove terminal margin effect by printing 'next_char' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000163static void cmdedit_set_out_char(int next_char)
164{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000165 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000166
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000167 if (c == '\0') {
168 /* erase character after end of input string */
169 c = ' ';
170 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000171#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000172 /* Display non-printable characters in reverse */
173 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000174 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000175 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000176 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000177 c += '@';
178 if (c == 127)
179 c = '?';
180 printf("\033[7m%c\033[0m", c);
181 } else
182#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000183 {
184 if (initial_settings.c_lflag & ECHO)
185 putchar(c);
186 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000187 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000188 /* terminal is scrolled down */
189 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000190 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000191 /* destroy "(auto)margin" */
192 putchar(next_char);
193 putchar('\b');
194 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000195// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000196 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000197}
198
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000199/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000200static void input_end(void)
201{
202 while (cursor < len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000203 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000204}
205
206/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000207static void goto_new_line(void)
208{
Mark Whitley4e338752001-01-26 20:42:23 +0000209 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000210 if (cmdedit_x)
211 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000212}
213
214
Rob Landley88621d72006-08-29 19:41:06 +0000215static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000216{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000217 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000218 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000219}
Eric Andersen81fe1232003-07-29 06:38:40 +0000220
Rob Landley88621d72006-08-29 19:41:06 +0000221static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000222{
223 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000224}
225
Eric Andersenaff114c2004-04-14 17:51:38 +0000226/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000227/* (optimized for slow terminals) */
228static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000229{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000230 int count_y;
231
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000232 if (num > cursor)
233 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000234 if (!num)
235 return;
236 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000237
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000238 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000239 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000240 if (num <= 4) {
241 do putchar('\b'); while (--num);
242 return;
243 }
244 printf("\033[%uD", num);
245 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000246 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000247
248 /* Need to go one or more lines up */
249 num -= cmdedit_x;
250 count_y = 1 + (num / cmdedit_termw);
251 cmdedit_y -= count_y;
252 cmdedit_x = cmdedit_termw * count_y - num;
253 /* go to 1st col; go up; go to correct column */
254 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000255}
256
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000257static void put_prompt(void)
258{
259 out1str(cmdedit_prompt);
Eric Andersenc470f442003-07-28 09:56:35 +0000260 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000261 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000262// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000263 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000264}
265
Eric Andersenaff114c2004-04-14 17:51:38 +0000266/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000267static void redraw(int y, int back_cursor)
268{
Eric Andersenc470f442003-07-28 09:56:35 +0000269 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000270 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000271 putchar('\r');
272 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000273 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000274 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000275 input_backward(back_cursor);
276}
277
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000278#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000279#define DELBUFSIZ 128
280static char *delbuf; /* a (malloced) place to store deleted characters */
281static char *delp;
282static char newdelflag; /* whether delbuf should be reused yet */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000283#endif
284
285/* Delete the char in front of the cursor, optionally saving it
286 * for later putback */
287static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000288{
Mark Whitley4e338752001-01-26 20:42:23 +0000289 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000290
Mark Whitley4e338752001-01-26 20:42:23 +0000291 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000292 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000293
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000294#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000295 if (save) {
296 if (newdelflag) {
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000297 if (!delbuf)
298 delbuf = malloc(DELBUFSIZ);
299 /* safe if malloc fails */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000300 delp = delbuf;
301 newdelflag = 0;
302 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000303 if (delbuf && (delp - delbuf < DELBUFSIZ))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000304 *delp++ = command_ps[j];
305 }
306#endif
307
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000308 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000309 len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000310 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000311 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000312 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000313}
314
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000315#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000316static void put(void)
317{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000318 int ocursor;
319 int j = delp - delbuf;
320
Paul Fox3f11b1b2005-08-04 19:04:46 +0000321 if (j == 0)
322 return;
323 ocursor = cursor;
324 /* open hole and then fill it */
325 memmove(command_ps + cursor + j, command_ps + cursor, len - cursor + 1);
326 strncpy(command_ps + cursor, delbuf, j);
327 len += j;
328 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000329 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000330}
331#endif
332
Mark Whitley4e338752001-01-26 20:42:23 +0000333/* Delete the char in back of the cursor */
334static void input_backspace(void)
335{
336 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000337 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000338 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000339 }
340}
341
342
Eric Andersenaff114c2004-04-14 17:51:38 +0000343/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000344static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000345{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000346 if (cursor < len)
347 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000348}
349
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000350#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000351
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000352static char **matches;
353static int num_matches;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000354
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000355static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000356{
357 int nm = num_matches;
358 int nm1 = nm + 1;
359
360 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000361 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000362 num_matches++;
363}
364
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000365#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000366
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000367static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000368{
369 struct passwd *entry;
370 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000371
Eric Andersenc470f442003-07-28 09:56:35 +0000372 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000373 userlen = strlen(ud);
374
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000375 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000376 char *sav_ud = ud - 1;
377 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000378 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000379
Eric Andersenc470f442003-07-28 09:56:35 +0000380 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000381 home = home_pwd_buf;
382 } else {
383 /* "~user/..." */
384 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000385 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000386 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000387 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000388 ud = temp;
389 if (entry)
390 home = entry->pw_dir;
391 }
392 if (home) {
393 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000394 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000395
396 /* /home/user/... */
397 sprintf(temp2, "%s%s", home, ud);
398 strcpy(sav_ud, temp2);
399 }
400 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000401 } else {
402 /* "~[^/]*" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000403 setpwent();
404
405 while ((entry = getpwent()) != NULL) {
406 /* Null usernames should result in all users as possible completions. */
407 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000408 add_match(xasprintf("~%s/", entry->pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000409 }
410 }
411
412 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000413 }
414}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000415#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000416
417enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000418 FIND_EXE_ONLY = 0,
419 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000420 FIND_FILE_ONLY = 2,
421};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000422
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000423#if ENABLE_ASH
Glenn L McGrath67285962004-01-14 09:34:51 +0000424const char *cmdedit_path_lookup;
Glenn L McGrath67285962004-01-14 09:34:51 +0000425#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000426static int path_parse(char ***p, int flags)
427{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000428 int npth;
Glenn L McGrath67285962004-01-14 09:34:51 +0000429 const char *tmp;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000430#if ENABLE_ASH
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000431 const char *pth = cmdedit_path_lookup;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000432#else
433 const char *pth = getenv("PATH")
434#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000435
Mark Whitley4e338752001-01-26 20:42:23 +0000436 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000437 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000438 return 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000439 /* PATH=<empty> or PATH=:<empty> */
440 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
441 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000442
443 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000444 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000445
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000446 while (1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000447 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000448 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000449 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000450 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000451 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000452 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000453 }
454
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000455 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000456
457 tmp = pth;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000458 (*p)[0] = xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000459 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000460
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000461 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000462 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000463 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000464 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000465 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
466 if (*++tmp == 0)
467 break; /* :<empty> */
Eric Andersenc470f442003-07-28 09:56:35 +0000468 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000469 }
470
471 return npth;
472}
473
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000474static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000475{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000476 DIR *dir;
477 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000478 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000479 struct stat st;
480 char *path1[1];
481 char **paths = path1;
482 int npaths;
483 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000484 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000485 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000486
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000487 npaths = 1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000488 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000489
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000491 /* no dir, if flags==EXE_ONLY - get paths, else "." */
492 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000493 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000494 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000495 /* dirbuf = ".../.../.../" */
496 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000497#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000498 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000499 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000500#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000501 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000502 /* point to 'l' in "..../last_component" */
503 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000504 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000505
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000506 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000507
Mark Whitley4e338752001-01-26 20:42:23 +0000508 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000509 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000510 continue;
511
512 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000513 int len1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000514 char *str_found = next->d_name;
515
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000516 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000517 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000518 continue;
519 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000520 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000521 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000522 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000523 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000525 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000526 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000527 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000528 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000529 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000530 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000531 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000532
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000533 len1 = strlen(found);
534 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000535 found[len1] = '\0';
536 found[len1+1] = '\0';
537
Mark Whitley4e338752001-01-26 20:42:23 +0000538 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000539 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000540 if (found[len1-1] != '/') {
541 found[len1] = '/';
542 }
Mark Whitley4e338752001-01-26 20:42:23 +0000543 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000544 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000545 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000546 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000547 }
Mark Whitley4e338752001-01-26 20:42:23 +0000548 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000549 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000550 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000551 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000552 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000553 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000555 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000556 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000557 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000558 free(paths);
559 }
Erik Andersen6273f652000-03-17 01:12:41 +0000560}
Erik Andersenf0657d32000-04-12 17:49:52 +0000561
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000562
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000563#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000564
565#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000566 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
567 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000568
569static int find_match(char *matchBuf, int *len_with_quotes)
570{
571 int i, j;
572 int command_mode;
573 int c, c2;
574 int int_buf[BUFSIZ + 1];
575 int pos_buf[BUFSIZ + 1];
576
577 /* set to integer dimension characters and own positions */
578 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000579 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000580 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000581 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000582 break;
583 } else
584 pos_buf[i] = i;
585 }
586
587 /* mask \+symbol and convert '\t' to ' ' */
588 for (i = j = 0; matchBuf[i]; i++, j++)
589 if (matchBuf[i] == '\\') {
590 collapse_pos(j, j + 1);
591 int_buf[j] |= QUOT;
592 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000593#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000594 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000595 int_buf[j] = ' ' | QUOT;
596#endif
597 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000598#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000599 else if (matchBuf[i] == '\t')
600 int_buf[j] = ' ';
601#endif
602
603 /* mask "symbols" or 'symbols' */
604 c2 = 0;
605 for (i = 0; int_buf[i]; i++) {
606 c = int_buf[i];
607 if (c == '\'' || c == '"') {
608 if (c2 == 0)
609 c2 = c;
610 else {
611 if (c == c2)
612 c2 = 0;
613 else
614 int_buf[i] |= QUOT;
615 }
616 } else if (c2 != 0 && c != '$')
617 int_buf[i] |= QUOT;
618 }
619
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000620 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000621 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
622 for (i = 0; int_buf[i]; i++) {
623 c = int_buf[i];
624 c2 = int_buf[i + 1];
625 j = i ? int_buf[i - 1] : -1;
626 command_mode = 0;
627 if (c == ';' || c == '&' || c == '|') {
628 command_mode = 1 + (c == c2);
629 if (c == '&') {
630 if (j == '>' || j == '<')
631 command_mode = 0;
632 } else if (c == '|' && j == '>')
633 command_mode = 0;
634 }
635 if (command_mode) {
636 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000637 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000638 }
639 }
640 /* collapse `command...` */
641 for (i = 0; int_buf[i]; i++)
642 if (int_buf[i] == '`') {
643 for (j = i + 1; int_buf[j]; j++)
644 if (int_buf[j] == '`') {
645 collapse_pos(i, j + 1);
646 j = 0;
647 break;
648 }
649 if (j) {
650 /* not found close ` - command mode, collapse all previous */
651 collapse_pos(0, i + 1);
652 break;
653 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000654 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000655 }
656
657 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000658 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000659 c2 = 0;
660 for (i = 0; int_buf[i]; i++)
661 if (int_buf[i] == '(' || int_buf[i] == '{') {
662 if (int_buf[i] == '(')
663 c++;
664 else
665 c2++;
666 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000667 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000668 }
669 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
670 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
671 if (int_buf[i] == ')')
672 c--;
673 else
674 c2--;
675 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000676 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000677 }
678
679 /* skip first not quote space */
680 for (i = 0; int_buf[i]; i++)
681 if (int_buf[i] != ' ')
682 break;
683 if (i)
684 collapse_pos(0, i);
685
686 /* set find mode for completion */
687 command_mode = FIND_EXE_ONLY;
688 for (i = 0; int_buf[i]; i++)
689 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
690 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000691 && matchBuf[pos_buf[0]]=='c'
692 && matchBuf[pos_buf[1]]=='d'
693 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000694 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000695 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000696 command_mode = FIND_FILE_ONLY;
697 break;
698 }
699 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000700 for (i = 0; int_buf[i]; i++)
701 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000702 /* find last word */
703 for (--i; i >= 0; i--) {
704 c = int_buf[i];
705 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
706 collapse_pos(0, i + 1);
707 break;
708 }
709 }
710 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000711 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
712 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000713 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000714 while ((int_buf[i] & ~QUOT) == '/'
715 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
716 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000717 i++;
718 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000719
720 /* set only match and destroy quotes */
721 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000722 for (c = 0; pos_buf[i] >= 0; i++) {
723 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000724 j = pos_buf[i] + 1;
725 }
Eric Andersen4f990532001-05-31 17:15:57 +0000726 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000727 /* old lenght matchBuf with quotes symbols */
728 *len_with_quotes = j ? j - pos_buf[0] : 0;
729
730 return command_mode;
731}
732
Glenn L McGrath4d001292003-01-06 01:11:50 +0000733/*
734 display by column original ideas from ls applet,
735 very optimize by my :)
736*/
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000737static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000738{
739 int ncols, row;
740 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000741 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000742 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000743 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000744
745 /* find the longest file name- use that as the column width */
746 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000747 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000748 if (column_width < l)
749 column_width = l;
750 }
751 column_width += 2; /* min space for columns */
752 ncols = cmdedit_termw / column_width;
753
754 if (ncols > 1) {
755 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000756 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000757 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000758 } else {
759 ncols = 1;
760 }
761 for (row = 0; row < nrows; row++) {
762 int n = row;
763 int nc;
764
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000765 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000766 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000767 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000768 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000769 printf("%s\n", matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000770 }
771}
772
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000773static int match_compare(const void *a, const void *b)
774{
775 return strcmp(*(char**)a, *(char**)b);
776}
777
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000778static char *add_quote_for_spec_chars(char *found)
779{
780 int l = 0;
781 char *s = xmalloc((strlen(found) + 1) * 2);
782
783 while (*found) {
784 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
785 s[l++] = '\\';
786 s[l++] = *found++;
787 }
788 s[l] = 0;
789 return s;
790}
791
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000792static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000793{
794 /* Do TAB completion */
Eric Andersenc470f442003-07-28 09:56:35 +0000795 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +0000796 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000797 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +0000798 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000799 free(matches);
800 matches = (char **) NULL;
801 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000802 return;
803 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000804 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000805 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000806 int len_found;
807 char matchBuf[BUFSIZ];
808 int find_type;
809 int recalc_pos;
810
Eric Andersenc470f442003-07-28 09:56:35 +0000811 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000812
813 /* Make a local copy of the string -- up
814 * to the position of the cursor */
815 tmp = strncpy(matchBuf, command_ps, cursor);
816 tmp[cursor] = 0;
817
818 find_type = find_match(matchBuf, &recalc_pos);
819
820 /* Free up any memory already allocated */
821 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +0000822
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000823#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000824 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000825 * then try completing this word as a username. */
826
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000827 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000828 username_tab_completion(matchBuf, NULL);
829 if (!matches)
Mark Whitley4e338752001-01-26 20:42:23 +0000830#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000831 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +0000832 * in the current working directory that matches. */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000833 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000834 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000835 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000836 int i, n = 0;
837 qsort(matches, num_matches, sizeof(char*), match_compare);
838 for (i = 0; i < num_matches - 1; ++i) {
839 if (matches[i] && matches[i+1]) {
840 if (strcmp(matches[i], matches[i+1]) == 0) {
841 free(matches[i]);
842 matches[i] = 0;
843 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000844 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000845 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000846 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000847 }
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000848 matches[n++] = matches[num_matches-1];
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000849 num_matches = n;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000850 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000851 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000852 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000853 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000854 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000855 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000856 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000857 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000858 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000859 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000860 if (matches[len_found][(tmp - tmp1)] != *tmp) {
861 *tmp = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000862 break;
863 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000864 if (*tmp1 == 0) { /* have unique */
865 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000866 return;
867 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000868 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000869 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000870 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000871 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000872 /* for next completion current found */
873 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000874
875 len_found = strlen(tmp);
876 if (tmp[len_found-1] != '/') {
877 tmp[len_found] = ' ';
878 tmp[len_found+1] = '\0';
879 }
Mark Whitley4e338752001-01-26 20:42:23 +0000880 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000881 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000882 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000883 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +0000884
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000885 /* before word for match */
886 command_ps[cursor - recalc_pos] = 0;
887 /* save tail line */
888 strcpy(matchBuf, command_ps + cursor);
889 /* add match */
890 strcat(command_ps, tmp);
891 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000892 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000893 /* back to begin word for match */
894 input_backward(recalc_pos);
895 /* new pos */
896 recalc_pos = cursor + len_found;
897 /* new len */
898 len = strlen(command_ps);
899 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +0000900 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000901 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000902 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +0000903 } else {
904 /* Ok -- the last char was a TAB. Since they
905 * just hit TAB again, print a list of all the
906 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000907 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000908 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000909
910 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000911 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000912 showfiles();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000913 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000914 }
915 }
916}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000917#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000918
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000919
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000920#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000921
922static char *history[MAX_HISTORY+1]; /* history + current */
923/* saved history lines */
924static int n_history;
925/* current pointer to history line */
926static int cur_history;
927
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000928static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000929{
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000930 if (command_ps[0] != 0 || history[cur_history] == 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000931 free(history[cur_history]);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000932 history[cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000933 }
934 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000935}
936
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000937static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000938{
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000939 int ch = cur_history;
940
941 if (ch < n_history) {
942 get_previous_history(); /* save the current history line */
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000943 cur_history = ch + 1;
944 return cur_history;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000945 } else {
946 beep();
947 return 0;
948 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000949}
Robert Griebl350d26b2002-12-03 22:45:46 +0000950
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000951#if ENABLE_FEATURE_COMMAND_SAVEHISTORY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000952void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000953{
Robert Griebl350d26b2002-12-03 22:45:46 +0000954 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000955 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000956
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000957 /* cleanup old */
958
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000959 for (hi = n_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000960 hi--;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000961 free(history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000962 }
963
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000964 fp = fopen(fromfile, "r");
965 if (fp) {
966 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000967 char * hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000968 int l;
969
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000970 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000971 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000972 l = strlen(hl);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000973 if (l >= BUFSIZ)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000974 hl[BUFSIZ-1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000975 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000976 free(hl);
977 continue;
978 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000979 history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000980 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000981 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000982 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000983 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000984}
985
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000986void save_history (const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000987{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000988 FILE *fp = fopen(tofile, "w");
Eric Andersenc470f442003-07-28 09:56:35 +0000989
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000990 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000991 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000992
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000993 for (i = 0; i < n_history; i++) {
994 fprintf(fp, "%s\n", history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000995 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000996 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000997 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000998}
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000999#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +00001000
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001001#endif /* MAX_HISTORY > 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001002
1003
Erik Andersen6273f652000-03-17 01:12:41 +00001004/*
1005 * This function is used to grab a character buffer
1006 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +00001007 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +00001008 * a mini readline).
1009 *
1010 * The following standard commands are not implemented:
1011 * ESC-b -- Move back one word
1012 * ESC-f -- Move forward one word
1013 * ESC-d -- Delete back one word
1014 * ESC-h -- Delete forward one word
1015 * CTL-t -- Transpose two characters
1016 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001017 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001018 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001019 */
Eric Andersenc470f442003-07-28 09:56:35 +00001020
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001021#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001022static int vi_mode;
1023
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001024void setvimode(int viflag)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001025{
1026 vi_mode = viflag;
1027}
1028
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001029static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001030vi_Word_motion(char *command, int eat)
1031{
1032 while (cursor < len && !isspace(command[cursor]))
1033 input_forward();
1034 if (eat) while (cursor < len && isspace(command[cursor]))
1035 input_forward();
1036}
1037
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001038static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001039vi_word_motion(char *command, int eat)
1040{
1041 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001042 while (cursor < len
1043 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001044 input_forward();
1045 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001046 while (cursor < len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001047 input_forward();
1048 }
1049
1050 if (cursor < len)
1051 input_forward();
1052
1053 if (eat && cursor < len && isspace(command[cursor]))
1054 while (cursor < len && isspace(command[cursor]))
1055 input_forward();
1056}
1057
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001058static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001059vi_End_motion(char *command)
1060{
1061 input_forward();
1062 while (cursor < len && isspace(command[cursor]))
1063 input_forward();
1064 while (cursor < len-1 && !isspace(command[cursor+1]))
1065 input_forward();
1066}
1067
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001068static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001069vi_end_motion(char *command)
1070{
1071 if (cursor >= len-1)
1072 return;
1073 input_forward();
1074 while (cursor < len-1 && isspace(command[cursor]))
1075 input_forward();
1076 if (cursor >= len-1)
1077 return;
1078 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001079 while (cursor < len-1
1080 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1081 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001082 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001083 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001084 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001085 while (cursor < len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001086 input_forward();
1087 }
1088}
1089
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001090static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001091vi_Back_motion(char *command)
1092{
1093 while (cursor > 0 && isspace(command[cursor-1]))
1094 input_backward(1);
1095 while (cursor > 0 && !isspace(command[cursor-1]))
1096 input_backward(1);
1097}
1098
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001099static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001100vi_back_motion(char *command)
1101{
1102 if (cursor <= 0)
1103 return;
1104 input_backward(1);
1105 while (cursor > 0 && isspace(command[cursor]))
1106 input_backward(1);
1107 if (cursor <= 0)
1108 return;
1109 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001110 while (cursor > 0
1111 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1112 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001113 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001114 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001115 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001116 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001117 input_backward(1);
1118 }
1119}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001120#else
1121enum { vi_mode = 0 };
1122#endif
1123
1124
1125/*
1126 * cmdedit_read_input and its helpers
1127 */
1128
1129#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
1130#define getTermSettings(fd, argp) tcgetattr(fd, argp);
1131
1132static sighandler_t previous_SIGWINCH_handler;
1133
1134static void cmdedit_reset_term(void)
1135{
1136 if (handlers_sets & SET_RESET_TERM) {
1137 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
1138 handlers_sets &= ~SET_RESET_TERM;
1139 }
1140 if (handlers_sets & SET_WCHG_HANDLERS) {
1141 /* restore SIGWINCH handler */
1142 signal(SIGWINCH, previous_SIGWINCH_handler);
1143 handlers_sets &= ~SET_WCHG_HANDLERS;
1144 }
1145 fflush(stdout);
1146}
1147
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001148static void cmdedit_setwidth(unsigned w, int redraw_flg)
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001149{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001150 cmdedit_termw = w;
1151 if (redraw_flg) {
1152 /* new y for current cursor */
1153 int new_y = (cursor + cmdedit_prmt_len) / w;
1154 /* redraw */
1155 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
1156 fflush(stdout);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001157 }
1158}
1159
1160static void win_changed(int nsig)
1161{
1162 int width;
1163 get_terminal_width_height(0, &width, NULL);
1164 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1165 if (nsig == SIGWINCH)
1166 signal(SIGWINCH, win_changed); /* rearm ourself */
1167}
1168
1169static void cmdedit_init(void)
1170{
1171 cmdedit_prmt_len = 0;
1172 if (!(handlers_sets & SET_WCHG_HANDLERS)) {
1173 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1174 win_changed(0); /* do initial resizing */
1175 handlers_sets |= SET_WCHG_HANDLERS;
1176 }
1177
1178 if (!(handlers_sets & SET_ATEXIT)) {
1179#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1180 struct passwd *entry;
1181
1182 my_euid = geteuid();
1183 entry = getpwuid(my_euid);
1184 if (entry) {
1185 user_buf = xstrdup(entry->pw_name);
1186 home_pwd_buf = xstrdup(entry->pw_dir);
1187 }
1188#endif
1189
1190#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
1191
1192#if !ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1193 my_euid = geteuid();
1194#endif
1195 my_uid = getuid();
1196 my_gid = getgid();
1197#endif /* FEATURE_COMMAND_TAB_COMPLETION */
1198 handlers_sets |= SET_ATEXIT;
1199// Crap. We should be able to do it without atexit.
1200 atexit(cmdedit_reset_term); /* be sure to do this only once */
1201 }
1202}
1203
1204#if !ENABLE_FEATURE_SH_FANCY_PROMPT
1205static void parse_prompt(const char *prmt_ptr)
1206{
1207 cmdedit_prompt = prmt_ptr;
1208 cmdedit_prmt_len = strlen(prmt_ptr);
1209 put_prompt();
1210}
1211#else
1212static void parse_prompt(const char *prmt_ptr)
1213{
1214 int prmt_len = 0;
1215 size_t cur_prmt_len = 0;
1216 char flg_not_length = '[';
1217 char *prmt_mem_ptr = xzalloc(1);
1218 char *pwd_buf = xgetcwd(0);
1219 char buf2[PATH_MAX + 1];
1220 char buf[2];
1221 char c;
1222 char *pbuf;
1223
1224 if (!pwd_buf) {
1225 pwd_buf = (char *)bb_msg_unknown;
1226 }
1227
1228 while (*prmt_ptr) {
1229 pbuf = buf;
1230 pbuf[1] = 0;
1231 c = *prmt_ptr++;
1232 if (c == '\\') {
1233 const char *cp = prmt_ptr;
1234 int l;
1235
1236 c = bb_process_escape_sequence(&prmt_ptr);
1237 if (prmt_ptr == cp) {
1238 if (*cp == 0)
1239 break;
1240 c = *prmt_ptr++;
1241 switch (c) {
1242#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1243 case 'u':
1244 pbuf = user_buf;
1245 break;
1246#endif
1247 case 'h':
1248 pbuf = hostname_buf;
1249 if (!pbuf) {
1250 pbuf = xzalloc(256);
1251 if (gethostname(pbuf, 255) < 0) {
1252 strcpy(pbuf, "?");
1253 } else {
1254 char *s = strchr(pbuf, '.');
1255 if (s)
1256 *s = '\0';
1257 }
1258 hostname_buf = pbuf;
1259 }
1260 break;
1261 case '$':
1262 c = (my_euid == 0 ? '#' : '$');
1263 break;
1264#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1265 case 'w':
1266 pbuf = pwd_buf;
1267 l = strlen(home_pwd_buf);
1268 if (home_pwd_buf[0] != 0
1269 && strncmp(home_pwd_buf, pbuf, l) == 0
1270 && (pbuf[l]=='/' || pbuf[l]=='\0')
1271 && strlen(pwd_buf+l)<PATH_MAX
1272 ) {
1273 pbuf = buf2;
1274 *pbuf = '~';
1275 strcpy(pbuf+1, pwd_buf+l);
1276 }
1277 break;
1278#endif
1279 case 'W':
1280 pbuf = pwd_buf;
1281 cp = strrchr(pbuf,'/');
1282 if (cp != NULL && cp != pbuf)
1283 pbuf += (cp-pbuf) + 1;
1284 break;
1285 case '!':
1286 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
1287 break;
1288 case 'e': case 'E': /* \e \E = \033 */
1289 c = '\033';
1290 break;
1291 case 'x': case 'X':
1292 for (l = 0; l < 3;) {
1293 int h;
1294 buf2[l++] = *prmt_ptr;
1295 buf2[l] = 0;
1296 h = strtol(buf2, &pbuf, 16);
1297 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
1298 l--;
1299 break;
1300 }
1301 prmt_ptr++;
1302 }
1303 buf2[l] = 0;
1304 c = (char)strtol(buf2, NULL, 16);
1305 if (c == 0)
1306 c = '?';
1307 pbuf = buf;
1308 break;
1309 case '[': case ']':
1310 if (c == flg_not_length) {
1311 flg_not_length = flg_not_length == '[' ? ']' : '[';
1312 continue;
1313 }
1314 break;
1315 }
1316 }
1317 }
1318 if (pbuf == buf)
1319 *pbuf = c;
1320 cur_prmt_len = strlen(pbuf);
1321 prmt_len += cur_prmt_len;
1322 if (flg_not_length != ']')
1323 cmdedit_prmt_len += cur_prmt_len;
1324 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
1325 }
1326 if (pwd_buf!=(char *)bb_msg_unknown)
1327 free(pwd_buf);
1328 cmdedit_prompt = prmt_mem_ptr;
1329 put_prompt();
1330}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001331#endif
1332
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001333/*
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001334 * the emacs and vi modes share much of the code in the big
1335 * command loop. commands entered when in vi's command mode (aka
1336 * "escape mode") get an extra bit added to distinguish them --
1337 * this keeps them from being self-inserted. this clutters the
1338 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001339 */
1340
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001341#define vbit 0x100
1342
1343/* leave out the "vi-mode"-only case labels if vi editing isn't
1344 * configured. */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001345#define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001346
1347/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001348#undef CTRL
1349#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001350
Eric Andersen044228d2001-07-17 01:12:36 +00001351
1352int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001353{
Erik Andersenc7c634b2000-03-19 05:28:55 +00001354 int lastWasTab = FALSE;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001355 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001356 unsigned char c;
1357 smallint break_out = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001358#if ENABLE_FEATURE_COMMAND_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001359 smallint vi_cmdmode = 0;
1360 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001361#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001362 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001363 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001364 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001365 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001366 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001367
Eric Andersen34506362001-08-02 05:02:46 +00001368 getTermSettings(0, (void *) &initial_settings);
1369 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1370 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1371 /* Turn off echoing and CTRL-C, so we can trap it */
1372 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001373 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1374 new_settings.c_cc[VMIN] = 1;
1375 new_settings.c_cc[VTIME] = 0;
1376 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001377#ifndef _POSIX_VDISABLE
1378#define _POSIX_VDISABLE '\0'
1379#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001380 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001381 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001382 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001383
Eric Andersen6faae7d2001-02-16 20:09:17 +00001384 /* Now initialize things */
1385 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001386 /* Print out the command prompt */
1387 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001388
Erik Andersenc7c634b2000-03-19 05:28:55 +00001389 while (1) {
Eric Andersenc470f442003-07-28 09:56:35 +00001390 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001391
Eric Andersen7467c8d2001-07-12 20:26:32 +00001392 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001393 /* if we can't read input then exit */
1394 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001395
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001396 ic = c;
1397
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001398#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001399 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001400 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001401 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001402#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001403 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001404 case '\n':
1405 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001406 vi_case('\n'|vbit:)
1407 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001408 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001409 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001410 break_out = 1;
1411 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001412 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001413 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001414 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001415 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001416 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001417 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001418 vi_case('h'|vbit:)
1419 vi_case('\b'|vbit:)
1420 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001421 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001422 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001423 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001424 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001425 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001426 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001427 goto_new_line();
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001428#if !ENABLE_ASH
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001429 command[0] = '\0';
Eric Andersen7467c8d2001-07-12 20:26:32 +00001430 len = 0;
1431 lastWasTab = FALSE;
1432 put_prompt();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001433#else
1434 len = 0;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001435 break_out = -1; /* to control traps */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001436#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001437 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001438 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001439 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001440 * if the len=0 and no chars to delete */
1441 if (len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001442 errno = 0;
1443 prepare_to_die:
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001444// So, our API depends on whether we have ash compiled in or not? Crap...
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001445#if !ENABLE_ASH
Mark Whitley4e338752001-01-26 20:42:23 +00001446 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001447 goto_new_line();
1448 /* cmdedit_reset_term() called in atexit */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001449// FIXME. this is definitely not good
Eric Andersen7467c8d2001-07-12 20:26:32 +00001450 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001451#else
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001452 /* to control stopped jobs */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001453 break_out = len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001454 break;
1455#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001456 } else {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001457 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001458 }
1459 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001460 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001461 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001462 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001463 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001464 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001465 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001466 vi_case('l'|vbit:)
1467 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001468 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001469 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001470 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001471 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001472 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001473 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001474 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001475 break;
1476 case '\t':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001477#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001478 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001479#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001480 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001481 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001482 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001483 command[cursor] = 0;
Eric Andersen65a07302002-04-13 13:26:49 +00001484 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001485 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001486 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001487 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001488 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001489 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001490 printf("\033[H");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001491 redraw(0, len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001492 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001493#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001494 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001495 vi_case(CTRL('N')|vbit:)
1496 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001497 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001498 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001499 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001500 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001501 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001502 vi_case(CTRL('P')|vbit:)
1503 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001504 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001505 if (cur_history > 0) {
1506 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001507 goto rewrite_line;
1508 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001509 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001510 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001511 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001512#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001513 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001514 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001515 /* Control-U -- Clear line before cursor */
1516 if (cursor) {
1517 strcpy(command, command + cursor);
1518 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001519 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001520 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001521 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001522 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001523 /* Control-W -- Remove the last word */
1524 while (cursor > 0 && isspace(command[cursor-1]))
1525 input_backspace();
1526 while (cursor > 0 &&!isspace(command[cursor-1]))
1527 input_backspace();
1528 break;
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001529#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001530 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001531 vi_cmdmode = 0;
1532 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001533 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001534 input_backward(cursor);
1535 vi_cmdmode = 0;
1536 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001537 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001538 input_forward();
1539 vi_cmdmode = 0;
1540 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001541 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001542 input_end();
1543 vi_cmdmode = 0;
1544 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001545 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001546 input_delete(1);
1547 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001548 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001549 if (cursor > 0) {
1550 input_backward(1);
1551 input_delete(1);
1552 }
1553 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001554 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001555 vi_Word_motion(command, 1);
1556 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001557 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001558 vi_word_motion(command, 1);
1559 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001560 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001561 vi_End_motion(command);
1562 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001563 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001564 vi_end_motion(command);
1565 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001566 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001567 vi_Back_motion(command);
1568 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001569 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001570 vi_back_motion(command);
1571 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001572 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001573 vi_cmdmode = 0;
1574 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001575 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001576 goto clear_to_eol;
1577
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001578 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001579 vi_cmdmode = 0;
1580 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001581 case 'd'|vbit: {
1582 int nc, sc;
1583 sc = cursor;
1584 prevc = ic;
1585 if (safe_read(0, &c, 1) < 1)
1586 goto prepare_to_die;
1587 if (c == (prevc & 0xff)) {
1588 /* "cc", "dd" */
1589 input_backward(cursor);
1590 goto clear_to_eol;
1591 break;
1592 }
1593 switch (c) {
1594 case 'w':
1595 case 'W':
1596 case 'e':
1597 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001598 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001599 case 'w': /* "dw", "cw" */
1600 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001601 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001602 case 'W': /* 'dW', 'cW' */
1603 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001604 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001605 case 'e': /* 'de', 'ce' */
1606 vi_end_motion(command);
1607 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001608 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001609 case 'E': /* 'dE', 'cE' */
1610 vi_End_motion(command);
1611 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001612 break;
1613 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001614 nc = cursor;
1615 input_backward(cursor - sc);
1616 while (nc-- > cursor)
1617 input_delete(1);
1618 break;
1619 case 'b': /* "db", "cb" */
1620 case 'B': /* implemented as B */
1621 if (c == 'b')
1622 vi_back_motion(command);
1623 else
1624 vi_Back_motion(command);
1625 while (sc-- > cursor)
1626 input_delete(1);
1627 break;
1628 case ' ': /* "d ", "c " */
1629 input_delete(1);
1630 break;
1631 case '$': /* "d$", "c$" */
1632 clear_to_eol:
1633 while (cursor < len)
1634 input_delete(1);
1635 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001636 }
1637 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001638 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001639 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001640 input_forward();
1641 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001642 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001643 put();
1644 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001645 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001646 if (safe_read(0, &c, 1) < 1)
1647 goto prepare_to_die;
1648 if (c == 0)
1649 beep();
1650 else {
1651 *(command + cursor) = c;
1652 putchar(c);
1653 putchar('\b');
1654 }
1655 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001656#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001657
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001658 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001659
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001660#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001661 if (vi_mode) {
1662 /* ESC: insert mode --> command mode */
1663 vi_cmdmode = 1;
1664 input_backward(1);
1665 break;
1666 }
1667#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001668 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001669 if (safe_read(0, &c, 1) < 1)
1670 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001671 /* different vt100 emulations */
1672 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001673 vi_case('['|vbit:)
1674 vi_case('O'|vbit:)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001675 if (safe_read(0, &c, 1) < 1)
1676 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001677 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001678 if (c >= '1' && c <= '9') {
1679 unsigned char dummy;
1680
1681 if (safe_read(0, &dummy, 1) < 1)
1682 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001683 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001684 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001685 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001686 switch (c) {
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001687#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001688 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001689 input_tab(&lastWasTab);
1690 break;
1691#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001692#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001693 case 'A':
1694 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001695 if (cur_history > 0) {
1696 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001697 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001698 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001699 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001700 break;
1701 case 'B':
1702 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001703 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001704 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001705 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001706 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001707 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001708 len = strlen(strcpy(command, history[cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001709 /* redraw and go to eol (bol, in vi */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001710 redraw(cmdedit_y, vi_mode ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001711 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001712#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001713 case 'C':
1714 /* Right Arrow -- Move forward one character */
1715 input_forward();
1716 break;
1717 case 'D':
1718 /* Left Arrow -- Move back one character */
1719 input_backward(1);
1720 break;
1721 case '3':
1722 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001723 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001724 break;
1725 case '1':
1726 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001727 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001728 input_backward(cursor);
1729 break;
1730 case '4':
1731 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001732 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001733 input_end();
1734 break;
1735 default:
Glenn L McGrath475820c2004-01-22 12:42:23 +00001736 c = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001737 beep();
1738 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001739 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001740
Eric Andersenc470f442003-07-28 09:56:35 +00001741 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001742#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001743 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001744 if (c == CTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001745 if (safe_read(0, &c, 1) < 1)
1746 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001747 if (c == 0) {
1748 beep();
1749 break;
1750 }
1751 } else
1752#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001753 {
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001754#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001755 if (vi_cmdmode) /* don't self-insert */
1756 break;
1757#endif
1758 if (!Isprint(c)) /* Skip non-printable characters */
1759 break;
1760 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001761
Eric Andersenc470f442003-07-28 09:56:35 +00001762 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001763 break;
1764
1765 len++;
1766
Eric Andersenc470f442003-07-28 09:56:35 +00001767 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001768 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001769 *(command + cursor + 1) = 0;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001770 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001771 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001772 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001773
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001774 memmove(command + sc + 1, command + sc, len - sc);
1775 *(command + sc) = c;
1776 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001777 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001778 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001779 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001780 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001781 }
1782
Erik Andersenc7c634b2000-03-19 05:28:55 +00001783 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001784 }
Eric Andersenc470f442003-07-28 09:56:35 +00001785 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001786 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001787
1788 if (c != '\t')
1789 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001790 }
1791
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001792 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001793 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001794
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001795#if MAX_HISTORY > 0
Erik Andersenc7c634b2000-03-19 05:28:55 +00001796 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001797 /* cleanup may be saved current command line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001798 if (len > 0) { /* no put empty line */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001799 int i = n_history;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001800
1801 free(history[MAX_HISTORY]);
1802 history[MAX_HISTORY] = 0;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001803 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001804 if (i >= MAX_HISTORY) {
1805 free(history[0]);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001806 for (i = 0; i < MAX_HISTORY-1; i++)
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001807 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001808 }
Rob Landleyd921b2e2006-08-03 15:41:12 +00001809 history[i++] = xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001810 cur_history = i;
1811 n_history = i;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001812 USE_FEATURE_SH_FANCY_PROMPT(num_ok_lines++;)
Erik Andersen6273f652000-03-17 01:12:41 +00001813 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001814#else /* MAX_HISTORY == 0 */
1815 /* dont put empty line */
1816 USE_FEATURE_SH_FANCY_PROMPT(if (len > 0) num_ok_lines++;)
1817#endif /* MAX_HISTORY */
1818
Eric Andersen27bb7902003-12-23 20:24:51 +00001819 if (break_out > 0) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001820 command[len++] = '\n';
1821 command[len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001822 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001823
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001824#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001825 input_tab(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001826#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001827
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001828#if ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001829 free(cmdedit_prompt);
1830#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001831 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001832 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001833}
1834
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001835#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001836
1837
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001838/*
1839 * Testing
1840 */
1841
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001842#ifdef TEST
1843
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001844#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001845
1846const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001847
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001848int main(int argc, char **argv)
1849{
1850 char buff[BUFSIZ];
1851 char *prompt =
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001852#if ENABLE_FEATURE_SH_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001853 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1854 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1855 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001856#else
1857 "% ";
1858#endif
1859
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001860#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001861 setlocale(LC_ALL, "");
1862#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001863 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001864 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001865 l = cmdedit_read_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001866 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001867 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001868 buff[l-1] = 0;
1869 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001870 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001871 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001872 return 0;
1873}
1874
Eric Andersenc470f442003-07-28 09:56:35 +00001875#endif /* TEST */