Implement suggestion from Adam Slattery, (don't default to killing closing bug #1190.
[oweals/busybox.git] / lash.c
diff --git a/lash.c b/lash.c
index 689c720f60ff61d84af1a2fdd247b907c069fc6d..7c932a91021fdb6c377dcd6449eb35f2af6783cf 100644 (file)
--- a/lash.c
+++ b/lash.c
@@ -44,6 +44,7 @@
 #include <sys/wait.h>
 #include <unistd.h>
 #include <getopt.h>
+#include <termios.h>
 #include "busybox.h"
 #include "cmdedit.h"
 
@@ -134,6 +135,7 @@ static void mark_open(int fd);
 static void mark_closed(int fd);
 static void close_all(void);
 static void checkjobs(struct jobset *job_list);
+static void remove_job(struct jobset *j_list, struct job *job);
 static int get_command(FILE * source, char *command);
 static int parse_command(char **command_ptr, struct job *job, int *inbg);
 static int run_command(struct job *newjob, int inbg, int outpipe[2]);
@@ -180,8 +182,11 @@ static struct jobset job_list = { NULL, NULL };
 static int argc;
 static char **argv;
 static struct close_me *close_me_head;
+static int last_return_code;
+static int last_bg_pid;
 static unsigned int last_jobid;
-
+static int shell_terminal;
+static pid_t shell_pgrp;
 static char *PS1;
 static char *PS2 = "> ";
 
@@ -299,10 +304,9 @@ static int builtin_fg_bg(struct child_prog *child)
        }
 
        if (*child->argv[0] == 'f') {
-               /* Make this job the foreground job */
-               /* suppress messages when run from /linuxrc mag@sysgo.de */
-               if (tcsetpgrp(0, job->pgrp) && errno != ENOTTY)
-                       perror_msg("tcsetpgrp"); 
+               /* Put the job into the foreground.  */
+               tcsetpgrp(shell_terminal, job->pgrp);
+
                child->family->job_list->fg = job;
        }
 
@@ -310,10 +314,16 @@ static int builtin_fg_bg(struct child_prog *child)
        for (i = 0; i < job->num_progs; i++)
                job->progs[i].is_stopped = 0;
 
-       kill(-job->pgrp, SIGCONT);
-
        job->stopped_progs = 0;
 
+       if ( (i=kill(- job->pgrp, SIGCONT)) < 0) {
+               if (i == ESRCH) {
+                       remove_job(&job_list, job);
+               } else {
+                       perror_msg("kill (SIGCONT)");
+               }
+       }
+
        return EXIT_SUCCESS;
 }
 
@@ -534,6 +544,11 @@ static void remove_job(struct jobset *j_list, struct job *job)
                prevjob->next = job->next;
        }
 
+       if (j_list->head)
+               last_jobid = j_list->head->jobid;
+       else
+               last_jobid = 0;
+
        free(job);
 }
 
@@ -574,10 +589,15 @@ static void checkjobs(struct jobset *j_list)
                        job->stopped_progs++;
                        job->progs[prognum].is_stopped = 1;
 
+#if 0
+                       /* Printing this stuff is a pain, since it tends to
+                        * overwrite the prompt an inconveinient moments.  So
+                        * don't do that.  */
                        if (job->stopped_progs == job->num_progs) {
                                printf(JOB_STATUS_FORMAT, job->jobid, "Stopped",
                                           job->text);
                        }
+#endif 
                }
        }
 
@@ -712,14 +732,78 @@ static int get_command(FILE * source, char *command)
        return 0;
 }
 
+static char* itoa(register int i)
+{
+       static char a[7]; /* Max 7 ints */
+       register char *b = a + sizeof(a) - 1;
+       int   sign = (i < 0);
+
+       if (sign)
+               i = -i;
+       *b = 0;
+       do
+       {
+               *--b = '0' + (i % 10);
+               i /= 10;
+       }
+       while (i);
+       if (sign)
+               *--b = '-';
+       return b;
+}
+
+char * strsep_space( char *string, int * ix)
+{
+       char *token, *begin;
+
+       begin = string;
+
+       /* Short circuit the trivial case */
+       if ( !string || ! string[*ix])
+               return NULL;
+
+       /* Find the end of the token. */
+       while( string && string[*ix] && !isspace(string[*ix]) ) {
+               (*ix)++;
+       }
+
+       /* Find the end of any whitespace trailing behind 
+        * the token and let that be part of the token */
+       while( string && string[*ix] && isspace(string[*ix]) ) {
+               (*ix)++;
+       }
+
+       if (! string && *ix==0) {
+               /* Nothing useful was found */
+               return NULL;
+       }
+
+       token = xmalloc(*ix+1);
+       token[*ix] = '\0';
+       strncpy(token, string,  *ix); 
+
+       return token;
+}
 
 static int expand_arguments(char *command)
 {
-       int ix = 0;
+       int total_length=0, length, i, retval, ix = 0;
+       expand_t expand_result;
+       char *tmpcmd, *cmd, *cmd_copy;
+       char *src, *dst, *var;
+       const char *out_of_space = "out of space during expansion";
+       int flags = GLOB_NOCHECK
+#ifdef GLOB_BRACE
+               | GLOB_BRACE
+#endif 
+#ifdef GLOB_TILDE
+               | GLOB_TILDE
+#endif 
+               ;
 
        /* get rid of the terminating \n */
        chomp(command);
-       
+
        /* Fix up escape sequences to be the Real Thing(tm) */
        while( command && command[ix]) {
                if (command[ix] == '\\') {
@@ -729,6 +813,139 @@ static int expand_arguments(char *command)
                }
                ix++;
        }
+       /* Use glob and then fixup environment variables and such */
+
+       /* It turns out that glob is very stupid.  We have to feed it one word at a
+        * time since it can't cope with a full string.  Here we convert command
+        * (char*) into cmd (char**, one word per string) */
+
+       /* We need a clean copy, so strsep can mess up the copy while
+        * we write stuff into the original (in a minute) */
+       cmd = cmd_copy = strdup(command);
+       *command = '\0';
+       for (ix = 0, tmpcmd = cmd; 
+                       (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
+               if (*tmpcmd == '\0')
+                       break;
+               /* we need to trim() the result for glob! */
+               trim(tmpcmd);
+               retval = glob(tmpcmd, flags, NULL, &expand_result);
+               free(tmpcmd); /* Free mem allocated by strsep_space */
+               if (retval == GLOB_NOSPACE) {
+                       /* Mem may have been allocated... */
+                       globfree (&expand_result);
+                       error_msg(out_of_space);
+                       return FALSE;
+               } else if (retval != 0) {
+                       /* Some other error.  GLOB_NOMATCH shouldn't
+                        * happen because of the GLOB_NOCHECK flag in 
+                        * the glob call. */
+                       error_msg("syntax error");
+                       return FALSE;
+               } else {
+                       /* Convert from char** (one word per string) to a simple char*,
+                        * but don't overflow command which is BUFSIZ in length */
+                       for (i=0; i < expand_result.gl_pathc; i++) {
+                               length=strlen(expand_result.gl_pathv[i]);
+                               if (total_length+length+1 >= BUFSIZ) {
+                                       error_msg(out_of_space);
+                                       return FALSE;
+                               }
+                               strcat(command+total_length, " ");
+                               total_length+=1;
+                               strcat(command+total_length, expand_result.gl_pathv[i]);
+                               total_length+=length;
+                       }
+                       globfree (&expand_result);
+               }
+       }
+       free(cmd_copy);
+       trim(command);
+
+       /* Now do the shell variable substitutions which 
+        * wordexp can't do for us, namely $? and $! */
+       src = command;
+       while((dst = strchr(src,'$')) != NULL){
+               var = NULL;
+               switch(*(dst+1)) {
+                       case '?':
+                               var = itoa(last_return_code);
+                               break;
+                       case '!':
+                               if (last_bg_pid==-1)
+                                       *(var)='\0';
+                               else
+                                       var = itoa(last_bg_pid);
+                               break;
+                               /* Everything else like $$, $#, $[0-9], etc. should all be
+                                * expanded by wordexp(), so we can in theory skip that stuff
+                                * here, but just to be on the safe side (i.e., since uClibc
+                                * wordexp doesn't do this stuff yet), lets leave it in for
+                                * now. */
+                       case '$':
+                               var = itoa(getpid());
+                               break;
+                       case '#':
+                               var = itoa(argc-1);
+                               break;
+                       case '0':case '1':case '2':case '3':case '4':
+                       case '5':case '6':case '7':case '8':case '9':
+                               {
+                                       int ixx=*(dst + 1)-48;
+                                       if (ixx >= argc) {
+                                               var='\0';
+                                       } else {
+                                               var = argv[ixx];
+                                       }
+                               }
+                               break;
+
+               }
+               if (var) {
+                       /* a single character construction was found, and 
+                        * already handled in the case statement */
+                       src=dst+2;
+               } else {
+                       /* Looks like an environment variable */
+                       char delim_hold;
+                       int num_skip_chars=0;
+                       int dstlen = strlen(dst);
+                       /* Is this a ${foo} type variable? */
+                       if (dstlen >=2 && *(dst+1) == '{') {
+                               src=strchr(dst+1, '}');
+                               num_skip_chars=1;
+                       } else {
+                               src=dst+1;
+                               while(isalnum(*src) || *src=='_') src++;
+                       }
+                       if (src == NULL) {
+                               src = dst+dstlen;
+                       }
+                       delim_hold=*src;
+                       *src='\0';  /* temporary */
+                       var = getenv(dst + 1 + num_skip_chars);
+                       *src=delim_hold;
+                       src += num_skip_chars;
+               }
+               if (var == NULL) {
+                       /* Seems we got an un-expandable variable.  So delete it. */
+                       var = "";
+               }
+               {
+                       int subst_len = strlen(var);
+                       int trail_len = strlen(src);
+                       if (dst+subst_len+trail_len >= command+BUFSIZ) {
+                               error_msg(out_of_space);
+                               return FALSE;
+                       }
+                       /* Move stuff to the end of the string to accommodate
+                        * filling the created gap with the new stuff */
+                       memmove(dst+subst_len, src, trail_len+1);
+                       /* Now copy in the new stuff */
+                       memcpy(dst, var, subst_len);
+                       src = dst+subst_len;
+               }
+       }
 
        return TRUE;
 }
@@ -1076,12 +1293,13 @@ static void insert_job(struct job *newjob, int inbg)
                printf("[%d] %d\n", thejob->jobid,
                           newjob->progs[newjob->num_progs - 1].pid);
                last_jobid = newjob->jobid;
+               last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
        } else {
                newjob->job_list->fg = thejob;
 
                /* move the new process group into the foreground */
                /* suppress messages when run from /linuxrc mag@sysgo.de */
-               if (tcsetpgrp(0, newjob->pgrp) && errno != ENOTTY)
+               if (tcsetpgrp(shell_terminal, newjob->pgrp) && errno != ENOTTY)
                        perror_msg("tcsetpgrp");
        }
 }
@@ -1131,7 +1349,13 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2])
                }
 
                if (!(child->pid = fork())) {
+                       /* Set the handling for job control signals back to the default.  */
+                       signal(SIGINT, SIG_DFL);
+                       signal(SIGQUIT, SIG_DFL);
+                       signal(SIGTSTP, SIG_DFL);
+                       signal(SIGTTIN, SIG_DFL);
                        signal(SIGTTOU, SIG_DFL);
+                       signal(SIGCHLD, SIG_DFL);
 
                        close_all();
 
@@ -1192,14 +1416,10 @@ static int busy_loop(FILE * input)
        newjob.job_context = DEFAULT_CONTEXT;
 
        /* save current owner of TTY so we can restore it on exit */
-       parent_pgrp = tcgetpgrp(0);
+       parent_pgrp = tcgetpgrp(shell_terminal);
 
        command = (char *) xcalloc(BUFSIZ, sizeof(char));
 
-       /* don't pay any attention to this signal; it just confuses 
-          things and isn't really meant for shells anyway */
-       signal(SIGTTOU, SIG_IGN);
-
        while (1) {
                if (!job_list.fg) {
                        /* no job is in the foreground */
@@ -1246,6 +1466,8 @@ static int busy_loop(FILE * input)
                                job_list.fg->running_progs--;
                                job_list.fg->progs[i].pid = 0;
 
+                               last_return_code=WEXITSTATUS(status);
+
                                if (!job_list.fg->running_progs) {
                                        /* child exited */
                                        remove_job(&job_list, job_list.fg);
@@ -1266,7 +1488,7 @@ static int busy_loop(FILE * input)
                        if (!job_list.fg) {
                                /* move the shell to the foreground */
                                /* suppress messages when run from /linuxrc mag@sysgo.de */
-                               if (tcsetpgrp(0, getpgrp()) && errno != ENOTTY)
+                               if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
                                        perror_msg("tcsetpgrp"); 
                        }
                }
@@ -1274,7 +1496,7 @@ static int busy_loop(FILE * input)
        free(command);
 
        /* return controlling TTY back to parent process group before exiting */
-       if (tcsetpgrp(0, parent_pgrp))
+       if (tcsetpgrp(shell_terminal, parent_pgrp))
                perror_msg("tcsetpgrp");
 
        /* return exit status if called with "-c" */
@@ -1300,6 +1522,31 @@ void free_memory(void)
 }
 #endif
 
+/* Make sure we have a controlling tty.  If we get started under a job
+ * aware app (like bash for example), make sure we are now in charge so
+ * we don't fight over who gets the foreground */
+static void setup_job_control()
+{
+       /* Loop until we are in the foreground.  */
+       while (tcgetpgrp (shell_terminal) != (shell_pgrp = getpgrp ()))
+               kill (- shell_pgrp, SIGTTIN);
+
+       /* Ignore interactive and job-control signals.  */
+       signal(SIGINT, SIG_IGN);
+       signal(SIGQUIT, SIG_IGN);
+       signal(SIGTSTP, SIG_IGN);
+       signal(SIGTTIN, SIG_IGN);
+       signal(SIGTTOU, SIG_IGN);
+       signal(SIGCHLD, SIG_IGN);
+
+       /* Put ourselves in our own process group.  */
+       setsid();
+       shell_pgrp = getpid ();
+       setpgid (shell_pgrp, shell_pgrp);
+
+       /* Grab control of the terminal.  */
+       tcsetpgrp(shell_terminal, shell_pgrp);
+}
 
 int shell_main(int argc_l, char **argv_l)
 {
@@ -1315,13 +1562,12 @@ int shell_main(int argc_l, char **argv_l)
        close_me_head = NULL;
        job_list.head = NULL;
        job_list.fg = NULL;
+       last_return_code=1;
 
        if (argv[0] && argv[0][0] == '-') {
                FILE *prof_input;
                prof_input = fopen("/etc/profile", "r");
-               if (!prof_input) {
-                       printf( "Couldn't open file '/etc/profile'\n");
-               } else {
+               if (prof_input) {
                        int tmp_fd = fileno(prof_input);
                        mark_open(tmp_fd);      
                        /* Now run the file */
@@ -1359,6 +1605,7 @@ int shell_main(int argc_l, char **argv_l)
                        isatty(fileno(stdin)) && isatty(fileno(stdout))) {
                interactive=TRUE;
        }
+       setup_job_control();
        if (interactive==TRUE) {
                //printf( "optind=%d  argv[optind]='%s'\n", optind, argv[optind]);
                /* Looks like they want an interactive shell */
@@ -1387,4 +1634,3 @@ int shell_main(int argc_l, char **argv_l)
        
        return (busy_loop(input));
 }
-