/* vi: set sw=4 ts=4: */
/*
- * Termios command line History and Editting for NetBSD sh (ash)
+ * Termios command line History and Editting, originally
+ * intended for NetBSD sh (ash)
* Copyright (c) 1999
* Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
* Etc: Dave Cinege <dcinege@psychosis.com>
- * Adjusted for busybox: Erik Andersen <andersee@debian.org>
+ * Majorly adjusted/re-written for busybox:
+ * Erik Andersen <andersee@debian.org>
*
* You may use this code as you wish, so long as the original author(s)
* are attributed in any redistributions of the source code.
terminal width. (more then one line.) However, history will.
*/
-#include "internal.h"
+#include "busybox.h"
#ifdef BB_FEATURE_SH_COMMAND_EDITING
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
-#include <termio.h>
+#include <sys/ioctl.h>
#include <ctype.h>
#include <signal.h>
-#define MAX_HISTORY 15 /* Maximum length of the linked list for the command line history */
+static const int MAX_HISTORY = 15; /* Maximum length of the linked list for the command line history */
+
+enum {
+ ESC = 27,
+ DEL = 127,
+};
-#define ESC 27
-#define DEL 127
#define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0)
#define whitespace(c) (((c) == ' ') || ((c) == '\t'))
static struct history *his_front = NULL; /* First element in command line list */
static struct history *his_end = NULL; /* Last element in command line list */
-static struct termio old_term, new_term; /* Current termio and the previous termio before starting ash */
+
+/* ED: sparc termios is broken: revert back to old termio handling. */
+#ifdef BB_FEATURE_USE_TERMIOS
+
+#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 */
+static struct termios initial_settings, new_settings;
+
+
+#ifndef _POSIX_VDISABLE
+#define _POSIX_VDISABLE '\0'
+#endif
+
+#endif
+
+
static int cmdedit_termw = 80; /* actual terminal width */
static int cmdedit_scroll = 27; /* width of EOL scrolling region */
static int history_counter = 0; /* Number of commands in history list */
static int reset_term = 0; /* Set to true if the terminal needs to be reset upon exit */
+static int exithandler_set = 0; /* Set to true when atexit() has been called */
+
+
+/* Link into lash to reset context to 0
+ * on ^C and such */
+extern unsigned int shell_context;
+
struct history {
char *s;
#define xwrite write
-void
-cmdedit_setwidth(int w)
+/*
+ * TODO: Someday we want to implement 'horizontal scrolling' of the
+ * command-line when the user has typed more than the current width. This
+ * would allow the user to see a 'window' of what he has typed.
+ */
+static void cmdedit_setwidth(int w)
{
- if (w > 20) {
+ if (w > 20) {
cmdedit_termw = w;
cmdedit_scroll = w / 3;
- } else {
- errorMsg("\n*** Error: minimum screen width is 21\n");
- }
+ } else {
+ error_msg("\n*** Error: minimum screen width is 21\n");
+ }
}
-void cmdedit_reset_term(void)
+static void win_changed(int junk)
+{
+ struct winsize win = { 0, 0, 0, 0 };
+ ioctl(0, TIOCGWINSZ, &win);
+ if (win.ws_col > 0) {
+ cmdedit_setwidth( win.ws_col - 1);
+ }
+}
+
+
+static void cmdedit_reset_term(void)
{
if (reset_term)
- ioctl(fileno(stdin), TCSETA, (void *) &old_term);
+ /* sparc and other have broken termios support: use old termio handling. */
+ setTermSettings(fileno(stdin), (void*) &initial_settings);
+#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
}
-void clean_up_and_die(int sig)
+static void clean_up_and_die(int sig)
{
cmdedit_reset_term();
- fprintf(stdout, "\n");
- exit(TRUE);
+ printf("\n");
+ if (sig!=SIGINT)
+ exit(EXIT_SUCCESS);
}
/* Go to HOME position */
-void input_home(int outputFd, int *cursor)
+static void input_home(int outputFd, int *cursor)
{
while (*cursor > 0) {
xwrite(outputFd, "\b", 1);
}
/* Go to END position */
-void input_end(int outputFd, int *cursor, int len)
+static void input_end(int outputFd, int *cursor, int len)
{
while (*cursor < len) {
xwrite(outputFd, "\033[C", 3);
}
/* Delete the char in back of the cursor */
-void input_backspace(char* command, int outputFd, int *cursor, int *len)
+static void input_backspace(char* command, int outputFd, int *cursor, int *len)
{
int j = 0;
+/* Debug crap */
+//fprintf(stderr, "\nerik: len=%d, cursor=%d, strlen(command)='%d'\n", *len, *cursor, strlen(command));
+//xwrite(outputFd, command, *len);
+//*cursor = *len;
+
+
if (*cursor > 0) {
xwrite(outputFd, "\b \b", 3);
--*cursor;
}
/* Delete the char in front of the cursor */
-void input_delete(char* command, int outputFd, int cursor, int *len)
+static void input_delete(char* command, int outputFd, int cursor, int *len)
{
int j = 0;
}
/* Move forward one charactor */
-void input_forward(int outputFd, int *cursor, int len)
+static void input_forward(int outputFd, int *cursor, int len)
{
if (*cursor < len) {
xwrite(outputFd, "\033[C", 3);
}
/* Move back one charactor */
-void input_backward(int outputFd, int *cursor)
+static void input_backward(int outputFd, int *cursor)
{
if (*cursor > 0) {
xwrite(outputFd, "\033[D", 3);
#ifdef BB_FEATURE_SH_TAB_COMPLETION
-char** username_tab_completion(char* command, int *num_matches)
+static char** username_tab_completion(char* command, int *num_matches)
{
char **matches = (char **) NULL;
*num_matches=0;
}
#include <dirent.h>
-char** exe_n_cwd_tab_completion(char* command, int *num_matches)
+static char** exe_n_cwd_tab_completion(char* command, int *num_matches)
{
char *dirName;
- char **matches = (char **) NULL;
+ char **matches;
DIR *dir;
struct dirent *next;
- matches = malloc( sizeof(char*)*50);
+ matches = xmalloc( sizeof(char*)*50);
/* Stick a wildcard onto the command, for later use */
strcat( command, "*");
/* See if this matches */
if (check_wildcard_match(next->d_name, command) == TRUE) {
/* Cool, found a match. Add it to the list */
- matches[*num_matches] = malloc(strlen(next->d_name)+1);
+ matches[*num_matches] = xmalloc(strlen(next->d_name)+1);
strcpy( matches[*num_matches], next->d_name);
++*num_matches;
//matches = realloc( matches, sizeof(char*)*(*num_matches));
return (matches);
}
-void input_tab(char* command, int outputFd, int *cursor, int *len)
+static void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len, int lastWasTab)
{
/* Do TAB completion */
static int num_matches=0;
static char **matches = (char **) NULL;
- int pos = cursor;
+ int pos = *cursor;
if (lastWasTab == FALSE) {
/* For now, we will not bother with trying to distinguish
* whether the cursor is in/at a command extression -- we
- * will always try all possable matches. If you don't like
+ * will always try all possible matches. If you don't like
* that then feel free to fix it.
*/
/* Make a local copy of the string -- up
* to the position of the cursor */
- matchBuf = (char *) calloc(BUFSIZ, sizeof(char));
- strncpy(matchBuf, command, cursor);
+ matchBuf = (char *) xcalloc(BUFSIZ, sizeof(char));
+ strncpy(matchBuf, command, *cursor);
tmp=matchBuf;
/* skip past any command seperator tokens */
if (matches && num_matches==1) {
/* write out the matched command */
strncpy(command+pos, matches[0]+pos, strlen(matches[0])-pos);
- len=strlen(command);
- cursor=len;
+ *len=strlen(command);
+ *cursor=*len;
xwrite(outputFd, matches[0]+pos, strlen(matches[0])-pos);
- break;
+ return;
}
} else {
/* Ok -- the last char was a TAB. Since they
/* Rewrite the prompt */
xwrite(outputFd, prompt, strlen(prompt));
/* Rewrite the command */
- xwrite(outputFd, command, len);
+ xwrite(outputFd, command, *len);
/* Put the cursor back to where it used to be */
- for (cursor=len; cursor > pos; cursor--)
+ for (cursor=len; *cursor > pos; cursor--)
xwrite(outputFd, "\b", 1);
}
}
}
#endif
-void get_previous_history(struct history **hp, char* command)
+static void get_previous_history(struct history **hp, char* command)
{
- free((*hp)->s);
+ if ((*hp)->s)
+ free((*hp)->s);
(*hp)->s = strdup(command);
*hp = (*hp)->p;
}
-void get_next_history(struct history **hp, char* command)
+static void get_next_history(struct history **hp, char* command)
{
- free((*hp)->s);
+ if ((*hp)->s)
+ free((*hp)->s);
(*hp)->s = strdup(command);
*hp = (*hp)->n;
-
- cmdedit_redraw( NULL, hp->s, -2, -2);
}
-#if 0
-/* prompt : if !=NULL, print the prompt
- * command: the command line to be displayed
- * where : where to display changes from.
- * -1 for no change, -2 for new line
- * cursor : desired location for the cursor.
- * -1 for Beginning of line.
- * -2 for End of Line,
- */
-static void
-cmdedit_redraw(char* prompt, char* command, int where, int cursor)
-{
- static char* last_command;
- int cmdedit_width;
-
- if (where == -2) {
- /* Rewrite the prompt and clean up static variables */
- xwrite(outputFd, "\n", 1);
- if (prompt) {
- strcpy(last_command, prompt);
- xwrite(outputFd, prompt, strlen(prompt));
- } else {
- last_command[0] = '\0';
- xwrite(outputFd, "# ", 2);
- }
- cmdedit_width = cmdedit_termw - cmdedit_strlen(prompt);
- } else if (strcmp(command, last_command) != 0) {
- strcpy(last_command, prompt);
- }
-
- /* erase old command from command line */
- len = strlen(command)-strlen(last_command);
- while (len>0)
- input_backspace(command, outputFd, &cursor, &len);
- input_home(outputFd, &cursor);
-
- /* Rewrite the command */
- xwrite(outputFd, command+where, len);
-
- /* Put the where it is supposed to be */
- for (cursor=len; cursor > where; cursor--)
- xwrite(outputFd, "\b", 1);
-
- /* write new command */
- strcpy(command, hp->s);
- len = strlen(hp->s);
- xwrite(outputFd, command+where, len);
- cursor = len;
-}
-#endif
-
/*
* This function is used to grab a character buffer
* from the input file descriptor and allows you to
char c = 0;
struct history *hp = his_end;
- memset(command, 0, sizeof(command));
if (!reset_term) {
- ioctl(inputFd, TCGETA, (void *) &old_term);
- memcpy(&new_term, &old_term, sizeof(struct termio));
-
- new_term.c_cc[VMIN] = 1;
- new_term.c_cc[VTIME] = 0;
- new_term.c_lflag &= ~ICANON; /* unbuffered input */
- new_term.c_lflag &= ~ECHO;
+
+ 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 */
reset_term = 1;
- ioctl(inputFd, TCSETA, (void *) &new_term);
- } else {
- ioctl(inputFd, TCSETA, (void *) &new_term);
}
+ setTermSettings(inputFd, (void*) &new_settings);
memset(command, 0, BUFSIZ);
+ /* Print out the command prompt */
+ xwrite(outputFd, prompt, strlen(prompt));
+
while (1) {
if ((ret = read(inputFd, &c, 1)) < 1)
return;
+ //fprintf(stderr, "got a '%c' (%d)\n", c, c);
switch (c) {
case '\n':
/* Control-b -- Move back one character */
input_backward(outputFd, &cursor);
break;
+ case 3:
+ /* Control-c -- stop gathering input */
+
+ /* Link into lash to reset context to 0 on ^C and such */
+ shell_context = 0;
+
+ /* Go to the next line */
+ xwrite(outputFd, "\n", 1);
+
+#if 0
+ /* Rewrite the prompt */
+ xwrite(outputFd, prompt, strlen(prompt));
+
+ /* Reset the command string */
+ memset(command, 0, BUFSIZ);
+ len = cursor = 0;
+#endif
+ return;
+
case 4:
/* Control-d -- Delete one character, or exit
* if the len=0 and no chars to delete */
break;
case '\b':
case DEL:
- /* control-h and DEL */
+ /* Control-h and DEL */
input_backspace(command, outputFd, &cursor, &len);
break;
case '\t':
#ifdef BB_FEATURE_SH_TAB_COMPLETION
- input_tab(command, outputFd, &cursor, &len);
+ input_tab(command, prompt, outputFd, &cursor,
+&len, lastWasTab);
#endif
break;
case 14:
rewrite_line:
/* erase old command from command line */
len = strlen(command)-strlen(hp->s);
- while (len>0)
+
+ while (len>cursor)
+ input_delete(command, outputFd, cursor, &len);
+ while (cursor>0)
input_backspace(command, outputFd, &cursor, &len);
input_home(outputFd, &cursor);
}
nr = len + 1;
- ioctl(inputFd, TCSETA, (void *) &old_term);
+ setTermSettings(inputFd, (void *) &initial_settings);
reset_term = 0;
struct history *h = his_end;
if (!h) {
- /* No previous history */
- h = his_front = malloc(sizeof(struct history));
- h->n = malloc(sizeof(struct history));
+ /* No previous history -- this memory is never freed */
+ h = his_front = xmalloc(sizeof(struct history));
+ h->n = xmalloc(sizeof(struct history));
h->p = NULL;
h->s = strdup(command);
his_end = h->n;
history_counter++;
} else {
- /* Add a new history command */
- h->n = malloc(sizeof(struct history));
+ /* Add a new history command -- this memory is never freed */
+ h->n = xmalloc(sizeof(struct history));
h->n->p = h;
h->n->n = NULL;
extern void cmdedit_init(void)
{
- atexit(cmdedit_reset_term);
+ win_changed(0);
+ signal(SIGWINCH, win_changed);
+
+ if(exithandler_set == 0) {
+ atexit(cmdedit_reset_term); /* be sure to do this only once */
+ exithandler_set = 1;
+ }
+ signal(SIGKILL, clean_up_and_die);
signal(SIGINT, clean_up_and_die);
signal(SIGQUIT, clean_up_and_die);
signal(SIGTERM, clean_up_and_die);
}
+
+/*
+** Undo the effects of cmdedit_init() as good as we can:
+** I am not aware of a way to revoke an atexit() handler,
+** but, fortunately, our particular handler can be made
+** a no-op by setting reset_term = 0.
+*/
+extern void cmdedit_terminate(void)
+{
+ cmdedit_reset_term();
+ reset_term = 0;
+ signal(SIGKILL, SIG_DFL);
+ signal(SIGINT, SIG_DFL);
+ signal(SIGQUIT, SIG_DFL);
+ signal(SIGTERM, SIG_DFL);
+ signal(SIGWINCH, SIG_DFL);
+}
+
+
+
#endif /* BB_FEATURE_SH_COMMAND_EDITING */