Added support for ignoring '-g' per GNU ls, thanks to David Vrabel
[oweals/busybox.git] / sh.c
diff --git a/sh.c b/sh.c
index f17097c643dc16e56b9c9fac68fc9cd89ebfaa28..785e9cc3fa233e0346e591bd3989bb6c9a7d7c47 100644 (file)
--- a/sh.c
+++ b/sh.c
@@ -37,6 +37,9 @@
 #include <sys/ioctl.h>
 #include <sys/wait.h>
 #include <unistd.h>
+#ifdef BB_FEATURE_SH_COMMAND_EDITING
+#include "cmdedit.h"
+#endif
 
 
 #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
@@ -110,28 +113,45 @@ static int busy_loop(FILE * input);
 static struct builtInCommand bltins[] = {
        {"bg", "Resume a job in the background", "bg [%%job]", shell_fg_bg},
        {"cd", "Change working directory", "cd [dir]", shell_cd},
-       //{"echo", "Echo arguments on stdout", "echo arg1 [...]", shell_echo},
-       {"env", "Print all environment variables", "env", shell_env},
        {"exit", "Exit from shell()", "exit", shell_exit},
        {"fg", "Bring job into the foreground", "fg [%%job]", shell_fg_bg},
        {"jobs", "Lists the active jobs", "jobs", shell_jobs},
-       {"pwd", "Print current directory", "pwd", shell_pwd},
        {"export", "Set environment variable", "export [VAR=value]", shell_export},
        {"unset", "Unset environment variable", "unset VAR", shell_unset},
-       
+       {NULL, NULL, NULL, NULL}
+};
+
+/* Table of built-in functions */
+static struct builtInCommand bltins_forking[] = {
+       {"env", "Print all environment variables", "env", shell_env},
+       {"pwd", "Print current directory", "pwd", shell_pwd},
        {".", "Source-in and run commands in a file", ". filename", shell_source},
        {"help", "List shell built-in commands", "help", shell_help},
        {NULL, NULL, NULL, NULL}
 };
 
 static const char shell_usage[] =
-
-       "sh [FILE]...\n\n" "The BusyBox command interpreter (shell).\n\n";
-
+       "sh [FILE]...\n"
+       "   or: sh -c command [args]...\n"
+#ifndef BB_FEATURE_TRIVIAL_HELP
+       "\nlash: The BusyBox command interpreter (shell).\n\n"
+#endif
+       ;
 
 static char cwd[1024];
 static char *prompt = "# ";
+static char *local_pending_command = NULL;
 
+#ifdef BB_FEATURE_SH_COMMAND_EDITING
+void win_changed(int sig)
+{
+       struct winsize win = { 0, 0 };
+       ioctl(0, TIOCGWINSZ, &win);
+       if (win.ws_col > 0) {
+               cmdedit_setwidth( win.ws_col - 1);
+       }
+}
+#endif
 
 
 /* built-in 'cd <path>' handler */
@@ -169,8 +189,7 @@ static int shell_exit(struct job *cmd, struct jobSet *junk)
        if (!cmd->progs[0].argv[1] == 1)
                exit TRUE;
 
-       else
-               exit(atoi(cmd->progs[0].argv[1]));
+       return(atoi(cmd->progs[0].argv[1]));
 }
 
 /* built-in 'fg' and 'bg' handler */
@@ -207,8 +226,9 @@ static int shell_fg_bg(struct job *cmd, struct jobSet *jobList)
 
        if (*cmd->progs[0].argv[0] == 'f') {
                /* Make this job the foreground job */
-               if (tcsetpgrp(0, job->pgrp))
-                       perror("tcsetpgrp");
+               /* suppress messages when run from /linuxrc mag@sysgo.de */
+               if (tcsetpgrp(0, job->pgrp) && errno != ENOTTY)
+                       perror("tcsetpgrp"); 
                jobList->fg = job;
        }
 
@@ -233,6 +253,9 @@ static int shell_help(struct job *cmd, struct jobSet *junk)
        for (x = bltins; x->cmd; x++) {
                fprintf(stdout, "%s\t%s\n", x->cmd, x->descr);
        }
+       for (x = bltins_forking; x->cmd; x++) {
+               fprintf(stdout, "%s\t%s\n", x->cmd, x->descr);
+       }
        fprintf(stdout, "\n\n");
        return TRUE;
 }
@@ -390,6 +413,17 @@ static void checkJobs(struct jobSet *jobList)
 
 static int getCommand(FILE * source, char *command)
 {
+       if (source == NULL) {
+               if (local_pending_command) {
+                       /* a command specified (-c option): return it & mark it done */
+                       strcpy(command, local_pending_command);
+                       free(local_pending_command);
+                       local_pending_command = NULL;
+                       return 0;
+               }
+               return 1;
+       }
+
        if (source == stdin) {
 #ifdef BB_FEATURE_SH_COMMAND_EDITING
                int len;
@@ -398,7 +432,7 @@ static int getCommand(FILE * source, char *command)
                fflush(stdout);
                promptStr=(char*)malloc(sizeof(char)*(len+1));
                sprintf(promptStr, "%s %s", cwd, prompt);
-               cmdedit_read_input(promptStr, fileno(stdin), fileno(stdout), command);
+               cmdedit_read_input(promptStr, command);
                free( promptStr);
                return 0;
 #else
@@ -696,7 +730,6 @@ static int parseCommand(char **commandPtr, struct job *job, int *isBg)
                strcpy(job->text, *commandPtr);
        } else {
                /* This leaves any trailing spaces, which is a bit sloppy */
-
                count = returnCommand - *commandPtr;
                job->text = malloc(count + 1);
                strncpy(job->text, *commandPtr, count);
@@ -708,6 +741,7 @@ static int parseCommand(char **commandPtr, struct job *job, int *isBg)
        return 0;
 }
 
+
 static int runCommand(struct job newJob, struct jobSet *jobList, int inBg)
 {
        struct job *job;
@@ -715,14 +749,10 @@ static int runCommand(struct job newJob, struct jobSet *jobList, int inBg)
        int nextin, nextout;
        int pipefds[2];                         /* pipefd[0] is for reading */
        struct builtInCommand *x;
+#ifdef BB_FEATURE_STANDALONE_SHELL
+       const struct BB_applet *a = applets;
+#endif
 
-       /* handle built-ins here -- we don't fork() so we can't background
-          these very easily */
-       for (x = bltins; x->cmd; x++) {
-               if (!strcmp(newJob.progs[0].argv[0], x->cmd)) {
-                       return (x->function(&newJob, jobList));
-               }
-       }
 
        nextin = 0, nextout = 1;
        for (i = 0; i < newJob.numProgs; i++) {
@@ -733,6 +763,13 @@ static int runCommand(struct job newJob, struct jobSet *jobList, int inBg)
                        nextout = 1;
                }
 
+               /* Match any built-ins here */
+               for (x = bltins; x->cmd; x++) {
+                       if (!strcmp(newJob.progs[i].argv[0], x->cmd)) {
+                               return (x->function(&newJob, jobList));
+                       }
+               }
+
                if (!(newJob.progs[i].pid = fork())) {
                        signal(SIGTTOU, SIG_DFL);
 
@@ -749,6 +786,25 @@ static int runCommand(struct job newJob, struct jobSet *jobList, int inBg)
                        /* explicit redirections override pipes */
                        setupRedirections(newJob.progs + i);
 
+                       /* Match any built-ins here */
+                       for (x = bltins_forking; x->cmd; x++) {
+                               if (!strcmp(newJob.progs[i].argv[0], x->cmd)) {
+                                       exit (x->function(&newJob, jobList));
+                               }
+                       }
+#ifdef BB_FEATURE_STANDALONE_SHELL
+                       /* Handle busybox internals here */
+                       while (a->name != 0) {
+                               if (strcmp(newJob.progs[i].argv[0], a->name) == 0) {
+                                       int argc;
+                                       char** argv=newJob.progs[i].argv;
+                                       for(argc=0;*argv!=NULL; argv++, argc++);
+                                       exit((*(a->main)) (argc, newJob.progs[i].argv));
+                               }
+                               a++;
+                       }
+#endif
+
                        execvp(newJob.progs[i].argv[0], newJob.progs[i].argv);
                        fatalError("sh: %s: %s\n", newJob.progs[i].argv[0],
                                           strerror(errno));
@@ -793,15 +849,14 @@ static int runCommand(struct job newJob, struct jobSet *jobList, int inBg)
        if (inBg) {
                /* we don't wait for background jobs to return -- append it 
                   to the list of backgrounded jobs and leave it alone */
-
                printf("[%d] %d\n", job->jobId,
                           newJob.progs[newJob.numProgs - 1].pid);
        } else {
                jobList->fg = job;
 
                /* move the new process group into the foreground */
-
-               if (tcsetpgrp(0, newJob.pgrp))
+               /* suppress messages when run from /linuxrc mag@sysgo.de */
+               if (tcsetpgrp(0, newJob.pgrp) && errno != ENOTTY)
                        perror("tcsetpgrp");
        }
 
@@ -853,10 +908,14 @@ static int busy_loop(FILE * input)
        char *nextCommand = NULL;
        struct jobSet jobList = { NULL, NULL };
        struct job newJob;
+       pid_t  parent_pgrp;
        int i;
        int status;
        int inBg;
 
+       /* save current owner of TTY so we can restore it on exit */
+       parent_pgrp = tcgetpgrp(0);
+
        command = (char *) calloc(BUFSIZ, sizeof(char));
 
        /* don't pay any attention to this signal; it just confuses 
@@ -898,10 +957,6 @@ static int busy_loop(FILE * input)
 
                                        removeJob(&jobList, jobList.fg);
                                        jobList.fg = NULL;
-
-                                       /* move the shell to the foreground */
-                                       if (tcsetpgrp(0, getpid()))
-                                               perror("tcsetpgrp");
                                }
                        } else {
                                /* the child was stopped */
@@ -917,13 +972,18 @@ static int busy_loop(FILE * input)
 
                        if (!jobList.fg) {
                                /* move the shell to the foreground */
-                               if (tcsetpgrp(0, getpid()))
-                                       perror("tcsetpgrp");
+                               /* suppress messages when run from /linuxrc mag@sysgo.de */
+                               if (tcsetpgrp(0, getpid()) && errno != ENOTTY)
+                                       perror("tcsetpgrp"); 
                        }
                }
        }
        free(command);
 
+       /* return controlling TTY back to parent process group before exiting */
+       if (tcsetpgrp(0, parent_pgrp))
+               perror("tcsetpgrp"); 
+
        return 0;
 }
 
@@ -932,35 +992,51 @@ int shell_main(int argc, char **argv)
 {
        FILE *input = stdin;
 
-       if (argc > 2) {
-               usage(shell_usage);
-       }
        /* initialize the cwd */
        getcwd(cwd, sizeof(cwd));
 
+#ifdef BB_FEATURE_SH_COMMAND_EDITING
+       cmdedit_init();
+       signal(SIGWINCH, win_changed);
+       win_changed(0);
+#endif
 
        //if (argv[0] && argv[0][0] == '-') {
        //      shell_source("/etc/profile");
        //}
 
        if (argc < 2) {
-               fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER,
-                               BB_BT);
-               fprintf(stdout,
-                               "Enter 'help' for a list of built-in commands.\n\n");
+               fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, BB_BT);
+               fprintf(stdout, "Enter 'help' for a list of built-in commands.\n\n");
        } else {
-               input = fopen(argv[1], "r");
-               if (!input)
-                       fatalError("A: Couldn't open file '%s': %s\n", argv[1],
-                                          strerror(errno));
-//              else
-//                      fatalError("Got it.\n");
-               //exit(shell_source(argv[1]));
-
-               /* Set terminal IO to canonical mode, and save old term settings. */
-#ifdef BB_FEATURE_SH_COMMAND_EDITING
-               cmdedit_init();
-#endif
+               if (argv[1][0]=='-' && argv[1][1]=='c') {
+                       int i;
+                       local_pending_command = (char *) calloc(BUFSIZ, sizeof(char));
+                       if (local_pending_command == 0) {
+                               fatalError("sh: out of memory\n");
+                       }
+                       for(i=2; i<argc; i++)
+                       {
+                               if (strlen(local_pending_command) + strlen(argv[i]) >= BUFSIZ) {
+                                 fatalError("sh: commands for -c option too long\n");
+                               }
+                               strcat(local_pending_command, argv[i]);
+                               if (i + 1 < argc)
+                                 strcat(local_pending_command, " ");
+                       }
+                       input = NULL;
+                         
+               }
+               else if (argv[1][0]=='-') {
+                       usage(shell_usage);
+               }
+               else {
+                       input = fopen(argv[1], "r");
+                       if (!input) {
+                               fatalError("sh: Couldn't open file '%s': %s\n", argv[1],
+                                                  strerror(errno));
+                       }
+               }
        }
 
        return (busy_loop(input));