1 /* vi: set sw=4 ts=4: */
3 * lash -- the BusyBox Lame-Ass SHell
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Based in part on ladsh.c by Michael K. Johnson and Erik W. Troan, which is
8 * under the following liberal license: "We have placed this source code in the
9 * public domain. Use it in any project, free or commercial."
11 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
14 /* This shell's parsing engine is officially at a dead-end. Future
15 * work shell work should be done using hush, msh, or ash. This is
16 * still a very useful, small shell -- it just don't need any more
17 * features beyond what it already has...
20 //For debugging/development on the shell only...
28 #ifdef CONFIG_LOCALE_SUPPORT
33 #define expand_t glob_t
35 /* Always enable for the moment... */
36 #define CONFIG_LASH_PIPE_N_REDIRECTS
37 #define CONFIG_LASH_JOB_CONTROL
39 static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
40 #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
43 #ifdef CONFIG_LASH_PIPE_N_REDIRECTS
44 enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
50 DEFAULT_CONTEXT = 0x1,
51 IF_TRUE_CONTEXT = 0x2,
52 IF_FALSE_CONTEXT = 0x4,
53 THEN_EXP_CONTEXT = 0x8,
54 ELSE_EXP_CONTEXT = 0x10
57 #define LASH_OPT_DONE (1)
58 #define LASH_OPT_SAW_QUOTE (2)
60 #ifdef CONFIG_LASH_PIPE_N_REDIRECTS
62 enum redir_type type; /* type of redirection */
63 int fd; /* file descriptor being redirected */
64 char *filename; /* file to redirect fd to */
69 pid_t pid; /* 0 if exited */
70 char **argv; /* program name and arguments */
71 int num_redirects; /* elements in redirection array */
72 int is_stopped; /* is the program currently running? */
73 struct job *family; /* pointer back to the child's parent job */
74 #ifdef CONFIG_LASH_PIPE_N_REDIRECTS
75 struct redir_struct *redirects; /* I/O redirects */
80 struct job *head; /* head of list of running jobs */
81 struct job *fg; /* current foreground job */
85 int jobid; /* job number */
86 int num_progs; /* total number of programs in job */
87 int running_progs; /* number of programs running */
88 char *text; /* name of job */
89 char *cmdbuf; /* buffer various argv's point into */
90 pid_t pgrp; /* process group ID for the job */
91 struct child_prog *progs; /* array of programs in job */
92 struct job *next; /* to track background commands */
93 int stopped_progs; /* number of programs alive, but stopped */
94 unsigned int job_context; /* bitmask defining current context */
95 struct jobset *job_list;
98 struct built_in_command {
100 char *descr; /* description */
101 int (*function) (struct child_prog *); /* function ptr */
104 /* function prototypes for builtins */
105 static int builtin_cd(struct child_prog *cmd);
106 static int builtin_exec(struct child_prog *cmd);
107 static int builtin_exit(struct child_prog *cmd);
108 static int builtin_fg_bg(struct child_prog *cmd);
109 static int builtin_help(struct child_prog *cmd);
110 static int builtin_jobs(struct child_prog *dummy);
111 static int builtin_pwd(struct child_prog *dummy);
112 static int builtin_export(struct child_prog *cmd);
113 static int builtin_source(struct child_prog *cmd);
114 static int builtin_unset(struct child_prog *cmd);
115 static int builtin_read(struct child_prog *cmd);
118 /* function prototypes for shell stuff */
119 static void checkjobs(struct jobset *job_list);
120 static void remove_job(struct jobset *j_list, struct job *job);
121 static int get_command(FILE * source, char *command);
122 static int parse_command(char **command_ptr, struct job *job, int *inbg);
123 static int run_command(struct job *newjob, int inbg, int outpipe[2]);
124 static int pseudo_exec(struct child_prog *cmd) ATTRIBUTE_NORETURN;
125 static int busy_loop(FILE * input);
128 /* Table of built-in functions (these are non-forking builtins, meaning they
129 * can change global variables in the parent shell process but they will not
130 * work with pipes and redirects; 'unset foo | whatever' will not work) */
131 static struct built_in_command bltins[] = {
132 {"bg", "Resume a job in the background", builtin_fg_bg},
133 {"cd", "Change working directory", builtin_cd},
134 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
135 {"exit", "Exit from shell()", builtin_exit},
136 {"fg", "Bring job into the foreground", builtin_fg_bg},
137 {"jobs", "Lists the active jobs", builtin_jobs},
138 {"export", "Set environment variable", builtin_export},
139 {"unset", "Unset environment variable", builtin_unset},
140 {"read", "Input environment variable", builtin_read},
141 {".", "Source-in and run commands in a file", builtin_source},
142 /* to do: add ulimit */
146 /* Table of forking built-in functions (things that fork cannot change global
147 * variables in the parent process, such as the current working directory) */
148 static struct built_in_command bltins_forking[] = {
149 {"pwd", "Print current directory", builtin_pwd},
150 {"help", "List shell built-in commands", builtin_help},
155 static int shell_context; /* Type prompt trigger (PS1 or PS2) */
158 /* Globals that are static to this file */
159 static const char *cwd;
160 static char *local_pending_command;
161 static struct jobset job_list = { NULL, NULL };
164 static llist_t *close_me_list;
165 static int last_return_code;
166 static int last_bg_pid;
167 static unsigned int last_jobid;
168 static int shell_terminal;
170 static char *PS2 = "> ";
174 static inline void debug_printf(const char *format, ...)
177 va_start(args, format);
178 vfprintf(stderr, format, args);
182 static inline void debug_printf(const char ATTRIBUTE_UNUSED *format, ...) { }
186 Most builtins need access to the struct child_prog that has
187 their arguments, previously coded as cmd->progs[0]. That coding
188 can exhibit a bug, if the builtin is not the first command in
189 a pipeline: "echo foo | exec sort" will attempt to exec foo.
191 builtin previous use notes
192 ------ ----------------- ---------
194 exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
196 fg_bg cmd->progs[0], job_list->head, job_list->fg
205 I added "struct job *family;" to struct child_prog,
206 and switched API to builtin_foo(struct child_prog *child);
207 So cmd->text becomes child->family->text
208 cmd->job_context becomes child->family->job_context
209 cmd->progs[0] becomes *child
210 job_list becomes child->family->job_list
213 /* built-in 'cd <path>' handler */
214 static int builtin_cd(struct child_prog *child)
218 if (child->argv[1] == NULL)
219 newdir = getenv("HOME");
221 newdir = child->argv[1];
223 bb_perror_msg("cd: %s", newdir);
226 cwd = xgetcwd((char *)cwd);
228 cwd = bb_msg_unknown;
232 /* built-in 'exec' handler */
233 static int builtin_exec(struct child_prog *child)
235 if (child->argv[1] == NULL)
236 return EXIT_SUCCESS; /* Really? */
238 while(close_me_list) close((long)llist_pop(&close_me_list));
243 /* built-in 'exit' handler */
244 static int builtin_exit(struct child_prog *child)
246 if (child->argv[1] == NULL)
249 exit (atoi(child->argv[1]));
252 /* built-in 'fg' and 'bg' handler */
253 static int builtin_fg_bg(struct child_prog *child)
256 struct job *job=NULL;
258 /* If they gave us no args, assume they want the last backgrounded task */
259 if (!child->argv[1]) {
260 for (job = child->family->job_list->head; job; job = job->next) {
261 if (job->jobid == last_jobid) {
266 bb_error_msg("%s: no current job", child->argv[0]);
270 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
271 bb_error_msg(bb_msg_invalid_arg, child->argv[1], child->argv[0]);
274 for (job = child->family->job_list->head; job; job = job->next) {
275 if (job->jobid == jobnum) {
280 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
285 if (*child->argv[0] == 'f') {
286 /* Put the job into the foreground. */
287 tcsetpgrp(shell_terminal, job->pgrp);
289 child->family->job_list->fg = job;
292 /* Restart the processes in the job */
293 for (i = 0; i < job->num_progs; i++)
294 job->progs[i].is_stopped = 0;
296 job->stopped_progs = 0;
298 if ( (i=kill(- job->pgrp, SIGCONT)) < 0) {
300 remove_job(&job_list, job);
302 bb_perror_msg("kill (SIGCONT)");
309 /* built-in 'help' handler */
310 static int builtin_help(struct child_prog ATTRIBUTE_UNUSED *dummy)
312 struct built_in_command *x;
314 printf("\nBuilt-in commands:\n"
315 "-------------------\n");
316 for (x = bltins; x->cmd; x++) {
319 printf("%s\t%s\n", x->cmd, x->descr);
321 for (x = bltins_forking; x->cmd; x++) {
324 printf("%s\t%s\n", x->cmd, x->descr);
330 /* built-in 'jobs' handler */
331 static int builtin_jobs(struct child_prog *child)
336 for (job = child->family->job_list->head; job; job = job->next) {
337 if (job->running_progs == job->stopped_progs)
338 status_string = "Stopped";
340 status_string = "Running";
342 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
348 /* built-in 'pwd' handler */
349 static int builtin_pwd(struct child_prog ATTRIBUTE_UNUSED *dummy)
351 cwd = xgetcwd((char *)cwd);
353 cwd = bb_msg_unknown;
358 /* built-in 'export VAR=value' handler */
359 static int builtin_export(struct child_prog *child)
362 char *v = child->argv[1];
366 for (e = environ; *e; e++) {
373 bb_perror_msg("export");
374 #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
375 if (strncmp(v, "PS1=", 4)==0)
379 #ifdef CONFIG_LOCALE_SUPPORT
380 if(strncmp(v, "LC_ALL=", 7)==0)
381 setlocale(LC_ALL, getenv("LC_ALL"));
382 if(strncmp(v, "LC_CTYPE=", 9)==0)
383 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
389 /* built-in 'read VAR' handler */
390 static int builtin_read(struct child_prog *child)
394 char string[MAX_READ];
396 if (child->argv[1]) {
397 /* argument (VAR) given: put "VAR=" into buffer */
398 safe_strncpy(string, child->argv[1], MAX_READ-1);
399 len = strlen(string);
402 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
403 res = strlen(string);
405 string[--res] = '\0'; /* chomp trailing newline */
407 ** string should now contain "VAR=<value>"
408 ** copy it (putenv() won't do that, so we must make sure
409 ** the string resides in a static buffer!)
412 if ((s = strdup(string)))
415 bb_perror_msg("read");
418 fgets(string, sizeof(string), stdin);
423 /* Built-in '.' handler (read-in and execute commands from file) */
424 static int builtin_source(struct child_prog *child)
429 input = fopen_or_warn(child->argv[1], "r");
434 llist_add_to(&close_me_list, (void *)(long)fileno(input));
435 /* Now run the file */
436 status = busy_loop(input);
438 llist_pop(&close_me_list);
442 /* built-in 'unset VAR' handler */
443 static int builtin_unset(struct child_prog *child)
445 if (child->argv[1] == NULL) {
446 printf(bb_msg_requires_arg, "unset");
449 unsetenv(child->argv[1]);
453 #ifdef CONFIG_LASH_JOB_CONTROL
454 /* free up all memory from a job */
455 static void free_job(struct job *cmd)
460 for (i = 0; i < cmd->num_progs; i++) {
461 free(cmd->progs[i].argv);
462 #ifdef CONFIG_LASH_PIPE_N_REDIRECTS
463 if (cmd->progs[i].redirects)
464 free(cmd->progs[i].redirects);
470 keep = cmd->job_list;
471 memset(cmd, 0, sizeof(struct job));
472 cmd->job_list = keep;
475 /* remove a job from a jobset */
476 static void remove_job(struct jobset *j_list, struct job *job)
481 if (job == j_list->head) {
482 j_list->head = job->next;
484 prevjob = j_list->head;
485 while (prevjob->next != job)
486 prevjob = prevjob->next;
487 prevjob->next = job->next;
491 last_jobid = j_list->head->jobid;
498 /* Checks to see if any background processes have exited -- if they
499 have, figure out why and see if a job has completed */
500 static void checkjobs(struct jobset *j_list)
507 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
508 for (job = j_list->head; job; job = job->next) {
510 while (prognum < job->num_progs &&
511 job->progs[prognum].pid != childpid) prognum++;
512 if (prognum < job->num_progs)
516 /* This happens on backticked commands */
520 if (WIFEXITED(status) || WIFSIGNALED(status)) {
522 job->running_progs--;
523 job->progs[prognum].pid = 0;
525 if (!job->running_progs) {
526 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
528 remove_job(j_list, job);
532 job->stopped_progs++;
533 job->progs[prognum].is_stopped = 1;
537 if (childpid == -1 && errno != ECHILD)
538 bb_perror_msg("waitpid");
541 static void checkjobs(struct jobset *j_list)
544 static void free_job(struct job *cmd)
547 static void remove_job(struct jobset *j_list, struct job *job)
552 #ifdef CONFIG_LASH_PIPE_N_REDIRECTS
553 /* squirrel != NULL means we squirrel away copies of stdin, stdout,
554 * and stderr if they are redirected. */
555 static int setup_redirects(struct child_prog *prog, int squirrel[])
560 struct redir_struct *redir = prog->redirects;
562 for (i = 0; i < prog->num_redirects; i++, redir++) {
563 switch (redir->type) {
567 case REDIRECT_OVERWRITE:
568 mode = O_WRONLY | O_CREAT | O_TRUNC;
570 case REDIRECT_APPEND:
571 mode = O_WRONLY | O_CREAT | O_APPEND;
575 openfd = open(redir->filename, mode, 0666);
577 /* this could get lost if stderr has been redirected, but
578 bash and ash both lose it as well (though zsh doesn't!) */
579 bb_perror_msg("error opening %s", redir->filename);
583 if (openfd != redir->fd) {
584 if (squirrel && redir->fd < 3) {
585 squirrel[redir->fd] = dup(redir->fd);
586 fcntl (squirrel[redir->fd], F_SETFD, FD_CLOEXEC);
588 dup2(openfd, redir->fd);
596 static void restore_redirects(int squirrel[])
599 for (i=0; i<3; i++) {
602 /* No error checking. I sure wouldn't know what
603 * to do with an error if I found one! */
610 static inline int setup_redirects(struct child_prog *prog, int squirrel[])
614 static inline void restore_redirects(int squirrel[])
619 static inline void cmdedit_set_initial_prompt(void)
621 #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
630 static inline void setup_prompt_string(char **prompt_str)
632 #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
633 /* Set up the prompt */
634 if (shell_context == 0) {
636 PS1=xmalloc(strlen(cwd)+4);
637 sprintf(PS1, "%s %c ", cwd, ( geteuid() != 0 ) ? '$': '#');
643 *prompt_str = (shell_context==0)? PS1 : PS2;
647 static int get_command(FILE * source, char *command)
651 if (source == NULL) {
652 if (local_pending_command) {
653 /* a command specified (-c option): return it & mark it done */
654 strcpy(command, local_pending_command);
655 free(local_pending_command);
656 local_pending_command = NULL;
662 if (source == stdin) {
663 setup_prompt_string(&prompt_str);
665 #ifdef CONFIG_FEATURE_COMMAND_EDITING
667 ** enable command line editing only while a command line
668 ** is actually being read; otherwise, we'll end up bequeathing
669 ** atexit() handlers and other unwanted stuff to our
670 ** child processes (rob@sysgo.de)
672 cmdedit_read_input(prompt_str, command);
675 fputs(prompt_str, stdout);
679 if (!fgets(command, BUFSIZ - 2, source)) {
688 static char * strsep_space( char *string, int * ix)
690 /* Short circuit the trivial case */
691 if ( !string || ! string[*ix])
694 /* Find the end of the token. */
695 while (string[*ix] && !isspace(string[*ix]) ) {
699 /* Find the end of any whitespace trailing behind
700 * the token and let that be part of the token */
701 while (string[*ix] && (isspace)(string[*ix]) ) {
706 /* Nothing useful was found */
710 return xstrndup(string, *ix);
713 static int expand_arguments(char *command)
715 int total_length=0, length, i, retval, ix = 0;
716 expand_t expand_result;
717 char *tmpcmd, *cmd, *cmd_copy;
718 char *src, *dst, *var;
719 const char * const out_of_space = "out of space during expansion";
720 int flags = GLOB_NOCHECK
729 /* get rid of the terminating \n */
732 /* Fix up escape sequences to be the Real Thing(tm) */
733 while( command && command[ix]) {
734 if (command[ix] == '\\') {
735 const char *tmp = command+ix+1;
736 command[ix] = bb_process_escape_sequence( &tmp );
737 memmove(command+ix + 1, tmp, strlen(tmp)+1);
741 /* Use glob and then fixup environment variables and such */
743 /* It turns out that glob is very stupid. We have to feed it one word at a
744 * time since it can't cope with a full string. Here we convert command
745 * (char*) into cmd (char**, one word per string) */
747 /* We need a clean copy, so strsep can mess up the copy while
748 * we write stuff into the original (in a minute) */
749 cmd = cmd_copy = xstrdup(command);
751 for (ix = 0, tmpcmd = cmd;
752 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
755 /* we need to trim() the result for glob! */
757 retval = glob(tmpcmd, flags, NULL, &expand_result);
758 free(tmpcmd); /* Free mem allocated by strsep_space */
759 if (retval == GLOB_NOSPACE) {
760 /* Mem may have been allocated... */
761 globfree (&expand_result);
762 bb_error_msg(out_of_space);
764 } else if (retval != 0) {
765 /* Some other error. GLOB_NOMATCH shouldn't
766 * happen because of the GLOB_NOCHECK flag in
768 bb_error_msg("syntax error");
771 /* Convert from char** (one word per string) to a simple char*,
772 * but don't overflow command which is BUFSIZ in length */
773 for (i=0; i < expand_result.gl_pathc; i++) {
774 length=strlen(expand_result.gl_pathv[i]);
775 if (total_length+length+1 >= BUFSIZ) {
776 bb_error_msg(out_of_space);
779 strcat(command+total_length, " ");
781 strcat(command+total_length, expand_result.gl_pathv[i]);
782 total_length+=length;
784 globfree (&expand_result);
790 /* Now do the shell variable substitutions which
791 * wordexp can't do for us, namely $? and $! */
793 while((dst = strchr(src,'$')) != NULL){
797 var = itoa(last_return_code);
803 var = itoa(last_bg_pid);
805 /* Everything else like $$, $#, $[0-9], etc. should all be
806 * expanded by wordexp(), so we can in theory skip that stuff
807 * here, but just to be on the safe side (i.e., since uClibc
808 * wordexp doesn't do this stuff yet), lets leave it in for
811 var = itoa(getpid());
816 case '0':case '1':case '2':case '3':case '4':
817 case '5':case '6':case '7':case '8':case '9':
819 int ixx=*(dst+1)-48+1;
830 /* a single character construction was found, and
831 * already handled in the case statement */
834 /* Looks like an environment variable */
836 int num_skip_chars=0;
837 int dstlen = strlen(dst);
838 /* Is this a ${foo} type variable? */
839 if (dstlen >=2 && *(dst+1) == '{') {
840 src=strchr(dst+1, '}');
844 while((isalnum)(*src) || *src=='_') src++;
850 *src='\0'; /* temporary */
851 var = getenv(dst + 1 + num_skip_chars);
853 src += num_skip_chars;
856 /* Seems we got an un-expandable variable. So delete it. */
860 int subst_len = strlen(var);
861 int trail_len = strlen(src);
862 if (dst+subst_len+trail_len >= command+BUFSIZ) {
863 bb_error_msg(out_of_space);
866 /* Move stuff to the end of the string to accommodate
867 * filling the created gap with the new stuff */
868 memmove(dst+subst_len, src, trail_len+1);
869 /* Now copy in the new stuff */
870 memcpy(dst, var, subst_len);
878 /* Return cmd->num_progs as 0 if no command is present (e.g. an empty
879 line). If a valid command is found, command_ptr is set to point to
880 the beginning of the next command (if the original command had more
881 then one job associated with it) or NULL if no more commands are
883 static int parse_command(char **command_ptr, struct job *job, int *inbg)
886 char *return_command = NULL;
892 struct child_prog *prog;
893 #ifdef CONFIG_LASH_PIPE_N_REDIRECTS
898 /* skip leading white space */
899 *command_ptr = skip_whitespace(*command_ptr);
901 /* this handles empty lines or leading '#' characters */
902 if (!**command_ptr || (**command_ptr == '#')) {
909 job->progs = xmalloc(sizeof(*job->progs));
911 /* We set the argv elements to point inside of this string. The
912 memory is freed by free_job(). Allocate twice the original
913 length in case we need to quote every single character.
915 Getting clean memory relieves us of the task of NULL
916 terminating things and makes the rest of this look a bit
917 cleaner (though it is, admittedly, a tad less efficient) */
918 job->cmdbuf = command = xzalloc(2*strlen(*command_ptr) + 1);
922 prog->num_redirects = 0;
923 prog->is_stopped = 0;
925 #ifdef CONFIG_LASH_PIPE_N_REDIRECTS
926 prog->redirects = NULL;
930 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
931 prog->argv[0] = job->cmdbuf;
936 while (*src && !(flag & LASH_OPT_DONE)) {
943 bb_error_msg("character expected after \\");
948 /* in shell, "\'" should yield \' */
953 } else if (*src == '*' || *src == '?' || *src == '[' ||
954 *src == ']') *buf++ = '\\';
956 } else if (isspace(*src)) {
957 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) {
959 /* +1 here leaves room for the NULL which ends argv */
960 if ((argc_l + 1) == argv_alloced) {
962 prog->argv = xrealloc(prog->argv,
963 sizeof(*prog->argv) *
966 prog->argv[argc_l] = buf;
967 flag ^= LASH_OPT_SAW_QUOTE;
974 flag |= LASH_OPT_SAW_QUOTE;
977 case '#': /* comment */
981 flag |= LASH_OPT_DONE;
984 #ifdef CONFIG_LASH_PIPE_N_REDIRECTS
985 case '>': /* redirects */
987 i = prog->num_redirects++;
988 prog->redirects = xrealloc(prog->redirects,
989 sizeof(*prog->redirects) *
992 prog->redirects[i].fd = -1;
993 if (buf != prog->argv[argc_l]) {
994 /* the stuff before this character may be the file number
996 prog->redirects[i].fd =
997 strtol(prog->argv[argc_l], &chptr, 10);
999 if (*chptr && *prog->argv[argc_l]) {
1001 prog->argv[argc_l] = buf;
1005 if (prog->redirects[i].fd == -1) {
1007 prog->redirects[i].fd = 1;
1009 prog->redirects[i].fd = 0;
1012 if (*src++ == '>') {
1014 prog->redirects[i].type =
1015 REDIRECT_APPEND, src++;
1017 prog->redirects[i].type = REDIRECT_OVERWRITE;
1019 prog->redirects[i].type = REDIRECT_INPUT;
1022 /* This isn't POSIX sh compliant. Oh well. */
1024 chptr = skip_whitespace(chptr);
1027 bb_error_msg("file name expected after %c", *(src-1));
1033 prog->redirects[i].filename = buf;
1034 while (*chptr && !isspace(*chptr))
1037 src = chptr - 1; /* we src++ later */
1038 prog->argv[argc_l] = ++buf;
1041 case '|': /* pipe */
1042 /* finish this command */
1043 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE)
1046 goto empty_command_in_pipe;
1048 prog->argv[argc_l] = NULL;
1050 /* and start the next */
1052 job->progs = xrealloc(job->progs,
1053 sizeof(*job->progs) * job->num_progs);
1054 prog = job->progs + (job->num_progs - 1);
1055 prog->num_redirects = 0;
1056 prog->redirects = NULL;
1057 prog->is_stopped = 0;
1062 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
1063 prog->argv[0] = ++buf;
1066 src = skip_whitespace(src);
1069 empty_command_in_pipe:
1070 bb_error_msg("empty command in pipe");
1075 src--; /* we'll ++ it at the end of the loop */
1080 #ifdef CONFIG_LASH_JOB_CONTROL
1081 case '&': /* background */
1085 case ';': /* multiple commands */
1086 flag |= LASH_OPT_DONE;
1087 return_command = *command_ptr + (src - *command_ptr) + 1;
1093 bb_error_msg("character expected after \\");
1097 if (*src == '*' || *src == '[' || *src == ']'
1098 || *src == '?') *buf++ = '\\';
1107 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) {
1114 prog->argv[argc_l] = NULL;
1116 if (!return_command) {
1117 job->text = xstrdup(*command_ptr);
1119 /* This leaves any trailing spaces, which is a bit sloppy */
1120 job->text = xstrndup(*command_ptr, return_command - *command_ptr);
1123 *command_ptr = return_command;
1128 /* Run the child_prog, no matter what kind of command it uses.
1130 static int pseudo_exec(struct child_prog *child)
1132 struct built_in_command *x;
1134 /* Check if the command matches any of the non-forking builtins.
1135 * Depending on context, this might be redundant. But it's
1136 * easier to waste a few CPU cycles than it is to figure out
1137 * if this is one of those cases.
1139 for (x = bltins; x->cmd; x++) {
1140 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1141 _exit(x->function(child));
1145 /* Check if the command matches any of the forking builtins. */
1146 for (x = bltins_forking; x->cmd; x++) {
1147 if (strcmp(child->argv[0], x->cmd) == 0) {
1149 _exit (x->function(child));
1153 /* Check if the command matches any busybox internal
1154 * commands ("applets") here. Following discussions from
1155 * November 2000 on busybox@busybox.net, don't use
1156 * bb_get_last_path_component(). This way explicit (with
1157 * slashes) filenames will never be interpreted as an
1158 * applet, just like with builtins. This way the user can
1159 * override an applet with an explicit filename reference.
1160 * The only downside to this change is that an explicit
1161 * /bin/foo invocation will fork and exec /bin/foo, even if
1162 * /bin/foo is a symlink to busybox.
1165 if (ENABLE_FEATURE_SH_STANDALONE_SHELL) {
1166 char **argv_l = child->argv;
1169 for(argc_l=0; *argv_l; argv_l++, argc_l++);
1171 run_applet_by_name(child->argv[0], argc_l, child->argv);
1174 execvp(child->argv[0], child->argv);
1176 /* Do not use bb_perror_msg_and_die() here, since we must not
1177 * call exit() but should call _exit() instead */
1178 bb_perror_msg("%s", child->argv[0]);
1179 _exit(EXIT_FAILURE);
1182 static void insert_job(struct job *newjob, int inbg)
1185 struct jobset *j_list=newjob->job_list;
1187 /* find the ID for thejob to use */
1189 for (thejob = j_list->head; thejob; thejob = thejob->next)
1190 if (thejob->jobid >= newjob->jobid)
1191 newjob->jobid = thejob->jobid + 1;
1193 /* add thejob to the list of running jobs */
1194 if (!j_list->head) {
1195 thejob = j_list->head = xmalloc(sizeof(*thejob));
1197 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
1198 thejob->next = xmalloc(sizeof(*thejob));
1199 thejob = thejob->next;
1202 *thejob = *newjob; /* physically copy the struct job */
1203 thejob->next = NULL;
1204 thejob->running_progs = thejob->num_progs;
1205 thejob->stopped_progs = 0;
1207 #ifdef CONFIG_LASH_JOB_CONTROL
1209 /* we don't wait for background thejobs to return -- append it
1210 to the list of backgrounded thejobs and leave it alone */
1211 printf("[%d] %d\n", thejob->jobid,
1212 newjob->progs[newjob->num_progs - 1].pid);
1213 last_jobid = newjob->jobid;
1214 last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
1216 newjob->job_list->fg = thejob;
1218 /* move the new process group into the foreground */
1219 /* Ignore errors since child could have already exited */
1220 tcsetpgrp(shell_terminal, newjob->pgrp);
1225 static int run_command(struct job *newjob, int inbg, int outpipe[2])
1227 /* struct job *thejob; */
1229 int nextin, nextout;
1230 int pipefds[2]; /* pipefd[0] is for reading */
1231 struct built_in_command *x;
1232 struct child_prog *child;
1234 nextin = 0, nextout = 1;
1235 for (i = 0; i < newjob->num_progs; i++) {
1236 child = & (newjob->progs[i]);
1238 if ((i + 1) < newjob->num_progs) {
1239 if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
1240 nextout = pipefds[1];
1242 if (outpipe[1]!=-1) {
1243 nextout = outpipe[1];
1250 /* Check if the command matches any non-forking builtins,
1251 * but only if this is a simple command.
1252 * Non-forking builtins within pipes have to fork anyway,
1253 * and are handled in pseudo_exec. "echo foo | read bar"
1254 * is doomed to failure, and doesn't work on bash, either.
1256 if (newjob->num_progs == 1) {
1257 /* Check if the command sets an environment variable. */
1258 if (strchr(child->argv[0], '=') != NULL) {
1259 child->argv[1] = child->argv[0];
1260 return builtin_export(child);
1263 for (x = bltins; x->cmd; x++) {
1264 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1266 int squirrel[] = {-1, -1, -1};
1267 setup_redirects(child, squirrel);
1268 rcode = x->function(child);
1269 restore_redirects(squirrel);
1275 #if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
1276 if (!(child->pid = fork()))
1278 if (!(child->pid = vfork()))
1281 /* Set the handling for job control signals back to the default. */
1282 signal(SIGINT, SIG_DFL);
1283 signal(SIGQUIT, SIG_DFL);
1284 signal(SIGTSTP, SIG_DFL);
1285 signal(SIGTTIN, SIG_DFL);
1286 signal(SIGTTOU, SIG_DFL);
1287 signal(SIGCHLD, SIG_DFL);
1289 /* Close all open filehandles. */
1290 while(close_me_list) close((long)llist_pop(&close_me_list));
1292 if (outpipe[1]!=-1) {
1302 dup2(nextout, 2); /* Really? */
1307 /* explicit redirects override pipes */
1308 setup_redirects(child,NULL);
1312 if (outpipe[1]!=-1) {
1316 /* put our child in the process group whose leader is the
1317 first process in this pipe */
1318 setpgid(child->pid, newjob->progs[0].pid);
1324 /* If there isn't another process, nextin is garbage
1325 but it doesn't matter */
1326 nextin = pipefds[0];
1329 newjob->pgrp = newjob->progs[0].pid;
1331 insert_job(newjob, inbg);
1336 static int busy_loop(FILE * input)
1339 char *next_command = NULL;
1344 #ifdef CONFIG_LASH_JOB_CONTROL
1346 /* save current owner of TTY so we can restore it on exit */
1347 parent_pgrp = tcgetpgrp(shell_terminal);
1349 newjob.job_list = &job_list;
1350 newjob.job_context = DEFAULT_CONTEXT;
1352 command = xzalloc(BUFSIZ);
1356 /* no job is in the foreground */
1358 /* see if any background processes have exited */
1359 checkjobs(&job_list);
1361 if (!next_command) {
1362 if (get_command(input, command))
1364 next_command = command;
1367 if (! expand_arguments(next_command)) {
1369 command = xzalloc(BUFSIZ);
1370 next_command = NULL;
1374 if (!parse_command(&next_command, &newjob, &inbg) &&
1376 int pipefds[2] = {-1,-1};
1377 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
1379 run_command(&newjob, inbg, pipefds);
1383 command = (char *) xzalloc(BUFSIZ);
1384 next_command = NULL;
1387 /* a job is running in the foreground; wait for it */
1389 while (!job_list.fg->progs[i].pid ||
1390 job_list.fg->progs[i].is_stopped == 1) i++;
1392 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) {
1393 if (errno != ECHILD) {
1394 bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
1398 if (WIFEXITED(status) || WIFSIGNALED(status)) {
1399 /* the child exited */
1400 job_list.fg->running_progs--;
1401 job_list.fg->progs[i].pid = 0;
1403 last_return_code=WEXITSTATUS(status);
1405 if (!job_list.fg->running_progs) {
1407 remove_job(&job_list, job_list.fg);
1411 #ifdef CONFIG_LASH_JOB_CONTROL
1413 /* the child was stopped */
1414 job_list.fg->stopped_progs++;
1415 job_list.fg->progs[i].is_stopped = 1;
1417 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1418 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1419 "Stopped", job_list.fg->text);
1425 /* move the shell to the foreground */
1426 /* suppress messages when run from /linuxrc mag@sysgo.de */
1427 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
1428 bb_perror_msg("tcsetpgrp");
1435 #ifdef CONFIG_LASH_JOB_CONTROL
1436 /* return controlling TTY back to parent process group before exiting */
1437 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
1438 bb_perror_msg("tcsetpgrp");
1441 /* return exit status if called with "-c" */
1442 if (input == NULL && WIFEXITED(status))
1443 return WEXITSTATUS(status);
1448 #ifdef CONFIG_FEATURE_CLEAN_UP
1449 static void free_memory(void)
1451 if (cwd && cwd!=bb_msg_unknown) {
1454 if (local_pending_command)
1455 free(local_pending_command);
1457 if (job_list.fg && !job_list.fg->running_progs) {
1458 remove_job(&job_list, job_list.fg);
1462 void free_memory(void);
1465 #ifdef CONFIG_LASH_JOB_CONTROL
1466 /* Make sure we have a controlling tty. If we get started under a job
1467 * aware app (like bash for example), make sure we are now in charge so
1468 * we don't fight over who gets the foreground */
1469 static void setup_job_control(void)
1474 /* Loop until we are in the foreground. */
1475 while ((status = tcgetpgrp (shell_terminal)) >= 0) {
1476 if (status == (shell_pgrp = getpgrp ())) {
1479 kill (- shell_pgrp, SIGTTIN);
1482 /* Ignore interactive and job-control signals. */
1483 signal(SIGINT, SIG_IGN);
1484 signal(SIGQUIT, SIG_IGN);
1485 signal(SIGTSTP, SIG_IGN);
1486 signal(SIGTTIN, SIG_IGN);
1487 signal(SIGTTOU, SIG_IGN);
1488 signal(SIGCHLD, SIG_IGN);
1490 /* Put ourselves in our own process group. */
1492 shell_pgrp = getpid ();
1493 setpgid(shell_pgrp, shell_pgrp);
1495 /* Grab control of the terminal. */
1496 tcsetpgrp(shell_terminal, shell_pgrp);
1499 static inline void setup_job_control(void)
1504 int lash_main(int argc_l, char **argv_l)
1507 FILE *input = stdin;
1511 /* These variables need re-initializing when recursing */
1513 close_me_list = NULL;
1514 job_list.head = NULL;
1518 if (argv[0] && argv[0][0] == '-') {
1520 prof_input = fopen("/etc/profile", "r");
1522 llist_add_to(&close_me_list, (void *)(long)fileno(prof_input));
1523 /* Now run the file */
1524 busy_loop(prof_input);
1525 fclose_if_not_stdin(prof_input);
1526 llist_pop(&close_me_list);
1530 opt = getopt32(argc_l, argv_l, "+ic:", &local_pending_command);
1531 #define LASH_OPT_i (1<<0)
1532 #define LASH_OPT_c (1<<2)
1533 if (opt & LASH_OPT_c) {
1538 /* A shell is interactive if the `-i' flag was given, or if all of
1539 * the following conditions are met:
1541 * no arguments remaining or the -s flag given
1542 * standard input is a terminal
1543 * standard output is a terminal
1544 * Refer to Posix.2, the description of the `sh' utility. */
1545 if (argv[optind]==NULL && input==stdin &&
1546 isatty(STDIN_FILENO) && isatty(STDOUT_FILENO))
1550 setup_job_control();
1551 if (opt & LASH_OPT_i) {
1552 /* Looks like they want an interactive shell */
1553 if (!ENABLE_FEATURE_SH_EXTRA_QUIET) {
1554 printf("\n\n%s Built-in shell (lash)\n"
1555 "Enter 'help' for a list of built-in commands.\n\n",
1558 } else if (!local_pending_command && argv[optind]) {
1559 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
1560 input = xfopen(argv[optind], "r");
1561 /* be lazy, never mark this closed */
1562 llist_add_to(&close_me_list, (void *)(long)fileno(input));
1565 /* initialize the cwd -- this is never freed...*/
1568 cwd = bb_msg_unknown;
1570 if (ENABLE_FEATURE_CLEAN_UP) atexit(free_memory);
1572 if (ENABLE_FEATURE_COMMAND_EDITING) cmdedit_set_initial_prompt();
1575 return (busy_loop(input));