blob: 617f3a29a17690edc3bfeeffdbe04799c59a4536 [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
Eric Andersencbe31da2001-02-20 06:14:08 +000031#include <sys/ioctl.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +000032#include "busybox.h"
"Robert P. J. Day"4eddb422006-07-03 00:46:47 +000033#include "cmdedit.h"
Glenn L McGrath67285962004-01-14 09:34:51 +000034
Glenn L McGrath475820c2004-01-22 12:42:23 +000035
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000036/* FIXME: obsolete CONFIG item? */
37#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
38
39
Glenn L McGrath3b251852004-01-03 12:07:32 +000040#ifdef TEST
Eric Andersen5f2c79d2001-02-16 18:36:04 +000041
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000042#define ENABLE_FEATURE_COMMAND_EDITING 0
43#define ENABLE_FEATURE_COMMAND_TAB_COMPLETION 0
44#define ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION 0
45#define ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT 0
46#define ENABLE_FEATURE_CLEAN_UP 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +000047
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +000048#endif /* TEST */
Eric Andersen5f2c79d2001-02-16 18:36:04 +000049
Eric Andersen5165fbe2001-02-20 06:42:29 +000050
Denis Vlasenko82b39e82007-01-21 19:18:19 +000051/* Entire file (except TESTing part) sits inside this #if */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000052#if ENABLE_FEATURE_COMMAND_EDITING
Erik Andersen13456d12000-03-16 08:09:57 +000053
Denis Vlasenko82b39e82007-01-21 19:18:19 +000054
55#if ENABLE_LOCALE_SUPPORT
56#define Isprint(c) isprint(c)
57#else
58#define Isprint(c) ((c) >= ' ' && (c) != ((unsigned char)'\233'))
59#endif
60
Denis Vlasenko7e46cf72006-12-23 01:21:55 +000061#define ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR \
62(ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION || ENABLE_FEATURE_SH_FANCY_PROMPT)
Eric Andersen5f2c79d2001-02-16 18:36:04 +000063
Denis Vlasenko82b39e82007-01-21 19:18:19 +000064/* Maximum length of command line history */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000065#if !ENABLE_FEATURE_COMMAND_HISTORY
Robert Griebl350d26b2002-12-03 22:45:46 +000066#define MAX_HISTORY 15
67#else
Denis Vlasenko9d4533e2006-11-02 22:09:37 +000068#define MAX_HISTORY (CONFIG_FEATURE_COMMAND_HISTORY + 0)
Robert Griebl350d26b2002-12-03 22:45:46 +000069#endif
70
Erik Andersen1d1d9502000-04-21 01:26:49 +000071
Denis Vlasenko82b39e82007-01-21 19:18:19 +000072/* Current termios and the previous termios before starting sh */
Eric Andersen63a86222000-11-07 06:52:13 +000073static struct termios initial_settings, new_settings;
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000074
Mark Whitley4e338752001-01-26 20:42:23 +000075static
Denis Vlasenko82b39e82007-01-21 19:18:19 +000076volatile unsigned cmdedit_termw = 80; /* actual terminal width */
Mark Whitley4e338752001-01-26 20:42:23 +000077static
Denis Vlasenko82b39e82007-01-21 19:18:19 +000078volatile int handlers_sets = 0; /* Set next bits: */
Mark Whitley4e338752001-01-26 20:42:23 +000079enum {
Eric Andersenc470f442003-07-28 09:56:35 +000080 SET_ATEXIT = 1, /* when atexit() has been called
Eric Andersenb3d6e2d2001-03-13 22:57:56 +000081 and get euid,uid,gid to fast compare */
Eric Andersen7467c8d2001-07-12 20:26:32 +000082 SET_WCHG_HANDLERS = 2, /* winchg signal handler */
83 SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
Mark Whitley4e338752001-01-26 20:42:23 +000084};
Erik Andersen13456d12000-03-16 08:09:57 +000085
Mark Whitley4e338752001-01-26 20:42:23 +000086
Eric Andersenc470f442003-07-28 09:56:35 +000087static int cmdedit_x; /* real x terminal position */
88static int cmdedit_y; /* pseudoreal y terminal position */
Denis Vlasenko82b39e82007-01-21 19:18:19 +000089static int cmdedit_prmt_len; /* length of prompt without colores string */
Eric Andersen86349772000-12-18 20:25:50 +000090
Denis Vlasenko82b39e82007-01-21 19:18:19 +000091static int cursor;
92static int len;
93static char *command_ps;
94static SKIP_FEATURE_SH_FANCY_PROMPT(const) char *cmdedit_prompt;
Eric Andersen5f2c79d2001-02-16 18:36:04 +000095
Denis Vlasenko7f1dc212006-12-19 01:10:25 +000096#if ENABLE_FEATURE_SH_FANCY_PROMPT
Glenn L McGrath062c74f2002-11-27 09:29:49 +000097static char *hostname_buf;
Eric Andersen5f2c79d2001-02-16 18:36:04 +000098static int num_ok_lines = 1;
99#endif
100
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000101#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
102static char *user_buf = "";
103static char *home_pwd_buf = "";
104#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000105
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000106#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR || ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000107static int my_euid;
108#endif
109
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000110#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000111static int my_uid;
112static int my_gid;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000113#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000114
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000115/* Put 'command_ps[cursor]', cursor++.
116 * Advance cursor on screen. If we reached right margin, scroll text up
117 * and remove terminal margin effect by printing 'next_char' */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000118static void cmdedit_set_out_char(int next_char)
119{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000120 int c = (unsigned char)command_ps[cursor];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000121
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000122 if (c == '\0') {
123 /* erase character after end of input string */
124 c = ' ';
125 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000126#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000127 /* Display non-printable characters in reverse */
128 if (!Isprint(c)) {
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000129 if (c >= 128)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000130 c -= 128;
Eric Andersenf9ff8a72001-03-15 20:51:09 +0000131 if (c < ' ')
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000132 c += '@';
133 if (c == 127)
134 c = '?';
135 printf("\033[7m%c\033[0m", c);
136 } else
137#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000138 {
139 if (initial_settings.c_lflag & ECHO)
140 putchar(c);
141 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000142 if (++cmdedit_x >= cmdedit_termw) {
Mark Whitley4e338752001-01-26 20:42:23 +0000143 /* terminal is scrolled down */
144 cmdedit_y++;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000145 cmdedit_x = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000146 /* destroy "(auto)margin" */
147 putchar(next_char);
148 putchar('\b');
149 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000150// Huh? What if command_ps[cursor] == '\0' (we are at the end already?)
Mark Whitley4e338752001-01-26 20:42:23 +0000151 cursor++;
Erik Andersen13456d12000-03-16 08:09:57 +0000152}
153
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000154/* Move to end of line (by printing all chars till the end) */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000155static void input_end(void)
156{
157 while (cursor < len)
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000158 cmdedit_set_out_char(' ');
Mark Whitley4e338752001-01-26 20:42:23 +0000159}
160
161/* Go to the next line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000162static void goto_new_line(void)
163{
Mark Whitley4e338752001-01-26 20:42:23 +0000164 input_end();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000165 if (cmdedit_x)
166 putchar('\n');
Mark Whitley4e338752001-01-26 20:42:23 +0000167}
168
169
Rob Landley88621d72006-08-29 19:41:06 +0000170static void out1str(const char *s)
Erik Andersenf0657d32000-04-12 17:49:52 +0000171{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000172 if (s)
Robert Grieblb2301592002-07-30 23:13:51 +0000173 fputs(s, stdout);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000174}
Eric Andersen81fe1232003-07-29 06:38:40 +0000175
Rob Landley88621d72006-08-29 19:41:06 +0000176static void beep(void)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000177{
178 putchar('\007');
Erik Andersen13456d12000-03-16 08:09:57 +0000179}
180
Eric Andersenaff114c2004-04-14 17:51:38 +0000181/* Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000182/* special for slow terminal */
183static void input_backward(int num)
184{
185 if (num > cursor)
186 num = cursor;
Eric Andersenc470f442003-07-28 09:56:35 +0000187 cursor -= num; /* new cursor (in command, not terminal) */
Erik Andersen13456d12000-03-16 08:09:57 +0000188
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000189 if (cmdedit_x >= num) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000190 cmdedit_x -= num;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000191 if (num <= 4)
192 while (num > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000193 putchar('\b');
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000194 num--;
195 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000196 else
197 printf("\033[%dD", num);
198 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000199 /* Need to go one or more lines up */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000200 int count_y;
201
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000202 //if (cmdedit_x) {
203 // /* back to first column */
204 // putchar('\r');
205 // num -= cmdedit_x;
206 //}
207 num -= cmdedit_x;//
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000208 count_y = 1 + num / cmdedit_termw;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000209 //printf("\033[%dA", count_y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000210 cmdedit_y -= count_y;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000211 cmdedit_x = cmdedit_termw * count_y - num;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000212 //printf("\033[%dC", cmdedit_x);
213 /* go to 1st col; go up; go to correct column */
214 printf("\r" "\033[%dA" "\033[%dC", count_y, cmdedit_x);//
Erik Andersen13456d12000-03-16 08:09:57 +0000215 }
Erik Andersen13456d12000-03-16 08:09:57 +0000216}
217
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000218static void put_prompt(void)
219{
220 out1str(cmdedit_prompt);
Eric Andersenc470f442003-07-28 09:56:35 +0000221 cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000222 cursor = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +0000223 cmdedit_y = 0; /* new quasireal y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000224}
225
Eric Andersenaff114c2004-04-14 17:51:38 +0000226/* draw prompt, editor line, and clear tail */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000227static void redraw(int y, int back_cursor)
228{
Eric Andersenc470f442003-07-28 09:56:35 +0000229 if (y > 0) /* up to start y */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000230 printf("\033[%dA", y);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000231 putchar('\r');
232 put_prompt();
Eric Andersenc470f442003-07-28 09:56:35 +0000233 input_end(); /* rewrite */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000234 printf("\033[J"); /* erase after cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000235 input_backward(back_cursor);
236}
237
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000238#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000239#define DELBUFSIZ 128
240static char *delbuf; /* a (malloced) place to store deleted characters */
241static char *delp;
242static char newdelflag; /* whether delbuf should be reused yet */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000243#endif
244
245/* Delete the char in front of the cursor, optionally saving it
246 * for later putback */
247static void input_delete(int save)
Erik Andersenf0657d32000-04-12 17:49:52 +0000248{
Mark Whitley4e338752001-01-26 20:42:23 +0000249 int j = cursor;
Erik Andersena2685732000-04-09 18:27:46 +0000250
Mark Whitley4e338752001-01-26 20:42:23 +0000251 if (j == len)
Erik Andersenf0657d32000-04-12 17:49:52 +0000252 return;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000253
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000254#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000255 if (save) {
256 if (newdelflag) {
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000257 if (!delbuf)
258 delbuf = malloc(DELBUFSIZ);
259 /* safe if malloc fails */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000260 delp = delbuf;
261 newdelflag = 0;
262 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +0000263 if (delbuf && (delp - delbuf < DELBUFSIZ))
Paul Fox3f11b1b2005-08-04 19:04:46 +0000264 *delp++ = command_ps[j];
265 }
266#endif
267
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000268 strcpy(command_ps + j, command_ps + j + 1);
Mark Whitley4e338752001-01-26 20:42:23 +0000269 len--;
Paul Fox3f11b1b2005-08-04 19:04:46 +0000270 input_end(); /* rewrite new line */
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000271 cmdedit_set_out_char(' '); /* erase char */
Eric Andersenc470f442003-07-28 09:56:35 +0000272 input_backward(cursor - j); /* back to old pos cursor */
Erik Andersenf0657d32000-04-12 17:49:52 +0000273}
274
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000275#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000276static void put(void)
277{
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000278 int ocursor;
279 int j = delp - delbuf;
280
Paul Fox3f11b1b2005-08-04 19:04:46 +0000281 if (j == 0)
282 return;
283 ocursor = cursor;
284 /* open hole and then fill it */
285 memmove(command_ps + cursor + j, command_ps + cursor, len - cursor + 1);
286 strncpy(command_ps + cursor, delbuf, j);
287 len += j;
288 input_end(); /* rewrite new line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000289 input_backward(cursor - ocursor - j + 1); /* at end of new text */
Paul Fox3f11b1b2005-08-04 19:04:46 +0000290}
291#endif
292
Mark Whitley4e338752001-01-26 20:42:23 +0000293/* Delete the char in back of the cursor */
294static void input_backspace(void)
295{
296 if (cursor > 0) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000297 input_backward(1);
Paul Fox3f11b1b2005-08-04 19:04:46 +0000298 input_delete(0);
Mark Whitley4e338752001-01-26 20:42:23 +0000299 }
300}
301
302
Eric Andersenaff114c2004-04-14 17:51:38 +0000303/* Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +0000304static void input_forward(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000305{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000306 if (cursor < len)
307 cmdedit_set_out_char(command_ps[cursor + 1]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000308}
309
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000310#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Mark Whitley4e338752001-01-26 20:42:23 +0000311
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000312static char **matches;
313static int num_matches;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000314
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000315static void add_match(char *matched)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000316{
317 int nm = num_matches;
318 int nm1 = nm + 1;
319
320 matches = xrealloc(matches, nm1 * sizeof(char *));
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000321 matches[nm] = matched;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000322 num_matches++;
323}
324
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000325#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000326
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000327static void username_tab_completion(char *ud, char *with_shash_flg)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000328{
329 struct passwd *entry;
330 int userlen;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000331
Eric Andersenc470f442003-07-28 09:56:35 +0000332 ud++; /* ~user/... to user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000333 userlen = strlen(ud);
334
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000335 if (with_shash_flg) { /* "~/..." or "~user/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000336 char *sav_ud = ud - 1;
337 char *home = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000338 char *temp;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000339
Eric Andersenc470f442003-07-28 09:56:35 +0000340 if (*ud == '/') { /* "~/..." */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000341 home = home_pwd_buf;
342 } else {
343 /* "~user/..." */
344 temp = strchr(ud, '/');
Eric Andersenc470f442003-07-28 09:56:35 +0000345 *temp = 0; /* ~user\0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000346 entry = getpwnam(ud);
Eric Andersenc470f442003-07-28 09:56:35 +0000347 *temp = '/'; /* restore ~user/... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000348 ud = temp;
349 if (entry)
350 home = entry->pw_dir;
351 }
352 if (home) {
353 if ((userlen + strlen(home) + 1) < BUFSIZ) {
Eric Andersenc470f442003-07-28 09:56:35 +0000354 char temp2[BUFSIZ]; /* argument size */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000355
356 /* /home/user/... */
357 sprintf(temp2, "%s%s", home, ud);
358 strcpy(sav_ud, temp2);
359 }
360 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000361 } else {
362 /* "~[^/]*" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000363 setpwent();
364
365 while ((entry = getpwent()) != NULL) {
366 /* Null usernames should result in all users as possible completions. */
367 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000368 add_match(xasprintf("~%s/", entry->pw_name));
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000369 }
370 }
371
372 endpwent();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000373 }
374}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000375#endif /* FEATURE_COMMAND_USERNAME_COMPLETION */
Mark Whitley4e338752001-01-26 20:42:23 +0000376
377enum {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000378 FIND_EXE_ONLY = 0,
379 FIND_DIR_ONLY = 1,
Mark Whitley4e338752001-01-26 20:42:23 +0000380 FIND_FILE_ONLY = 2,
381};
Erik Andersen1dbe3402000-03-19 10:46:06 +0000382
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000383#if ENABLE_ASH
Glenn L McGrath67285962004-01-14 09:34:51 +0000384const char *cmdedit_path_lookup;
Glenn L McGrath67285962004-01-14 09:34:51 +0000385#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000386static int path_parse(char ***p, int flags)
387{
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000388 int npth;
Glenn L McGrath67285962004-01-14 09:34:51 +0000389 const char *tmp;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000390#if ENABLE_ASH
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000391 const char *pth = cmdedit_path_lookup;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000392#else
393 const char *pth = getenv("PATH")
394#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000395
Mark Whitley4e338752001-01-26 20:42:23 +0000396 /* if not setenv PATH variable, to search cur dir "." */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000397 if (flags != FIND_EXE_ONLY)
Mark Whitley4e338752001-01-26 20:42:23 +0000398 return 1;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000399 /* PATH=<empty> or PATH=:<empty> */
400 if (!pth || !pth[0] || LONE_CHAR(pth, ':'))
401 return 1;
Mark Whitley4e338752001-01-26 20:42:23 +0000402
403 tmp = pth;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000404 npth = 0;
Mark Whitley4e338752001-01-26 20:42:23 +0000405
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000406 while (1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000407 npth++; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000408 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000409 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000410 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000411 if (*++tmp == '\0')
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000412 break; /* :<empty> */
Mark Whitley4e338752001-01-26 20:42:23 +0000413 }
414
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000415 *p = xmalloc(npth * sizeof(char *));
Mark Whitley4e338752001-01-26 20:42:23 +0000416
417 tmp = pth;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000418 (*p)[0] = xstrdup(tmp);
Eric Andersenc470f442003-07-28 09:56:35 +0000419 npth = 1; /* count words is + 1 count ':' */
Mark Whitley4e338752001-01-26 20:42:23 +0000420
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000421 while (1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000422 tmp = strchr(tmp, ':');
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000423 if (!tmp)
Mark Whitley4e338752001-01-26 20:42:23 +0000424 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000425 (*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
426 if (*++tmp == 0)
427 break; /* :<empty> */
Eric Andersenc470f442003-07-28 09:56:35 +0000428 (*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
Mark Whitley4e338752001-01-26 20:42:23 +0000429 }
430
431 return npth;
432}
433
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000434static void exe_n_cwd_tab_completion(char *command, int type)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000435{
Erik Andersen1dbe3402000-03-19 10:46:06 +0000436 DIR *dir;
437 struct dirent *next;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000438 char dirbuf[BUFSIZ];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000439 struct stat st;
440 char *path1[1];
441 char **paths = path1;
442 int npaths;
443 int i;
Eric Andersene5dfced2001-04-09 22:48:12 +0000444 char *found;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000445 char *pfind = strrchr(command, '/');
Mark Whitley4e338752001-01-26 20:42:23 +0000446
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000447 npaths = 1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000448 path1[0] = ".";
Mark Whitley4e338752001-01-26 20:42:23 +0000449
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000450 if (pfind == NULL) {
Mark Whitley4e338752001-01-26 20:42:23 +0000451 /* no dir, if flags==EXE_ONLY - get paths, else "." */
452 npaths = path_parse(&paths, type);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000453 pfind = command;
Mark Whitley4e338752001-01-26 20:42:23 +0000454 } else {
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000455 /* dirbuf = ".../.../.../" */
456 safe_strncpy(dirbuf, command, (pfind - command) + 2);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000457#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +0000458 if (dirbuf[0] == '~') /* ~/... or ~user/... */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000459 username_tab_completion(dirbuf, dirbuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000460#endif
Mark Whitley4e338752001-01-26 20:42:23 +0000461 paths[0] = dirbuf;
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000462 /* point to 'l' in "..../last_component" */
463 pfind++;
Mark Whitley4e338752001-01-26 20:42:23 +0000464 }
Erik Andersenc7c634b2000-03-19 05:28:55 +0000465
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000466 for (i = 0; i < npaths; i++) {
Erik Andersenc7c634b2000-03-19 05:28:55 +0000467
Mark Whitley4e338752001-01-26 20:42:23 +0000468 dir = opendir(paths[i]);
Eric Andersenc470f442003-07-28 09:56:35 +0000469 if (!dir) /* Don't print an error */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000470 continue;
471
472 while ((next = readdir(dir)) != NULL) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000473 int len1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000474 char *str_found = next->d_name;
475
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000476 /* matched? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000477 if (strncmp(str_found, pfind, strlen(pfind)))
Mark Whitley4e338752001-01-26 20:42:23 +0000478 continue;
479 /* not see .name without .match */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000480 if (*str_found == '.' && *pfind == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000481 if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
Mark Whitley4e338752001-01-26 20:42:23 +0000482 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000483 str_found = ""; /* only "/" */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000484 }
Eric Andersene5dfced2001-04-09 22:48:12 +0000485 found = concat_path_file(paths[i], str_found);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000486 /* hmm, remover in progress? */
Eric Andersenc470f442003-07-28 09:56:35 +0000487 if (stat(found, &st) < 0)
Eric Andersene5dfced2001-04-09 22:48:12 +0000488 goto cont;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000489 /* find with dirs? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000490 if (paths[i] != dirbuf)
Eric Andersenc470f442003-07-28 09:56:35 +0000491 strcpy(found, next->d_name); /* only name */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000492
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000493 len1 = strlen(found);
494 found = xrealloc(found, len1 + 2);
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000495 found[len1] = '\0';
496 found[len1+1] = '\0';
497
Mark Whitley4e338752001-01-26 20:42:23 +0000498 if (S_ISDIR(st.st_mode)) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000499 /* name is directory */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000500 if (found[len1-1] != '/') {
501 found[len1] = '/';
502 }
Mark Whitley4e338752001-01-26 20:42:23 +0000503 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000504 /* not put found file if search only dirs for cd */
Eric Andersenc470f442003-07-28 09:56:35 +0000505 if (type == FIND_DIR_ONLY)
Eric Andersene5dfced2001-04-09 22:48:12 +0000506 goto cont;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000507 }
Mark Whitley4e338752001-01-26 20:42:23 +0000508 /* Add it to the list */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000509 add_match(found);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000510 continue;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000511 cont:
Eric Andersene5dfced2001-04-09 22:48:12 +0000512 free(found);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000513 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000514 closedir(dir);
Erik Andersen1dbe3402000-03-19 10:46:06 +0000515 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000516 if (paths != path1) {
Eric Andersenc470f442003-07-28 09:56:35 +0000517 free(paths[0]); /* allocated memory only in first member */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000518 free(paths);
519 }
Erik Andersen6273f652000-03-17 01:12:41 +0000520}
Erik Andersenf0657d32000-04-12 17:49:52 +0000521
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000522
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000523#define QUOT (UCHAR_MAX+1)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000524
525#define collapse_pos(is, in) { \
Paul Fox574fee42005-07-19 20:41:06 +0000526 memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
527 memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000528
529static int find_match(char *matchBuf, int *len_with_quotes)
530{
531 int i, j;
532 int command_mode;
533 int c, c2;
534 int int_buf[BUFSIZ + 1];
535 int pos_buf[BUFSIZ + 1];
536
537 /* set to integer dimension characters and own positions */
538 for (i = 0;; i++) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000539 int_buf[i] = (unsigned char)matchBuf[i];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000540 if (int_buf[i] == 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000541 pos_buf[i] = -1; /* indicator end line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000542 break;
543 } else
544 pos_buf[i] = i;
545 }
546
547 /* mask \+symbol and convert '\t' to ' ' */
548 for (i = j = 0; matchBuf[i]; i++, j++)
549 if (matchBuf[i] == '\\') {
550 collapse_pos(j, j + 1);
551 int_buf[j] |= QUOT;
552 i++;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000553#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenc470f442003-07-28 09:56:35 +0000554 if (matchBuf[i] == '\t') /* algorithm equivalent */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000555 int_buf[j] = ' ' | QUOT;
556#endif
557 }
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000558#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000559 else if (matchBuf[i] == '\t')
560 int_buf[j] = ' ';
561#endif
562
563 /* mask "symbols" or 'symbols' */
564 c2 = 0;
565 for (i = 0; int_buf[i]; i++) {
566 c = int_buf[i];
567 if (c == '\'' || c == '"') {
568 if (c2 == 0)
569 c2 = c;
570 else {
571 if (c == c2)
572 c2 = 0;
573 else
574 int_buf[i] |= QUOT;
575 }
576 } else if (c2 != 0 && c != '$')
577 int_buf[i] |= QUOT;
578 }
579
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000580 /* skip commands with arguments if line has commands delimiters */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000581 /* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
582 for (i = 0; int_buf[i]; i++) {
583 c = int_buf[i];
584 c2 = int_buf[i + 1];
585 j = i ? int_buf[i - 1] : -1;
586 command_mode = 0;
587 if (c == ';' || c == '&' || c == '|') {
588 command_mode = 1 + (c == c2);
589 if (c == '&') {
590 if (j == '>' || j == '<')
591 command_mode = 0;
592 } else if (c == '|' && j == '>')
593 command_mode = 0;
594 }
595 if (command_mode) {
596 collapse_pos(0, i + command_mode);
Eric Andersenc470f442003-07-28 09:56:35 +0000597 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000598 }
599 }
600 /* collapse `command...` */
601 for (i = 0; int_buf[i]; i++)
602 if (int_buf[i] == '`') {
603 for (j = i + 1; int_buf[j]; j++)
604 if (int_buf[j] == '`') {
605 collapse_pos(i, j + 1);
606 j = 0;
607 break;
608 }
609 if (j) {
610 /* not found close ` - command mode, collapse all previous */
611 collapse_pos(0, i + 1);
612 break;
613 } else
Eric Andersenc470f442003-07-28 09:56:35 +0000614 i--; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000615 }
616
617 /* collapse (command...(command...)...) or {command...{command...}...} */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000618 c = 0; /* "recursive" level */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000619 c2 = 0;
620 for (i = 0; int_buf[i]; i++)
621 if (int_buf[i] == '(' || int_buf[i] == '{') {
622 if (int_buf[i] == '(')
623 c++;
624 else
625 c2++;
626 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000627 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000628 }
629 for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
630 if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
631 if (int_buf[i] == ')')
632 c--;
633 else
634 c2--;
635 collapse_pos(0, i + 1);
Eric Andersenc470f442003-07-28 09:56:35 +0000636 i = -1; /* hack incremet */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000637 }
638
639 /* skip first not quote space */
640 for (i = 0; int_buf[i]; i++)
641 if (int_buf[i] != ' ')
642 break;
643 if (i)
644 collapse_pos(0, i);
645
646 /* set find mode for completion */
647 command_mode = FIND_EXE_ONLY;
648 for (i = 0; int_buf[i]; i++)
649 if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
650 if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000651 && matchBuf[pos_buf[0]]=='c'
652 && matchBuf[pos_buf[1]]=='d'
653 ) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000654 command_mode = FIND_DIR_ONLY;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000655 } else {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000656 command_mode = FIND_FILE_ONLY;
657 break;
658 }
659 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000660 for (i = 0; int_buf[i]; i++)
661 /* "strlen" */;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000662 /* find last word */
663 for (--i; i >= 0; i--) {
664 c = int_buf[i];
665 if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
666 collapse_pos(0, i + 1);
667 break;
668 }
669 }
670 /* skip first not quoted '\'' or '"' */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000671 for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
672 /*skip*/;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000673 /* collapse quote or unquote // or /~ */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000674 while ((int_buf[i] & ~QUOT) == '/'
675 && ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~')
676 ) {
Mark Whitley7e5291f2001-03-08 19:31:12 +0000677 i++;
678 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000679
680 /* set only match and destroy quotes */
681 j = 0;
Eric Andersen4f990532001-05-31 17:15:57 +0000682 for (c = 0; pos_buf[i] >= 0; i++) {
683 matchBuf[c++] = matchBuf[pos_buf[i]];
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000684 j = pos_buf[i] + 1;
685 }
Eric Andersen4f990532001-05-31 17:15:57 +0000686 matchBuf[c] = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000687 /* old lenght matchBuf with quotes symbols */
688 *len_with_quotes = j ? j - pos_buf[0] : 0;
689
690 return command_mode;
691}
692
Glenn L McGrath4d001292003-01-06 01:11:50 +0000693/*
694 display by column original ideas from ls applet,
695 very optimize by my :)
696*/
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000697static void showfiles(void)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000698{
699 int ncols, row;
700 int column_width = 0;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000701 int nfiles = num_matches;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000702 int nrows = nfiles;
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000703 int l;
Glenn L McGrath4d001292003-01-06 01:11:50 +0000704
705 /* find the longest file name- use that as the column width */
706 for (row = 0; row < nrows; row++) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000707 l = strlen(matches[row]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000708 if (column_width < l)
709 column_width = l;
710 }
711 column_width += 2; /* min space for columns */
712 ncols = cmdedit_termw / column_width;
713
714 if (ncols > 1) {
715 nrows /= ncols;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000716 if (nfiles % ncols)
Glenn L McGrath4d001292003-01-06 01:11:50 +0000717 nrows++; /* round up fractionals */
Glenn L McGrath4d001292003-01-06 01:11:50 +0000718 } else {
719 ncols = 1;
720 }
721 for (row = 0; row < nrows; row++) {
722 int n = row;
723 int nc;
724
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000725 for (nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000726 printf("%s%-*s", matches[n],
Mike Frysinger57ec5742006-12-28 21:41:09 +0000727 (int)(column_width - strlen(matches[n])), "");
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000728 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000729 printf("%s\n", matches[n]);
Glenn L McGrath4d001292003-01-06 01:11:50 +0000730 }
731}
732
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000733static int match_compare(const void *a, const void *b)
734{
735 return strcmp(*(char**)a, *(char**)b);
736}
737
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000738static char *add_quote_for_spec_chars(char *found)
739{
740 int l = 0;
741 char *s = xmalloc((strlen(found) + 1) * 2);
742
743 while (*found) {
744 if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
745 s[l++] = '\\';
746 s[l++] = *found++;
747 }
748 s[l] = 0;
749 return s;
750}
751
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000752static void input_tab(int *lastWasTab)
Erik Andersenf0657d32000-04-12 17:49:52 +0000753{
754 /* Do TAB completion */
Eric Andersenc470f442003-07-28 09:56:35 +0000755 if (lastWasTab == 0) { /* free all memory */
Erik Andersenf0657d32000-04-12 17:49:52 +0000756 if (matches) {
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000757 while (num_matches > 0)
Mark Whitley4e338752001-01-26 20:42:23 +0000758 free(matches[--num_matches]);
Erik Andersenf0657d32000-04-12 17:49:52 +0000759 free(matches);
760 matches = (char **) NULL;
761 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000762 return;
763 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000764 if (!*lastWasTab) {
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000765 char *tmp, *tmp1;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000766 int len_found;
767 char matchBuf[BUFSIZ];
768 int find_type;
769 int recalc_pos;
770
Eric Andersenc470f442003-07-28 09:56:35 +0000771 *lastWasTab = TRUE; /* flop trigger */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000772
773 /* Make a local copy of the string -- up
774 * to the position of the cursor */
775 tmp = strncpy(matchBuf, command_ps, cursor);
776 tmp[cursor] = 0;
777
778 find_type = find_match(matchBuf, &recalc_pos);
779
780 /* Free up any memory already allocated */
781 input_tab(0);
Erik Andersenf0657d32000-04-12 17:49:52 +0000782
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000783#if ENABLE_FEATURE_COMMAND_USERNAME_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000784 /* If the word starts with `~' and there is no slash in the word,
Erik Andersenf0657d32000-04-12 17:49:52 +0000785 * then try completing this word as a username. */
786
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000787 if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000788 username_tab_completion(matchBuf, NULL);
789 if (!matches)
Mark Whitley4e338752001-01-26 20:42:23 +0000790#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000791 /* Try to match any executable in our path and everything
Erik Andersenf0657d32000-04-12 17:49:52 +0000792 * in the current working directory that matches. */
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000793 exe_n_cwd_tab_completion(matchBuf, find_type);
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000794 /* Sort, then remove any duplicates found */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000795 if (matches) {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000796 int i, n = 0;
797 qsort(matches, num_matches, sizeof(char*), match_compare);
798 for (i = 0; i < num_matches - 1; ++i) {
799 if (matches[i] && matches[i+1]) {
800 if (strcmp(matches[i], matches[i+1]) == 0) {
801 free(matches[i]);
802 matches[i] = 0;
803 } else {
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000804 matches[n++] = matches[i];
Denis Vlasenko92758142006-10-03 19:56:34 +0000805 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000806 }
Denis Vlasenko92758142006-10-03 19:56:34 +0000807 }
Denis Vlasenkof58906b2006-12-19 19:30:37 +0000808 matches[n++] = matches[num_matches-1];
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000809 num_matches = n;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000810 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000811 /* Did we find exactly one match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000812 if (!matches || num_matches > 1) {
Mark Whitley4e338752001-01-26 20:42:23 +0000813 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000814 if (!matches)
Eric Andersenc470f442003-07-28 09:56:35 +0000815 return; /* not found */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000816 /* find minimal match */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000817 tmp1 = xstrdup(matches[0]);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000818 for (tmp = tmp1; *tmp; tmp++)
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000819 for (len_found = 1; len_found < num_matches; len_found++)
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000820 if (matches[len_found][(tmp - tmp1)] != *tmp) {
821 *tmp = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000822 break;
823 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000824 if (*tmp1 == 0) { /* have unique */
825 free(tmp1);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000826 return;
827 }
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000828 tmp = add_quote_for_spec_chars(tmp1);
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000829 free(tmp1);
Eric Andersenc470f442003-07-28 09:56:35 +0000830 } else { /* one match */
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000831 tmp = add_quote_for_spec_chars(matches[0]);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000832 /* for next completion current found */
833 *lastWasTab = FALSE;
Denis Vlasenkod56b47f2006-12-21 22:24:46 +0000834
835 len_found = strlen(tmp);
836 if (tmp[len_found-1] != '/') {
837 tmp[len_found] = ' ';
838 tmp[len_found+1] = '\0';
839 }
Mark Whitley4e338752001-01-26 20:42:23 +0000840 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000841 len_found = strlen(tmp);
Mark Whitley4e338752001-01-26 20:42:23 +0000842 /* have space to placed match? */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000843 if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
Mark Whitley4e338752001-01-26 20:42:23 +0000844
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000845 /* before word for match */
846 command_ps[cursor - recalc_pos] = 0;
847 /* save tail line */
848 strcpy(matchBuf, command_ps + cursor);
849 /* add match */
850 strcat(command_ps, tmp);
851 /* add tail */
Mark Whitley4e338752001-01-26 20:42:23 +0000852 strcat(command_ps, matchBuf);
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000853 /* back to begin word for match */
854 input_backward(recalc_pos);
855 /* new pos */
856 recalc_pos = cursor + len_found;
857 /* new len */
858 len = strlen(command_ps);
859 /* write out the matched command */
Eric Andersen4f990532001-05-31 17:15:57 +0000860 redraw(cmdedit_y, len - recalc_pos);
Erik Andersenf0657d32000-04-12 17:49:52 +0000861 }
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000862 free(tmp);
Erik Andersenf0657d32000-04-12 17:49:52 +0000863 } else {
864 /* Ok -- the last char was a TAB. Since they
865 * just hit TAB again, print a list of all the
866 * available choices... */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000867 if (matches && num_matches > 0) {
Eric Andersenc470f442003-07-28 09:56:35 +0000868 int sav_cursor = cursor; /* change goto_new_line() */
Erik Andersenf0657d32000-04-12 17:49:52 +0000869
870 /* Go to the next line */
Mark Whitley4e338752001-01-26 20:42:23 +0000871 goto_new_line();
"Vladimir N. Oleynik"fdb871c2006-01-25 11:53:47 +0000872 showfiles();
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000873 redraw(0, len - sav_cursor);
Erik Andersenf0657d32000-04-12 17:49:52 +0000874 }
875 }
876}
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000877#endif /* FEATURE_COMMAND_TAB_COMPLETION */
Erik Andersenf0657d32000-04-12 17:49:52 +0000878
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000879
Denis Vlasenko9d4533e2006-11-02 22:09:37 +0000880#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000881
882static char *history[MAX_HISTORY+1]; /* history + current */
883/* saved history lines */
884static int n_history;
885/* current pointer to history line */
886static int cur_history;
887
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000888static void get_previous_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000889{
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000890 if (command_ps[0] != 0 || history[cur_history] == 0) {
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000891 free(history[cur_history]);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000892 history[cur_history] = xstrdup(command_ps);
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000893 }
894 cur_history--;
Erik Andersenf0657d32000-04-12 17:49:52 +0000895}
896
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000897static int get_next_history(void)
Erik Andersenf0657d32000-04-12 17:49:52 +0000898{
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000899 int ch = cur_history;
900
901 if (ch < n_history) {
902 get_previous_history(); /* save the current history line */
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000903 cur_history = ch + 1;
904 return cur_history;
Glenn L McGrath062c74f2002-11-27 09:29:49 +0000905 } else {
906 beep();
907 return 0;
908 }
Erik Andersenf0657d32000-04-12 17:49:52 +0000909}
Robert Griebl350d26b2002-12-03 22:45:46 +0000910
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000911#if ENABLE_FEATURE_COMMAND_SAVEHISTORY
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000912void load_history(const char *fromfile)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000913{
Robert Griebl350d26b2002-12-03 22:45:46 +0000914 FILE *fp;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000915 int hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000916
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000917 /* cleanup old */
918
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000919 for (hi = n_history; hi > 0;) {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000920 hi--;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000921 free(history[hi]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000922 }
923
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000924 fp = fopen(fromfile, "r");
925 if (fp) {
926 for (hi = 0; hi < MAX_HISTORY;) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000927 char * hl = xmalloc_getline(fp);
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000928 int l;
929
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000930 if (!hl)
Robert Griebl350d26b2002-12-03 22:45:46 +0000931 break;
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000932 l = strlen(hl);
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000933 if (l >= BUFSIZ)
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000934 hl[BUFSIZ-1] = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000935 if (l == 0 || hl[0] == ' ') {
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000936 free(hl);
937 continue;
938 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000939 history[hi++] = hl;
Robert Griebl350d26b2002-12-03 22:45:46 +0000940 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000941 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000942 }
Glenn L McGrathfdbbb042002-12-09 11:10:40 +0000943 cur_history = n_history = hi;
Robert Griebl350d26b2002-12-03 22:45:46 +0000944}
945
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000946void save_history (const char *tofile)
Robert Griebl350d26b2002-12-03 22:45:46 +0000947{
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000948 FILE *fp = fopen(tofile, "w");
Eric Andersenc470f442003-07-28 09:56:35 +0000949
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000950 if (fp) {
Robert Griebl350d26b2002-12-03 22:45:46 +0000951 int i;
Eric Andersenc470f442003-07-28 09:56:35 +0000952
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000953 for (i = 0; i < n_history; i++) {
954 fprintf(fp, "%s\n", history[i]);
Robert Griebl350d26b2002-12-03 22:45:46 +0000955 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +0000956 fclose(fp);
Robert Griebl350d26b2002-12-03 22:45:46 +0000957 }
Robert Griebl350d26b2002-12-03 22:45:46 +0000958}
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000959#endif /* FEATURE_COMMAND_SAVEHISTORY */
Robert Griebl350d26b2002-12-03 22:45:46 +0000960
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000961#endif /* MAX_HISTORY > 0 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +0000962
963
Erik Andersen6273f652000-03-17 01:12:41 +0000964/*
965 * This function is used to grab a character buffer
966 * from the input file descriptor and allows you to
Eric Andersen9b3ce772004-04-12 15:03:51 +0000967 * a string with full command editing (sort of like
Erik Andersen6273f652000-03-17 01:12:41 +0000968 * a mini readline).
969 *
970 * The following standard commands are not implemented:
971 * ESC-b -- Move back one word
972 * ESC-f -- Move forward one word
973 * ESC-d -- Delete back one word
974 * ESC-h -- Delete forward one word
975 * CTL-t -- Transpose two characters
976 *
Paul Fox3f11b1b2005-08-04 19:04:46 +0000977 * Minimalist vi-style command line editing available if configured.
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000978 * vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
Erik Andersen6273f652000-03-17 01:12:41 +0000979 */
Eric Andersenc470f442003-07-28 09:56:35 +0000980
Denis Vlasenko7f1dc212006-12-19 01:10:25 +0000981#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +0000982static int vi_mode;
983
Denis Vlasenko82b39e82007-01-21 19:18:19 +0000984void setvimode(int viflag)
Paul Fox3f11b1b2005-08-04 19:04:46 +0000985{
986 vi_mode = viflag;
987}
988
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000989static void
Paul Fox3f11b1b2005-08-04 19:04:46 +0000990vi_Word_motion(char *command, int eat)
991{
992 while (cursor < len && !isspace(command[cursor]))
993 input_forward();
994 if (eat) while (cursor < len && isspace(command[cursor]))
995 input_forward();
996}
997
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000998static void
Paul Fox3f11b1b2005-08-04 19:04:46 +0000999vi_word_motion(char *command, int eat)
1000{
1001 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001002 while (cursor < len
1003 && (isalnum(command[cursor+1]) || command[cursor+1] == '_'))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001004 input_forward();
1005 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001006 while (cursor < len && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001007 input_forward();
1008 }
1009
1010 if (cursor < len)
1011 input_forward();
1012
1013 if (eat && cursor < len && isspace(command[cursor]))
1014 while (cursor < len && isspace(command[cursor]))
1015 input_forward();
1016}
1017
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001018static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001019vi_End_motion(char *command)
1020{
1021 input_forward();
1022 while (cursor < len && isspace(command[cursor]))
1023 input_forward();
1024 while (cursor < len-1 && !isspace(command[cursor+1]))
1025 input_forward();
1026}
1027
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001028static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001029vi_end_motion(char *command)
1030{
1031 if (cursor >= len-1)
1032 return;
1033 input_forward();
1034 while (cursor < len-1 && isspace(command[cursor]))
1035 input_forward();
1036 if (cursor >= len-1)
1037 return;
1038 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001039 while (cursor < len-1
1040 && (isalnum(command[cursor+1]) || command[cursor+1] == '_')
1041 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001042 input_forward();
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001043 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001044 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001045 while (cursor < len-1 && ispunct(command[cursor+1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001046 input_forward();
1047 }
1048}
1049
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001050static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001051vi_Back_motion(char *command)
1052{
1053 while (cursor > 0 && isspace(command[cursor-1]))
1054 input_backward(1);
1055 while (cursor > 0 && !isspace(command[cursor-1]))
1056 input_backward(1);
1057}
1058
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +00001059static void
Paul Fox3f11b1b2005-08-04 19:04:46 +00001060vi_back_motion(char *command)
1061{
1062 if (cursor <= 0)
1063 return;
1064 input_backward(1);
1065 while (cursor > 0 && isspace(command[cursor]))
1066 input_backward(1);
1067 if (cursor <= 0)
1068 return;
1069 if (isalnum(command[cursor]) || command[cursor] == '_') {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001070 while (cursor > 0
1071 && (isalnum(command[cursor-1]) || command[cursor-1] == '_')
1072 ) {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001073 input_backward(1);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001074 }
Paul Fox3f11b1b2005-08-04 19:04:46 +00001075 } else if (ispunct(command[cursor])) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001076 while (cursor > 0 && ispunct(command[cursor-1]))
Paul Fox3f11b1b2005-08-04 19:04:46 +00001077 input_backward(1);
1078 }
1079}
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001080#else
1081enum { vi_mode = 0 };
1082#endif
1083
1084
1085/*
1086 * cmdedit_read_input and its helpers
1087 */
1088
1089#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
1090#define getTermSettings(fd, argp) tcgetattr(fd, argp);
1091
1092static sighandler_t previous_SIGWINCH_handler;
1093
1094static void cmdedit_reset_term(void)
1095{
1096 if (handlers_sets & SET_RESET_TERM) {
1097 setTermSettings(STDIN_FILENO, (void *) &initial_settings);
1098 handlers_sets &= ~SET_RESET_TERM;
1099 }
1100 if (handlers_sets & SET_WCHG_HANDLERS) {
1101 /* restore SIGWINCH handler */
1102 signal(SIGWINCH, previous_SIGWINCH_handler);
1103 handlers_sets &= ~SET_WCHG_HANDLERS;
1104 }
1105 fflush(stdout);
1106}
1107
1108static void cmdedit_setwidth(int w, int redraw_flg)
1109{
1110 cmdedit_termw = cmdedit_prmt_len + 2;
1111 if (w <= cmdedit_termw) {
1112 cmdedit_termw = cmdedit_termw % w;
1113 }
1114 if (w > cmdedit_termw) {
1115 cmdedit_termw = w;
1116
1117 if (redraw_flg) {
1118 /* new y for current cursor */
1119 int new_y = (cursor + cmdedit_prmt_len) / w;
1120
1121 /* redraw */
1122 redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
1123 fflush(stdout);
1124 }
1125 }
1126}
1127
1128static void win_changed(int nsig)
1129{
1130 int width;
1131 get_terminal_width_height(0, &width, NULL);
1132 cmdedit_setwidth(width, nsig /* - just a yes/no flag */);
1133 if (nsig == SIGWINCH)
1134 signal(SIGWINCH, win_changed); /* rearm ourself */
1135}
1136
1137static void cmdedit_init(void)
1138{
1139 cmdedit_prmt_len = 0;
1140 if (!(handlers_sets & SET_WCHG_HANDLERS)) {
1141 previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
1142 win_changed(0); /* do initial resizing */
1143 handlers_sets |= SET_WCHG_HANDLERS;
1144 }
1145
1146 if (!(handlers_sets & SET_ATEXIT)) {
1147#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1148 struct passwd *entry;
1149
1150 my_euid = geteuid();
1151 entry = getpwuid(my_euid);
1152 if (entry) {
1153 user_buf = xstrdup(entry->pw_name);
1154 home_pwd_buf = xstrdup(entry->pw_dir);
1155 }
1156#endif
1157
1158#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
1159
1160#if !ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1161 my_euid = geteuid();
1162#endif
1163 my_uid = getuid();
1164 my_gid = getgid();
1165#endif /* FEATURE_COMMAND_TAB_COMPLETION */
1166 handlers_sets |= SET_ATEXIT;
1167// Crap. We should be able to do it without atexit.
1168 atexit(cmdedit_reset_term); /* be sure to do this only once */
1169 }
1170}
1171
1172#if !ENABLE_FEATURE_SH_FANCY_PROMPT
1173static void parse_prompt(const char *prmt_ptr)
1174{
1175 cmdedit_prompt = prmt_ptr;
1176 cmdedit_prmt_len = strlen(prmt_ptr);
1177 put_prompt();
1178}
1179#else
1180static void parse_prompt(const char *prmt_ptr)
1181{
1182 int prmt_len = 0;
1183 size_t cur_prmt_len = 0;
1184 char flg_not_length = '[';
1185 char *prmt_mem_ptr = xzalloc(1);
1186 char *pwd_buf = xgetcwd(0);
1187 char buf2[PATH_MAX + 1];
1188 char buf[2];
1189 char c;
1190 char *pbuf;
1191
1192 if (!pwd_buf) {
1193 pwd_buf = (char *)bb_msg_unknown;
1194 }
1195
1196 while (*prmt_ptr) {
1197 pbuf = buf;
1198 pbuf[1] = 0;
1199 c = *prmt_ptr++;
1200 if (c == '\\') {
1201 const char *cp = prmt_ptr;
1202 int l;
1203
1204 c = bb_process_escape_sequence(&prmt_ptr);
1205 if (prmt_ptr == cp) {
1206 if (*cp == 0)
1207 break;
1208 c = *prmt_ptr++;
1209 switch (c) {
1210#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1211 case 'u':
1212 pbuf = user_buf;
1213 break;
1214#endif
1215 case 'h':
1216 pbuf = hostname_buf;
1217 if (!pbuf) {
1218 pbuf = xzalloc(256);
1219 if (gethostname(pbuf, 255) < 0) {
1220 strcpy(pbuf, "?");
1221 } else {
1222 char *s = strchr(pbuf, '.');
1223 if (s)
1224 *s = '\0';
1225 }
1226 hostname_buf = pbuf;
1227 }
1228 break;
1229 case '$':
1230 c = (my_euid == 0 ? '#' : '$');
1231 break;
1232#if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR
1233 case 'w':
1234 pbuf = pwd_buf;
1235 l = strlen(home_pwd_buf);
1236 if (home_pwd_buf[0] != 0
1237 && strncmp(home_pwd_buf, pbuf, l) == 0
1238 && (pbuf[l]=='/' || pbuf[l]=='\0')
1239 && strlen(pwd_buf+l)<PATH_MAX
1240 ) {
1241 pbuf = buf2;
1242 *pbuf = '~';
1243 strcpy(pbuf+1, pwd_buf+l);
1244 }
1245 break;
1246#endif
1247 case 'W':
1248 pbuf = pwd_buf;
1249 cp = strrchr(pbuf,'/');
1250 if (cp != NULL && cp != pbuf)
1251 pbuf += (cp-pbuf) + 1;
1252 break;
1253 case '!':
1254 snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
1255 break;
1256 case 'e': case 'E': /* \e \E = \033 */
1257 c = '\033';
1258 break;
1259 case 'x': case 'X':
1260 for (l = 0; l < 3;) {
1261 int h;
1262 buf2[l++] = *prmt_ptr;
1263 buf2[l] = 0;
1264 h = strtol(buf2, &pbuf, 16);
1265 if (h > UCHAR_MAX || (pbuf - buf2) < l) {
1266 l--;
1267 break;
1268 }
1269 prmt_ptr++;
1270 }
1271 buf2[l] = 0;
1272 c = (char)strtol(buf2, NULL, 16);
1273 if (c == 0)
1274 c = '?';
1275 pbuf = buf;
1276 break;
1277 case '[': case ']':
1278 if (c == flg_not_length) {
1279 flg_not_length = flg_not_length == '[' ? ']' : '[';
1280 continue;
1281 }
1282 break;
1283 }
1284 }
1285 }
1286 if (pbuf == buf)
1287 *pbuf = c;
1288 cur_prmt_len = strlen(pbuf);
1289 prmt_len += cur_prmt_len;
1290 if (flg_not_length != ']')
1291 cmdedit_prmt_len += cur_prmt_len;
1292 prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
1293 }
1294 if (pwd_buf!=(char *)bb_msg_unknown)
1295 free(pwd_buf);
1296 cmdedit_prompt = prmt_mem_ptr;
1297 put_prompt();
1298}
Paul Fox3f11b1b2005-08-04 19:04:46 +00001299#endif
1300
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001301/*
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001302 * the emacs and vi modes share much of the code in the big
1303 * command loop. commands entered when in vi's command mode (aka
1304 * "escape mode") get an extra bit added to distinguish them --
1305 * this keeps them from being self-inserted. this clutters the
1306 * big switch a bit, but keeps all the code in one place.
Paul Fox3f11b1b2005-08-04 19:04:46 +00001307 */
1308
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001309#define vbit 0x100
1310
1311/* leave out the "vi-mode"-only case labels if vi editing isn't
1312 * configured. */
1313#define vi_case(caselabel) USE_FEATURE_COMMAND_EDITING(caselabel)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001314
1315/* convert uppercase ascii to equivalent control char, for readability */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001316#undef CTRL
1317#define CTRL(a) ((a) & ~0x40)
Paul Fox3f11b1b2005-08-04 19:04:46 +00001318
Eric Andersen044228d2001-07-17 01:12:36 +00001319
1320int cmdedit_read_input(char *prompt, char command[BUFSIZ])
Erik Andersen13456d12000-03-16 08:09:57 +00001321{
Erik Andersenc7c634b2000-03-19 05:28:55 +00001322 int lastWasTab = FALSE;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001323 unsigned int ic;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001324 unsigned char c;
1325 smallint break_out = 0;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001326#if ENABLE_FEATURE_COMMAND_EDITING_VI
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001327 smallint vi_cmdmode = 0;
1328 smalluint prevc;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001329#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001330 /* prepare before init handlers */
Eric Andersenc470f442003-07-28 09:56:35 +00001331 cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
Mark Whitley4e338752001-01-26 20:42:23 +00001332 len = 0;
Mark Whitley4e338752001-01-26 20:42:23 +00001333 command_ps = command;
1334
Eric Andersen34506362001-08-02 05:02:46 +00001335 getTermSettings(0, (void *) &initial_settings);
1336 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
1337 new_settings.c_lflag &= ~ICANON; /* unbuffered input */
1338 /* Turn off echoing and CTRL-C, so we can trap it */
1339 new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
Eric Andersen34506362001-08-02 05:02:46 +00001340 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
1341 new_settings.c_cc[VMIN] = 1;
1342 new_settings.c_cc[VTIME] = 0;
1343 /* Turn off CTRL-C, so we can trap it */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001344# ifndef _POSIX_VDISABLE
1345# define _POSIX_VDISABLE '\0'
1346# endif
Eric Andersenc470f442003-07-28 09:56:35 +00001347 new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001348 command[0] = 0;
1349
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001350 setTermSettings(0, (void *) &new_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001351 handlers_sets |= SET_RESET_TERM;
Erik Andersen13456d12000-03-16 08:09:57 +00001352
Eric Andersen6faae7d2001-02-16 20:09:17 +00001353 /* Now initialize things */
1354 cmdedit_init();
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001355 /* Print out the command prompt */
1356 parse_prompt(prompt);
Eric Andersenb3dc3b82001-01-04 11:08:45 +00001357
Erik Andersenc7c634b2000-03-19 05:28:55 +00001358 while (1) {
Eric Andersenc470f442003-07-28 09:56:35 +00001359 fflush(stdout); /* buffered out to fast */
Mark Whitley4e338752001-01-26 20:42:23 +00001360
Eric Andersen7467c8d2001-07-12 20:26:32 +00001361 if (safe_read(0, &c, 1) < 1)
Eric Andersened424db2001-04-23 15:28:28 +00001362 /* if we can't read input then exit */
1363 goto prepare_to_die;
Erik Andersenf3b3d172000-04-09 18:24:05 +00001364
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001365 ic = c;
1366
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001367#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001368 newdelflag = 1;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001369 if (vi_cmdmode)
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001370 ic |= vbit;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001371#endif
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001372 switch (ic) {
Erik Andersenf0657d32000-04-12 17:49:52 +00001373 case '\n':
1374 case '\r':
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001375 vi_case( case '\n'|vbit: )
1376 vi_case( case '\r'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001377 /* Enter */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001378 goto_new_line();
Erik Andersenf0657d32000-04-12 17:49:52 +00001379 break_out = 1;
1380 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001381 case CTRL('A'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001382 vi_case( case '0'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001383 /* Control-a -- Beginning of line */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001384 input_backward(cursor);
Mark Whitley4e338752001-01-26 20:42:23 +00001385 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001386 case CTRL('B'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001387 vi_case( case 'h'|vbit: )
1388 vi_case( case '\b'|vbit: )
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001389 vi_case( case '\x7f'|vbit: ) /* DEL */
Erik Andersenf0657d32000-04-12 17:49:52 +00001390 /* Control-b -- Move back one character */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001391 input_backward(1);
Erik Andersenf0657d32000-04-12 17:49:52 +00001392 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001393 case CTRL('C'):
1394 vi_case( case CTRL('C')|vbit: )
Eric Andersen86349772000-12-18 20:25:50 +00001395 /* Control-c -- stop gathering input */
Mark Whitley4e338752001-01-26 20:42:23 +00001396 goto_new_line();
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001397#if !ENABLE_ASH
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001398 command[0] = 0;
Eric Andersen7467c8d2001-07-12 20:26:32 +00001399 len = 0;
1400 lastWasTab = FALSE;
1401 put_prompt();
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001402#else
1403 len = 0;
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001404 break_out = -1; /* to control traps */
Glenn L McGrath16e45d72004-02-04 08:24:39 +00001405#endif
Eric Andersen7467c8d2001-07-12 20:26:32 +00001406 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001407 case CTRL('D'):
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001408 /* Control-d -- Delete one character, or exit
Erik Andersenf0657d32000-04-12 17:49:52 +00001409 * if the len=0 and no chars to delete */
1410 if (len == 0) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001411 errno = 0;
1412 prepare_to_die:
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001413// So, our API depends on whether we have ash compiled in or not? Crap...
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001414#if !ENABLE_ASH
Mark Whitley4e338752001-01-26 20:42:23 +00001415 printf("exit");
Eric Andersen7467c8d2001-07-12 20:26:32 +00001416 goto_new_line();
1417 /* cmdedit_reset_term() called in atexit */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001418// FIXME. this is definitely not good
Eric Andersen7467c8d2001-07-12 20:26:32 +00001419 exit(EXIT_SUCCESS);
Eric Andersen044228d2001-07-17 01:12:36 +00001420#else
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001421 /* to control stopped jobs */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001422 break_out = len = -1;
Eric Andersen044228d2001-07-17 01:12:36 +00001423 break;
1424#endif
Erik Andersenf0657d32000-04-12 17:49:52 +00001425 } else {
Paul Fox3f11b1b2005-08-04 19:04:46 +00001426 input_delete(0);
Erik Andersenf0657d32000-04-12 17:49:52 +00001427 }
1428 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001429 case CTRL('E'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001430 vi_case( case '$'|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001431 /* Control-e -- End of line */
Mark Whitley4e338752001-01-26 20:42:23 +00001432 input_end();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001433 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001434 case CTRL('F'):
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001435 vi_case( case 'l'|vbit: )
1436 vi_case( case ' '|vbit: )
Erik Andersenc7c634b2000-03-19 05:28:55 +00001437 /* Control-f -- Move forward one character */
Mark Whitley4e338752001-01-26 20:42:23 +00001438 input_forward();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001439 break;
Erik Andersenf0657d32000-04-12 17:49:52 +00001440 case '\b':
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001441 case '\x7f': /* DEL */
Erik Andersen1d1d9502000-04-21 01:26:49 +00001442 /* Control-h and DEL */
Mark Whitley4e338752001-01-26 20:42:23 +00001443 input_backspace();
Erik Andersenc7c634b2000-03-19 05:28:55 +00001444 break;
1445 case '\t':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001446#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001447 input_tab(&lastWasTab);
Erik Andersena2685732000-04-09 18:27:46 +00001448#endif
Erik Andersenc7c634b2000-03-19 05:28:55 +00001449 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001450 case CTRL('K'):
Eric Andersenc470f442003-07-28 09:56:35 +00001451 /* Control-k -- clear to end of line */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001452 command[cursor] = 0;
Eric Andersen65a07302002-04-13 13:26:49 +00001453 len = cursor;
Eric Andersenae103612002-04-24 23:08:23 +00001454 printf("\033[J");
Eric Andersen65a07302002-04-13 13:26:49 +00001455 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001456 case CTRL('L'):
1457 vi_case( case CTRL('L')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001458 /* Control-l -- clear screen */
Eric Andersenc470f442003-07-28 09:56:35 +00001459 printf("\033[H");
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001460 redraw(0, len - cursor);
Eric Andersenf1f2bd02001-12-21 11:20:15 +00001461 break;
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001462#if MAX_HISTORY > 0
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001463 case CTRL('N'):
1464 vi_case( case CTRL('N')|vbit: )
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001465 vi_case( case 'j'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001466 /* Control-n -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001467 if (get_next_history())
Erik Andersenf0657d32000-04-12 17:49:52 +00001468 goto rewrite_line;
Erik Andersenf0657d32000-04-12 17:49:52 +00001469 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001470 case CTRL('P'):
1471 vi_case( case CTRL('P')|vbit: )
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001472 vi_case( case 'k'|vbit: )
Erik Andersenf0657d32000-04-12 17:49:52 +00001473 /* Control-p -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001474 if (cur_history > 0) {
1475 get_previous_history();
Erik Andersenf0657d32000-04-12 17:49:52 +00001476 goto rewrite_line;
1477 } else {
Mark Whitley4e338752001-01-26 20:42:23 +00001478 beep();
Erik Andersenf0657d32000-04-12 17:49:52 +00001479 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001480 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001481#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001482 case CTRL('U'):
1483 vi_case( case CTRL('U')|vbit: )
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001484 /* Control-U -- Clear line before cursor */
1485 if (cursor) {
1486 strcpy(command, command + cursor);
1487 redraw(cmdedit_y, len -= cursor);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001488 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001489 break;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001490 case CTRL('W'):
1491 vi_case( case CTRL('W')|vbit: )
Eric Andersen27bb7902003-12-23 20:24:51 +00001492 /* Control-W -- Remove the last word */
1493 while (cursor > 0 && isspace(command[cursor-1]))
1494 input_backspace();
1495 while (cursor > 0 &&!isspace(command[cursor-1]))
1496 input_backspace();
1497 break;
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001498#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001499 case 'i'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001500 vi_cmdmode = 0;
1501 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001502 case 'I'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001503 input_backward(cursor);
1504 vi_cmdmode = 0;
1505 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001506 case 'a'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001507 input_forward();
1508 vi_cmdmode = 0;
1509 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001510 case 'A'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001511 input_end();
1512 vi_cmdmode = 0;
1513 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001514 case 'x'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001515 input_delete(1);
1516 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001517 case 'X'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001518 if (cursor > 0) {
1519 input_backward(1);
1520 input_delete(1);
1521 }
1522 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001523 case 'W'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001524 vi_Word_motion(command, 1);
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 'E'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001530 vi_End_motion(command);
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 'B'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001536 vi_Back_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 'C'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001542 vi_cmdmode = 0;
1543 /* fall through */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001544 case 'D'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001545 goto clear_to_eol;
1546
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001547 case 'c'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001548 vi_cmdmode = 0;
1549 /* fall through */
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001550 case 'd'|vbit: {
1551 int nc, sc;
1552 sc = cursor;
1553 prevc = ic;
1554 if (safe_read(0, &c, 1) < 1)
1555 goto prepare_to_die;
1556 if (c == (prevc & 0xff)) {
1557 /* "cc", "dd" */
1558 input_backward(cursor);
1559 goto clear_to_eol;
1560 break;
1561 }
1562 switch (c) {
1563 case 'w':
1564 case 'W':
1565 case 'e':
1566 case 'E':
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001567 switch (c) {
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001568 case 'w': /* "dw", "cw" */
1569 vi_word_motion(command, vi_cmdmode);
Denis Vlasenko92758142006-10-03 19:56:34 +00001570 break;
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 'e': /* 'de', 'ce' */
1575 vi_end_motion(command);
1576 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001577 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001578 case 'E': /* 'dE', 'cE' */
1579 vi_End_motion(command);
1580 input_forward();
Denis Vlasenko92758142006-10-03 19:56:34 +00001581 break;
1582 }
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001583 nc = cursor;
1584 input_backward(cursor - sc);
1585 while (nc-- > cursor)
1586 input_delete(1);
1587 break;
1588 case 'b': /* "db", "cb" */
1589 case 'B': /* implemented as B */
1590 if (c == 'b')
1591 vi_back_motion(command);
1592 else
1593 vi_Back_motion(command);
1594 while (sc-- > cursor)
1595 input_delete(1);
1596 break;
1597 case ' ': /* "d ", "c " */
1598 input_delete(1);
1599 break;
1600 case '$': /* "d$", "c$" */
1601 clear_to_eol:
1602 while (cursor < len)
1603 input_delete(1);
1604 break;
Paul Fox3f11b1b2005-08-04 19:04:46 +00001605 }
1606 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001607 }
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001608 case 'p'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001609 input_forward();
1610 /* fallthrough */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001611 case 'P'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001612 put();
1613 break;
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001614 case 'r'|vbit:
Paul Fox3f11b1b2005-08-04 19:04:46 +00001615 if (safe_read(0, &c, 1) < 1)
1616 goto prepare_to_die;
1617 if (c == 0)
1618 beep();
1619 else {
1620 *(command + cursor) = c;
1621 putchar(c);
1622 putchar('\b');
1623 }
1624 break;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001625#endif /* FEATURE_COMMAND_EDITING_VI */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001626
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001627 case '\x1b': /* ESC */
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001628
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001629#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001630 if (vi_mode) {
1631 /* ESC: insert mode --> command mode */
1632 vi_cmdmode = 1;
1633 input_backward(1);
1634 break;
1635 }
1636#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001637 /* escape sequence follows */
Eric Andersen7467c8d2001-07-12 20:26:32 +00001638 if (safe_read(0, &c, 1) < 1)
1639 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001640 /* different vt100 emulations */
1641 if (c == '[' || c == 'O') {
Paul Fox0f2dd9f2006-03-07 20:26:11 +00001642 vi_case( case '['|vbit: )
1643 vi_case( case 'O'|vbit: )
Eric Andersen7467c8d2001-07-12 20:26:32 +00001644 if (safe_read(0, &c, 1) < 1)
1645 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001646 }
Glenn L McGrath475820c2004-01-22 12:42:23 +00001647 if (c >= '1' && c <= '9') {
1648 unsigned char dummy;
1649
1650 if (safe_read(0, &dummy, 1) < 1)
1651 goto prepare_to_die;
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001652 if (dummy != '~')
Glenn L McGrath475820c2004-01-22 12:42:23 +00001653 c = 0;
1654 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001655 switch (c) {
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001656#if ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Eric Andersenc470f442003-07-28 09:56:35 +00001657 case '\t': /* Alt-Tab */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001658 input_tab(&lastWasTab);
1659 break;
1660#endif
Denis Vlasenko9d4533e2006-11-02 22:09:37 +00001661#if MAX_HISTORY > 0
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001662 case 'A':
1663 /* Up Arrow -- Get previous command from history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001664 if (cur_history > 0) {
1665 get_previous_history();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001666 goto rewrite_line;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001667 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001668 beep();
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001669 break;
1670 case 'B':
1671 /* Down Arrow -- Get next command in history */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001672 if (!get_next_history())
Paul Fox3f11b1b2005-08-04 19:04:46 +00001673 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001674 /* Rewrite the line with the selected history item */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001675 rewrite_line:
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001676 /* change command */
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001677 len = strlen(strcpy(command, history[cur_history]));
Paul Fox3f11b1b2005-08-04 19:04:46 +00001678 /* redraw and go to eol (bol, in vi */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001679 redraw(cmdedit_y, vi_mode ? 9999 : 0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001680 break;
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001681#endif
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001682 case 'C':
1683 /* Right Arrow -- Move forward one character */
1684 input_forward();
1685 break;
1686 case 'D':
1687 /* Left Arrow -- Move back one character */
1688 input_backward(1);
1689 break;
1690 case '3':
1691 /* Delete */
Paul Fox3f11b1b2005-08-04 19:04:46 +00001692 input_delete(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001693 break;
1694 case '1':
1695 case 'H':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001696 /* <Home> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001697 input_backward(cursor);
1698 break;
1699 case '4':
1700 case 'F':
Glenn L McGrath7fc504c2004-02-22 11:13:28 +00001701 /* <End> */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001702 input_end();
1703 break;
1704 default:
Glenn L McGrath475820c2004-01-22 12:42:23 +00001705 c = 0;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001706 beep();
1707 }
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001708 break;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001709
Eric Andersenc470f442003-07-28 09:56:35 +00001710 default: /* If it's regular input, do the normal thing */
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001711#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001712 /* Control-V -- Add non-printable symbol */
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001713 if (c == CTRL('V')) {
Eric Andersen7467c8d2001-07-12 20:26:32 +00001714 if (safe_read(0, &c, 1) < 1)
1715 goto prepare_to_die;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001716 if (c == 0) {
1717 beep();
1718 break;
1719 }
1720 } else
1721#endif
Paul Fox3f11b1b2005-08-04 19:04:46 +00001722 {
Denis Vlasenko966ec7c2006-11-01 09:13:26 +00001723#if ENABLE_FEATURE_COMMAND_EDITING_VI
Paul Fox3f11b1b2005-08-04 19:04:46 +00001724 if (vi_cmdmode) /* don't self-insert */
1725 break;
1726#endif
1727 if (!Isprint(c)) /* Skip non-printable characters */
1728 break;
1729 }
Erik Andersenc7c634b2000-03-19 05:28:55 +00001730
Eric Andersenc470f442003-07-28 09:56:35 +00001731 if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001732 break;
1733
1734 len++;
1735
Eric Andersenc470f442003-07-28 09:56:35 +00001736 if (cursor == (len - 1)) { /* Append if at the end of the line */
Erik Andersenf0657d32000-04-12 17:49:52 +00001737 *(command + cursor) = c;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001738 *(command + cursor + 1) = 0;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001739 cmdedit_set_out_char(' ');
Eric Andersenc470f442003-07-28 09:56:35 +00001740 } else { /* Insert otherwise */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001741 int sc = cursor;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001742
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001743 memmove(command + sc + 1, command + sc, len - sc);
1744 *(command + sc) = c;
1745 sc++;
Mark Whitley4e338752001-01-26 20:42:23 +00001746 /* rewrite from cursor */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001747 input_end();
Mark Whitley4e338752001-01-26 20:42:23 +00001748 /* to prev x pos + 1 */
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001749 input_backward(cursor - sc);
Erik Andersenc7c634b2000-03-19 05:28:55 +00001750 }
1751
Erik Andersenc7c634b2000-03-19 05:28:55 +00001752 break;
Erik Andersen13456d12000-03-16 08:09:57 +00001753 }
Eric Andersenc470f442003-07-28 09:56:35 +00001754 if (break_out) /* Enter is the command terminator, no more input. */
Erik Andersenc7c634b2000-03-19 05:28:55 +00001755 break;
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001756
1757 if (c != '\t')
1758 lastWasTab = FALSE;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001759 }
1760
Glenn L McGrath78b0e372001-06-26 02:06:08 +00001761 setTermSettings(0, (void *) &initial_settings);
Mark Whitley4e338752001-01-26 20:42:23 +00001762 handlers_sets &= ~SET_RESET_TERM;
Erik Andersenc7c634b2000-03-19 05:28:55 +00001763
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 Vlasenko0a8a7742006-12-21 22:27:10 +00001767 if (len > 0) { /* no put empty line */
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]);
1771 history[MAX_HISTORY] = 0;
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 }
Rob Landleyd921b2e2006-08-03 15:41:12 +00001778 history[i++] = xstrdup(command);
Glenn L McGrath062c74f2002-11-27 09:29:49 +00001779 cur_history = i;
1780 n_history = i;
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001781 USE_FEATURE_SH_FANCY_PROMPT(num_ok_lines++;)
Erik Andersen6273f652000-03-17 01:12:41 +00001782 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001783#else /* MAX_HISTORY == 0 */
1784 /* dont put empty line */
1785 USE_FEATURE_SH_FANCY_PROMPT(if (len > 0) num_ok_lines++;)
1786#endif /* MAX_HISTORY */
1787
Eric Andersen27bb7902003-12-23 20:24:51 +00001788 if (break_out > 0) {
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001789 command[len++] = '\n';
1790 command[len] = '\0';
Eric Andersen044228d2001-07-17 01:12:36 +00001791 }
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001792
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001793#if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_COMMAND_TAB_COMPLETION
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001794 input_tab(0);
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001795#endif
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001796
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001797#if ENABLE_FEATURE_SH_FANCY_PROMPT
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001798 free(cmdedit_prompt);
1799#endif
Eric Andersen501c88b2000-07-28 15:14:45 +00001800 cmdedit_reset_term();
Eric Andersen044228d2001-07-17 01:12:36 +00001801 return len;
Eric Andersen501c88b2000-07-28 15:14:45 +00001802}
1803
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001804#endif /* FEATURE_COMMAND_EDITING */
Eric Andersen501c88b2000-07-28 15:14:45 +00001805
1806
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001807/*
1808 * Testing
1809 */
1810
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001811#ifdef TEST
1812
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001813#include <locale.h>
Denis Vlasenko82b39e82007-01-21 19:18:19 +00001814
1815const char *applet_name = "debug stuff usage";
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001816
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001817int main(int argc, char **argv)
1818{
1819 char buff[BUFSIZ];
1820 char *prompt =
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001821#if ENABLE_FEATURE_SH_FANCY_PROMPT
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001822 "\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:"
1823 "\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] "
1824 "\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001825#else
1826 "% ";
1827#endif
1828
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001829#if ENABLE_FEATURE_NONPRINTABLE_INVERSE_PUT
Eric Andersenf9ff8a72001-03-15 20:51:09 +00001830 setlocale(LC_ALL, "");
1831#endif
Denis Vlasenko7f1dc212006-12-19 01:10:25 +00001832 while (1) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001833 int l;
Eric Andersen27bb7902003-12-23 20:24:51 +00001834 l = cmdedit_read_input(prompt, buff);
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001835 if (l <= 0 || buff[l-1] != '\n')
Eric Andersen27bb7902003-12-23 20:24:51 +00001836 break;
Denis Vlasenko0a8a7742006-12-21 22:27:10 +00001837 buff[l-1] = 0;
1838 printf("*** cmdedit_read_input() returned line =%s=\n", buff);
Eric Andersen7467c8d2001-07-12 20:26:32 +00001839 }
Eric Andersen27bb7902003-12-23 20:24:51 +00001840 printf("*** cmdedit_read_input() detect ^D\n");
Eric Andersen5f2c79d2001-02-16 18:36:04 +00001841 return 0;
1842}
1843
Eric Andersenc470f442003-07-28 09:56:35 +00001844#endif /* TEST */