/* 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.
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
-#include <termio.h>
+#include <sys/ioctl.h>
#include <ctype.h>
#include <signal.h>
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 */
+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 */
void
cmdedit_setwidth(int w)
{
- if (w > 20) {
+ if (w > 20) {
cmdedit_termw = w;
cmdedit_scroll = w / 3;
- } else {
+ } else {
errorMsg("\n*** Error: minimum screen width is 21\n");
- }
+ }
}
+
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);
}
void clean_up_and_die(int sig)
{
cmdedit_reset_term();
fprintf(stdout, "\n");
- exit(TRUE);
+ if (sig!=SIGINT)
+ exit(TRUE);
}
/* Go to HOME position */
{
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;
return (matches);
}
-void input_tab(char* command, int outputFd, int *cursor, int *len)
+void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len)
{
/* Do TAB completion */
static int num_matches=0;
/* 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.
*/
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
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);
if ((ret = read(inputFd, &c, 1)) < 1)
return;
+ //fprintf(stderr, "got a '%c' (%d)\n", c, c);
switch (c) {
case '\n':
case 2:
/* Control-b -- Move back one character */
input_backward(outputFd, &cursor);
+ break;
+ case 3:
+ /* Control-c -- leave the current line,
+ * and start over on the next line */
+
+ /* Go to the next line */
+ xwrite(outputFd, "\n", 1);
+
+ /* Rewrite the prompt */
+ xwrite(outputFd, prompt, strlen(prompt));
+
+ /* Reset the command string */
+ memset(command, 0, sizeof(command));
+ len = cursor = 0;
+
break;
case 4:
/* Control-d -- Delete one character, or exit
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);
#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;
extern void cmdedit_init(void)
{
atexit(cmdedit_reset_term);
+ signal(SIGKILL, clean_up_and_die);
signal(SIGINT, clean_up_and_die);
signal(SIGQUIT, clean_up_and_die);
signal(SIGTERM, clean_up_and_die);