blob: 39cab19a02a60f91e520d226f91daeda5f26d5f0 [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 */
Erik Andersen13456d12000-03-16 08:09:57 +0000122
Mark Whitley4e338752001-01-26 20:42:23 +0000123
Eric Andersenc470f442003-07-28 09:56:35 +0000124static int cmdedit_x; /* real x terminal position */
125static int cmdedit_y; /* pseudoreal y terminal position */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000126static int cmdedit_prmt_len; /* length of prompt (without colors etc) */
Eric Andersen86349772000-12-18 20:25:50 +0000127
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000128static int cursor;
129static int len;
130static char *command_ps;
131static SKIP_FEATURE_SH_FANCY_PROMPT(const) char *cmdedit_prompt;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000132
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000133#if ENABLE_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000134static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000135static int num_ok_lines = 1;
136#endif
137
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000138#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
139static char *user_buf = "";
140static char *home_pwd_buf = "";
141#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000142
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000143#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000144static int my_uid;
145static int my_gid;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000146#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000147
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000148/* Put 'command_ps[cursor]', cursor++.
149 * Advance cursor on screen. If we reached right margin, scroll text up
150 * and remove terminal margin effect by printing 'next_char' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000151static void cmdedit_set_out_char(int next_char)
152{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000153 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000154
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000155 if (c == '\0') {
156 /* erase character after end of input string */
157 c = ' ';
158 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000159#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000160 /* Display non-printable characters in reverse */
161 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000162 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000163 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000164 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000165 c += '@';
166 if (c == 127)
167 c = '?';
168 printf("\033[7m%c\033[0m", c);
169 } else
170#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000171 {
172 if (initial_settings.c_lflag & ECHO)
173 putchar(c);
174 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000175 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000176 /* terminal is scrolled down */
177 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000178 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000179 /* destroy "(auto)margin" */
180 putchar(next_char);
181 putchar('\b');
182 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000183// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000184 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000185}
186
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000187/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000188static void input_end(void)
189{
190 while (cursor < len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000191 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000192}
193
194/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000195static void goto_new_line(void)
196{
Mark Whitley4e338752001-01-26 20:42:23 +0000197 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000198 if (cmdedit_x)
199 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000200}
201
202
Rob Landley88621d72006-08-29 19:41:06 +0000203static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000204{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000205 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000206 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000207}
Eric Andersen81fe1232003-07-29 06:38:40 +0000208
Rob Landley88621d72006-08-29 19:41:06 +0000209static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000210{
211 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000212}
213
Eric Andersenaff114c2004-04-14 17:51:38 +0000214/* Move back one character */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000215/* (optimized for slow terminals) */
216static void input_backward(unsigned num)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000217{
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000218 int count_y;
219
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000220 if (num > cursor)
221 num = cursor;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000222 if (!num)
223 return;
224 cursor -= num;
Erik Andersen13456d12000-03-16 08:09:57 +0000225
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000226 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000227 cmdedit_x -= num;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000228 if (num <= 4) {
229 do putchar('\b'); while (--num);
230 return;
231 }
232 printf("\033[%uD", num);
233 return;
Erik Andersen13456d12000-03-16 08:09:57 +0000234 }
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000235
236 /* Need to go one or more lines up */
237 num -= cmdedit_x;
238 count_y = 1 + (num / cmdedit_termw);
239 cmdedit_y -= count_y;
240 cmdedit_x = cmdedit_termw * count_y - num;
241 /* go to 1st col; go up; go to correct column */
242 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);
Erik Andersen13456d12000-03-16 08:09:57 +0000243}
244
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000245static void put_prompt(void)
246{
247 out1str(cmdedit_prompt);
Eric Andersenc470f442003-07-28 09:56:35 +0000248 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000249 cursor = 0;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +0000250// Huh? what if cmdedit_prmt_len >= width?
Eric Andersen7467c8d2001-07-12 20:26:32 +0000251 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000252}
253
Eric Andersenaff114c2004-04-14 17:51:38 +0000254/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000255static void redraw(int y, int back_cursor)
256{
Eric Andersenc470f442003-07-28 09:56:35 +0000257 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000258 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000259 putchar('\r');
260 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000261 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000262 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000263 input_backward(back_cursor);
264}
265
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000266#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000267#define DELBUFSIZ 128
268static char *delbuf; /* a (malloced) place to store deleted characters */
269static char *delp;
270static char newdelflag; /* whether delbuf should be reused yet */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000271#endif
272
273/* Delete the char in front of the cursor, optionally saving it
274 * for later putback */
275static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000276{
Mark Whitley4e338752001-01-26 20:42:23 +0000277 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000278
Mark Whitley4e338752001-01-26 20:42:23 +0000279 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000280 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000281
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000282#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000283 if (save) {
284 if (newdelflag) {
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000285 if (!delbuf)
286 delbuf = malloc(DELBUFSIZ);
287 /* safe if malloc fails */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000288 delp = delbuf;
289 newdelflag = 0;
290 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000291 if (delbuf && (delp - delbuf < DELBUFSIZ))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000292 *delp++ = command_ps[j];
293 }
294#endif
295
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000296 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000297 len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000298 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000299 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000300 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000301}
302
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000303#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000304static void put(void)
305{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000306 int ocursor;
307 int j = delp - delbuf;
308
Paul Fox3f11b1b2005-08-04 19:04:46 +0000309 if (j == 0)
310 return;
311 ocursor = cursor;
312 /* open hole and then fill it */
313 memmove(command_ps + cursor + j, command_ps + cursor, len - cursor + 1);
314 strncpy(command_ps + cursor, delbuf, j);
315 len += j;
316 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000317 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000318}
319#endif
320
Mark Whitley4e338752001-01-26 20:42:23 +0000321/* Delete the char in back of the cursor */
322static void input_backspace(void)
323{
324 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000325 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000326 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000327 }
328}
329
Eric Andersenaff114c2004-04-14 17:51:38 +0000330/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000331static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000332{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000333 if (cursor < len)
334 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000335}
336
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000337
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000338#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000339
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000340static char **matches;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000341static unsigned num_matches;
342
343static void free_tab_completion_data(void)
344{
345 if (matches) {
346 while (num_matches)
347 free(matches[--num_matches]);
348 free(matches);
349 matches = NULL;
350 }
351}
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000352
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000353static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000354{
355 int nm = num_matches;
356 int nm1 = nm + 1;
357
358 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000359 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000360 num_matches++;
361}
362
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000363#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000364static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000365{
366 struct passwd *entry;
367 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000368
Eric Andersenc470f442003-07-28 09:56:35 +0000369 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000370 userlen = strlen(ud);
371
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000372 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000373 char *sav_ud = ud - 1;
374 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000375 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000376
Eric Andersenc470f442003-07-28 09:56:35 +0000377 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000378 home = home_pwd_buf;
379 } else {
380 /* "~user/..." */
381 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000382 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000383 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000384 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000385 ud = temp;
386 if (entry)
387 home = entry->pw_dir;
388 }
389 if (home) {
390 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000391 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000392
393 /* /home/user/... */
394 sprintf(temp2, "%s%s", home, ud);
395 strcpy(sav_ud, temp2);
396 }
397 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000398 } else {
399 /* "~[^/]*" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000400 setpwent();
401
402 while ((entry = getpwent()) != NULL) {
403 /* Null usernames should result in all users as possible completions. */
404 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000405 add_match(xasprintf("~%s/", entry->pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000406 }
407 }
408
409 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000410 }
411}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000412#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000413
414enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415 FIND_EXE_ONLY = 0,
416 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000417 FIND_FILE_ONLY = 2,
418};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000419
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000420#if ENABLE_ASH
Glenn L McGrath67285962004-01-14 09:34:51 +0000421const char *cmdedit_path_lookup;
Glenn L McGrath67285962004-01-14 09:34:51 +0000422#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000423static int path_parse(char ***p, int flags)
424{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000425 int npth;
Glenn L McGrath67285962004-01-14 09:34:51 +0000426 const char *tmp;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000427#if ENABLE_ASH
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000428 const char *pth = cmdedit_path_lookup;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000429#else
430 const char *pth = getenv("PATH")
431#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000432
Mark Whitley4e338752001-01-26 20:42:23 +0000433 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000434 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000435 return 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000436 /* PATH=<empty> or PATH=:<empty> */
437 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
438 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000439
440 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000441 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000442
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000443 while (1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000444 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000445 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000446 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000447 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000448 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000449 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000450 }
451
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000452 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000453
454 tmp = pth;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000455 (*p)[0] = xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000456 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000457
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000458 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000459 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000460 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000461 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000462 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
463 if (*++tmp == 0)
464 break; /* :<empty> */
Eric Andersenc470f442003-07-28 09:56:35 +0000465 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000466 }
467
468 return npth;
469}
470
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000471static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000472{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000473 DIR *dir;
474 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000475 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000476 struct stat st;
477 char *path1[1];
478 char **paths = path1;
479 int npaths;
480 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000481 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000482 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000483
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000484 npaths = 1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000485 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000486
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000487 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000488 /* no dir, if flags==EXE_ONLY - get paths, else "." */
489 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000491 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000492 /* dirbuf = ".../.../.../" */
493 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000494#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000495 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000496 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000497#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000498 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000499 /* point to 'l' in "..../last_component" */
500 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000501 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000502
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000503 for (i = 0; i < npaths; i++) {
Mark Whitley4e338752001-01-26 20:42:23 +0000504 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000505 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000506 continue;
507
508 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000509 int len1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000510 char *str_found = next->d_name;
511
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000512 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000513 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000514 continue;
515 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000517 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000518 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000519 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000520 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000521 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000522 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000523 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000524 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000525 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000526 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000527 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000528
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000529 len1 = strlen(found);
530 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000531 found[len1] = '\0';
532 found[len1+1] = '\0';
533
Mark Whitley4e338752001-01-26 20:42:23 +0000534 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000535 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000536 if (found[len1-1] != '/') {
537 found[len1] = '/';
538 }
Mark Whitley4e338752001-01-26 20:42:23 +0000539 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000540 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000541 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000542 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000543 }
Mark Whitley4e338752001-01-26 20:42:23 +0000544 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000545 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000546 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000547 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000548 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000549 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000550 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000551 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000552 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000553 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000554 free(paths);
555 }
Erik Andersen6273f652000-03-17 01:12:41 +0000556}
Erik Andersenf0657d32000-04-12 17:49:52 +0000557
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000558#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000559
560#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000561 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
562 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000563
564static int find_match(char *matchBuf, int *len_with_quotes)
565{
566 int i, j;
567 int command_mode;
568 int c, c2;
569 int int_buf[BUFSIZ + 1];
570 int pos_buf[BUFSIZ + 1];
571
572 /* set to integer dimension characters and own positions */
573 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000574 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000575 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000576 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000577 break;
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000578 }
579 pos_buf[i] = i;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000580 }
581
582 /* mask \+symbol and convert '\t' to ' ' */
583 for (i = j = 0; matchBuf[i]; i++, j++)
584 if (matchBuf[i] == '\\') {
585 collapse_pos(j, j + 1);
586 int_buf[j] |= QUOT;
587 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000588#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000589 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000590 int_buf[j] = ' ' | QUOT;
591#endif
592 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000593#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000594 else if (matchBuf[i] == '\t')
595 int_buf[j] = ' ';
596#endif
597
598 /* mask "symbols" or 'symbols' */
599 c2 = 0;
600 for (i = 0; int_buf[i]; i++) {
601 c = int_buf[i];
602 if (c == '\'' || c == '"') {
603 if (c2 == 0)
604 c2 = c;
605 else {
606 if (c == c2)
607 c2 = 0;
608 else
609 int_buf[i] |= QUOT;
610 }
611 } else if (c2 != 0 && c != '$')
612 int_buf[i] |= QUOT;
613 }
614
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000615 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000616 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
617 for (i = 0; int_buf[i]; i++) {
618 c = int_buf[i];
619 c2 = int_buf[i + 1];
620 j = i ? int_buf[i - 1] : -1;
621 command_mode = 0;
622 if (c == ';' || c == '&' || c == '|') {
623 command_mode = 1 + (c == c2);
624 if (c == '&') {
625 if (j == '>' || j == '<')
626 command_mode = 0;
627 } else if (c == '|' && j == '>')
628 command_mode = 0;
629 }
630 if (command_mode) {
631 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000632 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000633 }
634 }
635 /* collapse `command...` */
636 for (i = 0; int_buf[i]; i++)
637 if (int_buf[i] == '`') {
638 for (j = i + 1; int_buf[j]; j++)
639 if (int_buf[j] == '`') {
640 collapse_pos(i, j + 1);
641 j = 0;
642 break;
643 }
644 if (j) {
645 /* not found close ` - command mode, collapse all previous */
646 collapse_pos(0, i + 1);
647 break;
648 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000649 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000650 }
651
652 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000653 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000654 c2 = 0;
655 for (i = 0; int_buf[i]; i++)
656 if (int_buf[i] == '(' || int_buf[i] == '{') {
657 if (int_buf[i] == '(')
658 c++;
659 else
660 c2++;
661 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000662 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000663 }
664 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
665 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
666 if (int_buf[i] == ')')
667 c--;
668 else
669 c2--;
670 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000671 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000672 }
673
674 /* skip first not quote space */
675 for (i = 0; int_buf[i]; i++)
676 if (int_buf[i] != ' ')
677 break;
678 if (i)
679 collapse_pos(0, i);
680
681 /* set find mode for completion */
682 command_mode = FIND_EXE_ONLY;
683 for (i = 0; int_buf[i]; i++)
684 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
685 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000686 && matchBuf[pos_buf[0]]=='c'
687 && matchBuf[pos_buf[1]]=='d'
688 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000689 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000690 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000691 command_mode = FIND_FILE_ONLY;
692 break;
693 }
694 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000695 for (i = 0; int_buf[i]; i++)
696 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000697 /* find last word */
698 for (--i; i >= 0; i--) {
699 c = int_buf[i];
700 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
701 collapse_pos(0, i + 1);
702 break;
703 }
704 }
705 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000706 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
707 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000708 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000709 while ((int_buf[i] & ~QUOT) == '/'
710 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
711 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000712 i++;
713 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000714
715 /* set only match and destroy quotes */
716 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000717 for (c = 0; pos_buf[i] >= 0; i++) {
718 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000719 j = pos_buf[i] + 1;
720 }
Eric Andersen4f990532001-05-31 17:15:57 +0000721 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000722 /* old lenght matchBuf with quotes symbols */
723 *len_with_quotes = j ? j - pos_buf[0] : 0;
724
725 return command_mode;
726}
727
Glenn L McGrath4d001292003-01-06 01:11:50 +0000728/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000729 * display by column (original idea from ls applet,
730 * very optimized by me :)
731 */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000732static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000733{
734 int ncols, row;
735 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000736 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000737 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000738 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000739
740 /* find the longest file name- use that as the column width */
741 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000742 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000743 if (column_width < l)
744 column_width = l;
745 }
746 column_width += 2; /* min space for columns */
747 ncols = cmdedit_termw / column_width;
748
749 if (ncols > 1) {
750 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000751 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000752 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000753 } else {
754 ncols = 1;
755 }
756 for (row = 0; row < nrows; row++) {
757 int n = row;
758 int nc;
759
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000760 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000761 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000762 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000763 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000764 printf("%s\n", matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000765 }
766}
767
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000768static char *add_quote_for_spec_chars(char *found)
769{
770 int l = 0;
771 char *s = xmalloc((strlen(found) + 1) * 2);
772
773 while (*found) {
774 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
775 s[l++] = '\\';
776 s[l++] = *found++;
777 }
778 s[l] = 0;
779 return s;
780}
781
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000782static int match_compare(const void *a, const void *b)
783{
784 return strcmp(*(char**)a, *(char**)b);
785}
786
787/* Do TAB completion */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000788static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000789{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000790 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000791 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000792 int len_found;
793 char matchBuf[BUFSIZ];
794 int find_type;
795 int recalc_pos;
796
Eric Andersenc470f442003-07-28 09:56:35 +0000797 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000798
799 /* Make a local copy of the string -- up
800 * to the position of the cursor */
801 tmp = strncpy(matchBuf, command_ps, cursor);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000802 tmp[cursor] = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000803
804 find_type = find_match(matchBuf, &recalc_pos);
805
806 /* Free up any memory already allocated */
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000807 free_tab_completion_data();
Erik Andersenf0657d32000-04-12 17:49:52 +0000808
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000809#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000810 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000811 * then try completing this word as a username. */
812
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000813 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000814 username_tab_completion(matchBuf, NULL);
815 if (!matches)
Mark Whitley4e338752001-01-26 20:42:23 +0000816#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000817 /* Try to match any executable in our path and everything
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000818 * in the current working directory */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000819 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000820 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000821 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000822 int i, n = 0;
823 qsort(matches, num_matches, sizeof(char*), match_compare);
824 for (i = 0; i < num_matches - 1; ++i) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000825 if (matches[i] && matches[i+1]) { /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000826 if (strcmp(matches[i], matches[i+1]) == 0) {
827 free(matches[i]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000828 matches[i] = NULL; /* paranoia */
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000829 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000830 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000831 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000832 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000833 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000834 matches[n] = matches[i];
835 num_matches = n + 1;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000836 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000837 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000838 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000839 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000840 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000841 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000842 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000843 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000844 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000845 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000846 if (matches[len_found][(tmp - tmp1)] != *tmp) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000847 *tmp = '\0';
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000848 break;
849 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000850 if (*tmp1 == '\0') { /* have unique */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000851 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000852 return;
853 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000854 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000855 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000856 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000857 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000858 /* for next completion current found */
859 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000860
861 len_found = strlen(tmp);
862 if (tmp[len_found-1] != '/') {
863 tmp[len_found] = ' ';
864 tmp[len_found+1] = '\0';
865 }
Mark Whitley4e338752001-01-26 20:42:23 +0000866 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000867 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000868 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000869 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000870 /* before word for match */
871 command_ps[cursor - recalc_pos] = 0;
872 /* save tail line */
873 strcpy(matchBuf, command_ps + cursor);
874 /* add match */
875 strcat(command_ps, tmp);
876 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000877 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000878 /* back to begin word for match */
879 input_backward(recalc_pos);
880 /* new pos */
881 recalc_pos = cursor + len_found;
882 /* new len */
883 len = strlen(command_ps);
884 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +0000885 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000886 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000887 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +0000888 } else {
889 /* Ok -- the last char was a TAB. Since they
890 * just hit TAB again, print a list of all the
891 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000892 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000893 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000894
895 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000896 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000897 showfiles();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000898 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000899 }
900 }
901}
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000902
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000903#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000904
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000905
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000906#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000907
908static char *history[MAX_HISTORY+1]; /* history + current */
909/* saved history lines */
910static int n_history;
911/* current pointer to history line */
912static int cur_history;
913
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000914static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000915{
Denis Vlasenko5592fac2007-01-21 19:19:46 +0000916 if (command_ps[0] != '\0' || history[cur_history] == NULL) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000917 free(history[cur_history]);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000918 history[cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000919 }
920 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000921}
922
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000923static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000924{
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000925 int ch = cur_history;
926
927 if (ch < n_history) {
928 get_previous_history(); /* save the current history line */
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000929 cur_history = ch + 1;
930 return cur_history;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000931 } else {
932 beep();
933 return 0;
934 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000935}
Robert Griebl350d26b2002-12-03 22:45:46 +0000936
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000937#if ENABLE_FEATURE_COMMAND_SAVEHISTORY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000938void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000939{
Robert Griebl350d26b2002-12-03 22:45:46 +0000940 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000941 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000942
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000943 /* cleanup old */
944
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000945 for (hi = n_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000946 hi--;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000947 free(history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000948 }
949
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000950 fp = fopen(fromfile, "r");
951 if (fp) {
952 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000953 char * hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000954 int l;
955
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000956 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000957 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000958 l = strlen(hl);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000959 if (l >= BUFSIZ)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000960 hl[BUFSIZ-1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000961 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000962 free(hl);
963 continue;
964 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000965 history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000966 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000967 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000968 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000969 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000970}
971
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000972void save_history (const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000973{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000974 FILE *fp = fopen(tofile, "w");
Eric Andersenc470f442003-07-28 09:56:35 +0000975
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000976 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000977 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000978
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000979 for (i = 0; i < n_history; i++) {
980 fprintf(fp, "%s\n", history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000981 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000982 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000983 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000984}
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000985#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +0000986
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000987#endif /* MAX_HISTORY > 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000988
989
Erik Andersen6273f652000-03-17 01:12:41 +0000990/*
991 * This function is used to grab a character buffer
992 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +0000993 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +0000994 * a mini readline).
995 *
996 * The following standard commands are not implemented:
997 * ESC-b -- Move back one word
998 * ESC-f -- Move forward one word
999 * ESC-d -- Delete back one word
1000 * ESC-h -- Delete forward one word
1001 * CTL-t -- Transpose two characters
1002 *
Paul Fox3f11b1b2005-08-04 19:04:46 +00001003 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001004 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +00001005 */
Eric Andersenc470f442003-07-28 09:56:35 +00001006
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001007#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001008static int vi_mode;
1009
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001010void setvimode(int viflag)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001011{
1012 vi_mode = viflag;
1013}
1014
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001015static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001016vi_Word_motion(char *command, int eat)
1017{
1018 while (cursor < len && !isspace(command[cursor]))
1019 input_forward();
1020 if (eat) while (cursor < len && isspace(command[cursor]))
1021 input_forward();
1022}
1023
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001024static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001025vi_word_motion(char *command, int eat)
1026{
1027 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001028 while (cursor < len
1029 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001030 input_forward();
1031 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001032 while (cursor < len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001033 input_forward();
1034 }
1035
1036 if (cursor < len)
1037 input_forward();
1038
1039 if (eat && cursor < len && isspace(command[cursor]))
1040 while (cursor < len && isspace(command[cursor]))
1041 input_forward();
1042}
1043
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001044static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001045vi_End_motion(char *command)
1046{
1047 input_forward();
1048 while (cursor < len && isspace(command[cursor]))
1049 input_forward();
1050 while (cursor < len-1 && !isspace(command[cursor+1]))
1051 input_forward();
1052}
1053
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001054static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001055vi_end_motion(char *command)
1056{
1057 if (cursor >= len-1)
1058 return;
1059 input_forward();
1060 while (cursor < len-1 && isspace(command[cursor]))
1061 input_forward();
1062 if (cursor >= len-1)
1063 return;
1064 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001065 while (cursor < len-1
1066 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1067 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001068 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001069 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001070 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001071 while (cursor < len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001072 input_forward();
1073 }
1074}
1075
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001076static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001077vi_Back_motion(char *command)
1078{
1079 while (cursor > 0 && isspace(command[cursor-1]))
1080 input_backward(1);
1081 while (cursor > 0 && !isspace(command[cursor-1]))
1082 input_backward(1);
1083}
1084
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001085static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001086vi_back_motion(char *command)
1087{
1088 if (cursor <= 0)
1089 return;
1090 input_backward(1);
1091 while (cursor > 0 && isspace(command[cursor]))
1092 input_backward(1);
1093 if (cursor <= 0)
1094 return;
1095 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001096 while (cursor > 0
1097 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1098 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001099 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001100 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001101 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001102 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001103 input_backward(1);
1104 }
1105}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001106#else
1107enum { vi_mode = 0 };
1108#endif
1109
1110
1111/*
1112 * cmdedit_read_input and its helpers
1113 */
1114
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001115#if !ENABLE_FEATURE_SH_FANCY_PROMPT
1116static void parse_prompt(const char *prmt_ptr)
1117{
1118 cmdedit_prompt = prmt_ptr;
1119 cmdedit_prmt_len = strlen(prmt_ptr);
1120 put_prompt();
1121}
1122#else
1123static void parse_prompt(const char *prmt_ptr)
1124{
1125 int prmt_len = 0;
1126 size_t cur_prmt_len = 0;
1127 char flg_not_length = '[';
1128 char *prmt_mem_ptr = xzalloc(1);
1129 char *pwd_buf = xgetcwd(0);
1130 char buf2[PATH_MAX + 1];
1131 char buf[2];
1132 char c;
1133 char *pbuf;
1134
1135 if (!pwd_buf) {
1136 pwd_buf = (char *)bb_msg_unknown;
1137 }
1138
1139 while (*prmt_ptr) {
1140 pbuf = buf;
1141 pbuf[1] = 0;
1142 c = *prmt_ptr++;
1143 if (c == '\\') {
1144 const char *cp = prmt_ptr;
1145 int l;
1146
1147 c = bb_process_escape_sequence(&prmt_ptr);
1148 if (prmt_ptr == cp) {
1149 if (*cp == 0)
1150 break;
1151 c = *prmt_ptr++;
1152 switch (c) {
1153#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1154 case 'u':
1155 pbuf = user_buf;
1156 break;
1157#endif
1158 case 'h':
1159 pbuf = hostname_buf;
1160 if (!pbuf) {
1161 pbuf = xzalloc(256);
1162 if (gethostname(pbuf, 255) < 0) {
1163 strcpy(pbuf, "?");
1164 } else {
1165 char *s = strchr(pbuf, '.');
1166 if (s)
1167 *s = '\0';
1168 }
1169 hostname_buf = pbuf;
1170 }
1171 break;
1172 case '$':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001173 c = (geteuid() == 0 ? '#' : '$');
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001174 break;
1175#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1176 case 'w':
1177 pbuf = pwd_buf;
1178 l = strlen(home_pwd_buf);
1179 if (home_pwd_buf[0] != 0
1180 && strncmp(home_pwd_buf, pbuf, l) == 0
1181 && (pbuf[l]=='/' || pbuf[l]=='\0')
1182 && strlen(pwd_buf+l)<PATH_MAX
1183 ) {
1184 pbuf = buf2;
1185 *pbuf = '~';
1186 strcpy(pbuf+1, pwd_buf+l);
1187 }
1188 break;
1189#endif
1190 case 'W':
1191 pbuf = pwd_buf;
1192 cp = strrchr(pbuf,'/');
1193 if (cp != NULL && cp != pbuf)
1194 pbuf += (cp-pbuf) + 1;
1195 break;
1196 case '!':
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001197 pbuf = buf2;
1198 snprintf(buf2, sizeof(buf2), "%d", num_ok_lines);
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001199 break;
1200 case 'e': case 'E': /* \e \E = \033 */
1201 c = '\033';
1202 break;
1203 case 'x': case 'X':
1204 for (l = 0; l < 3;) {
1205 int h;
1206 buf2[l++] = *prmt_ptr;
1207 buf2[l] = 0;
1208 h = strtol(buf2, &pbuf, 16);
1209 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
1210 l--;
1211 break;
1212 }
1213 prmt_ptr++;
1214 }
1215 buf2[l] = 0;
1216 c = (char)strtol(buf2, NULL, 16);
1217 if (c == 0)
1218 c = '?';
1219 pbuf = buf;
1220 break;
1221 case '[': case ']':
1222 if (c == flg_not_length) {
1223 flg_not_length = flg_not_length == '[' ? ']' : '[';
1224 continue;
1225 }
1226 break;
1227 }
1228 }
1229 }
1230 if (pbuf == buf)
1231 *pbuf = c;
1232 cur_prmt_len = strlen(pbuf);
1233 prmt_len += cur_prmt_len;
1234 if (flg_not_length != ']')
1235 cmdedit_prmt_len += cur_prmt_len;
1236 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
1237 }
1238 if (pwd_buf!=(char *)bb_msg_unknown)
1239 free(pwd_buf);
1240 cmdedit_prompt = prmt_mem_ptr;
1241 put_prompt();
1242}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001243#endif
1244
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001245#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
1246#define getTermSettings(fd, argp) tcgetattr(fd, argp);
1247
1248static sighandler_t previous_SIGWINCH_handler;
1249
1250static void cmdedit_reset_term(void)
1251{
1252 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
1253 /* restore SIGWINCH handler */
1254 signal(SIGWINCH, previous_SIGWINCH_handler);
1255 fflush(stdout);
1256}
1257
1258static void cmdedit_setwidth(unsigned w, int redraw_flg)
1259{
1260 cmdedit_termw = w;
1261 if (redraw_flg) {
1262 /* new y for current cursor */
1263 int new_y = (cursor + cmdedit_prmt_len) / w;
1264 /* redraw */
1265 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
1266 fflush(stdout);
1267 }
1268}
1269
1270static void win_changed(int nsig)
1271{
1272 int width;
1273 get_terminal_width_height(0, &width, NULL);
1274 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1275 if (nsig == SIGWINCH)
1276 signal(SIGWINCH, win_changed); /* rearm ourself */
1277}
1278
1279static void cmdedit_init(void)
1280{
1281 cmdedit_prmt_len = 0;
1282 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1283 win_changed(0); /* do initial resizing */
1284
1285#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1286 {
1287 struct passwd *entry;
1288
1289 entry = getpwuid(geteuid());
1290 if (entry) {
1291 user_buf = xstrdup(entry->pw_name);
1292 home_pwd_buf = xstrdup(entry->pw_dir);
1293 }
1294 }
1295#endif
1296
1297#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
1298 my_uid = getuid();
1299 my_gid = getgid();
1300#endif
1301// Crap. We should be able to do it without atexit.
1302 atexit(cmdedit_reset_term); /* be sure to do this only once */
1303}
1304
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001305/*
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001306 * The emacs and vi modes share much of the code in the big
1307 * command loop. Commands entered when in vi's command mode (aka
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001308 * "escape mode") get an extra bit added to distinguish them --
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001309 * this keeps them from being self-inserted. This clutters the
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001310 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001311 */
1312
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001313#define vbit 0x100
1314
1315/* leave out the "vi-mode"-only case labels if vi editing isn't
1316 * configured. */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001317#define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(case caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001318
1319/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001320#undef CTRL
1321#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001322
Eric Andersen044228d2001-07-17 01:12:36 +00001323
1324int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001325{
Erik Andersenc7c634b2000-03-19 05:28:55 +00001326 int lastWasTab = FALSE;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001327 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001328 unsigned char c;
1329 smallint break_out = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001330#if ENABLE_FEATURE_COMMAND_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001331 smallint vi_cmdmode = 0;
1332 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001333#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001334 /* prepare before init handlers */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001335 cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001336 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001337 command_ps = command;
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001338 command[0] = '\0';
Mark Whitley4e338752001-01-26 20:42:23 +00001339
Eric Andersen34506362001-08-02 05:02:46 +00001340 getTermSettings(0, (void *) &initial_settings);
1341 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1342 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1343 /* Turn off echoing and CTRL-C, so we can trap it */
1344 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001345 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1346 new_settings.c_cc[VMIN] = 1;
1347 new_settings.c_cc[VTIME] = 0;
1348 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001349#ifndef _POSIX_VDISABLE
1350#define _POSIX_VDISABLE '\0'
1351#endif
Eric Andersenc470f442003-07-28 09:56:35 +00001352 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001353 setTermSettings(0, (void *) &new_settings);
Erik Andersen13456d12000-03-16 08:09:57 +00001354
Eric Andersen6faae7d2001-02-16 20:09:17 +00001355 /* Now initialize things */
1356 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001357 /* Print out the command prompt */
1358 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001359
Erik Andersenc7c634b2000-03-19 05:28:55 +00001360 while (1) {
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001361 fflush(stdout);
Mark Whitley4e338752001-01-26 20:42:23 +00001362
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001363 if (safe_read(0, &c, 1) < 1) {
Eric Andersened424db2001-04-23 15:28:28 +00001364 /* if we can't read input then exit */
1365 goto prepare_to_die;
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001366 }
Erik Andersenf3b3d172000-04-09 18:24:05 +00001367
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001368 ic = c;
1369
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001370#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001371 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001372 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001373 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001374#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001375 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001376 case '\n':
1377 case '\r':
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001378 vi_case('\n'|vbit:)
1379 vi_case('\r'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001380 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001381 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001382 break_out = 1;
1383 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001384 case CTRL('A'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001385 vi_case('0'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001386 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001387 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001388 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001389 case CTRL('B'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001390 vi_case('h'|vbit:)
1391 vi_case('\b'|vbit:)
1392 vi_case('\x7f'|vbit:) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001393 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001394 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001395 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001396 case CTRL('C'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001397 vi_case(CTRL('C')|vbit:)
Eric Andersen86349772000-12-18 20:25:50 +00001398 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001399 goto_new_line();
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001400#if !ENABLE_ASH
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001401 command[0] = '\0';
Eric Andersen7467c8d2001-07-12 20:26:32 +00001402 len = 0;
1403 lastWasTab = FALSE;
1404 put_prompt();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001405#else
1406 len = 0;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001407 break_out = -1; /* to control traps */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001408#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001409 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001410 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001411 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001412 * if the len=0 and no chars to delete */
1413 if (len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001414 errno = 0;
1415 prepare_to_die:
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001416// So, our API depends on whether we have ash compiled in or not? Crap...
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001417#if !ENABLE_ASH
Mark Whitley4e338752001-01-26 20:42:23 +00001418 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001419 goto_new_line();
1420 /* cmdedit_reset_term() called in atexit */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001421// FIXME. this is definitely not good
Eric Andersen7467c8d2001-07-12 20:26:32 +00001422 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001423#else
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001424 /* to control stopped jobs */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001425 break_out = len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001426 break;
1427#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001428 } else {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001429 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001430 }
1431 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001432 case CTRL('E'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001433 vi_case('$'|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001434 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001435 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001436 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001437 case CTRL('F'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001438 vi_case('l'|vbit:)
1439 vi_case(' '|vbit:)
Erik Andersenc7c634b2000-03-19 05:28:55 +00001440 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001441 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001442 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001443 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001444 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001445 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001446 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001447 break;
1448 case '\t':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001449#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001450 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001451#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001452 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001453 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001454 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001455 command[cursor] = 0;
Eric Andersen65a07302002-04-13 13:26:49 +00001456 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001457 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001458 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001459 case CTRL('L'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001460 vi_case(CTRL('L')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001461 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001462 printf("\033[H");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001463 redraw(0, len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001464 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001465#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001466 case CTRL('N'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001467 vi_case(CTRL('N')|vbit:)
1468 vi_case('j'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001469 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001470 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001471 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001472 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001473 case CTRL('P'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001474 vi_case(CTRL('P')|vbit:)
1475 vi_case('k'|vbit:)
Erik Andersenf0657d32000-04-12 17:49:52 +00001476 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001477 if (cur_history > 0) {
1478 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001479 goto rewrite_line;
1480 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001481 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001482 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001483 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001484#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001485 case CTRL('U'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001486 vi_case(CTRL('U')|vbit:)
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001487 /* Control-U -- Clear line before cursor */
1488 if (cursor) {
1489 strcpy(command, command + cursor);
1490 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001491 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001492 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001493 case CTRL('W'):
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001494 vi_case(CTRL('W')|vbit:)
Eric Andersen27bb7902003-12-23 20:24:51 +00001495 /* Control-W -- Remove the last word */
1496 while (cursor > 0 && isspace(command[cursor-1]))
1497 input_backspace();
1498 while (cursor > 0 &&!isspace(command[cursor-1]))
1499 input_backspace();
1500 break;
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001501#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001502 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001503 vi_cmdmode = 0;
1504 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001505 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001506 input_backward(cursor);
1507 vi_cmdmode = 0;
1508 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001509 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001510 input_forward();
1511 vi_cmdmode = 0;
1512 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001513 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001514 input_end();
1515 vi_cmdmode = 0;
1516 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001517 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001518 input_delete(1);
1519 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001520 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001521 if (cursor > 0) {
1522 input_backward(1);
1523 input_delete(1);
1524 }
1525 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001526 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001527 vi_Word_motion(command, 1);
1528 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001529 case 'w'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001530 vi_word_motion(command, 1);
1531 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001532 case 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001533 vi_End_motion(command);
1534 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001535 case 'e'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001536 vi_end_motion(command);
1537 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001538 case 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001539 vi_Back_motion(command);
1540 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001541 case 'b'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001542 vi_back_motion(command);
1543 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001544 case 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001545 vi_cmdmode = 0;
1546 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001547 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001548 goto clear_to_eol;
1549
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001550 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001551 vi_cmdmode = 0;
1552 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001553 case 'd'|vbit: {
1554 int nc, sc;
1555 sc = cursor;
1556 prevc = ic;
1557 if (safe_read(0, &c, 1) < 1)
1558 goto prepare_to_die;
1559 if (c == (prevc & 0xff)) {
1560 /* "cc", "dd" */
1561 input_backward(cursor);
1562 goto clear_to_eol;
1563 break;
1564 }
1565 switch (c) {
1566 case 'w':
1567 case 'W':
1568 case 'e':
1569 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001570 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001571 case 'w': /* "dw", "cw" */
1572 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001573 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001574 case 'W': /* 'dW', 'cW' */
1575 vi_Word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001576 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001577 case 'e': /* 'de', 'ce' */
1578 vi_end_motion(command);
1579 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001580 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001581 case 'E': /* 'dE', 'cE' */
1582 vi_End_motion(command);
1583 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001584 break;
1585 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001586 nc = cursor;
1587 input_backward(cursor - sc);
1588 while (nc-- > cursor)
1589 input_delete(1);
1590 break;
1591 case 'b': /* "db", "cb" */
1592 case 'B': /* implemented as B */
1593 if (c == 'b')
1594 vi_back_motion(command);
1595 else
1596 vi_Back_motion(command);
1597 while (sc-- > cursor)
1598 input_delete(1);
1599 break;
1600 case ' ': /* "d ", "c " */
1601 input_delete(1);
1602 break;
1603 case '$': /* "d$", "c$" */
1604 clear_to_eol:
1605 while (cursor < len)
1606 input_delete(1);
1607 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001608 }
1609 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001610 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001611 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001612 input_forward();
1613 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001614 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001615 put();
1616 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001617 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001618 if (safe_read(0, &c, 1) < 1)
1619 goto prepare_to_die;
1620 if (c == 0)
1621 beep();
1622 else {
1623 *(command + cursor) = c;
1624 putchar(c);
1625 putchar('\b');
1626 }
1627 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001628#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001629
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001630 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001631
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001632#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001633 if (vi_mode) {
1634 /* ESC: insert mode --> command mode */
1635 vi_cmdmode = 1;
1636 input_backward(1);
1637 break;
1638 }
1639#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001640 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001641 if (safe_read(0, &c, 1) < 1)
1642 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001643 /* different vt100 emulations */
1644 if (c == '[' || c == 'O') {
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001645 vi_case('['|vbit:)
1646 vi_case('O'|vbit:)
Eric Andersen7467c8d2001-07-12 20:26:32 +00001647 if (safe_read(0, &c, 1) < 1)
1648 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001649 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001650 if (c >= '1' && c <= '9') {
1651 unsigned char dummy;
1652
1653 if (safe_read(0, &dummy, 1) < 1)
1654 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001655 if (dummy != '~')
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001656 c = '\0';
Glenn L McGrath475820c2004-01-22 12:42:23 +00001657 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001658 switch (c) {
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001659#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001660 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001661 input_tab(&lastWasTab);
1662 break;
1663#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001664#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001665 case 'A':
1666 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001667 if (cur_history > 0) {
1668 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001669 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001670 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001671 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001672 break;
1673 case 'B':
1674 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001675 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001676 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001677 rewrite_line:
Denis Vlasenko47bdb3a2007-01-21 19:18:59 +00001678 /* Rewrite the line with the selected history item */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001679 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001680 len = strlen(strcpy(command, history[cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001681 /* redraw and go to eol (bol, in vi */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001682 redraw(cmdedit_y, vi_mode ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001683 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001684#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001685 case 'C':
1686 /* Right Arrow -- Move forward one character */
1687 input_forward();
1688 break;
1689 case 'D':
1690 /* Left Arrow -- Move back one character */
1691 input_backward(1);
1692 break;
1693 case '3':
1694 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001695 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001696 break;
1697 case '1':
1698 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001699 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001700 input_backward(cursor);
1701 break;
1702 case '4':
1703 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001704 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001705 input_end();
1706 break;
1707 default:
Glenn L McGrath475820c2004-01-22 12:42:23 +00001708 c = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001709 beep();
1710 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001711 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001712
Eric Andersenc470f442003-07-28 09:56:35 +00001713 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001714#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001715 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001716 if (c == CTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001717 if (safe_read(0, &c, 1) < 1)
1718 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001719 if (c == 0) {
1720 beep();
1721 break;
1722 }
1723 } else
1724#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001725 {
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001726#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001727 if (vi_cmdmode) /* don't self-insert */
1728 break;
1729#endif
1730 if (!Isprint(c)) /* Skip non-printable characters */
1731 break;
1732 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001733
Eric Andersenc470f442003-07-28 09:56:35 +00001734 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001735 break;
1736
1737 len++;
1738
Eric Andersenc470f442003-07-28 09:56:35 +00001739 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001740 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001741 *(command + cursor + 1) = 0;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001742 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001743 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001744 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001745
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001746 memmove(command + sc + 1, command + sc, len - sc);
1747 *(command + sc) = c;
1748 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001749 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001750 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001751 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001752 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001753 }
1754
Erik Andersenc7c634b2000-03-19 05:28:55 +00001755 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001756 }
Eric Andersenc470f442003-07-28 09:56:35 +00001757 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001758 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001759
1760 if (c != '\t')
1761 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001762 }
1763
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001764#if MAX_HISTORY > 0
Erik Andersenc7c634b2000-03-19 05:28:55 +00001765 /* Handle command history log */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001766 /* cleanup may be saved current command line */
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001767 if (len > 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001768 int i = n_history;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001769
1770 free(history[MAX_HISTORY]);
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001771 history[MAX_HISTORY] = NULL;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001772 /* After max history, remove the oldest command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001773 if (i >= MAX_HISTORY) {
1774 free(history[0]);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001775 for (i = 0; i < MAX_HISTORY-1; i++)
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001776 history[i] = history[i+1];
Erik Andersen13456d12000-03-16 08:09:57 +00001777 }
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001778// Maybe "if (!i || strcmp(history[i-1], command) != 0) ..."
1779// (i.e. do not save dups?)
Rob Landleyd921b2e2006-08-03 15:41:12 +00001780 history[i++] = xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001781 cur_history = i;
1782 n_history = i;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001783 USE_FEATURE_SH_FANCY_PROMPT(num_ok_lines++;)
Erik Andersen6273f652000-03-17 01:12:41 +00001784 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001785#else /* MAX_HISTORY == 0 */
1786 /* dont put empty line */
1787 USE_FEATURE_SH_FANCY_PROMPT(if (len > 0) num_ok_lines++;)
1788#endif /* MAX_HISTORY */
1789
Eric Andersen27bb7902003-12-23 20:24:51 +00001790 if (break_out > 0) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001791 command[len++] = '\n';
1792 command[len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001793 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001794
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001795#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001796 free_tab_completion_data();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001797#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001798
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001799#if ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001800 free(cmdedit_prompt);
1801#endif
Denis Vlasenko5592fac2007-01-21 19:19:46 +00001802 /* restore initial_settings and SIGWINCH handler */
Eric Andersen501c88b2000-07-28 15:14:45 +00001803 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001804 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001805}
1806
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001807#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001808
1809
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001810/*
1811 * Testing
1812 */
1813
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001814#ifdef TEST
1815
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001816#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001817
1818const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001819
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001820int main(int argc, char **argv)
1821{
1822 char buff[BUFSIZ];
1823 char *prompt =
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001824#if ENABLE_FEATURE_SH_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001825 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1826 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1827 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001828#else
1829 "% ";
1830#endif
1831
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001832#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001833 setlocale(LC_ALL, "");
1834#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001835 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001836 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001837 l = cmdedit_read_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001838 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001839 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001840 buff[l-1] = 0;
1841 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001842 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001843 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001844 return 0;
1845}
1846
Eric Andersenc470f442003-07-28 09:56:35 +00001847#endif /* TEST */