2000-03-19 10:58:55 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2000-03-16 13:39:57 +05:30
|
|
|
/*
|
2004-04-14 23:21:38 +05:30
|
|
|
* Termios command line History and Editing.
|
2000-03-16 13:39:57 +05:30
|
|
|
*
|
2003-07-29 12:08:40 +05:30
|
|
|
* Copyright (c) 1986-2003 may safely be consumed by a BSD or GPL license.
|
2001-07-13 01:56:32 +05:30
|
|
|
* Written by: Vladimir Oleynik <dzo@simtreas.ru>
|
2001-02-17 00:06:04 +05:30
|
|
|
*
|
|
|
|
* Used ideas:
|
|
|
|
* Adam Rogoyski <rogoyski@cs.utexas.edu>
|
|
|
|
* Dave Cinege <dcinege@psychosis.com>
|
|
|
|
* Jakub Jelinek (c) 1995
|
2003-07-29 12:08:40 +05:30
|
|
|
* Erik Andersen <andersen@codepoet.org> (Majorly adjusted for busybox)
|
2000-03-16 13:39:57 +05:30
|
|
|
*
|
2001-02-17 00:06:04 +05:30
|
|
|
* This code is 'as is' with no warranty.
|
2000-03-16 13:39:57 +05:30
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Usage and Known bugs:
|
|
|
|
Terminal key codes are not extensive, and more will probably
|
|
|
|
need to be added. This version was created on Debian GNU/Linux 2.x.
|
|
|
|
Delete, Backspace, Home, End, and the arrow keys were tested
|
|
|
|
to work in an Xterm and console. Ctrl-A also works as Home.
|
2001-01-27 02:12:23 +05:30
|
|
|
Ctrl-E also works as End.
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
Small bugs (simple effect):
|
|
|
|
- not true viewing if terminal size (x*y symbols) less
|
|
|
|
size (prompt + editor`s line + 2 symbols)
|
2001-03-14 04:27:56 +05:30
|
|
|
- not true viewing if length prompt less terminal width
|
2001-02-17 00:06:04 +05:30
|
|
|
*/
|
2001-01-27 02:12:23 +05:30
|
|
|
|
|
|
|
|
2001-02-20 11:44:08 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
2000-09-26 03:15:58 +05:30
|
|
|
#include "busybox.h"
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2004-01-14 15:04:51 +05:30
|
|
|
#include "../shell/cmdedit.h"
|
|
|
|
|
2004-01-22 18:12:23 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_LOCALE_SUPPORT
|
2001-04-10 04:18:12 +05:30
|
|
|
#define Isprint(c) isprint((c))
|
|
|
|
#else
|
|
|
|
#define Isprint(c) ( (c) >= ' ' && (c) != ((unsigned char)'\233') )
|
|
|
|
#endif
|
|
|
|
|
2004-01-03 17:37:32 +05:30
|
|
|
#ifdef TEST
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2003-12-24 01:54:51 +05:30
|
|
|
/* pretect redefined for test */
|
|
|
|
#undef CONFIG_FEATURE_COMMAND_EDITING
|
|
|
|
#undef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
|
|
|
|
#undef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
|
|
|
|
#undef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
|
|
|
|
#undef CONFIG_FEATURE_CLEAN_UP
|
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#define CONFIG_FEATURE_COMMAND_EDITING
|
|
|
|
#define CONFIG_FEATURE_COMMAND_TAB_COMPLETION
|
|
|
|
#define CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
|
|
|
|
#define CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
|
|
|
|
#define CONFIG_FEATURE_CLEAN_UP
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2006-01-25 17:23:47 +05:30
|
|
|
#endif /* TEST */
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
|
2001-02-20 12:12:29 +05:30
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#if defined(CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION) || defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
|
|
|
|
#define CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
|
2001-02-17 00:06:04 +05:30
|
|
|
#endif
|
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
|
2004-01-22 18:12:23 +05:30
|
|
|
#include "pwd_.h"
|
2006-01-25 17:23:47 +05:30
|
|
|
#endif /* advanced FEATURES */
|
2001-02-02 04:13:49 +05:30
|
|
|
|
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
/* Maximum length of the linked list for the command line history */
|
2002-12-04 04:15:46 +05:30
|
|
|
#ifndef CONFIG_FEATURE_COMMAND_HISTORY
|
|
|
|
#define MAX_HISTORY 15
|
|
|
|
#else
|
|
|
|
#define MAX_HISTORY CONFIG_FEATURE_COMMAND_HISTORY
|
|
|
|
#endif
|
|
|
|
|
2002-11-27 14:59:49 +05:30
|
|
|
#if MAX_HISTORY < 1
|
|
|
|
#warning cmdedit: You set MAX_HISTORY < 1. The history algorithm switched off.
|
|
|
|
#else
|
|
|
|
static char *history[MAX_HISTORY+1]; /* history + current */
|
|
|
|
/* saved history lines */
|
|
|
|
static int n_history;
|
|
|
|
/* current pointer to history line */
|
|
|
|
static int cur_history;
|
|
|
|
#endif
|
2000-04-21 06:56:49 +05:30
|
|
|
|
2001-06-26 07:36:08 +05:30
|
|
|
#include <termios.h>
|
|
|
|
#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
|
|
|
|
#define getTermSettings(fd,argp) tcgetattr(fd, argp);
|
2000-04-21 06:56:49 +05:30
|
|
|
|
|
|
|
/* Current termio and the previous termio before starting sh */
|
2000-11-07 12:22:13 +05:30
|
|
|
static struct termios initial_settings, new_settings;
|
2000-05-20 06:10:08 +05:30
|
|
|
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
static
|
2003-07-28 15:26:35 +05:30
|
|
|
volatile int cmdedit_termw = 80; /* actual terminal width */
|
2001-01-27 02:12:23 +05:30
|
|
|
static
|
2003-07-28 15:26:35 +05:30
|
|
|
volatile int handlers_sets = 0; /* Set next bites: */
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
enum {
|
2003-07-28 15:26:35 +05:30
|
|
|
SET_ATEXIT = 1, /* when atexit() has been called
|
2001-03-14 04:27:56 +05:30
|
|
|
and get euid,uid,gid to fast compare */
|
2001-07-13 01:56:32 +05:30
|
|
|
SET_WCHG_HANDLERS = 2, /* winchg signal handler */
|
|
|
|
SET_RESET_TERM = 4, /* if the terminal needs to be reset upon exit */
|
2001-01-27 02:12:23 +05:30
|
|
|
};
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
static int cmdedit_x; /* real x terminal position */
|
|
|
|
static int cmdedit_y; /* pseudoreal y terminal position */
|
|
|
|
static int cmdedit_prmt_len; /* lenght prompt without colores string */
|
2000-12-19 01:55:50 +05:30
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
static int cursor; /* required global for signal handler */
|
|
|
|
static int len; /* --- "" - - "" - -"- --""-- --""--- */
|
|
|
|
static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
|
2001-02-17 00:06:04 +05:30
|
|
|
static
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
|
2001-02-17 00:06:04 +05:30
|
|
|
const
|
|
|
|
#endif
|
2003-07-28 15:26:35 +05:30
|
|
|
char *cmdedit_prompt; /* --- "" - - "" - -"- --""-- --""--- */
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
|
2001-02-17 00:06:04 +05:30
|
|
|
static char *user_buf = "";
|
|
|
|
static char *home_pwd_buf = "";
|
|
|
|
static int my_euid;
|
|
|
|
#endif
|
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
|
2002-11-27 14:59:49 +05:30
|
|
|
static char *hostname_buf;
|
2001-02-17 00:06:04 +05:30
|
|
|
static int num_ok_lines = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
|
2001-02-17 00:06:04 +05:30
|
|
|
static int my_euid;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int my_uid;
|
|
|
|
static int my_gid;
|
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
static void cmdedit_setwidth(int w, int redraw_flg);
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
static void win_changed(int nsig)
|
2001-01-04 16:38:45 +05:30
|
|
|
{
|
2003-07-28 15:26:35 +05:30
|
|
|
static sighandler_t previous_SIGWINCH_handler; /* for reset */
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
/* emulate || signal call */
|
|
|
|
if (nsig == -SIGWINCH || nsig == SIGWINCH) {
|
2003-09-15 14:03:45 +05:30
|
|
|
int width = 0;
|
|
|
|
get_terminal_width_height(0, &width, NULL);
|
|
|
|
cmdedit_setwidth(width, nsig == SIGWINCH);
|
2001-01-04 16:38:45 +05:30
|
|
|
}
|
2001-01-31 03:53:17 +05:30
|
|
|
/* Unix not all standart in recall signal */
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
if (nsig == -SIGWINCH) /* save previous handler */
|
2001-01-27 02:12:23 +05:30
|
|
|
previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
|
2003-07-28 15:26:35 +05:30
|
|
|
else if (nsig == SIGWINCH) /* signaled called handler */
|
|
|
|
signal(SIGWINCH, win_changed); /* set for next call */
|
2006-01-25 17:23:47 +05:30
|
|
|
else /* nsig == 0 */
|
2001-02-17 00:06:04 +05:30
|
|
|
/* set previous handler */
|
2003-07-28 15:26:35 +05:30
|
|
|
signal(SIGWINCH, previous_SIGWINCH_handler); /* reset */
|
2001-01-04 16:38:45 +05:30
|
|
|
}
|
2000-04-13 06:48:56 +05:30
|
|
|
|
2001-01-04 16:38:45 +05:30
|
|
|
static void cmdedit_reset_term(void)
|
2000-03-16 13:39:57 +05:30
|
|
|
{
|
2001-02-17 00:06:04 +05:30
|
|
|
if ((handlers_sets & SET_RESET_TERM) != 0) {
|
2001-04-10 04:18:12 +05:30
|
|
|
/* sparc and other have broken termios support: use old termio handling. */
|
2004-03-27 15:32:48 +05:30
|
|
|
setTermSettings(STDIN_FILENO, (void *) &initial_settings);
|
2001-01-27 02:12:23 +05:30
|
|
|
handlers_sets &= ~SET_RESET_TERM;
|
|
|
|
}
|
2001-02-17 00:06:04 +05:30
|
|
|
if ((handlers_sets & SET_WCHG_HANDLERS) != 0) {
|
2001-01-27 02:12:23 +05:30
|
|
|
/* reset SIGWINCH handler to previous (default) */
|
|
|
|
win_changed(0);
|
|
|
|
handlers_sets &= ~SET_WCHG_HANDLERS;
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
2000-03-16 13:39:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
/* special for recount position for scroll and remove terminal margin effect */
|
2001-02-17 00:06:04 +05:30
|
|
|
static void cmdedit_set_out_char(int next_char)
|
|
|
|
{
|
|
|
|
|
2001-03-16 02:21:09 +05:30
|
|
|
int c = (int)((unsigned char) command_ps[cursor]);
|
2001-02-17 00:06:04 +05:30
|
|
|
|
|
|
|
if (c == 0)
|
2003-07-28 15:26:35 +05:30
|
|
|
c = ' '; /* destroy end char? */
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
|
2003-07-28 15:26:35 +05:30
|
|
|
if (!Isprint(c)) { /* Inverse put non-printable characters */
|
2001-03-16 02:21:09 +05:30
|
|
|
if (c >= 128)
|
2001-02-17 00:06:04 +05:30
|
|
|
c -= 128;
|
2001-03-16 02:21:09 +05:30
|
|
|
if (c < ' ')
|
2001-02-17 00:06:04 +05:30
|
|
|
c += '@';
|
|
|
|
if (c == 127)
|
|
|
|
c = '?';
|
|
|
|
printf("\033[7m%c\033[0m", c);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
putchar(c);
|
|
|
|
if (++cmdedit_x >= cmdedit_termw) {
|
2001-01-27 02:12:23 +05:30
|
|
|
/* terminal is scrolled down */
|
|
|
|
cmdedit_y++;
|
2001-02-17 00:06:04 +05:30
|
|
|
cmdedit_x = 0;
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
if (!next_char)
|
2001-01-27 02:12:23 +05:30
|
|
|
next_char = ' ';
|
|
|
|
/* destroy "(auto)margin" */
|
|
|
|
putchar(next_char);
|
|
|
|
putchar('\b');
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
cursor++;
|
2000-03-16 13:39:57 +05:30
|
|
|
}
|
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
/* Move to end line. Bonus: rewrite line from cursor */
|
|
|
|
static void input_end(void)
|
|
|
|
{
|
|
|
|
while (cursor < len)
|
|
|
|
cmdedit_set_out_char(0);
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
2000-07-04 11:52:18 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
/* Go to the next line */
|
2001-02-17 00:06:04 +05:30
|
|
|
static void goto_new_line(void)
|
|
|
|
{
|
2001-01-27 02:12:23 +05:30
|
|
|
input_end();
|
2001-02-17 00:06:04 +05:30
|
|
|
if (cmdedit_x)
|
|
|
|
putchar('\n');
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
2000-07-04 11:52:18 +05:30
|
|
|
|
2000-03-19 10:58:55 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
static inline void out1str(const char *s)
|
|
|
|
{
|
2002-07-31 04:43:51 +05:30
|
|
|
if ( s )
|
|
|
|
fputs(s, stdout);
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
2003-07-29 12:08:40 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
static inline void beep(void)
|
|
|
|
{
|
|
|
|
putchar('\007');
|
|
|
|
}
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2004-04-14 23:21:38 +05:30
|
|
|
/* Move back one character */
|
2001-02-17 00:06:04 +05:30
|
|
|
/* special for slow terminal */
|
|
|
|
static void input_backward(int num)
|
2001-01-27 02:12:23 +05:30
|
|
|
{
|
2001-02-17 00:06:04 +05:30
|
|
|
if (num > cursor)
|
|
|
|
num = cursor;
|
2003-07-28 15:26:35 +05:30
|
|
|
cursor -= num; /* new cursor (in command, not terminal) */
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
if (cmdedit_x >= num) { /* no to up line */
|
2001-02-17 00:06:04 +05:30
|
|
|
cmdedit_x -= num;
|
|
|
|
if (num < 4)
|
|
|
|
while (num-- > 0)
|
|
|
|
putchar('\b');
|
|
|
|
|
|
|
|
else
|
|
|
|
printf("\033[%dD", num);
|
|
|
|
} else {
|
|
|
|
int count_y;
|
|
|
|
|
|
|
|
if (cmdedit_x) {
|
2003-07-28 15:26:35 +05:30
|
|
|
putchar('\r'); /* back to first terminal pos. */
|
|
|
|
num -= cmdedit_x; /* set previous backward */
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
|
|
|
count_y = 1 + num / cmdedit_termw;
|
|
|
|
printf("\033[%dA", count_y);
|
|
|
|
cmdedit_y -= count_y;
|
|
|
|
/* require forward after uping */
|
|
|
|
cmdedit_x = cmdedit_termw * count_y - num;
|
2003-07-28 15:26:35 +05:30
|
|
|
printf("\033[%dC", cmdedit_x); /* set term cursor */
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void put_prompt(void)
|
|
|
|
{
|
2001-01-27 02:12:23 +05:30
|
|
|
out1str(cmdedit_prompt);
|
2003-07-28 15:26:35 +05:30
|
|
|
cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
|
2001-02-17 00:06:04 +05:30
|
|
|
cursor = 0;
|
2001-07-13 01:56:32 +05:30
|
|
|
cmdedit_y = 0; /* new quasireal y */
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
|
2001-03-14 04:27:56 +05:30
|
|
|
static void parse_prompt(const char *prmt_ptr)
|
2001-02-17 00:06:04 +05:30
|
|
|
{
|
2001-03-14 04:27:56 +05:30
|
|
|
cmdedit_prompt = prmt_ptr;
|
|
|
|
cmdedit_prmt_len = strlen(prmt_ptr);
|
|
|
|
put_prompt();
|
|
|
|
}
|
|
|
|
#else
|
2001-02-17 00:06:04 +05:30
|
|
|
static void parse_prompt(const char *prmt_ptr)
|
|
|
|
{
|
|
|
|
int prmt_len = 0;
|
2005-09-05 21:16:26 +05:30
|
|
|
size_t cur_prmt_len = 0;
|
2001-04-10 04:18:12 +05:30
|
|
|
char flg_not_length = '[';
|
|
|
|
char *prmt_mem_ptr = xcalloc(1, 1);
|
|
|
|
char *pwd_buf = xgetcwd(0);
|
|
|
|
char buf2[PATH_MAX + 1];
|
|
|
|
char buf[2];
|
|
|
|
char c;
|
|
|
|
char *pbuf;
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2001-05-11 22:28:46 +05:30
|
|
|
if (!pwd_buf) {
|
2003-03-19 14:43:01 +05:30
|
|
|
pwd_buf=(char *)bb_msg_unknown;
|
2001-05-11 22:28:46 +05:30
|
|
|
}
|
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
while (*prmt_ptr) {
|
2001-04-10 04:18:12 +05:30
|
|
|
pbuf = buf;
|
|
|
|
pbuf[1] = 0;
|
2001-02-17 00:06:04 +05:30
|
|
|
c = *prmt_ptr++;
|
|
|
|
if (c == '\\') {
|
2001-04-10 04:18:12 +05:30
|
|
|
const char *cp = prmt_ptr;
|
|
|
|
int l;
|
2003-07-28 15:26:35 +05:30
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
c = bb_process_escape_sequence(&prmt_ptr);
|
2001-04-10 04:18:12 +05:30
|
|
|
if(prmt_ptr==cp) {
|
|
|
|
if (*cp == 0)
|
2001-02-17 00:06:04 +05:30
|
|
|
break;
|
2001-04-10 04:18:12 +05:30
|
|
|
c = *prmt_ptr++;
|
|
|
|
switch (c) {
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
|
2001-04-10 04:18:12 +05:30
|
|
|
case 'u':
|
|
|
|
pbuf = user_buf;
|
|
|
|
break;
|
2003-07-28 15:26:35 +05:30
|
|
|
#endif
|
2001-04-10 04:18:12 +05:30
|
|
|
case 'h':
|
|
|
|
pbuf = hostname_buf;
|
2002-11-27 14:59:49 +05:30
|
|
|
if (pbuf == 0) {
|
2001-04-10 04:18:12 +05:30
|
|
|
pbuf = xcalloc(256, 1);
|
|
|
|
if (gethostname(pbuf, 255) < 0) {
|
|
|
|
strcpy(pbuf, "?");
|
2001-02-17 00:06:04 +05:30
|
|
|
} else {
|
2001-04-10 04:18:12 +05:30
|
|
|
char *s = strchr(pbuf, '.');
|
2001-02-17 00:06:04 +05:30
|
|
|
|
|
|
|
if (s)
|
|
|
|
*s = 0;
|
|
|
|
}
|
2001-04-10 04:18:12 +05:30
|
|
|
hostname_buf = pbuf;
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
2001-04-10 04:18:12 +05:30
|
|
|
break;
|
|
|
|
case '$':
|
2001-02-17 00:06:04 +05:30
|
|
|
c = my_euid == 0 ? '#' : '$';
|
|
|
|
break;
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
|
2001-04-10 04:18:12 +05:30
|
|
|
case 'w':
|
|
|
|
pbuf = pwd_buf;
|
|
|
|
l = strlen(home_pwd_buf);
|
|
|
|
if (home_pwd_buf[0] != 0 &&
|
|
|
|
strncmp(home_pwd_buf, pbuf, l) == 0 &&
|
|
|
|
(pbuf[l]=='/' || pbuf[l]=='\0') &&
|
|
|
|
strlen(pwd_buf+l)<PATH_MAX) {
|
|
|
|
pbuf = buf2;
|
|
|
|
*pbuf = '~';
|
|
|
|
strcpy(pbuf+1, pwd_buf+l);
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
2001-04-10 04:18:12 +05:30
|
|
|
break;
|
2003-07-28 15:26:35 +05:30
|
|
|
#endif
|
2001-04-10 04:18:12 +05:30
|
|
|
case 'W':
|
|
|
|
pbuf = pwd_buf;
|
|
|
|
cp = strrchr(pbuf,'/');
|
|
|
|
if ( (cp != NULL) && (cp != pbuf) )
|
|
|
|
pbuf += (cp-pbuf)+1;
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
snprintf(pbuf = buf2, sizeof(buf2), "%d", num_ok_lines);
|
|
|
|
break;
|
|
|
|
case 'e': case 'E': /* \e \E = \033 */
|
2001-02-17 00:06:04 +05:30
|
|
|
c = '\033';
|
|
|
|
break;
|
2003-07-28 15:26:35 +05:30
|
|
|
case 'x': case 'X':
|
2001-02-17 00:06:04 +05:30
|
|
|
for (l = 0; l < 3;) {
|
2001-04-10 04:18:12 +05:30
|
|
|
int h;
|
|
|
|
buf2[l++] = *prmt_ptr;
|
|
|
|
buf2[l] = 0;
|
|
|
|
h = strtol(buf2, &pbuf, 16);
|
|
|
|
if (h > UCHAR_MAX || (pbuf - buf2) < l) {
|
2001-02-17 00:06:04 +05:30
|
|
|
l--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prmt_ptr++;
|
|
|
|
}
|
2001-04-10 04:18:12 +05:30
|
|
|
buf2[l] = 0;
|
|
|
|
c = (char)strtol(buf2, 0, 16);
|
|
|
|
if(c==0)
|
|
|
|
c = '?';
|
|
|
|
pbuf = buf;
|
2001-02-17 00:06:04 +05:30
|
|
|
break;
|
2001-04-10 04:18:12 +05:30
|
|
|
case '[': case ']':
|
2001-02-17 00:06:04 +05:30
|
|
|
if (c == flg_not_length) {
|
|
|
|
flg_not_length = flg_not_length == '[' ? ']' : '[';
|
|
|
|
continue;
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
2001-02-17 00:06:04 +05:30
|
|
|
break;
|
2001-04-10 04:18:12 +05:30
|
|
|
}
|
2003-07-28 15:26:35 +05:30
|
|
|
}
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
2001-04-10 04:18:12 +05:30
|
|
|
if(pbuf == buf)
|
|
|
|
*pbuf = c;
|
2005-09-05 21:16:26 +05:30
|
|
|
cur_prmt_len = strlen(pbuf);
|
|
|
|
prmt_len += cur_prmt_len;
|
|
|
|
if (flg_not_length != ']')
|
|
|
|
cmdedit_prmt_len += cur_prmt_len;
|
2001-04-10 04:18:12 +05:30
|
|
|
prmt_mem_ptr = strcat(xrealloc(prmt_mem_ptr, prmt_len+1), pbuf);
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
2003-03-19 14:43:01 +05:30
|
|
|
if(pwd_buf!=(char *)bb_msg_unknown)
|
2001-07-02 21:06:57 +05:30
|
|
|
free(pwd_buf);
|
2001-03-14 04:27:56 +05:30
|
|
|
cmdedit_prompt = prmt_mem_ptr;
|
2001-02-17 00:06:04 +05:30
|
|
|
put_prompt();
|
|
|
|
}
|
2001-03-14 04:27:56 +05:30
|
|
|
#endif
|
2001-02-17 00:06:04 +05:30
|
|
|
|
|
|
|
|
2004-04-14 23:21:38 +05:30
|
|
|
/* draw prompt, editor line, and clear tail */
|
2001-02-17 00:06:04 +05:30
|
|
|
static void redraw(int y, int back_cursor)
|
|
|
|
{
|
2003-07-28 15:26:35 +05:30
|
|
|
if (y > 0) /* up to start y */
|
2001-02-17 00:06:04 +05:30
|
|
|
printf("\033[%dA", y);
|
|
|
|
putchar('\r');
|
|
|
|
put_prompt();
|
2003-07-28 15:26:35 +05:30
|
|
|
input_end(); /* rewrite */
|
|
|
|
printf("\033[J"); /* destroy tail after cursor */
|
2001-02-17 00:06:04 +05:30
|
|
|
input_backward(back_cursor);
|
2000-03-16 13:39:57 +05:30
|
|
|
}
|
|
|
|
|
2005-08-05 00:34:46 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
|
|
|
|
static char delbuf[BUFSIZ]; /* a place to store deleted characters */
|
|
|
|
static char *delp = delbuf;
|
2006-01-25 17:23:47 +05:30
|
|
|
static int newdelflag; /* whether delbuf should be reused yet */
|
2005-08-05 00:34:46 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Delete the char in front of the cursor, optionally saving it
|
|
|
|
* for later putback */
|
|
|
|
static void input_delete(int save)
|
2000-04-12 23:19:52 +05:30
|
|
|
{
|
2001-01-27 02:12:23 +05:30
|
|
|
int j = cursor;
|
2000-04-09 23:57:46 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
if (j == len)
|
2000-04-12 23:19:52 +05:30
|
|
|
return;
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2005-08-05 00:34:46 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
|
|
|
|
if (save) {
|
|
|
|
if (newdelflag) {
|
|
|
|
delp = delbuf;
|
|
|
|
newdelflag = 0;
|
|
|
|
}
|
|
|
|
if (delp - delbuf < BUFSIZ)
|
|
|
|
*delp++ = command_ps[j];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
strcpy(command_ps + j, command_ps + j + 1);
|
2001-01-27 02:12:23 +05:30
|
|
|
len--;
|
2005-08-05 00:34:46 +05:30
|
|
|
input_end(); /* rewrite new line */
|
2003-07-28 15:26:35 +05:30
|
|
|
cmdedit_set_out_char(0); /* destroy end char */
|
|
|
|
input_backward(cursor - j); /* back to old pos cursor */
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
|
|
|
|
2005-08-05 00:34:46 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
|
|
|
|
static void put(void)
|
|
|
|
{
|
|
|
|
int ocursor, j = delp - delbuf;
|
|
|
|
if (j == 0)
|
|
|
|
return;
|
|
|
|
ocursor = cursor;
|
|
|
|
/* open hole and then fill it */
|
|
|
|
memmove(command_ps + cursor + j, command_ps + cursor, len - cursor + 1);
|
|
|
|
strncpy(command_ps + cursor, delbuf, j);
|
|
|
|
len += j;
|
|
|
|
input_end(); /* rewrite new line */
|
|
|
|
input_backward(cursor-ocursor-j+1); /* at end of new text */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
/* Delete the char in back of the cursor */
|
|
|
|
static void input_backspace(void)
|
|
|
|
{
|
|
|
|
if (cursor > 0) {
|
2001-02-17 00:06:04 +05:30
|
|
|
input_backward(1);
|
2005-08-05 00:34:46 +05:30
|
|
|
input_delete(0);
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
2000-04-12 23:19:52 +05:30
|
|
|
|
|
|
|
|
2004-04-14 23:21:38 +05:30
|
|
|
/* Move forward one character */
|
2001-01-27 02:12:23 +05:30
|
|
|
static void input_forward(void)
|
|
|
|
{
|
2001-02-17 00:06:04 +05:30
|
|
|
if (cursor < len)
|
|
|
|
cmdedit_set_out_char(command_ps[cursor + 1]);
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
static void cmdedit_setwidth(int w, int redraw_flg)
|
2000-04-12 23:19:52 +05:30
|
|
|
{
|
2001-02-17 00:06:04 +05:30
|
|
|
cmdedit_termw = cmdedit_prmt_len + 2;
|
2001-02-17 01:39:17 +05:30
|
|
|
if (w <= cmdedit_termw) {
|
|
|
|
cmdedit_termw = cmdedit_termw % w;
|
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
if (w > cmdedit_termw) {
|
|
|
|
cmdedit_termw = w;
|
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
if (redraw_flg) {
|
|
|
|
/* new y for current cursor */
|
|
|
|
int new_y = (cursor + cmdedit_prmt_len) / w;
|
2001-01-27 02:12:23 +05:30
|
|
|
|
|
|
|
/* redraw */
|
2001-02-17 00:06:04 +05:30
|
|
|
redraw((new_y >= cmdedit_y ? new_y : cmdedit_y), len - cursor);
|
|
|
|
fflush(stdout);
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
2003-07-28 15:26:35 +05:30
|
|
|
}
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
|
|
|
|
2001-07-13 01:56:32 +05:30
|
|
|
static void cmdedit_init(void)
|
2001-01-27 02:12:23 +05:30
|
|
|
{
|
2001-03-19 23:18:55 +05:30
|
|
|
cmdedit_prmt_len = 0;
|
2001-02-17 00:06:04 +05:30
|
|
|
if ((handlers_sets & SET_WCHG_HANDLERS) == 0) {
|
|
|
|
/* emulate usage handler to set handler and call yours work */
|
2001-01-27 02:12:23 +05:30
|
|
|
win_changed(-SIGWINCH);
|
|
|
|
handlers_sets |= SET_WCHG_HANDLERS;
|
|
|
|
}
|
2000-04-12 23:19:52 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
if ((handlers_sets & SET_ATEXIT) == 0) {
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
|
2001-02-17 00:06:04 +05:30
|
|
|
struct passwd *entry;
|
|
|
|
|
|
|
|
my_euid = geteuid();
|
|
|
|
entry = getpwuid(my_euid);
|
|
|
|
if (entry) {
|
2003-03-19 14:43:01 +05:30
|
|
|
user_buf = bb_xstrdup(entry->pw_name);
|
|
|
|
home_pwd_buf = bb_xstrdup(entry->pw_dir);
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifndef CONFIG_FEATURE_GETUSERNAME_AND_HOMEDIR
|
2001-02-17 00:06:04 +05:30
|
|
|
my_euid = geteuid();
|
|
|
|
#endif
|
|
|
|
my_uid = getuid();
|
|
|
|
my_gid = getgid();
|
2003-07-28 15:26:35 +05:30
|
|
|
#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
|
2001-01-27 02:12:23 +05:30
|
|
|
handlers_sets |= SET_ATEXIT;
|
2003-07-28 15:26:35 +05:30
|
|
|
atexit(cmdedit_reset_term); /* be sure to do this only once */
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
|
|
|
}
|
2000-04-12 23:19:52 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2006-01-25 17:23:47 +05:30
|
|
|
static char **matches;
|
|
|
|
static int num_matches;
|
|
|
|
static char *add_char_to_match;
|
|
|
|
|
|
|
|
static void add_match(char *matched, int add_char)
|
|
|
|
{
|
|
|
|
int nm = num_matches;
|
|
|
|
int nm1 = nm + 1;
|
|
|
|
|
|
|
|
matches = xrealloc(matches, nm1 * sizeof(char *));
|
|
|
|
add_char_to_match = xrealloc(add_char_to_match, nm1);
|
|
|
|
matches[nm] = matched;
|
|
|
|
add_char_to_match[nm] = (char)add_char;
|
|
|
|
num_matches++;
|
|
|
|
}
|
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
static int is_execute(const struct stat *st)
|
|
|
|
{
|
|
|
|
if ((!my_euid && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) ||
|
|
|
|
(my_uid == st->st_uid && (st->st_mode & S_IXUSR)) ||
|
|
|
|
(my_gid == st->st_gid && (st->st_mode & S_IXGRP)) ||
|
|
|
|
(st->st_mode & S_IXOTH)) return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2006-01-25 17:23:47 +05:30
|
|
|
static void username_tab_completion(char *ud, char *with_shash_flg)
|
2000-03-17 06:42:41 +05:30
|
|
|
{
|
2001-02-17 00:06:04 +05:30
|
|
|
struct passwd *entry;
|
|
|
|
int userlen;
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
ud++; /* ~user/... to user/... */
|
2001-02-17 00:06:04 +05:30
|
|
|
userlen = strlen(ud);
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2006-01-25 17:23:47 +05:30
|
|
|
if (with_shash_flg) { /* "~/..." or "~user/..." */
|
2001-02-17 00:06:04 +05:30
|
|
|
char *sav_ud = ud - 1;
|
|
|
|
char *home = 0;
|
2006-01-25 17:23:47 +05:30
|
|
|
char *temp;
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
if (*ud == '/') { /* "~/..." */
|
2001-02-17 00:06:04 +05:30
|
|
|
home = home_pwd_buf;
|
|
|
|
} else {
|
|
|
|
/* "~user/..." */
|
|
|
|
temp = strchr(ud, '/');
|
2003-07-28 15:26:35 +05:30
|
|
|
*temp = 0; /* ~user\0 */
|
2001-02-17 00:06:04 +05:30
|
|
|
entry = getpwnam(ud);
|
2003-07-28 15:26:35 +05:30
|
|
|
*temp = '/'; /* restore ~user/... */
|
2001-02-17 00:06:04 +05:30
|
|
|
ud = temp;
|
|
|
|
if (entry)
|
|
|
|
home = entry->pw_dir;
|
|
|
|
}
|
|
|
|
if (home) {
|
|
|
|
if ((userlen + strlen(home) + 1) < BUFSIZ) {
|
2003-07-28 15:26:35 +05:30
|
|
|
char temp2[BUFSIZ]; /* argument size */
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
/* /home/user/... */
|
|
|
|
sprintf(temp2, "%s%s", home, ud);
|
|
|
|
strcpy(sav_ud, temp2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* "~[^/]*" */
|
|
|
|
setpwent();
|
|
|
|
|
|
|
|
while ((entry = getpwent()) != NULL) {
|
|
|
|
/* Null usernames should result in all users as possible completions. */
|
|
|
|
if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
|
2006-01-25 17:23:47 +05:30
|
|
|
add_match(bb_xasprintf("~%s", entry->pw_name), '/');
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endpwent();
|
|
|
|
}
|
2000-03-17 06:42:41 +05:30
|
|
|
}
|
2003-07-28 15:26:35 +05:30
|
|
|
#endif /* CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION */
|
2001-01-27 02:12:23 +05:30
|
|
|
|
|
|
|
enum {
|
2001-02-17 00:06:04 +05:30
|
|
|
FIND_EXE_ONLY = 0,
|
|
|
|
FIND_DIR_ONLY = 1,
|
2001-01-27 02:12:23 +05:30
|
|
|
FIND_FILE_ONLY = 2,
|
|
|
|
};
|
2000-03-19 16:16:06 +05:30
|
|
|
|
2004-01-14 15:04:51 +05:30
|
|
|
#ifdef CONFIG_ASH
|
|
|
|
const char *cmdedit_path_lookup;
|
|
|
|
#else
|
|
|
|
#define cmdedit_path_lookup getenv("PATH")
|
|
|
|
#endif
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
static int path_parse(char ***p, int flags)
|
|
|
|
{
|
2001-02-17 00:06:04 +05:30
|
|
|
int npth;
|
2004-01-14 15:04:51 +05:30
|
|
|
const char *tmp;
|
|
|
|
const char *pth;
|
2001-01-27 02:12:23 +05:30
|
|
|
|
|
|
|
/* if not setenv PATH variable, to search cur dir "." */
|
2004-01-14 15:04:51 +05:30
|
|
|
if (flags != FIND_EXE_ONLY || (pth = cmdedit_path_lookup) == 0 ||
|
2001-02-17 00:06:04 +05:30
|
|
|
/* PATH=<empty> or PATH=:<empty> */
|
|
|
|
*pth == 0 || (*pth == ':' && *(pth + 1) == 0)) {
|
2001-01-27 02:12:23 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = pth;
|
2001-02-17 00:06:04 +05:30
|
|
|
npth = 0;
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
for (;;) {
|
2003-07-28 15:26:35 +05:30
|
|
|
npth++; /* count words is + 1 count ':' */
|
2001-01-27 02:12:23 +05:30
|
|
|
tmp = strchr(tmp, ':');
|
2001-02-17 00:06:04 +05:30
|
|
|
if (tmp) {
|
|
|
|
if (*++tmp == 0)
|
2003-07-28 15:26:35 +05:30
|
|
|
break; /* :<empty> */
|
2001-02-17 00:06:04 +05:30
|
|
|
} else
|
2001-01-27 02:12:23 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
*p = xmalloc(npth * sizeof(char *));
|
2001-01-27 02:12:23 +05:30
|
|
|
|
|
|
|
tmp = pth;
|
2003-03-19 14:43:01 +05:30
|
|
|
(*p)[0] = bb_xstrdup(tmp);
|
2003-07-28 15:26:35 +05:30
|
|
|
npth = 1; /* count words is + 1 count ':' */
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
for (;;) {
|
2001-01-27 02:12:23 +05:30
|
|
|
tmp = strchr(tmp, ':');
|
2001-02-17 00:06:04 +05:30
|
|
|
if (tmp) {
|
2003-07-28 15:26:35 +05:30
|
|
|
(*p)[0][(tmp - pth)] = 0; /* ':' -> '\0' */
|
2001-02-17 00:06:04 +05:30
|
|
|
if (*++tmp == 0)
|
2003-07-28 15:26:35 +05:30
|
|
|
break; /* :<empty> */
|
2001-01-27 02:12:23 +05:30
|
|
|
} else
|
|
|
|
break;
|
2003-07-28 15:26:35 +05:30
|
|
|
(*p)[npth++] = &(*p)[0][(tmp - pth)]; /* p[next]=p[0][&'\0'+1] */
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return npth;
|
|
|
|
}
|
|
|
|
|
2006-01-25 17:23:47 +05:30
|
|
|
static char *add_quote_for_spec_chars(char *found, int add)
|
2000-03-17 06:42:41 +05:30
|
|
|
{
|
2001-02-17 00:06:04 +05:30
|
|
|
int l = 0;
|
|
|
|
char *s = xmalloc((strlen(found) + 1) * 2);
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
while (*found) {
|
|
|
|
if (strchr(" `\"#$%^&*()=+{}[]:;\'|\\<>", *found))
|
|
|
|
s[l++] = '\\';
|
|
|
|
s[l++] = *found++;
|
|
|
|
}
|
2006-01-25 17:23:47 +05:30
|
|
|
if(add)
|
|
|
|
s[l++] = (char)add;
|
2001-02-17 00:06:04 +05:30
|
|
|
s[l] = 0;
|
|
|
|
return s;
|
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2006-01-25 17:23:47 +05:30
|
|
|
static void exe_n_cwd_tab_completion(char *command, int type)
|
2001-02-17 00:06:04 +05:30
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *next;
|
|
|
|
char dirbuf[BUFSIZ];
|
|
|
|
struct stat st;
|
|
|
|
char *path1[1];
|
|
|
|
char **paths = path1;
|
|
|
|
int npaths;
|
|
|
|
int i;
|
2001-04-10 04:18:12 +05:30
|
|
|
char *found;
|
2001-02-17 00:06:04 +05:30
|
|
|
char *pfind = strrchr(command, '/');
|
|
|
|
|
|
|
|
path1[0] = ".";
|
|
|
|
|
|
|
|
if (pfind == NULL) {
|
2001-01-27 02:12:23 +05:30
|
|
|
/* no dir, if flags==EXE_ONLY - get paths, else "." */
|
|
|
|
npaths = path_parse(&paths, type);
|
2001-02-17 00:06:04 +05:30
|
|
|
pfind = command;
|
2001-01-27 02:12:23 +05:30
|
|
|
} else {
|
|
|
|
/* with dir */
|
2001-02-17 00:06:04 +05:30
|
|
|
/* save for change */
|
|
|
|
strcpy(dirbuf, command);
|
|
|
|
/* set dir only */
|
|
|
|
dirbuf[(pfind - command) + 1] = 0;
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
|
2003-07-28 15:26:35 +05:30
|
|
|
if (dirbuf[0] == '~') /* ~/... or ~user/... */
|
2006-01-25 17:23:47 +05:30
|
|
|
username_tab_completion(dirbuf, dirbuf);
|
2001-02-17 00:06:04 +05:30
|
|
|
#endif
|
|
|
|
/* "strip" dirname in command */
|
|
|
|
pfind++;
|
2001-01-27 02:12:23 +05:30
|
|
|
|
|
|
|
paths[0] = dirbuf;
|
2003-07-28 15:26:35 +05:30
|
|
|
npaths = 1; /* only 1 dir */
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
2000-03-19 16:16:06 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
for (i = 0; i < npaths; i++) {
|
2000-03-19 16:16:06 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
dir = opendir(paths[i]);
|
2003-07-28 15:26:35 +05:30
|
|
|
if (!dir) /* Don't print an error */
|
2001-02-17 00:06:04 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
while ((next = readdir(dir)) != NULL) {
|
|
|
|
char *str_found = next->d_name;
|
2006-01-25 17:23:47 +05:30
|
|
|
int add_chr = 0;
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
/* matched ? */
|
2001-02-17 00:06:04 +05:30
|
|
|
if (strncmp(str_found, pfind, strlen(pfind)))
|
2001-01-27 02:12:23 +05:30
|
|
|
continue;
|
|
|
|
/* not see .name without .match */
|
2001-02-17 00:06:04 +05:30
|
|
|
if (*str_found == '.' && *pfind == 0) {
|
|
|
|
if (*paths[i] == '/' && paths[i][1] == 0
|
2003-07-28 15:26:35 +05:30
|
|
|
&& str_found[1] == 0) str_found = ""; /* only "/" */
|
2001-02-17 00:06:04 +05:30
|
|
|
else
|
2001-01-27 02:12:23 +05:30
|
|
|
continue;
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
2001-04-10 04:18:12 +05:30
|
|
|
found = concat_path_file(paths[i], str_found);
|
2001-02-17 00:06:04 +05:30
|
|
|
/* hmm, remover in progress? */
|
2003-07-28 15:26:35 +05:30
|
|
|
if (stat(found, &st) < 0)
|
2001-04-10 04:18:12 +05:30
|
|
|
goto cont;
|
2001-02-17 00:06:04 +05:30
|
|
|
/* find with dirs ? */
|
|
|
|
if (paths[i] != dirbuf)
|
2003-07-28 15:26:35 +05:30
|
|
|
strcpy(found, next->d_name); /* only name */
|
2001-01-27 02:12:23 +05:30
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2001-02-17 00:06:04 +05:30
|
|
|
/* name is directory */
|
2006-01-25 17:23:47 +05:30
|
|
|
char *e = found + strlen(found) - 1;
|
|
|
|
|
|
|
|
add_chr = '/';
|
|
|
|
if(*e == '/')
|
|
|
|
*e = '\0';
|
2001-01-27 02:12:23 +05:30
|
|
|
} else {
|
2001-02-17 00:06:04 +05:30
|
|
|
/* not put found file if search only dirs for cd */
|
2003-07-28 15:26:35 +05:30
|
|
|
if (type == FIND_DIR_ONLY)
|
2001-04-10 04:18:12 +05:30
|
|
|
goto cont;
|
2001-02-17 00:06:04 +05:30
|
|
|
if (type == FIND_FILE_ONLY ||
|
2001-12-21 04:43:26 +05:30
|
|
|
(type == FIND_EXE_ONLY && is_execute(&st)))
|
2006-01-25 17:23:47 +05:30
|
|
|
add_chr = ' ';
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
/* Add it to the list */
|
2006-01-25 17:23:47 +05:30
|
|
|
add_match(found, add_chr);
|
|
|
|
continue;
|
2001-04-10 04:18:12 +05:30
|
|
|
cont:
|
|
|
|
free(found);
|
2000-03-19 16:16:06 +05:30
|
|
|
}
|
2001-02-17 00:06:04 +05:30
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
if (paths != path1) {
|
2003-07-28 15:26:35 +05:30
|
|
|
free(paths[0]); /* allocated memory only in first member */
|
2001-02-17 00:06:04 +05:30
|
|
|
free(paths);
|
2000-03-19 16:16:06 +05:30
|
|
|
}
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
|
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
#define QUOT (UCHAR_MAX+1)
|
2000-04-12 23:19:52 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
#define collapse_pos(is, in) { \
|
2005-07-20 02:11:06 +05:30
|
|
|
memmove(int_buf+(is), int_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); \
|
|
|
|
memmove(pos_buf+(is), pos_buf+(in), (BUFSIZ+1-(is)-(in))*sizeof(int)); }
|
2000-04-12 23:19:52 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
static int find_match(char *matchBuf, int *len_with_quotes)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int command_mode;
|
|
|
|
int c, c2;
|
|
|
|
int int_buf[BUFSIZ + 1];
|
|
|
|
int pos_buf[BUFSIZ + 1];
|
|
|
|
|
|
|
|
/* set to integer dimension characters and own positions */
|
|
|
|
for (i = 0;; i++) {
|
|
|
|
int_buf[i] = (int) ((unsigned char) matchBuf[i]);
|
|
|
|
if (int_buf[i] == 0) {
|
2003-07-28 15:26:35 +05:30
|
|
|
pos_buf[i] = -1; /* indicator end line */
|
2001-02-17 00:06:04 +05:30
|
|
|
break;
|
|
|
|
} else
|
|
|
|
pos_buf[i] = i;
|
|
|
|
}
|
2000-04-12 23:19:52 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
/* mask \+symbol and convert '\t' to ' ' */
|
|
|
|
for (i = j = 0; matchBuf[i]; i++, j++)
|
|
|
|
if (matchBuf[i] == '\\') {
|
|
|
|
collapse_pos(j, j + 1);
|
|
|
|
int_buf[j] |= QUOT;
|
|
|
|
i++;
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
|
2003-07-28 15:26:35 +05:30
|
|
|
if (matchBuf[i] == '\t') /* algorithm equivalent */
|
2001-02-17 00:06:04 +05:30
|
|
|
int_buf[j] = ' ' | QUOT;
|
|
|
|
#endif
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
|
2001-02-17 00:06:04 +05:30
|
|
|
else if (matchBuf[i] == '\t')
|
|
|
|
int_buf[j] = ' ';
|
|
|
|
#endif
|
2000-04-12 23:19:52 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
/* mask "symbols" or 'symbols' */
|
|
|
|
c2 = 0;
|
|
|
|
for (i = 0; int_buf[i]; i++) {
|
|
|
|
c = int_buf[i];
|
|
|
|
if (c == '\'' || c == '"') {
|
|
|
|
if (c2 == 0)
|
|
|
|
c2 = c;
|
|
|
|
else {
|
|
|
|
if (c == c2)
|
|
|
|
c2 = 0;
|
|
|
|
else
|
|
|
|
int_buf[i] |= QUOT;
|
|
|
|
}
|
|
|
|
} else if (c2 != 0 && c != '$')
|
|
|
|
int_buf[i] |= QUOT;
|
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
/* skip commands with arguments if line have commands delimiters */
|
|
|
|
/* ';' ';;' '&' '|' '&&' '||' but `>&' `<&' `>|' */
|
|
|
|
for (i = 0; int_buf[i]; i++) {
|
|
|
|
c = int_buf[i];
|
|
|
|
c2 = int_buf[i + 1];
|
|
|
|
j = i ? int_buf[i - 1] : -1;
|
|
|
|
command_mode = 0;
|
|
|
|
if (c == ';' || c == '&' || c == '|') {
|
|
|
|
command_mode = 1 + (c == c2);
|
|
|
|
if (c == '&') {
|
|
|
|
if (j == '>' || j == '<')
|
|
|
|
command_mode = 0;
|
|
|
|
} else if (c == '|' && j == '>')
|
|
|
|
command_mode = 0;
|
|
|
|
}
|
|
|
|
if (command_mode) {
|
|
|
|
collapse_pos(0, i + command_mode);
|
2003-07-28 15:26:35 +05:30
|
|
|
i = -1; /* hack incremet */
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
/* collapse `command...` */
|
|
|
|
for (i = 0; int_buf[i]; i++)
|
|
|
|
if (int_buf[i] == '`') {
|
|
|
|
for (j = i + 1; int_buf[j]; j++)
|
|
|
|
if (int_buf[j] == '`') {
|
|
|
|
collapse_pos(i, j + 1);
|
|
|
|
j = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (j) {
|
|
|
|
/* not found close ` - command mode, collapse all previous */
|
|
|
|
collapse_pos(0, i + 1);
|
|
|
|
break;
|
|
|
|
} else
|
2003-07-28 15:26:35 +05:30
|
|
|
i--; /* hack incremet */
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
/* collapse (command...(command...)...) or {command...{command...}...} */
|
2006-01-25 17:23:47 +05:30
|
|
|
c = 0; /* "recursive" level */
|
2001-02-17 00:06:04 +05:30
|
|
|
c2 = 0;
|
|
|
|
for (i = 0; int_buf[i]; i++)
|
|
|
|
if (int_buf[i] == '(' || int_buf[i] == '{') {
|
|
|
|
if (int_buf[i] == '(')
|
|
|
|
c++;
|
|
|
|
else
|
|
|
|
c2++;
|
|
|
|
collapse_pos(0, i + 1);
|
2003-07-28 15:26:35 +05:30
|
|
|
i = -1; /* hack incremet */
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
|
|
|
for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++)
|
|
|
|
if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) {
|
|
|
|
if (int_buf[i] == ')')
|
|
|
|
c--;
|
|
|
|
else
|
|
|
|
c2--;
|
|
|
|
collapse_pos(0, i + 1);
|
2003-07-28 15:26:35 +05:30
|
|
|
i = -1; /* hack incremet */
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
2000-04-12 23:19:52 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
/* skip first not quote space */
|
|
|
|
for (i = 0; int_buf[i]; i++)
|
|
|
|
if (int_buf[i] != ' ')
|
|
|
|
break;
|
|
|
|
if (i)
|
|
|
|
collapse_pos(0, i);
|
|
|
|
|
|
|
|
/* set find mode for completion */
|
|
|
|
command_mode = FIND_EXE_ONLY;
|
|
|
|
for (i = 0; int_buf[i]; i++)
|
|
|
|
if (int_buf[i] == ' ' || int_buf[i] == '<' || int_buf[i] == '>') {
|
|
|
|
if (int_buf[i] == ' ' && command_mode == FIND_EXE_ONLY
|
2001-03-14 04:27:56 +05:30
|
|
|
&& matchBuf[pos_buf[0]]=='c'
|
|
|
|
&& matchBuf[pos_buf[1]]=='d' )
|
2001-02-17 00:06:04 +05:30
|
|
|
command_mode = FIND_DIR_ONLY;
|
|
|
|
else {
|
|
|
|
command_mode = FIND_FILE_ONLY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* "strlen" */
|
|
|
|
for (i = 0; int_buf[i]; i++);
|
|
|
|
/* find last word */
|
|
|
|
for (--i; i >= 0; i--) {
|
|
|
|
c = int_buf[i];
|
|
|
|
if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') {
|
|
|
|
collapse_pos(0, i + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* skip first not quoted '\'' or '"' */
|
|
|
|
for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++);
|
|
|
|
/* collapse quote or unquote // or /~ */
|
2003-07-28 15:26:35 +05:30
|
|
|
while ((int_buf[i] & ~QUOT) == '/' &&
|
2001-03-09 01:01:12 +05:30
|
|
|
((int_buf[i + 1] & ~QUOT) == '/'
|
|
|
|
|| (int_buf[i + 1] & ~QUOT) == '~')) {
|
|
|
|
i++;
|
|
|
|
}
|
2001-02-17 00:06:04 +05:30
|
|
|
|
|
|
|
/* set only match and destroy quotes */
|
|
|
|
j = 0;
|
2001-05-31 22:45:57 +05:30
|
|
|
for (c = 0; pos_buf[i] >= 0; i++) {
|
|
|
|
matchBuf[c++] = matchBuf[pos_buf[i]];
|
2001-02-17 00:06:04 +05:30
|
|
|
j = pos_buf[i] + 1;
|
|
|
|
}
|
2001-05-31 22:45:57 +05:30
|
|
|
matchBuf[c] = 0;
|
2001-02-17 00:06:04 +05:30
|
|
|
/* old lenght matchBuf with quotes symbols */
|
|
|
|
*len_with_quotes = j ? j - pos_buf[0] : 0;
|
|
|
|
|
|
|
|
return command_mode;
|
|
|
|
}
|
|
|
|
|
2003-01-06 06:41:50 +05:30
|
|
|
/*
|
|
|
|
display by column original ideas from ls applet,
|
|
|
|
very optimize by my :)
|
|
|
|
*/
|
2006-01-25 17:23:47 +05:30
|
|
|
static void showfiles(void)
|
2003-01-06 06:41:50 +05:30
|
|
|
{
|
|
|
|
int ncols, row;
|
|
|
|
int column_width = 0;
|
2006-01-25 17:23:47 +05:30
|
|
|
int nfiles = num_matches;
|
2003-01-06 06:41:50 +05:30
|
|
|
int nrows = nfiles;
|
2006-01-25 17:23:47 +05:30
|
|
|
char str_add_chr[2];
|
|
|
|
int l;
|
2003-01-06 06:41:50 +05:30
|
|
|
|
|
|
|
/* find the longest file name- use that as the column width */
|
|
|
|
for (row = 0; row < nrows; row++) {
|
2006-01-25 17:23:47 +05:30
|
|
|
l = strlen(matches[row]);
|
|
|
|
if(add_char_to_match[row])
|
|
|
|
l++;
|
2003-01-06 06:41:50 +05:30
|
|
|
if (column_width < l)
|
|
|
|
column_width = l;
|
|
|
|
}
|
|
|
|
column_width += 2; /* min space for columns */
|
|
|
|
ncols = cmdedit_termw / column_width;
|
|
|
|
|
|
|
|
if (ncols > 1) {
|
|
|
|
nrows /= ncols;
|
|
|
|
if(nfiles % ncols)
|
|
|
|
nrows++; /* round up fractionals */
|
|
|
|
} else {
|
|
|
|
ncols = 1;
|
|
|
|
}
|
2006-01-25 17:23:47 +05:30
|
|
|
str_add_chr[1] = 0;
|
2003-01-06 06:41:50 +05:30
|
|
|
for (row = 0; row < nrows; row++) {
|
|
|
|
int n = row;
|
|
|
|
int nc;
|
2006-01-25 17:23:47 +05:30
|
|
|
int acol;
|
|
|
|
|
|
|
|
for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) {
|
|
|
|
str_add_chr[0] = add_char_to_match[n];
|
|
|
|
acol = str_add_chr[0] ? column_width - 1 : column_width;
|
|
|
|
printf("%s%s", matches[n], str_add_chr);
|
|
|
|
l = strlen(matches[n]);
|
|
|
|
while(l < acol) {
|
|
|
|
putchar(' ');
|
|
|
|
l++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str_add_chr[0] = add_char_to_match[n];
|
|
|
|
printf("%s%s\n", matches[n], str_add_chr);
|
2003-01-06 06:41:50 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
|
|
|
|
static void input_tab(int *lastWasTab)
|
|
|
|
{
|
|
|
|
/* Do TAB completion */
|
2003-07-28 15:26:35 +05:30
|
|
|
if (lastWasTab == 0) { /* free all memory */
|
2000-04-12 23:19:52 +05:30
|
|
|
if (matches) {
|
2001-02-17 00:06:04 +05:30
|
|
|
while (num_matches > 0)
|
2001-01-27 02:12:23 +05:30
|
|
|
free(matches[--num_matches]);
|
2000-04-12 23:19:52 +05:30
|
|
|
free(matches);
|
|
|
|
matches = (char **) NULL;
|
2006-01-25 17:23:47 +05:30
|
|
|
free(add_char_to_match);
|
|
|
|
add_char_to_match = NULL;
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
2001-02-17 00:06:04 +05:30
|
|
|
return;
|
|
|
|
}
|
2001-12-21 04:43:26 +05:30
|
|
|
if (! *lastWasTab) {
|
2001-02-17 00:06:04 +05:30
|
|
|
|
2006-01-25 17:23:47 +05:30
|
|
|
char *tmp, *tmp1;
|
2001-02-17 00:06:04 +05:30
|
|
|
int len_found;
|
|
|
|
char matchBuf[BUFSIZ];
|
|
|
|
int find_type;
|
|
|
|
int recalc_pos;
|
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
*lastWasTab = TRUE; /* flop trigger */
|
2001-02-17 00:06:04 +05:30
|
|
|
|
|
|
|
/* Make a local copy of the string -- up
|
|
|
|
* to the position of the cursor */
|
|
|
|
tmp = strncpy(matchBuf, command_ps, cursor);
|
|
|
|
tmp[cursor] = 0;
|
|
|
|
|
|
|
|
find_type = find_match(matchBuf, &recalc_pos);
|
|
|
|
|
|
|
|
/* Free up any memory already allocated */
|
|
|
|
input_tab(0);
|
2000-04-12 23:19:52 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_USERNAME_COMPLETION
|
2001-02-17 00:06:04 +05:30
|
|
|
/* If the word starts with `~' and there is no slash in the word,
|
2000-04-12 23:19:52 +05:30
|
|
|
* then try completing this word as a username. */
|
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
if (matchBuf[0] == '~' && strchr(matchBuf, '/') == 0)
|
2006-01-25 17:23:47 +05:30
|
|
|
username_tab_completion(matchBuf, NULL);
|
|
|
|
if (!matches)
|
2001-01-27 02:12:23 +05:30
|
|
|
#endif
|
2001-02-17 00:06:04 +05:30
|
|
|
/* Try to match any executable in our path and everything
|
2000-04-12 23:19:52 +05:30
|
|
|
* in the current working directory that matches. */
|
2006-01-25 17:23:47 +05:30
|
|
|
exe_n_cwd_tab_completion(matchBuf, find_type);
|
|
|
|
/* Remove duplicate found and sort */
|
2001-06-26 07:36:08 +05:30
|
|
|
if(matches) {
|
2006-01-25 17:23:47 +05:30
|
|
|
int i, j, n, srt;
|
2001-06-26 07:36:08 +05:30
|
|
|
/* bubble */
|
2006-01-25 17:23:47 +05:30
|
|
|
n = num_matches;
|
|
|
|
for(i=0; i<(n-1); i++)
|
|
|
|
for(j=i+1; j<n; j++)
|
|
|
|
if(matches[i]!=NULL && matches[j]!=NULL) {
|
|
|
|
srt = strcmp(matches[i], matches[j]);
|
|
|
|
if(srt == 0) {
|
|
|
|
free(matches[j]);
|
|
|
|
matches[j]=0;
|
|
|
|
} else if(srt > 0) {
|
|
|
|
tmp1 = matches[i];
|
|
|
|
matches[i] = matches[j];
|
|
|
|
matches[j] = tmp1;
|
|
|
|
srt = add_char_to_match[i];
|
|
|
|
add_char_to_match[i] = add_char_to_match[j];
|
|
|
|
add_char_to_match[j] = srt;
|
|
|
|
}
|
2001-06-26 07:36:08 +05:30
|
|
|
}
|
2006-01-25 17:23:47 +05:30
|
|
|
j = n;
|
|
|
|
n = 0;
|
|
|
|
for(i=0; i<j; i++)
|
|
|
|
if(matches[i]) {
|
|
|
|
matches[n]=matches[i];
|
|
|
|
add_char_to_match[n]=add_char_to_match[i];
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
num_matches = n;
|
2001-06-26 07:36:08 +05:30
|
|
|
}
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Did we find exactly one match? */
|
2001-02-17 00:06:04 +05:30
|
|
|
if (!matches || num_matches > 1) {
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
beep();
|
2001-02-17 00:06:04 +05:30
|
|
|
if (!matches)
|
2003-07-28 15:26:35 +05:30
|
|
|
return; /* not found */
|
2001-02-17 00:06:04 +05:30
|
|
|
/* find minimal match */
|
2006-01-25 17:23:47 +05:30
|
|
|
tmp1 = bb_xstrdup(matches[0]);
|
|
|
|
for (tmp = tmp1; *tmp; tmp++)
|
2001-02-17 00:06:04 +05:30
|
|
|
for (len_found = 1; len_found < num_matches; len_found++)
|
2006-01-25 17:23:47 +05:30
|
|
|
if (matches[len_found][(tmp - tmp1)] != *tmp) {
|
|
|
|
*tmp = 0;
|
2001-02-17 00:06:04 +05:30
|
|
|
break;
|
|
|
|
}
|
2006-01-25 17:23:47 +05:30
|
|
|
if (*tmp1 == 0) { /* have unique */
|
|
|
|
free(tmp1);
|
2001-02-17 00:06:04 +05:30
|
|
|
return;
|
|
|
|
}
|
2006-01-25 17:23:47 +05:30
|
|
|
tmp = add_quote_for_spec_chars(tmp1, 0);
|
|
|
|
free(tmp1);
|
2003-07-28 15:26:35 +05:30
|
|
|
} else { /* one match */
|
2006-01-25 17:23:47 +05:30
|
|
|
tmp = add_quote_for_spec_chars(matches[0], add_char_to_match[0]);
|
2001-02-17 00:06:04 +05:30
|
|
|
/* for next completion current found */
|
|
|
|
*lastWasTab = FALSE;
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
2001-02-17 00:06:04 +05:30
|
|
|
len_found = strlen(tmp);
|
2001-01-27 02:12:23 +05:30
|
|
|
/* have space to placed match? */
|
2001-02-17 00:06:04 +05:30
|
|
|
if ((len_found - strlen(matchBuf) + len) < BUFSIZ) {
|
|
|
|
|
|
|
|
/* before word for match */
|
|
|
|
command_ps[cursor - recalc_pos] = 0;
|
|
|
|
/* save tail line */
|
|
|
|
strcpy(matchBuf, command_ps + cursor);
|
|
|
|
/* add match */
|
|
|
|
strcat(command_ps, tmp);
|
|
|
|
/* add tail */
|
2001-01-27 02:12:23 +05:30
|
|
|
strcat(command_ps, matchBuf);
|
2001-02-17 00:06:04 +05:30
|
|
|
/* back to begin word for match */
|
|
|
|
input_backward(recalc_pos);
|
|
|
|
/* new pos */
|
|
|
|
recalc_pos = cursor + len_found;
|
|
|
|
/* new len */
|
|
|
|
len = strlen(command_ps);
|
|
|
|
/* write out the matched command */
|
2001-05-31 22:45:57 +05:30
|
|
|
redraw(cmdedit_y, len - recalc_pos);
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
2006-01-25 17:23:47 +05:30
|
|
|
free(tmp);
|
2000-04-12 23:19:52 +05:30
|
|
|
} else {
|
|
|
|
/* Ok -- the last char was a TAB. Since they
|
|
|
|
* just hit TAB again, print a list of all the
|
|
|
|
* available choices... */
|
2001-02-17 00:06:04 +05:30
|
|
|
if (matches && num_matches > 0) {
|
2003-07-28 15:26:35 +05:30
|
|
|
int sav_cursor = cursor; /* change goto_new_line() */
|
2000-04-12 23:19:52 +05:30
|
|
|
|
|
|
|
/* Go to the next line */
|
2001-01-27 02:12:23 +05:30
|
|
|
goto_new_line();
|
2006-01-25 17:23:47 +05:30
|
|
|
showfiles();
|
2001-02-17 00:06:04 +05:30
|
|
|
redraw(0, len - sav_cursor);
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-07-28 15:26:35 +05:30
|
|
|
#endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */
|
2000-04-12 23:19:52 +05:30
|
|
|
|
2002-11-27 14:59:49 +05:30
|
|
|
#if MAX_HISTORY >= 1
|
|
|
|
static void get_previous_history(void)
|
2000-04-12 23:19:52 +05:30
|
|
|
{
|
2002-11-27 14:59:49 +05:30
|
|
|
if(command_ps[0] != 0 || history[cur_history] == 0) {
|
|
|
|
free(history[cur_history]);
|
2003-03-19 14:43:01 +05:30
|
|
|
history[cur_history] = bb_xstrdup(command_ps);
|
2002-11-27 14:59:49 +05:30
|
|
|
}
|
|
|
|
cur_history--;
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
|
|
|
|
2002-11-27 14:59:49 +05:30
|
|
|
static int get_next_history(void)
|
2000-04-12 23:19:52 +05:30
|
|
|
{
|
2002-11-27 14:59:49 +05:30
|
|
|
int ch = cur_history;
|
|
|
|
|
|
|
|
if (ch < n_history) {
|
|
|
|
get_previous_history(); /* save the current history line */
|
|
|
|
return (cur_history = ch+1);
|
|
|
|
} else {
|
|
|
|
beep();
|
|
|
|
return 0;
|
|
|
|
}
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
2002-12-04 04:15:46 +05:30
|
|
|
|
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
|
2006-03-07 02:17:33 +05:30
|
|
|
void load_history ( const char *fromfile )
|
2002-12-09 16:40:40 +05:30
|
|
|
{
|
2002-12-04 04:15:46 +05:30
|
|
|
FILE *fp;
|
2002-12-09 16:40:40 +05:30
|
|
|
int hi;
|
2002-12-04 04:15:46 +05:30
|
|
|
|
2002-12-09 16:40:40 +05:30
|
|
|
/* cleanup old */
|
|
|
|
|
|
|
|
for(hi = n_history; hi > 0; ) {
|
|
|
|
hi--;
|
|
|
|
free ( history [hi] );
|
2002-12-04 04:15:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (( fp = fopen ( fromfile, "r" ))) {
|
2003-07-28 15:26:35 +05:30
|
|
|
|
2002-12-09 16:40:40 +05:30
|
|
|
for ( hi = 0; hi < MAX_HISTORY; ) {
|
2003-03-19 14:43:01 +05:30
|
|
|
char * hl = bb_get_chomped_line_from_file(fp);
|
2002-12-09 16:40:40 +05:30
|
|
|
int l;
|
|
|
|
|
|
|
|
if(!hl)
|
2002-12-04 04:15:46 +05:30
|
|
|
break;
|
2002-12-09 16:40:40 +05:30
|
|
|
l = strlen(hl);
|
|
|
|
if(l >= BUFSIZ)
|
|
|
|
hl[BUFSIZ-1] = 0;
|
|
|
|
if(l == 0 || hl[0] == ' ') {
|
|
|
|
free(hl);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
history [hi++] = hl;
|
2002-12-04 04:15:46 +05:30
|
|
|
}
|
|
|
|
fclose ( fp );
|
|
|
|
}
|
2002-12-09 16:40:40 +05:30
|
|
|
cur_history = n_history = hi;
|
2002-12-04 04:15:46 +05:30
|
|
|
}
|
|
|
|
|
2006-03-07 02:17:33 +05:30
|
|
|
void save_history ( const char *tofile )
|
2002-12-04 04:15:46 +05:30
|
|
|
{
|
|
|
|
FILE *fp = fopen ( tofile, "w" );
|
2003-07-28 15:26:35 +05:30
|
|
|
|
2002-12-04 04:15:46 +05:30
|
|
|
if ( fp ) {
|
|
|
|
int i;
|
2003-07-28 15:26:35 +05:30
|
|
|
|
2002-12-04 04:15:46 +05:30
|
|
|
for ( i = 0; i < n_history; i++ ) {
|
2003-12-24 01:54:51 +05:30
|
|
|
fprintf(fp, "%s\n", history [i]);
|
2002-12-04 04:15:46 +05:30
|
|
|
}
|
|
|
|
fclose ( fp );
|
|
|
|
}
|
|
|
|
}
|
2002-12-09 16:40:40 +05:30
|
|
|
#endif
|
2002-12-04 04:15:46 +05:30
|
|
|
|
2002-11-27 14:59:49 +05:30
|
|
|
#endif
|
2000-04-12 23:19:52 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
enum {
|
|
|
|
ESC = 27,
|
|
|
|
DEL = 127,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-03-17 06:42:41 +05:30
|
|
|
/*
|
|
|
|
* This function is used to grab a character buffer
|
|
|
|
* from the input file descriptor and allows you to
|
2004-04-12 20:33:51 +05:30
|
|
|
* a string with full command editing (sort of like
|
2000-03-17 06:42:41 +05:30
|
|
|
* a mini readline).
|
|
|
|
*
|
|
|
|
* The following standard commands are not implemented:
|
|
|
|
* ESC-b -- Move back one word
|
|
|
|
* ESC-f -- Move forward one word
|
|
|
|
* ESC-d -- Delete back one word
|
|
|
|
* ESC-h -- Delete forward one word
|
|
|
|
* CTL-t -- Transpose two characters
|
|
|
|
*
|
2005-08-05 00:34:46 +05:30
|
|
|
* Minimalist vi-style command line editing available if configured.
|
|
|
|
* vi mode implemented 2005 by Paul Fox <pgf@foxharp.boston.ma.us>
|
2000-03-17 06:42:41 +05:30
|
|
|
*
|
|
|
|
*/
|
2003-07-28 15:26:35 +05:30
|
|
|
|
2005-08-05 00:34:46 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
|
|
|
|
static int vi_mode;
|
|
|
|
|
|
|
|
void setvimode ( int viflag )
|
|
|
|
{
|
|
|
|
vi_mode = viflag;
|
|
|
|
}
|
|
|
|
|
2005-09-22 18:29:26 +05:30
|
|
|
static void
|
2005-08-05 00:34:46 +05:30
|
|
|
vi_Word_motion(char *command, int eat)
|
|
|
|
{
|
|
|
|
while (cursor < len && !isspace(command[cursor]))
|
|
|
|
input_forward();
|
|
|
|
if (eat) while (cursor < len && isspace(command[cursor]))
|
|
|
|
input_forward();
|
|
|
|
}
|
|
|
|
|
2005-09-22 18:29:26 +05:30
|
|
|
static void
|
2005-08-05 00:34:46 +05:30
|
|
|
vi_word_motion(char *command, int eat)
|
|
|
|
{
|
|
|
|
if (isalnum(command[cursor]) || command[cursor] == '_') {
|
|
|
|
while (cursor < len &&
|
|
|
|
(isalnum(command[cursor+1]) ||
|
|
|
|
command[cursor+1] == '_'))
|
|
|
|
input_forward();
|
|
|
|
} else if (ispunct(command[cursor])) {
|
|
|
|
while (cursor < len &&
|
|
|
|
(ispunct(command[cursor+1])))
|
|
|
|
input_forward();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cursor < len)
|
|
|
|
input_forward();
|
|
|
|
|
|
|
|
if (eat && cursor < len && isspace(command[cursor]))
|
|
|
|
while (cursor < len && isspace(command[cursor]))
|
|
|
|
input_forward();
|
|
|
|
}
|
|
|
|
|
2005-09-22 18:29:26 +05:30
|
|
|
static void
|
2005-08-05 00:34:46 +05:30
|
|
|
vi_End_motion(char *command)
|
|
|
|
{
|
|
|
|
input_forward();
|
|
|
|
while (cursor < len && isspace(command[cursor]))
|
|
|
|
input_forward();
|
|
|
|
while (cursor < len-1 && !isspace(command[cursor+1]))
|
|
|
|
input_forward();
|
|
|
|
}
|
|
|
|
|
2005-09-22 18:29:26 +05:30
|
|
|
static void
|
2005-08-05 00:34:46 +05:30
|
|
|
vi_end_motion(char *command)
|
|
|
|
{
|
|
|
|
if (cursor >= len-1)
|
|
|
|
return;
|
|
|
|
input_forward();
|
|
|
|
while (cursor < len-1 && isspace(command[cursor]))
|
|
|
|
input_forward();
|
|
|
|
if (cursor >= len-1)
|
|
|
|
return;
|
|
|
|
if (isalnum(command[cursor]) || command[cursor] == '_') {
|
|
|
|
while (cursor < len-1 &&
|
|
|
|
(isalnum(command[cursor+1]) ||
|
|
|
|
command[cursor+1] == '_'))
|
|
|
|
input_forward();
|
|
|
|
} else if (ispunct(command[cursor])) {
|
|
|
|
while (cursor < len-1 &&
|
|
|
|
(ispunct(command[cursor+1])))
|
|
|
|
input_forward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-22 18:29:26 +05:30
|
|
|
static void
|
2005-08-05 00:34:46 +05:30
|
|
|
vi_Back_motion(char *command)
|
|
|
|
{
|
|
|
|
while (cursor > 0 && isspace(command[cursor-1]))
|
|
|
|
input_backward(1);
|
|
|
|
while (cursor > 0 && !isspace(command[cursor-1]))
|
|
|
|
input_backward(1);
|
|
|
|
}
|
|
|
|
|
2005-09-22 18:29:26 +05:30
|
|
|
static void
|
2005-08-05 00:34:46 +05:30
|
|
|
vi_back_motion(char *command)
|
|
|
|
{
|
|
|
|
if (cursor <= 0)
|
|
|
|
return;
|
|
|
|
input_backward(1);
|
|
|
|
while (cursor > 0 && isspace(command[cursor]))
|
|
|
|
input_backward(1);
|
|
|
|
if (cursor <= 0)
|
|
|
|
return;
|
|
|
|
if (isalnum(command[cursor]) || command[cursor] == '_') {
|
|
|
|
while (cursor > 0 &&
|
|
|
|
(isalnum(command[cursor-1]) ||
|
|
|
|
command[cursor-1] == '_'))
|
|
|
|
input_backward(1);
|
|
|
|
} else if (ispunct(command[cursor])) {
|
|
|
|
while (cursor > 0 &&
|
|
|
|
(ispunct(command[cursor-1])))
|
|
|
|
input_backward(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-01-25 05:38:53 +05:30
|
|
|
/*
|
2005-08-05 00:34:46 +05:30
|
|
|
* the normal emacs mode and vi's insert mode are the same.
|
|
|
|
* commands entered when in vi command mode ("escape mode") get
|
|
|
|
* an extra bit added to distinguish them. this lets them share
|
|
|
|
* much of the code in the big switch and while loop. i
|
|
|
|
* experimented with an ugly macro to make the case labels for
|
|
|
|
* these cases go away entirely when vi mode isn't configured, in
|
|
|
|
* hopes of letting the jump tables get smaller:
|
|
|
|
* #define vcase(caselabel) caselabel
|
|
|
|
* and then
|
2006-01-25 17:23:47 +05:30
|
|
|
* case CNTRL('A'):
|
|
|
|
* case vcase(VICMD('0'):)
|
2005-08-05 00:34:46 +05:30
|
|
|
* but it didn't seem to make any difference in code size,
|
|
|
|
* and the macro-ized code was too ugly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define VI_cmdbit 0x100
|
|
|
|
#define VICMD(somecmd) ((somecmd)|VI_cmdbit)
|
|
|
|
|
|
|
|
/* convert uppercase ascii to equivalent control char, for readability */
|
|
|
|
#define CNTRL(uc_char) ((uc_char) - 0x40)
|
|
|
|
|
2001-07-17 06:42:36 +05:30
|
|
|
|
|
|
|
int cmdedit_read_input(char *prompt, char command[BUFSIZ])
|
2000-03-16 13:39:57 +05:30
|
|
|
{
|
|
|
|
|
2000-03-19 10:58:55 +05:30
|
|
|
int break_out = 0;
|
|
|
|
int lastWasTab = FALSE;
|
2005-08-05 00:34:46 +05:30
|
|
|
unsigned char c;
|
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
|
|
|
|
unsigned int ic, prevc;
|
|
|
|
int vi_cmdmode = 0;
|
|
|
|
#endif
|
2001-02-17 00:06:04 +05:30
|
|
|
/* prepare before init handlers */
|
2003-07-28 15:26:35 +05:30
|
|
|
cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
|
2001-01-27 02:12:23 +05:30
|
|
|
len = 0;
|
|
|
|
command_ps = command;
|
|
|
|
|
2001-08-02 10:32:46 +05:30
|
|
|
getTermSettings(0, (void *) &initial_settings);
|
|
|
|
memcpy(&new_settings, &initial_settings, sizeof(struct termios));
|
|
|
|
new_settings.c_lflag &= ~ICANON; /* unbuffered input */
|
|
|
|
/* Turn off echoing and CTRL-C, so we can trap it */
|
|
|
|
new_settings.c_lflag &= ~(ECHO | ECHONL | ISIG);
|
|
|
|
/* Hmm, in linux c_cc[] not parsed if set ~ICANON */
|
|
|
|
new_settings.c_cc[VMIN] = 1;
|
|
|
|
new_settings.c_cc[VTIME] = 0;
|
|
|
|
/* Turn off CTRL-C, so we can trap it */
|
2001-06-26 07:36:08 +05:30
|
|
|
# ifndef _POSIX_VDISABLE
|
|
|
|
# define _POSIX_VDISABLE '\0'
|
|
|
|
# endif
|
2003-07-28 15:26:35 +05:30
|
|
|
new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
|
2001-02-17 00:06:04 +05:30
|
|
|
command[0] = 0;
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2001-06-26 07:36:08 +05:30
|
|
|
setTermSettings(0, (void *) &new_settings);
|
2001-02-17 00:06:04 +05:30
|
|
|
handlers_sets |= SET_RESET_TERM;
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2001-02-17 01:39:17 +05:30
|
|
|
/* Now initialize things */
|
|
|
|
cmdedit_init();
|
2001-03-16 02:21:09 +05:30
|
|
|
/* Print out the command prompt */
|
|
|
|
parse_prompt(prompt);
|
2001-01-04 16:38:45 +05:30
|
|
|
|
2000-03-19 10:58:55 +05:30
|
|
|
while (1) {
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
fflush(stdout); /* buffered out to fast */
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2001-07-13 01:56:32 +05:30
|
|
|
if (safe_read(0, &c, 1) < 1)
|
2001-04-23 20:58:28 +05:30
|
|
|
/* if we can't read input then exit */
|
|
|
|
goto prepare_to_die;
|
2000-04-09 23:54:05 +05:30
|
|
|
|
2005-08-05 00:34:46 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_EDITING_VI
|
|
|
|
newdelflag = 1;
|
|
|
|
ic = c;
|
|
|
|
if (vi_cmdmode)
|
2006-01-25 05:38:53 +05:30
|
|
|
ic |= VI_cmdbit;
|
2005-08-05 00:34:46 +05:30
|
|
|
switch (ic)
|
|
|
|
#else
|
|
|
|
switch (c)
|
|
|
|
#endif
|
|
|
|
{
|
2000-04-12 23:19:52 +05:30
|
|
|
case '\n':
|
|
|
|
case '\r':
|
2005-08-05 00:34:46 +05:30
|
|
|
case VICMD('\n'):
|
|
|
|
case VICMD('\r'):
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Enter */
|
2001-02-17 00:06:04 +05:30
|
|
|
goto_new_line();
|
2000-04-12 23:19:52 +05:30
|
|
|
break_out = 1;
|
|
|
|
break;
|
2005-08-05 00:34:46 +05:30
|
|
|
case CNTRL('A'):
|
|
|
|
case VICMD('0'):
|
2000-03-19 10:58:55 +05:30
|
|
|
/* Control-a -- Beginning of line */
|
2001-02-17 00:06:04 +05:30
|
|
|
input_backward(cursor);
|
2001-01-27 02:12:23 +05:30
|
|
|
break;
|
2005-08-05 00:34:46 +05:30
|
|
|
case CNTRL('B'):
|
|
|
|
case VICMD('h'):
|
|
|
|
case VICMD('\b'):
|
|
|
|
case VICMD(DEL):
|
2000-03-19 10:58:55 +05:30
|
|
|
/* Control-b -- Move back one character */
|
2001-02-17 00:06:04 +05:30
|
|
|
input_backward(1);
|
2000-04-21 06:56:49 +05:30
|
|
|
break;
|
2005-08-05 00:34:46 +05:30
|
|
|
case CNTRL('C'):
|
|
|
|
case VICMD(CNTRL('C')):
|
2000-12-19 01:55:50 +05:30
|
|
|
/* Control-c -- stop gathering input */
|
2001-01-27 02:12:23 +05:30
|
|
|
goto_new_line();
|
2004-02-04 13:54:39 +05:30
|
|
|
#ifndef CONFIG_ASH
|
2001-02-17 00:06:04 +05:30
|
|
|
command[0] = 0;
|
2001-07-13 01:56:32 +05:30
|
|
|
len = 0;
|
|
|
|
lastWasTab = FALSE;
|
|
|
|
put_prompt();
|
2004-02-04 13:54:39 +05:30
|
|
|
#else
|
|
|
|
len = 0;
|
2004-02-22 16:43:28 +05:30
|
|
|
break_out = -1; /* to control traps */
|
2004-02-04 13:54:39 +05:30
|
|
|
#endif
|
2001-07-13 01:56:32 +05:30
|
|
|
break;
|
2005-08-05 00:34:46 +05:30
|
|
|
case CNTRL('D'):
|
2001-02-17 00:06:04 +05:30
|
|
|
/* Control-d -- Delete one character, or exit
|
2000-04-12 23:19:52 +05:30
|
|
|
* if the len=0 and no chars to delete */
|
|
|
|
if (len == 0) {
|
2004-08-19 23:52:13 +05:30
|
|
|
errno = 0;
|
2001-04-23 20:58:28 +05:30
|
|
|
prepare_to_die:
|
2001-10-24 10:30:29 +05:30
|
|
|
#if !defined(CONFIG_ASH)
|
2001-01-27 02:12:23 +05:30
|
|
|
printf("exit");
|
2001-07-13 01:56:32 +05:30
|
|
|
goto_new_line();
|
|
|
|
/* cmdedit_reset_term() called in atexit */
|
|
|
|
exit(EXIT_SUCCESS);
|
2001-07-17 06:42:36 +05:30
|
|
|
#else
|
2004-02-22 16:43:28 +05:30
|
|
|
/* to control stopped jobs */
|
|
|
|
len = break_out = -1;
|
2001-07-17 06:42:36 +05:30
|
|
|
break;
|
|
|
|
#endif
|
2000-04-12 23:19:52 +05:30
|
|
|
} else {
|
2005-08-05 00:34:46 +05:30
|
|
|
input_delete(0);
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
|
|
|
break;
|
2005-08-05 00:34:46 +05:30
|
|
|
case CNTRL('E'):
|
|
|
|
case VICMD('$'):
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Control-e -- End of line */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_end();
|
2000-04-12 23:19:52 +05:30
|
|
|
break;
|
2005-08-05 00:34:46 +05:30
|
|
|
case CNTRL('F'):
|
|
|
|
case VICMD('l'):
|
|
|
|
case VICMD(' '):
|
2000-03-19 10:58:55 +05:30
|
|
|
/* Control-f -- Move forward one character */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_forward();
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
2000-04-12 23:19:52 +05:30
|
|
|
case '\b':
|
|
|
|
case DEL:
|
2000-04-21 06:56:49 +05:30
|
|
|
/* Control-h and DEL */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_backspace();
|
2000-04-12 23:19:52 +05:30
|
|
|
break;
|
|
|
|
case '\t':
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
|
2001-02-17 00:06:04 +05:30
|
|
|
input_tab(&lastWasTab);
|
2000-04-12 23:19:52 +05:30
|
|
|
#endif
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
2005-08-05 00:34:46 +05:30
|
|
|
case CNTRL('K'):
|
2003-07-28 15:26:35 +05:30
|
|
|
/* Control-k -- clear to end of line */
|
2002-04-13 18:56:49 +05:30
|
|
|
*(command + cursor) = 0;
|
|
|
|
len = cursor;
|
2002-04-25 04:38:23 +05:30
|
|
|
printf("\033[J");
|
2002-04-13 18:56:49 +05:30
|
|
|
break;
|
2005-08-05 00:34:46 +05:30
|
|
|
case CNTRL('L'):
|
|
|
|
case VICMD(CNTRL('L')):
|
2003-12-24 01:54:51 +05:30
|
|
|
/* Control-l -- clear screen */
|
2003-07-28 15:26:35 +05:30
|
|
|
printf("\033[H");
|
2002-11-27 14:59:49 +05:30
|
|
|
redraw(0, len-cursor);
|
2001-12-21 16:50:15 +05:30
|
|
|
break;
|
2002-11-27 14:59:49 +05:30
|
|
|
#if MAX_HISTORY >= 1
|
2005-08-05 00:34:46 +05:30
|
|
|
case CNTRL('N'):
|
|
|
|
case VICMD(CNTRL('N')):
|
|
|
|
case VICMD('j'):
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Control-n -- Get next command in history */
|
2002-11-27 14:59:49 +05:30
|
|
|
if (get_next_history())
|
2000-04-12 23:19:52 +05:30
|
|
|
goto rewrite_line;
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
2005-08-05 00:34:46 +05:30
|
|
|
case CNTRL('P'):
|
|
|
|
case VICMD(CNTRL('P')):
|
|
|
|
case VICMD('k'):
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Control-p -- Get previous command from history */
|
2002-11-27 14:59:49 +05:30
|
|
|
if (cur_history > 0) {
|
|
|
|
get_previous_history();
|
2000-04-12 23:19:52 +05:30
|
|
|
goto rewrite_line;
|
|
|
|
} else {
|
2001-01-27 02:12:23 +05:30
|
|
|
beep();
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
|
|
|
break;
|
2002-11-27 14:59:49 +05:30
|
|
|
#endif
|
2005-08-05 00:34:46 +05:30
|
|
|
case CNTRL('U'):
|
|
|
|
case VICMD(CNTRL('U')):
|
2001-02-17 00:06:04 +05:30
|
|
|
/* Control-U -- Clear line before cursor */
|
|
|
|
if (cursor) {
|
|
|
|
strcpy(command, command + cursor);
|
|
|
|
redraw(cmdedit_y, len -= cursor);
|
|
|
|
}
|
|
|
|
break;
|
2005-08-05 00:34:46 +05:30
|
|
|
case CNTRL('W'):
|
|
|
|
case VICMD(CNTRL('W')):
|
2003-12-24 01:54:51 +05:30
|
|
|
/* Control-W -- Remove the last word */
|
|
|
|
while (cursor > 0 && isspace(command[cursor-1]))
|
|
|
|
input_backspace();
|
|
|
|
while (cursor > 0 &&!isspace(command[cursor-1]))
|
|
|
|
input_backspace();
|
|
|
|
break;
|
2005-08-05 00:34:46 +05:30
|
|
|
#if CONFIG_FEATURE_COMMAND_EDITING_VI
|
|
|
|
case VICMD('i'):
|
|
|
|
vi_cmdmode = 0;
|
|
|
|
break;
|
|
|
|
case VICMD('I'):
|
|
|
|
input_backward(cursor);
|
|
|
|
vi_cmdmode = 0;
|
|
|
|
break;
|
|
|
|
case VICMD('a'):
|
|
|
|
input_forward();
|
|
|
|
vi_cmdmode = 0;
|
|
|
|
break;
|
|
|
|
case VICMD('A'):
|
|
|
|
input_end();
|
|
|
|
vi_cmdmode = 0;
|
|
|
|
break;
|
|
|
|
case VICMD('x'):
|
|
|
|
input_delete(1);
|
|
|
|
break;
|
|
|
|
case VICMD('X'):
|
|
|
|
if (cursor > 0) {
|
|
|
|
input_backward(1);
|
|
|
|
input_delete(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VICMD('W'):
|
|
|
|
vi_Word_motion(command, 1);
|
|
|
|
break;
|
|
|
|
case VICMD('w'):
|
|
|
|
vi_word_motion(command, 1);
|
|
|
|
break;
|
|
|
|
case VICMD('E'):
|
|
|
|
vi_End_motion(command);
|
|
|
|
break;
|
|
|
|
case VICMD('e'):
|
|
|
|
vi_end_motion(command);
|
|
|
|
break;
|
|
|
|
case VICMD('B'):
|
|
|
|
vi_Back_motion(command);
|
|
|
|
break;
|
|
|
|
case VICMD('b'):
|
|
|
|
vi_back_motion(command);
|
|
|
|
break;
|
|
|
|
case VICMD('C'):
|
|
|
|
vi_cmdmode = 0;
|
|
|
|
/* fall through */
|
|
|
|
case VICMD('D'):
|
|
|
|
goto clear_to_eol;
|
|
|
|
|
|
|
|
case VICMD('c'):
|
|
|
|
vi_cmdmode = 0;
|
|
|
|
/* fall through */
|
|
|
|
case VICMD('d'):
|
2006-01-25 05:38:53 +05:30
|
|
|
{
|
2005-08-05 00:34:46 +05:30
|
|
|
int nc, sc;
|
|
|
|
sc = cursor;
|
|
|
|
prevc = ic;
|
|
|
|
if (safe_read(0, &c, 1) < 1)
|
|
|
|
goto prepare_to_die;
|
|
|
|
if (c == (prevc & 0xff)) {
|
|
|
|
/* "cc", "dd" */
|
|
|
|
input_backward(cursor);
|
|
|
|
goto clear_to_eol;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch(c) {
|
|
|
|
case 'w':
|
|
|
|
case 'W':
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
switch (c) {
|
|
|
|
case 'w': /* "dw", "cw" */
|
2006-01-25 05:38:53 +05:30
|
|
|
vi_word_motion(command, vi_cmdmode);
|
2005-08-05 00:34:46 +05:30
|
|
|
break;
|
|
|
|
case 'W': /* 'dW', 'cW' */
|
|
|
|
vi_Word_motion(command, vi_cmdmode);
|
|
|
|
break;
|
|
|
|
case 'e': /* 'de', 'ce' */
|
|
|
|
vi_end_motion(command);
|
|
|
|
input_forward();
|
|
|
|
break;
|
|
|
|
case 'E': /* 'dE', 'cE' */
|
|
|
|
vi_End_motion(command);
|
|
|
|
input_forward();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nc = cursor;
|
|
|
|
input_backward(cursor - sc);
|
|
|
|
while (nc-- > cursor)
|
|
|
|
input_delete(1);
|
|
|
|
break;
|
|
|
|
case 'b': /* "db", "cb" */
|
|
|
|
case 'B': /* implemented as B */
|
|
|
|
if (c == 'b')
|
|
|
|
vi_back_motion(command);
|
|
|
|
else
|
|
|
|
vi_Back_motion(command);
|
|
|
|
while (sc-- > cursor)
|
|
|
|
input_delete(1);
|
|
|
|
break;
|
|
|
|
case ' ': /* "d ", "c " */
|
|
|
|
input_delete(1);
|
|
|
|
break;
|
|
|
|
case '$': /* "d$", "c$" */
|
|
|
|
clear_to_eol:
|
|
|
|
while (cursor < len)
|
|
|
|
input_delete(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VICMD('p'):
|
|
|
|
input_forward();
|
|
|
|
/* fallthrough */
|
|
|
|
case VICMD('P'):
|
|
|
|
put();
|
|
|
|
break;
|
|
|
|
case VICMD('r'):
|
|
|
|
if (safe_read(0, &c, 1) < 1)
|
|
|
|
goto prepare_to_die;
|
|
|
|
if (c == 0)
|
|
|
|
beep();
|
|
|
|
else {
|
|
|
|
*(command + cursor) = c;
|
|
|
|
putchar(c);
|
|
|
|
putchar('\b');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_FEATURE_COMMAND_EDITING_VI */
|
2000-03-19 10:58:55 +05:30
|
|
|
case ESC:{
|
2005-08-05 00:34:46 +05:30
|
|
|
#if CONFIG_FEATURE_COMMAND_EDITING_VI
|
|
|
|
if (vi_mode) {
|
|
|
|
/* ESC: insert mode --> command mode */
|
|
|
|
vi_cmdmode = 1;
|
|
|
|
input_backward(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2001-02-17 00:06:04 +05:30
|
|
|
/* escape sequence follows */
|
2001-07-13 01:56:32 +05:30
|
|
|
if (safe_read(0, &c, 1) < 1)
|
|
|
|
goto prepare_to_die;
|
2001-02-17 00:06:04 +05:30
|
|
|
/* different vt100 emulations */
|
|
|
|
if (c == '[' || c == 'O') {
|
2005-08-05 00:34:46 +05:30
|
|
|
case VICMD('['):
|
|
|
|
case VICMD('O'):
|
2001-07-13 01:56:32 +05:30
|
|
|
if (safe_read(0, &c, 1) < 1)
|
|
|
|
goto prepare_to_die;
|
2001-02-17 00:06:04 +05:30
|
|
|
}
|
2004-01-22 18:12:23 +05:30
|
|
|
if (c >= '1' && c <= '9') {
|
|
|
|
unsigned char dummy;
|
|
|
|
|
|
|
|
if (safe_read(0, &dummy, 1) < 1)
|
|
|
|
goto prepare_to_die;
|
|
|
|
if(dummy != '~')
|
|
|
|
c = 0;
|
|
|
|
}
|
2001-02-17 00:06:04 +05:30
|
|
|
switch (c) {
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_COMMAND_TAB_COMPLETION
|
2003-07-28 15:26:35 +05:30
|
|
|
case '\t': /* Alt-Tab */
|
2000-03-19 10:58:55 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
input_tab(&lastWasTab);
|
|
|
|
break;
|
|
|
|
#endif
|
2002-11-27 14:59:49 +05:30
|
|
|
#if MAX_HISTORY >= 1
|
2001-02-17 00:06:04 +05:30
|
|
|
case 'A':
|
|
|
|
/* Up Arrow -- Get previous command from history */
|
2002-11-27 14:59:49 +05:30
|
|
|
if (cur_history > 0) {
|
|
|
|
get_previous_history();
|
2001-02-17 00:06:04 +05:30
|
|
|
goto rewrite_line;
|
|
|
|
} else {
|
|
|
|
beep();
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
2001-02-17 00:06:04 +05:30
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
/* Down Arrow -- Get next command in history */
|
2002-11-27 14:59:49 +05:30
|
|
|
if (!get_next_history())
|
2005-08-05 00:34:46 +05:30
|
|
|
break;
|
2001-02-17 00:06:04 +05:30
|
|
|
/* Rewrite the line with the selected history item */
|
2002-11-27 14:59:49 +05:30
|
|
|
rewrite_line:
|
2001-02-17 00:06:04 +05:30
|
|
|
/* change command */
|
2002-11-27 14:59:49 +05:30
|
|
|
len = strlen(strcpy(command, history[cur_history]));
|
2005-08-05 00:34:46 +05:30
|
|
|
/* redraw and go to eol (bol, in vi */
|
|
|
|
#if CONFIG_FEATURE_COMMAND_EDITING_VI
|
|
|
|
redraw(cmdedit_y, vi_mode ? 9999:0);
|
|
|
|
#else
|
2001-02-17 00:06:04 +05:30
|
|
|
redraw(cmdedit_y, 0);
|
2005-08-05 00:34:46 +05:30
|
|
|
#endif
|
2001-02-17 00:06:04 +05:30
|
|
|
break;
|
2002-11-27 14:59:49 +05:30
|
|
|
#endif
|
2001-02-17 00:06:04 +05:30
|
|
|
case 'C':
|
|
|
|
/* Right Arrow -- Move forward one character */
|
|
|
|
input_forward();
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
/* Left Arrow -- Move back one character */
|
|
|
|
input_backward(1);
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
/* Delete */
|
2005-08-05 00:34:46 +05:30
|
|
|
input_delete(0);
|
2001-02-17 00:06:04 +05:30
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
case 'H':
|
2004-02-22 16:43:28 +05:30
|
|
|
/* <Home> */
|
2001-02-17 00:06:04 +05:30
|
|
|
input_backward(cursor);
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
case 'F':
|
2004-02-22 16:43:28 +05:30
|
|
|
/* <End> */
|
2001-02-17 00:06:04 +05:30
|
|
|
input_end();
|
|
|
|
break;
|
|
|
|
default:
|
2004-01-22 18:12:23 +05:30
|
|
|
c = 0;
|
2001-02-17 00:06:04 +05:30
|
|
|
beep();
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
2001-02-17 00:06:04 +05:30
|
|
|
break;
|
|
|
|
}
|
2000-03-19 10:58:55 +05:30
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
default: /* If it's regular input, do the normal thing */
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
|
2001-02-17 00:06:04 +05:30
|
|
|
/* Control-V -- Add non-printable symbol */
|
2005-08-05 00:34:46 +05:30
|
|
|
if (c == CNTRL('V')) {
|
2001-07-13 01:56:32 +05:30
|
|
|
if (safe_read(0, &c, 1) < 1)
|
|
|
|
goto prepare_to_die;
|
2001-02-17 00:06:04 +05:30
|
|
|
if (c == 0) {
|
|
|
|
beep();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
2005-08-05 00:34:46 +05:30
|
|
|
{
|
|
|
|
#if CONFIG_FEATURE_COMMAND_EDITING_VI
|
|
|
|
if (vi_cmdmode) /* don't self-insert */
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
if (!Isprint(c)) /* Skip non-printable characters */
|
|
|
|
break;
|
|
|
|
}
|
2000-03-19 10:58:55 +05:30
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
len++;
|
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
if (cursor == (len - 1)) { /* Append if at the end of the line */
|
2000-04-12 23:19:52 +05:30
|
|
|
*(command + cursor) = c;
|
2001-02-17 00:06:04 +05:30
|
|
|
*(command + cursor + 1) = 0;
|
|
|
|
cmdedit_set_out_char(0);
|
2003-07-28 15:26:35 +05:30
|
|
|
} else { /* Insert otherwise */
|
2001-02-17 00:06:04 +05:30
|
|
|
int sc = cursor;
|
2000-03-19 10:58:55 +05:30
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
memmove(command + sc + 1, command + sc, len - sc);
|
|
|
|
*(command + sc) = c;
|
|
|
|
sc++;
|
2001-01-27 02:12:23 +05:30
|
|
|
/* rewrite from cursor */
|
2001-02-17 00:06:04 +05:30
|
|
|
input_end();
|
2001-01-27 02:12:23 +05:30
|
|
|
/* to prev x pos + 1 */
|
2001-02-17 00:06:04 +05:30
|
|
|
input_backward(cursor - sc);
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
|
|
|
}
|
2003-07-28 15:26:35 +05:30
|
|
|
if (break_out) /* Enter is the command terminator, no more input. */
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
2001-02-17 00:06:04 +05:30
|
|
|
|
|
|
|
if (c != '\t')
|
|
|
|
lastWasTab = FALSE;
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2001-06-26 07:36:08 +05:30
|
|
|
setTermSettings(0, (void *) &initial_settings);
|
2001-01-27 02:12:23 +05:30
|
|
|
handlers_sets &= ~SET_RESET_TERM;
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2002-11-27 14:59:49 +05:30
|
|
|
#if MAX_HISTORY >= 1
|
2000-03-19 10:58:55 +05:30
|
|
|
/* Handle command history log */
|
2002-11-27 14:59:49 +05:30
|
|
|
/* cleanup may be saved current command line */
|
2004-02-04 13:54:39 +05:30
|
|
|
if (len> 0) { /* no put empty line */
|
2002-11-27 14:59:49 +05:30
|
|
|
int i = n_history;
|
2004-02-22 16:43:28 +05:30
|
|
|
|
|
|
|
free(history[MAX_HISTORY]);
|
|
|
|
history[MAX_HISTORY] = 0;
|
2000-03-19 10:58:55 +05:30
|
|
|
/* After max history, remove the oldest command */
|
2002-11-27 14:59:49 +05:30
|
|
|
if (i >= MAX_HISTORY) {
|
|
|
|
free(history[0]);
|
|
|
|
for(i = 0; i < (MAX_HISTORY-1); i++)
|
|
|
|
history[i] = history[i+1];
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
2003-03-19 14:43:01 +05:30
|
|
|
history[i++] = bb_xstrdup(command);
|
2002-11-27 14:59:49 +05:30
|
|
|
cur_history = i;
|
|
|
|
n_history = i;
|
2001-10-24 10:30:29 +05:30
|
|
|
#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
|
2001-02-17 00:06:04 +05:30
|
|
|
num_ok_lines++;
|
|
|
|
#endif
|
2000-03-16 13:39:57 +05:30
|
|
|
}
|
2002-11-27 14:59:49 +05:30
|
|
|
#else /* MAX_HISTORY < 1 */
|
|
|
|
#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
|
2004-02-04 13:54:39 +05:30
|
|
|
if (len > 0) { /* no put empty line */
|
2002-11-27 14:59:49 +05:30
|
|
|
num_ok_lines++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* MAX_HISTORY >= 1 */
|
2003-12-24 01:54:51 +05:30
|
|
|
if (break_out > 0) {
|
2003-07-28 15:26:35 +05:30
|
|
|
command[len++] = '\n'; /* set '\n' */
|
|
|
|
command[len] = 0;
|
2001-07-17 06:42:36 +05:30
|
|
|
}
|
2001-10-24 10:30:29 +05:30
|
|
|
#if defined(CONFIG_FEATURE_CLEAN_UP) && defined(CONFIG_FEATURE_COMMAND_TAB_COMPLETION)
|
2003-07-28 15:26:35 +05:30
|
|
|
input_tab(0); /* strong free */
|
2001-02-17 00:06:04 +05:30
|
|
|
#endif
|
2001-10-24 10:30:29 +05:30
|
|
|
#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
|
2001-02-17 00:06:04 +05:30
|
|
|
free(cmdedit_prompt);
|
|
|
|
#endif
|
2001-07-13 01:56:32 +05:30
|
|
|
cmdedit_reset_term();
|
2001-07-17 06:42:36 +05:30
|
|
|
return len;
|
2000-03-16 13:39:57 +05:30
|
|
|
}
|
|
|
|
|
2001-01-04 16:38:45 +05:30
|
|
|
|
2000-07-28 20:44:45 +05:30
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
#endif /* CONFIG_FEATURE_COMMAND_EDITING */
|
2001-02-17 00:06:04 +05:30
|
|
|
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
const char *bb_applet_name = "debug stuff usage";
|
2001-04-10 04:18:12 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
|
2001-03-16 02:21:09 +05:30
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
|
|
|
|
2001-02-17 00:06:04 +05:30
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char buff[BUFSIZ];
|
|
|
|
char *prompt =
|
2001-10-24 10:30:29 +05:30
|
|
|
#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
|
2001-03-14 04:27:56 +05:30
|
|
|
"\\[\\033[32;1m\\]\\u@\\[\\x1b[33;1m\\]\\h:\
|
2001-02-17 00:06:04 +05:30
|
|
|
\\[\\033[34;1m\\]\\w\\[\\033[35;1m\\] \
|
2001-03-14 04:27:56 +05:30
|
|
|
\\!\\[\\e[36;1m\\]\\$ \\[\\E[0m\\]";
|
2001-02-17 00:06:04 +05:30
|
|
|
#else
|
|
|
|
"% ";
|
|
|
|
#endif
|
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
|
2001-03-16 02:21:09 +05:30
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
#endif
|
2001-07-13 01:56:32 +05:30
|
|
|
while(1) {
|
2001-03-14 04:27:56 +05:30
|
|
|
int l;
|
2003-12-24 01:54:51 +05:30
|
|
|
l = cmdedit_read_input(prompt, buff);
|
|
|
|
if(l > 0 && buff[l-1] == '\n') {
|
2001-03-14 04:27:56 +05:30
|
|
|
buff[l-1] = 0;
|
2003-12-24 01:54:51 +05:30
|
|
|
printf("*** cmdedit_read_input() returned line =%s=\n", buff);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2001-07-13 01:56:32 +05:30
|
|
|
}
|
2003-12-24 01:54:51 +05:30
|
|
|
printf("*** cmdedit_read_input() detect ^D\n");
|
2001-02-17 00:06:04 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2000-07-28 20:44:45 +05:30
|
|
|
|
2003-07-28 15:26:35 +05:30
|
|
|
#endif /* TEST */
|