Add errno.h
[oweals/busybox.git] / cmdedit.c
index c32330bfd8428023a7ae70ee680581affb65b7f8..12c78dc763243f4533f746a12c5f6ccb50eeb1de 100644 (file)
--- a/cmdedit.c
+++ b/cmdedit.c
 #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'))
 
@@ -85,6 +88,12 @@ 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;
@@ -99,8 +108,7 @@ struct history {
  * 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.
  */
-void
-cmdedit_setwidth(int w)
+static void cmdedit_setwidth(int w)
 {
        if (w > 20) {
                cmdedit_termw = w;
@@ -110,8 +118,17 @@ cmdedit_setwidth(int w)
        }
 }
 
+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);
+       }
+}
 
-void cmdedit_reset_term(void)
+
+static void cmdedit_reset_term(void)
 {
        if (reset_term)
                /* sparc and other have broken termios support: use old termio handling. */
@@ -130,16 +147,16 @@ void cmdedit_reset_term(void)
 #endif
 }
 
-void clean_up_and_die(int sig)
+static void clean_up_and_die(int sig)
 {
        cmdedit_reset_term();
-       fprintf(stdout, "\n");
+       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);
@@ -148,7 +165,7 @@ void input_home(int outputFd, int *cursor)
 }
 
 /* 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);
@@ -157,7 +174,7 @@ void input_end(int outputFd, int *cursor, int len)
 }
 
 /* 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;
 
@@ -190,7 +207,7 @@ void input_backspace(char* command, int outputFd, int *cursor, int *len)
 }
 
 /* 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;
 
@@ -214,7 +231,7 @@ void input_delete(char* command, int outputFd, int cursor, int *len)
 }
 
 /* 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);
@@ -223,7 +240,7 @@ void input_forward(int outputFd, int *cursor, int len)
 }
 
 /* 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);
@@ -234,7 +251,7 @@ void input_backward(int outputFd, int *cursor)
 
 
 #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;
@@ -243,7 +260,7 @@ char** username_tab_completion(char* command, int *num_matches)
 }
 
 #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;
@@ -283,7 +300,7 @@ char** exe_n_cwd_tab_completion(char* command, int *num_matches)
        return (matches);
 }
 
-void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len, int lastWasTab)
+static void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len, int lastWasTab)
 {
        /* Do TAB completion */
        static int num_matches=0;
@@ -381,7 +398,7 @@ void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len,
 }
 #endif
 
-void get_previous_history(struct history **hp, char* command)
+static void get_previous_history(struct history **hp, char* command)
 {
        if ((*hp)->s)
                free((*hp)->s);
@@ -389,7 +406,7 @@ void get_previous_history(struct history **hp, char* command)
        *hp = (*hp)->p;
 }
 
-void get_next_history(struct history **hp, char* command)
+static void get_next_history(struct history **hp, char* command)
 {
        if ((*hp)->s)
                free((*hp)->s);
@@ -444,6 +461,9 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
 
        memset(command, 0, BUFSIZ);
 
+       /* Print out the command prompt */
+       xwrite(outputFd, prompt, strlen(prompt));
+
        while (1) {
 
                if ((ret = read(inputFd, &c, 1)) < 1)
@@ -466,20 +486,24 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
                        input_backward(outputFd, &cursor);
                        break;
                case 3:
-                       /* Control-c -- leave the current line, 
-                        * and start over on the next line */ 
+                       /* 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;
 
-                       break;
                case 4:
                        /* Control-d -- Delete one character, or exit 
                         * if the len=0 and no chars to delete */
@@ -711,6 +735,9 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
 
 extern void cmdedit_init(void)
 {
+       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;
@@ -735,6 +762,7 @@ extern void cmdedit_terminate(void)
        signal(SIGINT, SIG_DFL);
        signal(SIGQUIT, SIG_DFL);
        signal(SIGTERM, SIG_DFL);
+       signal(SIGWINCH, SIG_DFL);
 }