2000-03-19 10:58:55 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2000-03-16 13:39:57 +05:30
|
|
|
/*
|
2000-04-13 06:48:56 +05:30
|
|
|
* Termios command line History and Editting, originally
|
|
|
|
* intended for NetBSD sh (ash)
|
2000-03-16 13:39:57 +05:30
|
|
|
* Copyright (c) 1999
|
|
|
|
* Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
|
|
|
|
* Etc: Dave Cinege <dcinege@psychosis.com>
|
2000-04-13 06:48:56 +05:30
|
|
|
* Majorly adjusted/re-written for busybox:
|
|
|
|
* Erik Andersen <andersee@debian.org>
|
2000-03-16 13:39:57 +05:30
|
|
|
*
|
|
|
|
* You may use this code as you wish, so long as the original author(s)
|
|
|
|
* are attributed in any redistributions of the source code.
|
|
|
|
* This code is 'as is' with no warranty.
|
|
|
|
* This code may safely be consumed by a BSD or GPL license.
|
|
|
|
*
|
|
|
|
* v 0.5 19990328 Initial release
|
|
|
|
*
|
|
|
|
* Future plans: Simple file and path name completion. (like BASH)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
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-01-27 02:12:23 +05:30
|
|
|
|
|
|
|
Editor with vertical scrolling and completion by
|
|
|
|
Vladimir Oleynik. vodz@usa.net (c) 2001
|
|
|
|
|
|
|
|
Small bug: not true work if terminal size (x*y symbols) less
|
|
|
|
size (prompt + editor`s line + 2 symbols)
|
2000-03-16 13:39:57 +05:30
|
|
|
*/
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
|
|
|
|
|
2000-09-26 03:15:58 +05:30
|
|
|
#include "busybox.h"
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2000-03-16 13:39:57 +05:30
|
|
|
#ifdef BB_FEATURE_SH_COMMAND_EDITING
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2000-04-21 06:56:49 +05:30
|
|
|
#include <sys/ioctl.h>
|
2000-03-16 13:39:57 +05:30
|
|
|
#include <ctype.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
#ifdef BB_FEATURE_SH_TAB_COMPLETION
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2001-02-02 04:13:49 +05:30
|
|
|
#include "pwd_grp/pwd.h"
|
|
|
|
|
|
|
|
|
2001-01-24 04:00:04 +05:30
|
|
|
static const int MAX_HISTORY = 15; /* Maximum length of the linked list for the command line history */
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ESC = 27,
|
|
|
|
DEL = 127,
|
|
|
|
};
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2000-03-17 06:42:41 +05:30
|
|
|
#define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0)
|
|
|
|
#define whitespace(c) (((c) == ' ') || ((c) == '\t'))
|
2000-03-16 13:39:57 +05:30
|
|
|
|
|
|
|
static struct history *his_front = NULL; /* First element in command line list */
|
|
|
|
static struct history *his_end = NULL; /* Last element in command line list */
|
2000-04-21 06:56:49 +05:30
|
|
|
|
|
|
|
/* ED: sparc termios is broken: revert back to old termio handling. */
|
|
|
|
|
|
|
|
#if #cpu(sparc)
|
|
|
|
# include <termio.h>
|
|
|
|
# define termios termio
|
|
|
|
# define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp)
|
|
|
|
# define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp)
|
|
|
|
#else
|
|
|
|
# include <termios.h>
|
|
|
|
# define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
|
|
|
|
# define getTermSettings(fd,argp) tcgetattr(fd, argp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
|
|
|
|
#ifndef _POSIX_VDISABLE
|
|
|
|
#define _POSIX_VDISABLE '\0'
|
|
|
|
#endif
|
|
|
|
|
2000-04-21 06:56:49 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
static
|
|
|
|
volatile int cmdedit_termw; /* actual terminal width */
|
|
|
|
static int history_counter = 0; /* Number of commands in history list */
|
2000-04-21 06:56:49 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
static
|
|
|
|
volatile int handlers_sets = 0; /* Set next bites
|
|
|
|
when atexit() has been called
|
|
|
|
and set many "terminates" signal handlers
|
|
|
|
and winchg signal handler
|
|
|
|
and if the terminal needs to be reset upon exit
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
SET_ATEXIT = 1,
|
|
|
|
SET_TERM_HANDLERS = 2,
|
|
|
|
SET_WCHG_HANDLERS = 4,
|
|
|
|
SET_RESET_TERM = 8,
|
|
|
|
};
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2000-12-19 01:55:50 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
static int cmdedit_x; /* real x terminal position,
|
|
|
|
require put prompt in start x position */
|
|
|
|
static int cmdedit_y; /* pseudoreal y terminal position */
|
|
|
|
static int cmdedit_prmt_len; /* for fast running, without duplicate calculate */
|
|
|
|
|
|
|
|
static int cursor; /* required global for signal handler */
|
|
|
|
static int len; /* --- "" - - "" - -"- --""-- --""--- */
|
|
|
|
static char *command_ps; /* --- "" - - "" - -"- --""-- --""--- */
|
|
|
|
static const char *cmdedit_prompt;/* --- "" - - "" - -"- --""-- --""--- */
|
2000-12-19 01:55:50 +05:30
|
|
|
|
|
|
|
/* Link into lash to reset context to 0
|
|
|
|
* on ^C and such */
|
|
|
|
extern unsigned int shell_context;
|
|
|
|
|
2000-03-16 13:39:57 +05:30
|
|
|
|
|
|
|
struct history {
|
2000-03-19 10:58:55 +05:30
|
|
|
char *s;
|
|
|
|
struct history *p;
|
|
|
|
struct history *n;
|
2000-03-16 13:39:57 +05:30
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
|
|
|
struct winsize win = { 0, 0, 0, 0 };
|
2001-01-27 02:12:23 +05:30
|
|
|
static __sighandler_t previous_SIGWINCH_handler; /* for reset */
|
|
|
|
|
2001-01-31 06:00:45 +05:30
|
|
|
/* emulate signal call if not called as a sig handler */
|
2001-01-27 02:12:23 +05:30
|
|
|
if(nsig == -SIGWINCH || nsig == SIGWINCH) {
|
|
|
|
ioctl(0, TIOCGWINSZ, &win);
|
|
|
|
if (win.ws_col > 0) {
|
|
|
|
cmdedit_setwidth( win.ws_col, nsig == SIGWINCH );
|
2001-01-31 06:00:45 +05:30
|
|
|
} else {
|
|
|
|
/* Default to 79 if their console doesn't want to share */
|
|
|
|
cmdedit_setwidth( 79, nsig == SIGWINCH );
|
2001-01-31 03:53:17 +05:30
|
|
|
}
|
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
|
|
|
|
2001-01-31 03:53:17 +05:30
|
|
|
if(nsig == -SIGWINCH) /* save previous handler */
|
2001-01-27 02:12:23 +05:30
|
|
|
previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
|
|
|
|
else if(nsig == SIGWINCH) /* signaled called handler */
|
2001-01-31 03:53:17 +05:30
|
|
|
signal(SIGWINCH, win_changed); /* set for next call */
|
2001-01-27 02:12:23 +05:30
|
|
|
else /* set previous handler */
|
|
|
|
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-01-27 02:12:23 +05:30
|
|
|
if((handlers_sets & SET_RESET_TERM)!=0) {
|
2000-04-18 05:30:52 +05:30
|
|
|
/* sparc and other have broken termios support: use old termio handling. */
|
2000-04-21 06:56:49 +05:30
|
|
|
setTermSettings(fileno(stdin), (void*) &initial_settings);
|
2001-01-27 02:12:23 +05:30
|
|
|
handlers_sets &= ~SET_RESET_TERM;
|
|
|
|
}
|
|
|
|
if((handlers_sets & SET_WCHG_HANDLERS)!=0) {
|
|
|
|
/* reset SIGWINCH handler to previous (default) */
|
|
|
|
win_changed(0);
|
|
|
|
handlers_sets &= ~SET_WCHG_HANDLERS;
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
2000-07-25 23:31:20 +05:30
|
|
|
#ifdef BB_FEATURE_CLEAN_UP
|
|
|
|
if (his_front) {
|
|
|
|
struct history *n;
|
|
|
|
//while(his_front!=his_end) {
|
|
|
|
while(his_front!=his_end) {
|
|
|
|
n = his_front->n;
|
|
|
|
free(his_front->s);
|
|
|
|
free(his_front);
|
|
|
|
his_front=n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
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 */
|
|
|
|
static void cmdedit_set_out_char(int c, int next_char) {
|
|
|
|
putchar(c);
|
|
|
|
if(++cmdedit_x>=cmdedit_termw) {
|
|
|
|
/* terminal is scrolled down */
|
|
|
|
cmdedit_y++;
|
|
|
|
cmdedit_x=0;
|
|
|
|
|
|
|
|
if(!next_char)
|
|
|
|
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-01-27 02:12:23 +05:30
|
|
|
/* Move to end line. Bonus: rewrite line from cursor without use
|
|
|
|
special control terminal strings, also saved size and speed! */
|
|
|
|
static void input_end (void) {
|
|
|
|
while(cursor < len)
|
|
|
|
cmdedit_set_out_char(command_ps[cursor], 0);
|
|
|
|
}
|
2000-07-04 11:52:18 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
/* Go to the next line */
|
|
|
|
static void goto_new_line(void) {
|
|
|
|
input_end();
|
|
|
|
cmdedit_set_out_char('\n', 0);
|
|
|
|
}
|
2000-07-04 11:52:18 +05:30
|
|
|
|
2000-03-19 10:58:55 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
static inline void out1str(const char *s) { fputs (s, stdout); }
|
|
|
|
static inline void beep (void) { putchar('\007'); }
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
/* Go to HOME position */
|
|
|
|
static void input_home(void)
|
|
|
|
{
|
|
|
|
while(cmdedit_y>0) { /* up to start y */
|
|
|
|
out1str("\033[A");
|
|
|
|
cmdedit_y--;
|
|
|
|
}
|
|
|
|
putchar('\r');
|
|
|
|
cursor = 0;
|
|
|
|
out1str(cmdedit_prompt);
|
|
|
|
cmdedit_x = cmdedit_prmt_len;
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
/* Move back one charactor */
|
|
|
|
static void input_backward(void) {
|
|
|
|
if (cursor > 0) {
|
|
|
|
cursor--;
|
|
|
|
if(cmdedit_x!=0) { /* no first position in terminal line */
|
|
|
|
putchar('\b');
|
|
|
|
cmdedit_x--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
out1str("\033[A"); /* up */
|
|
|
|
cmdedit_y--;
|
|
|
|
|
|
|
|
/* to end in current terminal line */
|
|
|
|
while(cmdedit_x<(cmdedit_termw-1)) {
|
|
|
|
out1str("\033[C");
|
|
|
|
cmdedit_x++;
|
|
|
|
}
|
|
|
|
}
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
2000-03-16 13:39:57 +05:30
|
|
|
}
|
|
|
|
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Delete the char in front of the cursor */
|
2001-01-27 02:12:23 +05:30
|
|
|
static void input_delete(void)
|
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-01-27 02:12:23 +05:30
|
|
|
memmove (command_ps + j, command_ps + j + 1, BUFSIZ - j - 1);
|
|
|
|
len--;
|
|
|
|
input_end(); /* rewtite new line */
|
|
|
|
cmdedit_set_out_char(' ', 0); /* destroy end char */
|
|
|
|
while (j < cursor)
|
|
|
|
input_backward(); /* back to old pos cursor */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete the char in back of the cursor */
|
|
|
|
static void input_backspace(void)
|
|
|
|
{
|
|
|
|
if (cursor > 0) {
|
|
|
|
input_backward();
|
|
|
|
input_delete ();
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
}
|
2000-04-12 23:19:52 +05:30
|
|
|
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
/* Move forward one charactor */
|
|
|
|
static void input_forward(void)
|
|
|
|
{
|
|
|
|
if (cursor < len)
|
|
|
|
cmdedit_set_out_char(command_ps[cursor], command_ps[cursor + 1]);
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
|
|
|
|
static void clean_up_and_die(int sig)
|
2000-04-12 23:19:52 +05:30
|
|
|
{
|
2001-01-27 02:12:23 +05:30
|
|
|
goto_new_line();
|
|
|
|
if (sig!=SIGINT)
|
|
|
|
exit(EXIT_SUCCESS); /* cmdedit_reset_term() called in atexit */
|
|
|
|
cmdedit_reset_term();
|
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-01-27 02:12:23 +05:30
|
|
|
cmdedit_termw = cmdedit_prmt_len+2;
|
|
|
|
if (w > cmdedit_termw) {
|
|
|
|
|
|
|
|
cmdedit_termw = w;
|
|
|
|
|
|
|
|
if(redraw_flg) {
|
|
|
|
int sav_cursor = cursor;
|
|
|
|
|
|
|
|
/* set variables for new terminal size */
|
|
|
|
cmdedit_y = sav_cursor/w;
|
|
|
|
cmdedit_x = sav_cursor-cmdedit_y*w;
|
|
|
|
|
|
|
|
/* redraw */
|
|
|
|
input_home();
|
|
|
|
input_end();
|
|
|
|
while(sav_cursor<cursor)
|
|
|
|
input_backward();
|
|
|
|
}
|
|
|
|
} else {
|
2001-02-01 00:30:21 +05:30
|
|
|
error_msg("\n*** Error: minimum screen width is %d", cmdedit_termw);
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
extern void cmdedit_init(void)
|
|
|
|
{
|
|
|
|
if((handlers_sets & SET_WCHG_HANDLERS)==0) {
|
2001-01-31 03:53:17 +05:30
|
|
|
/* pretend we received a signal in order to set term size and sig handling */
|
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-01-27 02:12:23 +05:30
|
|
|
if((handlers_sets & SET_ATEXIT)==0) {
|
|
|
|
atexit(cmdedit_reset_term); /* be sure to do this only once */
|
|
|
|
handlers_sets |= SET_ATEXIT;
|
|
|
|
}
|
|
|
|
if((handlers_sets & SET_TERM_HANDLERS)==0) {
|
|
|
|
signal(SIGKILL, clean_up_and_die);
|
|
|
|
signal(SIGINT, clean_up_and_die);
|
|
|
|
signal(SIGQUIT, clean_up_and_die);
|
|
|
|
signal(SIGTERM, clean_up_and_die);
|
|
|
|
handlers_sets |= SET_TERM_HANDLERS;
|
|
|
|
}
|
|
|
|
}
|
2000-04-12 23:19:52 +05:30
|
|
|
|
|
|
|
#ifdef BB_FEATURE_SH_TAB_COMPLETION
|
2001-01-27 02:12:23 +05:30
|
|
|
|
|
|
|
#ifdef BB_FEATURE_USERNAME_COMPLETION
|
|
|
|
static char** username_tab_completion(char *ud, int *num_matches)
|
2000-03-17 06:42:41 +05:30
|
|
|
{
|
2001-01-27 02:12:23 +05:30
|
|
|
static struct passwd *entry;
|
|
|
|
int userlen;
|
2000-03-19 10:58:55 +05:30
|
|
|
char **matches = (char **) NULL;
|
2001-01-27 02:12:23 +05:30
|
|
|
char *temp;
|
|
|
|
int nm = 0;
|
|
|
|
|
2001-01-31 03:53:17 +05:30
|
|
|
setpwent ();
|
2001-01-27 02:12:23 +05:30
|
|
|
userlen = strlen (ud + 1);
|
|
|
|
|
2001-01-31 03:53:17 +05:30
|
|
|
while ((entry = getpwent ()) != NULL) {
|
2001-01-27 02:12:23 +05:30
|
|
|
/* Null usernames should result in all users as possible completions. */
|
|
|
|
if (!userlen || !strncmp (ud + 1, entry->pw_name, userlen)) {
|
|
|
|
|
|
|
|
temp = xmalloc (3 + strlen (entry->pw_name));
|
|
|
|
sprintf(temp, "~%s/", entry->pw_name);
|
|
|
|
|
|
|
|
matches = xrealloc(matches, (nm+1)*sizeof(char *));
|
|
|
|
matches[nm++] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-31 03:53:17 +05:30
|
|
|
endpwent ();
|
2001-01-27 02:12:23 +05:30
|
|
|
(*num_matches) = nm;
|
2000-03-19 10:58:55 +05:30
|
|
|
return (matches);
|
2000-03-17 06:42:41 +05:30
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
enum {
|
|
|
|
FIND_EXE_ONLY = 0,
|
|
|
|
FIND_DIR_ONLY = 1,
|
|
|
|
FIND_FILE_ONLY = 2,
|
|
|
|
};
|
2000-03-19 16:16:06 +05:30
|
|
|
|
|
|
|
#include <dirent.h>
|
2001-01-27 02:12:23 +05:30
|
|
|
|
|
|
|
static int path_parse(char ***p, int flags)
|
|
|
|
{
|
|
|
|
int npth;
|
|
|
|
char *tmp;
|
|
|
|
char *pth;
|
|
|
|
|
|
|
|
if(flags!=FIND_EXE_ONLY || (pth=getenv("PATH"))==0) {
|
|
|
|
/* if not setenv PATH variable, to search cur dir "." */
|
|
|
|
(*p) = xmalloc(sizeof(char *));
|
|
|
|
(*p)[0] = xstrdup(".");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = pth;
|
|
|
|
npth=0;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
npth++; /* count words is + 1 count ':' */
|
|
|
|
tmp = strchr(tmp, ':');
|
|
|
|
if(tmp)
|
|
|
|
tmp++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = xmalloc(npth*sizeof(char *));
|
|
|
|
|
|
|
|
tmp = pth;
|
|
|
|
(*p)[0] = xstrdup(tmp);
|
|
|
|
npth=1; /* count words is + 1 count ':' */
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
tmp = strchr(tmp, ':');
|
|
|
|
if(tmp) {
|
|
|
|
(*p)[0][(tmp-pth)]=0; /* ':' -> '\0'*/
|
|
|
|
tmp++;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
(*p)[npth++] = &(*p)[0][(tmp-pth)]; /* p[next]=p[0][&'\0'+1] */
|
|
|
|
}
|
|
|
|
|
|
|
|
return npth;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char** exe_n_cwd_tab_completion(char* command, int *num_matches, int type)
|
2000-03-17 06:42:41 +05:30
|
|
|
{
|
2000-03-19 16:16:06 +05:30
|
|
|
char *dirName;
|
2001-01-27 02:12:23 +05:30
|
|
|
char **matches = 0;
|
2000-03-19 16:16:06 +05:30
|
|
|
DIR *dir;
|
|
|
|
struct dirent *next;
|
2001-01-27 02:12:23 +05:30
|
|
|
char cmd [BUFSIZ+4];
|
|
|
|
char *dirbuf;
|
|
|
|
char found [BUFSIZ+4];
|
|
|
|
int nm = *num_matches;
|
|
|
|
struct stat st;
|
|
|
|
char **paths;
|
|
|
|
int npaths;
|
|
|
|
int i;
|
|
|
|
char full_pth[BUFSIZ+4+PATH_MAX];
|
|
|
|
|
|
|
|
|
|
|
|
strcpy(cmd, command); /* save for change (last '/' to '\0') */
|
|
|
|
|
|
|
|
dirName = strrchr(cmd, '/');
|
|
|
|
if(dirName==NULL) {
|
|
|
|
/* no dir, if flags==EXE_ONLY - get paths, else "." */
|
|
|
|
npaths = path_parse(&paths, type);
|
|
|
|
if(npaths==0)
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
/* with dir */
|
|
|
|
|
|
|
|
/* save dir */
|
|
|
|
dirbuf = xstrdup(cmd);
|
|
|
|
/* set only dirname */
|
|
|
|
dirbuf[(dirName-cmd)+1]=0;
|
|
|
|
|
|
|
|
/* strip dirname in cmd */
|
|
|
|
strcpy(cmd, dirName+1);
|
2000-03-19 16:16:06 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
paths = xmalloc(sizeof(char*));
|
|
|
|
paths[0] = dirbuf;
|
|
|
|
npaths = 1; /* only 1 dir */
|
|
|
|
}
|
2000-03-19 16:16:06 +05:30
|
|
|
|
2001-01-27 02:12:23 +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]);
|
2000-03-19 16:16:06 +05:30
|
|
|
if (!dir) {
|
|
|
|
/* Don't print an error, just shut up and return */
|
|
|
|
return (matches);
|
|
|
|
}
|
|
|
|
while ((next = readdir(dir)) != NULL) {
|
2001-01-27 02:12:23 +05:30
|
|
|
/* matched ? */
|
|
|
|
if(strncmp(next->d_name, cmd, strlen(cmd)))
|
|
|
|
continue;
|
|
|
|
/* not see .name without .match */
|
|
|
|
if(*next->d_name == '.' && *cmd != '.')
|
|
|
|
continue;
|
|
|
|
sprintf(full_pth, "%s/%s", paths[i], next->d_name);
|
|
|
|
/* hmm, remover in progress? */
|
|
|
|
if(stat(full_pth, &st)<0)
|
|
|
|
continue;
|
|
|
|
/* Cool, found a match. */
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
/* name is directory */
|
|
|
|
strcpy(found, next->d_name);
|
|
|
|
strcat(found, "/");
|
|
|
|
if(type==FIND_DIR_ONLY)
|
|
|
|
strcat(found, " ");
|
|
|
|
} else {
|
|
|
|
/* not put found file if search only dirs for cd */
|
|
|
|
if(type==FIND_DIR_ONLY)
|
2000-03-19 16:16:06 +05:30
|
|
|
continue;
|
2001-01-27 02:12:23 +05:30
|
|
|
strcpy(found, next->d_name);
|
|
|
|
strcat(found, " ");
|
2000-03-19 16:16:06 +05:30
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
/* Add it to the list */
|
|
|
|
matches = xrealloc(matches, (nm+1)*sizeof(char *));
|
|
|
|
matches[nm++] = xstrdup(found);
|
2000-03-19 16:16:06 +05:30
|
|
|
}
|
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
free(paths[0]); /* allocate memory only in first member */
|
|
|
|
free(paths);
|
|
|
|
*num_matches = nm;
|
2000-03-19 10:58:55 +05:30
|
|
|
return (matches);
|
2000-03-17 06:42:41 +05:30
|
|
|
}
|
2000-04-12 23:19:52 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
static void input_tab(int lastWasTab)
|
2000-04-12 23:19:52 +05:30
|
|
|
{
|
|
|
|
/* Do TAB completion */
|
2001-01-27 02:12:23 +05:30
|
|
|
static int num_matches;
|
|
|
|
static char **matches;
|
|
|
|
|
|
|
|
char matchBuf[BUFSIZ];
|
|
|
|
|
|
|
|
int pos = cursor;
|
|
|
|
int find_type=FIND_FILE_ONLY;
|
2000-04-12 23:19:52 +05:30
|
|
|
|
|
|
|
|
|
|
|
if (lastWasTab == FALSE) {
|
2001-01-27 02:12:23 +05:30
|
|
|
char *tmp, *tmp1;
|
|
|
|
int len_found;
|
2000-04-12 23:19:52 +05:30
|
|
|
|
|
|
|
/* For now, we will not bother with trying to distinguish
|
|
|
|
* whether the cursor is in/at a command extression -- we
|
2000-06-17 01:27:44 +05:30
|
|
|
* will always try all possible matches. If you don't like
|
2000-04-12 23:19:52 +05:30
|
|
|
* that then feel free to fix it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Make a local copy of the string -- up
|
|
|
|
* to the position of the cursor */
|
2001-01-27 02:12:23 +05:30
|
|
|
memset(matchBuf, 0, BUFSIZ);
|
|
|
|
tmp = strncpy(matchBuf, command_ps, cursor);
|
2000-04-12 23:19:52 +05:30
|
|
|
|
|
|
|
/* skip past any command seperator tokens */
|
2001-01-27 02:12:23 +05:30
|
|
|
while ( (tmp1=strpbrk(tmp, ";|&{(`")) != NULL) {
|
|
|
|
tmp = ++tmp1;
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* skip any leading white space */
|
2001-01-27 02:12:23 +05:30
|
|
|
while (*tmp == ' ')
|
|
|
|
tmp++;
|
|
|
|
|
|
|
|
if(strncmp(tmp, "cd ", 3)==0)
|
|
|
|
find_type = FIND_DIR_ONLY;
|
|
|
|
else if(strchr(tmp, ' ')==NULL)
|
|
|
|
find_type = FIND_EXE_ONLY;
|
|
|
|
|
|
|
|
/* find begin curent word */
|
|
|
|
if( (tmp1=strrchr(tmp, ' ')) != NULL) {
|
|
|
|
tmp = ++tmp1;
|
|
|
|
}
|
|
|
|
strcpy(matchBuf, tmp);
|
2000-04-12 23:19:52 +05:30
|
|
|
|
|
|
|
/* Free up any memory already allocated */
|
|
|
|
if (matches) {
|
2001-01-27 02:12:23 +05:30
|
|
|
while(num_matches>0)
|
|
|
|
free(matches[--num_matches]);
|
2000-04-12 23:19:52 +05:30
|
|
|
free(matches);
|
|
|
|
matches = (char **) NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
#ifdef BB_FEATURE_USERNAME_COMPLETION
|
2000-04-12 23:19:52 +05:30
|
|
|
/* If the word starts with `~' and there is no slash in the word,
|
|
|
|
* then try completing this word as a username. */
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
if (matchBuf[0]=='~' && strchr(matchBuf, '/')==0) {
|
|
|
|
matches = username_tab_completion(matchBuf, &num_matches);
|
|
|
|
}
|
|
|
|
#endif
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Try to match any executable in our path and everything
|
|
|
|
* in the current working directory that matches. */
|
|
|
|
if (!matches)
|
2001-01-27 02:12:23 +05:30
|
|
|
matches = exe_n_cwd_tab_completion(matchBuf, &num_matches, find_type);
|
2000-04-12 23:19:52 +05:30
|
|
|
|
|
|
|
/* Did we find exactly one match? */
|
2001-01-27 02:12:23 +05:30
|
|
|
if(!matches || num_matches>1) {
|
|
|
|
beep();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
len_found = strlen(matches[0]);
|
|
|
|
|
|
|
|
/* have space to placed match? */
|
|
|
|
if ( (len_found-strlen(matchBuf)+len) < BUFSIZ ) {
|
|
|
|
|
|
|
|
int recalc_pos = len;
|
|
|
|
|
|
|
|
/* before word for match */
|
|
|
|
command_ps[pos-strlen(matchBuf)]=0;
|
|
|
|
|
|
|
|
/* tail line */
|
|
|
|
strcpy(matchBuf, command_ps+pos);
|
|
|
|
|
|
|
|
/* add match */
|
|
|
|
strcat(command_ps, matches[0]);
|
|
|
|
/* add tail */
|
|
|
|
strcat(command_ps, matchBuf);
|
|
|
|
|
2000-04-12 23:19:52 +05:30
|
|
|
/* write out the matched command */
|
2001-01-27 02:12:23 +05:30
|
|
|
len=strlen(command_ps);
|
|
|
|
recalc_pos = len-recalc_pos+pos;
|
|
|
|
input_end(); /* write */
|
|
|
|
while(recalc_pos<cursor)
|
|
|
|
input_backward();
|
2000-11-02 22:32:26 +05:30
|
|
|
return;
|
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... */
|
|
|
|
if ( matches && num_matches>0 ) {
|
|
|
|
int i, col;
|
2001-01-27 02:12:23 +05:30
|
|
|
int sav_cursor = cursor;
|
2000-04-12 23:19:52 +05:30
|
|
|
|
|
|
|
/* Go to the next line */
|
2001-01-27 02:12:23 +05:30
|
|
|
goto_new_line();
|
2000-04-12 23:19:52 +05:30
|
|
|
for (i=0,col=0; i<num_matches; i++) {
|
2001-01-27 02:12:23 +05:30
|
|
|
printf("%s ", matches[i]);
|
|
|
|
col += strlen(matches[i])+2;
|
|
|
|
col -= (col/cmdedit_termw)*cmdedit_termw;
|
2000-04-12 23:19:52 +05:30
|
|
|
if (col > 60 && matches[i+1] != NULL) {
|
2001-01-27 02:12:23 +05:30
|
|
|
putchar('\n');
|
2000-04-12 23:19:52 +05:30
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
}
|
2001-01-27 02:12:23 +05:30
|
|
|
/* Go to the next line and rewrite the prompt */
|
|
|
|
printf("\n%s", cmdedit_prompt);
|
|
|
|
cmdedit_x = cmdedit_prmt_len;
|
|
|
|
cmdedit_y = 0;
|
|
|
|
cursor = 0;
|
|
|
|
input_end(); /* Rewrite the command */
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Put the cursor back to where it used to be */
|
2001-01-27 02:12:23 +05:30
|
|
|
while (sav_cursor < cursor)
|
|
|
|
input_backward();
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-01-04 16:38:45 +05:30
|
|
|
static void get_previous_history(struct history **hp, char* command)
|
2000-04-12 23:19:52 +05:30
|
|
|
{
|
2000-07-19 23:07:57 +05:30
|
|
|
if ((*hp)->s)
|
|
|
|
free((*hp)->s);
|
2000-04-12 23:19:52 +05:30
|
|
|
(*hp)->s = strdup(command);
|
|
|
|
*hp = (*hp)->p;
|
|
|
|
}
|
|
|
|
|
2001-01-04 16:38:45 +05:30
|
|
|
static void get_next_history(struct history **hp, char* command)
|
2000-04-12 23:19:52 +05:30
|
|
|
{
|
2000-07-19 23:07:57 +05:30
|
|
|
if ((*hp)->s)
|
|
|
|
free((*hp)->s);
|
2000-04-12 23:19:52 +05:30
|
|
|
(*hp)->s = strdup(command);
|
|
|
|
*hp = (*hp)->n;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* a string with full command editing (sortof like
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* Furthermore, the "vi" command editing keys are not implemented.
|
|
|
|
*
|
|
|
|
* TODO: implement TAB command completion. :)
|
|
|
|
*/
|
2000-04-12 23:19:52 +05:30
|
|
|
extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
|
2000-03-16 13:39:57 +05:30
|
|
|
{
|
|
|
|
|
2000-04-12 23:19:52 +05:30
|
|
|
int inputFd=fileno(stdin);
|
2001-01-27 02:12:23 +05:30
|
|
|
|
2000-03-19 10:58:55 +05:30
|
|
|
int j = 0;
|
|
|
|
int break_out = 0;
|
|
|
|
int ret = 0;
|
|
|
|
int lastWasTab = FALSE;
|
|
|
|
char c = 0;
|
|
|
|
struct history *hp = his_end;
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
len = 0;
|
|
|
|
cursor = 0;
|
|
|
|
command_ps = command;
|
|
|
|
|
|
|
|
if (new_settings.c_cc[VMIN]==0) {
|
2000-04-21 06:56:49 +05:30
|
|
|
|
|
|
|
getTermSettings(inputFd, (void*) &initial_settings);
|
|
|
|
memcpy(&new_settings, &initial_settings, sizeof(struct termios));
|
|
|
|
new_settings.c_cc[VMIN] = 1;
|
|
|
|
new_settings.c_cc[VTIME] = 0;
|
|
|
|
new_settings.c_cc[VINTR] = _POSIX_VDISABLE; /* Turn off CTRL-C, so we can trap it */
|
|
|
|
new_settings.c_lflag &= ~ICANON; /* unbuffered input */
|
|
|
|
new_settings.c_lflag &= ~(ECHO|ECHOCTL|ECHONL); /* Turn off echoing */
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
2000-04-21 06:56:49 +05:30
|
|
|
setTermSettings(inputFd, (void*) &new_settings);
|
2001-01-27 02:12:23 +05:30
|
|
|
handlers_sets |= SET_RESET_TERM;
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2000-04-12 23:19:52 +05:30
|
|
|
memset(command, 0, BUFSIZ);
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
cmdedit_init();
|
|
|
|
|
2001-01-04 16:38:45 +05:30
|
|
|
/* Print out the command prompt */
|
2001-01-27 02:12:23 +05:30
|
|
|
cmdedit_prompt = prompt;
|
|
|
|
cmdedit_prmt_len = strlen(prompt);
|
|
|
|
printf("%s", prompt);
|
|
|
|
cmdedit_x = cmdedit_prmt_len; /* count real x terminal position */
|
|
|
|
cmdedit_y = 0; /* quasireal y, not true work if line > xt*yt */
|
|
|
|
|
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
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
fflush(stdout); /* buffered out to fast */
|
|
|
|
|
2000-03-16 13:39:57 +05:30
|
|
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
2000-04-12 23:19:52 +05:30
|
|
|
return;
|
2000-04-21 06:56:49 +05:30
|
|
|
//fprintf(stderr, "got a '%c' (%d)\n", c, c);
|
2000-04-09 23:54:05 +05:30
|
|
|
|
2000-03-16 13:39:57 +05:30
|
|
|
switch (c) {
|
2000-04-12 23:19:52 +05:30
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
/* Enter */
|
2001-01-27 02:12:23 +05:30
|
|
|
*(command + len) = c;
|
|
|
|
len++;
|
|
|
|
input_end ();
|
2000-04-12 23:19:52 +05:30
|
|
|
break_out = 1;
|
|
|
|
break;
|
2000-03-19 10:58:55 +05:30
|
|
|
case 1:
|
|
|
|
/* Control-a -- Beginning of line */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_home();
|
|
|
|
break;
|
2000-03-19 10:58:55 +05:30
|
|
|
case 2:
|
|
|
|
/* Control-b -- Move back one character */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_backward();
|
2000-04-21 06:56:49 +05:30
|
|
|
break;
|
|
|
|
case 3:
|
2000-12-19 01:55:50 +05:30
|
|
|
/* Control-c -- stop gathering input */
|
|
|
|
|
|
|
|
/* Link into lash to reset context to 0 on ^C and such */
|
|
|
|
shell_context = 0;
|
2000-04-21 06:56:49 +05:30
|
|
|
|
|
|
|
/* Go to the next line */
|
2001-01-27 02:12:23 +05:30
|
|
|
goto_new_line();
|
2000-04-21 06:56:49 +05:30
|
|
|
|
2000-12-19 01:55:50 +05:30
|
|
|
#if 0
|
2000-04-21 06:56:49 +05:30
|
|
|
/* Rewrite the prompt */
|
2001-01-27 02:12:23 +05:30
|
|
|
printf("%s", prompt);
|
2000-04-21 06:56:49 +05:30
|
|
|
|
|
|
|
/* Reset the command string */
|
2000-07-14 06:43:37 +05:30
|
|
|
memset(command, 0, BUFSIZ);
|
2000-04-21 06:56:49 +05:30
|
|
|
len = cursor = 0;
|
2000-12-19 01:55:50 +05:30
|
|
|
#endif
|
|
|
|
return;
|
2000-04-21 06:56:49 +05:30
|
|
|
|
2000-04-12 23:19:52 +05:30
|
|
|
case 4:
|
|
|
|
/* Control-d -- Delete one character, or exit
|
|
|
|
* if the len=0 and no chars to delete */
|
|
|
|
if (len == 0) {
|
2001-01-27 02:12:23 +05:30
|
|
|
printf("exit");
|
2000-04-12 23:19:52 +05:30
|
|
|
clean_up_and_die(0);
|
|
|
|
} else {
|
2001-01-27 02:12:23 +05:30
|
|
|
input_delete();
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
|
|
|
break;
|
2000-04-12 23:19:52 +05:30
|
|
|
case 5:
|
|
|
|
/* Control-e -- End of line */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_end();
|
2000-04-12 23:19:52 +05:30
|
|
|
break;
|
2000-03-19 10:58:55 +05:30
|
|
|
case 6:
|
|
|
|
/* 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':
|
|
|
|
#ifdef BB_FEATURE_SH_TAB_COMPLETION
|
2001-01-27 02:12:23 +05:30
|
|
|
input_tab(lastWasTab);
|
2000-04-12 23:19:52 +05:30
|
|
|
#endif
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
|
|
|
case 14:
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Control-n -- Get next command in history */
|
2000-03-19 10:58:55 +05:30
|
|
|
if (hp && hp->n && hp->n->s) {
|
2000-04-12 23:19:52 +05:30
|
|
|
get_next_history(&hp, command);
|
|
|
|
goto rewrite_line;
|
|
|
|
} else {
|
2001-01-27 02:12:23 +05:30
|
|
|
beep();
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16:
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Control-p -- Get previous command from history */
|
2000-03-19 10:58:55 +05:30
|
|
|
if (hp && hp->p) {
|
2000-04-12 23:19:52 +05:30
|
|
|
get_previous_history(&hp, command);
|
|
|
|
goto rewrite_line;
|
|
|
|
} else {
|
2001-01-27 02:12:23 +05:30
|
|
|
beep();
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ESC:{
|
|
|
|
/* escape sequence follows */
|
|
|
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
2000-04-12 23:19:52 +05:30
|
|
|
return;
|
2000-03-19 10:58:55 +05:30
|
|
|
|
|
|
|
if (c == '[') { /* 91 */
|
|
|
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
2000-04-12 23:19:52 +05:30
|
|
|
return;
|
2000-03-19 10:58:55 +05:30
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'A':
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Up Arrow -- Get previous command from history */
|
2000-03-19 10:58:55 +05:30
|
|
|
if (hp && hp->p) {
|
2000-04-12 23:19:52 +05:30
|
|
|
get_previous_history(&hp, command);
|
|
|
|
goto rewrite_line;
|
|
|
|
} else {
|
2001-01-27 02:12:23 +05:30
|
|
|
beep();
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'B':
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Down Arrow -- Get next command in history */
|
2000-03-19 10:58:55 +05:30
|
|
|
if (hp && hp->n && hp->n->s) {
|
2000-04-12 23:19:52 +05:30
|
|
|
get_next_history(&hp, command);
|
|
|
|
goto rewrite_line;
|
|
|
|
} else {
|
2001-01-27 02:12:23 +05:30
|
|
|
beep();
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-04-12 23:19:52 +05:30
|
|
|
/* Rewrite the line with the selected history item */
|
|
|
|
rewrite_line:
|
2001-01-27 02:12:23 +05:30
|
|
|
/* return to begin of line */
|
|
|
|
input_home ();
|
|
|
|
/* for next memmoves without set '\0' */
|
|
|
|
memset (command, 0, BUFSIZ);
|
|
|
|
/* change command */
|
|
|
|
strcpy (command, hp->s);
|
2000-03-19 10:58:55 +05:30
|
|
|
/* write new command */
|
2001-01-27 02:12:23 +05:30
|
|
|
for (j=0; command[j]; j++)
|
|
|
|
cmdedit_set_out_char(command[j], 0);
|
|
|
|
ret = cursor;
|
|
|
|
/* erase tail if required */
|
|
|
|
for (j = ret; j < len; j++)
|
|
|
|
cmdedit_set_out_char(' ', 0);
|
|
|
|
/* and backward cursor */
|
|
|
|
for (j = ret; j < len; j++)
|
|
|
|
input_backward();
|
|
|
|
len = cursor; /* set new len */
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
/* Right Arrow -- Move forward one character */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_forward();
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
/* Left Arrow -- Move back one character */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_backward();
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
/* Delete */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_delete();
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
/* Home (Ctrl-A) */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_home();
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
/* End (Ctrl-E) */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_end();
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
2000-04-12 23:19:52 +05:30
|
|
|
default:
|
2001-01-27 02:12:23 +05:30
|
|
|
beep();
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
|
|
|
if (c == '1' || c == '3' || c == '4')
|
|
|
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
2000-04-12 23:19:52 +05:30
|
|
|
return; /* read 126 (~) */
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
|
|
|
if (c == 'O') {
|
|
|
|
/* 79 */
|
|
|
|
if ((ret = read(inputFd, &c, 1)) < 1)
|
2000-04-12 23:19:52 +05:30
|
|
|
return;
|
2000-03-19 10:58:55 +05:30
|
|
|
switch (c) {
|
|
|
|
case 'H':
|
|
|
|
/* Home (xterm) */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_home();
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
/* End (xterm) */
|
2001-01-27 02:12:23 +05:30
|
|
|
input_end();
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
2000-04-12 23:19:52 +05:30
|
|
|
default:
|
2001-01-27 02:12:23 +05:30
|
|
|
beep();
|
2000-03-19 10:58:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
c = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: /* If it's regular input, do the normal thing */
|
|
|
|
|
2000-04-12 23:19:52 +05:30
|
|
|
if (!isprint(c)) { /* Skip non-printable characters */
|
2000-03-19 10:58:55 +05:30
|
|
|
break;
|
2000-04-12 23:19:52 +05:30
|
|
|
}
|
2000-03-19 10:58:55 +05:30
|
|
|
|
|
|
|
if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
|
|
|
|
break;
|
|
|
|
|
|
|
|
len++;
|
|
|
|
|
|
|
|
if (cursor == (len - 1)) { /* Append if at the end of the line */
|
2000-04-12 23:19:52 +05:30
|
|
|
*(command + cursor) = c;
|
2001-01-27 02:12:23 +05:30
|
|
|
cmdedit_set_out_char(c, command[cursor+1]);
|
2000-03-19 10:58:55 +05:30
|
|
|
} else { /* Insert otherwise */
|
2000-04-12 23:19:52 +05:30
|
|
|
memmove(command + cursor + 1, command + cursor,
|
2000-03-19 10:58:55 +05:30
|
|
|
len - cursor - 1);
|
|
|
|
|
2000-04-12 23:19:52 +05:30
|
|
|
*(command + cursor) = c;
|
2001-01-27 02:12:23 +05:30
|
|
|
j = cursor+1;
|
|
|
|
/* rewrite from cursor */
|
|
|
|
input_end ();
|
|
|
|
/* to prev x pos + 1 */
|
|
|
|
while(cursor > j)
|
|
|
|
input_backward();
|
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;
|
|
|
|
}
|
|
|
|
if (c == '\t')
|
|
|
|
lastWasTab = TRUE;
|
|
|
|
else
|
|
|
|
lastWasTab = FALSE;
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2000-03-19 10:58:55 +05:30
|
|
|
if (break_out) /* Enter is the command terminator, no more input. */
|
|
|
|
break;
|
|
|
|
}
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
setTermSettings (inputFd, (void *) &initial_settings);
|
|
|
|
handlers_sets &= ~SET_RESET_TERM;
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2000-03-19 10:58:55 +05:30
|
|
|
/* Handle command history log */
|
2001-01-27 02:12:23 +05:30
|
|
|
if (len>1) { /* no put empty line (only '\n') */
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2000-03-19 10:58:55 +05:30
|
|
|
struct history *h = his_end;
|
2001-01-27 02:12:23 +05:30
|
|
|
char *ss;
|
|
|
|
|
|
|
|
command[len-1] = 0; /* destroy end '\n' */
|
|
|
|
ss = strdup(command); /* duplicate without '\n' */
|
|
|
|
command[len-1] = '\n'; /* restore '\n' */
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2000-03-19 10:58:55 +05:30
|
|
|
if (!h) {
|
2000-07-19 23:07:57 +05:30
|
|
|
/* No previous history -- this memory is never freed */
|
2000-09-13 08:16:14 +05:30
|
|
|
h = his_front = xmalloc(sizeof(struct history));
|
|
|
|
h->n = xmalloc(sizeof(struct history));
|
2000-03-16 13:39:57 +05:30
|
|
|
|
2000-03-19 10:58:55 +05:30
|
|
|
h->p = NULL;
|
2001-01-27 02:12:23 +05:30
|
|
|
h->s = ss;
|
2000-03-19 10:58:55 +05:30
|
|
|
h->n->p = h;
|
|
|
|
h->n->n = NULL;
|
|
|
|
h->n->s = NULL;
|
|
|
|
his_end = h->n;
|
|
|
|
history_counter++;
|
|
|
|
} else {
|
2000-07-19 23:07:57 +05:30
|
|
|
/* Add a new history command -- this memory is never freed */
|
2000-09-13 08:16:14 +05:30
|
|
|
h->n = xmalloc(sizeof(struct history));
|
2000-03-19 10:58:55 +05:30
|
|
|
|
|
|
|
h->n->p = h;
|
|
|
|
h->n->n = NULL;
|
|
|
|
h->n->s = NULL;
|
2001-01-27 02:12:23 +05:30
|
|
|
h->s = ss;
|
2000-03-19 10:58:55 +05:30
|
|
|
his_end = h->n;
|
|
|
|
|
|
|
|
/* After max history, remove the oldest command */
|
|
|
|
if (history_counter >= MAX_HISTORY) {
|
|
|
|
|
|
|
|
struct history *p = his_front->n;
|
|
|
|
|
|
|
|
p->p = NULL;
|
|
|
|
free(his_front->s);
|
|
|
|
free(his_front);
|
|
|
|
his_front = p;
|
|
|
|
} else {
|
|
|
|
history_counter++;
|
|
|
|
}
|
|
|
|
}
|
2000-03-16 13:39:57 +05:30
|
|
|
}
|
|
|
|
|
2000-04-12 23:19:52 +05:30
|
|
|
return;
|
2000-03-16 13:39:57 +05:30
|
|
|
}
|
|
|
|
|
2001-01-04 16:38:45 +05:30
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
/* Undo the effects of cmdedit_init(). */
|
2000-07-28 20:44:45 +05:30
|
|
|
extern void cmdedit_terminate(void)
|
|
|
|
{
|
|
|
|
cmdedit_reset_term();
|
2001-01-27 02:12:23 +05:30
|
|
|
if((handlers_sets & SET_TERM_HANDLERS)!=0) {
|
|
|
|
signal(SIGKILL, SIG_DFL);
|
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
signal(SIGQUIT, SIG_DFL);
|
|
|
|
signal(SIGTERM, SIG_DFL);
|
|
|
|
signal(SIGWINCH, SIG_DFL);
|
|
|
|
handlers_sets &= ~SET_TERM_HANDLERS;
|
|
|
|
}
|
2000-07-28 20:44:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-01-27 02:12:23 +05:30
|
|
|
#endif /* BB_FEATURE_SH_COMMAND_EDITING */
|