Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 3 | * lash -- the BusyBox Lame-Ass SHell |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 6 | * |
| 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." |
| 10 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 11 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 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... |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 18 | */ |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 19 | |
Eric Andersen | 1e7cea9 | 2000-12-06 23:47:38 +0000 | [diff] [blame] | 20 | //For debugging/development on the shell only... |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 21 | //#define DEBUG_SHELL |
Eric Andersen | a1d187a | 2000-07-17 19:14:41 +0000 | [diff] [blame] | 22 | |
| 23 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 24 | #include "busybox.h" |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 25 | #include <getopt.h> |
Mark Whitley | 4b541a8 | 2001-04-25 17:10:30 +0000 | [diff] [blame] | 26 | #include "cmdedit.h" |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 27 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 28 | #ifdef CONFIG_LOCALE_SUPPORT |
Mark Whitley | 1c6581a | 2001-03-27 16:35:16 +0000 | [diff] [blame] | 29 | #include <locale.h> |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 30 | #endif |
Eric Andersen | 13d1fa1 | 2001-03-08 23:59:45 +0000 | [diff] [blame] | 31 | |
Eric Andersen | 13d1fa1 | 2001-03-08 23:59:45 +0000 | [diff] [blame] | 32 | #include <glob.h> |
| 33 | #define expand_t glob_t |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 34 | |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 35 | /* Always enable for the moment... */ |
Eric Andersen | 95b2625 | 2004-02-10 01:30:21 +0000 | [diff] [blame] | 36 | #define CONFIG_LASH_PIPE_N_REDIRECTS |
| 37 | #define CONFIG_LASH_JOB_CONTROL |
Eric Andersen | 13d1fa1 | 2001-03-08 23:59:45 +0000 | [diff] [blame] | 38 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 39 | static const int MAX_READ = 128; /* size of input buffer for `read' builtin */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 40 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
| 41 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 42 | |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 43 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 44 | enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE, |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 45 | REDIRECT_APPEND |
| 46 | }; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 47 | #endif |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 48 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 49 | enum { |
| 50 | DEFAULT_CONTEXT = 0x1, |
| 51 | IF_TRUE_CONTEXT = 0x2, |
| 52 | IF_FALSE_CONTEXT = 0x4, |
| 53 | THEN_EXP_CONTEXT = 0x8, |
| 54 | ELSE_EXP_CONTEXT = 0x10 |
| 55 | }; |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 56 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 57 | #define LASH_OPT_DONE (1) |
| 58 | #define LASH_OPT_SAW_QUOTE (2) |
| 59 | |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 60 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 61 | struct redir_struct { |
| 62 | enum redir_type type; /* type of redirection */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 63 | int fd; /* file descriptor being redirected */ |
| 64 | char *filename; /* file to redirect fd to */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 65 | }; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 66 | #endif |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 67 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 68 | struct child_prog { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 69 | pid_t pid; /* 0 if exited */ |
| 70 | char **argv; /* program name and arguments */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 71 | int num_redirects; /* elements in redirection array */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 72 | int is_stopped; /* is the program currently running? */ |
| 73 | struct job *family; /* pointer back to the child's parent job */ |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 74 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS |
| 75 | struct redir_struct *redirects; /* I/O redirects */ |
| 76 | #endif |
| 77 | }; |
| 78 | |
| 79 | struct jobset { |
| 80 | struct job *head; /* head of list of running jobs */ |
| 81 | struct job *fg; /* current foreground job */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | struct job { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 85 | int jobid; /* job number */ |
| 86 | int num_progs; /* total number of programs in job */ |
| 87 | int running_progs; /* number of programs running */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 88 | char *text; /* name of job */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 89 | char *cmdbuf; /* buffer various argv's point into */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 90 | pid_t pgrp; /* process group ID for the job */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 91 | struct child_prog *progs; /* array of programs in job */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 92 | struct job *next; /* to track background commands */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 93 | int stopped_progs; /* number of programs alive, but stopped */ |
| 94 | unsigned int job_context; /* bitmask defining current context */ |
| 95 | struct jobset *job_list; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 98 | struct built_in_command { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 99 | char *cmd; /* name */ |
| 100 | char *descr; /* description */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 101 | int (*function) (struct child_prog *); /* function ptr */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 104 | /* function prototypes for builtins */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 105 | static int builtin_cd(struct child_prog *cmd); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 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); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 116 | |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 117 | |
| 118 | /* function prototypes for shell stuff */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 119 | static void checkjobs(struct jobset *job_list); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 120 | static void remove_job(struct jobset *j_list, struct job *job); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 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]); |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 124 | static int pseudo_exec(struct child_prog *cmd) ATTRIBUTE_NORETURN; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 125 | static int busy_loop(FILE * input); |
| 126 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 127 | |
Mark Whitley | 37653aa | 2000-07-12 23:36:17 +0000 | [diff] [blame] | 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) */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 131 | static struct built_in_command bltins[] = { |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 132 | {"bg", "Resume a job in the background", builtin_fg_bg}, |
| 133 | {"cd", "Change working directory", builtin_cd}, |
Eric Andersen | d2f5677 | 2000-09-21 02:48:07 +0000 | [diff] [blame] | 134 | {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec}, |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 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}, |
Matt Kraai | dd450a0 | 2000-09-13 03:43:36 +0000 | [diff] [blame] | 141 | {".", "Source-in and run commands in a file", builtin_source}, |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 142 | /* to do: add ulimit */ |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 143 | {NULL, NULL, NULL} |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Mark Whitley | 37653aa | 2000-07-12 23:36:17 +0000 | [diff] [blame] | 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) */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 148 | static struct built_in_command bltins_forking[] = { |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 149 | {"pwd", "Print current directory", builtin_pwd}, |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 150 | {"help", "List shell built-in commands", builtin_help}, |
| 151 | {NULL, NULL, NULL} |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
Eric Andersen | 0bcc813 | 2001-01-05 19:37:32 +0000 | [diff] [blame] | 154 | |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 155 | static int shell_context; /* Type prompt trigger (PS1 or PS2) */ |
Eric Andersen | 0bcc813 | 2001-01-05 19:37:32 +0000 | [diff] [blame] | 156 | |
| 157 | |
| 158 | /* Globals that are static to this file */ |
Eric Andersen | cfa88ec | 2001-05-11 18:08:16 +0000 | [diff] [blame] | 159 | static const char *cwd; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 160 | static char *local_pending_command; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 161 | static struct jobset job_list = { NULL, NULL }; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 162 | static int argc; |
| 163 | static char **argv; |
Rob Landley | f946379 | 2006-06-13 18:50:20 +0000 | [diff] [blame] | 164 | static llist_t *close_me_list; |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 165 | static int last_return_code; |
| 166 | static int last_bg_pid; |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 167 | static unsigned int last_jobid; |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 168 | static int shell_terminal; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 169 | static char *PS1; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 170 | static char *PS2 = "> "; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 171 | |
| 172 | |
Eric Andersen | b558e76 | 2000-11-30 22:43:16 +0000 | [diff] [blame] | 173 | #ifdef DEBUG_SHELL |
| 174 | static inline void debug_printf(const char *format, ...) |
| 175 | { |
| 176 | va_list args; |
| 177 | va_start(args, format); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 178 | vfprintf(stderr, format, args); |
Eric Andersen | b558e76 | 2000-11-30 22:43:16 +0000 | [diff] [blame] | 179 | va_end(args); |
| 180 | } |
| 181 | #else |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 182 | static inline void debug_printf(const char ATTRIBUTE_UNUSED *format, ...) { } |
Eric Andersen | b558e76 | 2000-11-30 22:43:16 +0000 | [diff] [blame] | 183 | #endif |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 184 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 185 | /* |
| 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. |
| 190 | |
| 191 | builtin previous use notes |
| 192 | ------ ----------------- --------- |
| 193 | cd cmd->progs[0] |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 194 | exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins |
| 195 | exit cmd->progs[0] |
| 196 | fg_bg cmd->progs[0], job_list->head, job_list->fg |
| 197 | help 0 |
| 198 | jobs job_list->head |
| 199 | pwd 0 |
Eric Andersen | 84e229c | 2001-03-29 22:48:33 +0000 | [diff] [blame] | 200 | export cmd->progs[0] |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 201 | source cmd->progs[0] |
| 202 | unset cmd->progs[0] |
| 203 | read cmd->progs[0] |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 204 | |
| 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 |
| 211 | */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 212 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 213 | /* built-in 'cd <path>' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 214 | static int builtin_cd(struct child_prog *child) |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 215 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 216 | char *newdir; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 217 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 218 | if (child->argv[1] == NULL) |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 219 | newdir = getenv("HOME"); |
| 220 | else |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 221 | newdir = child->argv[1]; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 222 | if (chdir(newdir)) { |
Rob Landley | 5483de1 | 2006-06-20 21:35:26 +0000 | [diff] [blame] | 223 | bb_perror_msg("cd: %s", newdir); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 224 | return EXIT_FAILURE; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 225 | } |
Eric Andersen | cfa88ec | 2001-05-11 18:08:16 +0000 | [diff] [blame] | 226 | cwd = xgetcwd((char *)cwd); |
Eric Andersen | 5f265b7 | 2001-05-11 16:58:46 +0000 | [diff] [blame] | 227 | if (!cwd) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 228 | cwd = bb_msg_unknown; |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 229 | return EXIT_SUCCESS; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Eric Andersen | d2f5677 | 2000-09-21 02:48:07 +0000 | [diff] [blame] | 232 | /* built-in 'exec' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 233 | static int builtin_exec(struct child_prog *child) |
Eric Andersen | d2f5677 | 2000-09-21 02:48:07 +0000 | [diff] [blame] | 234 | { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 235 | if (child->argv[1] == NULL) |
| 236 | return EXIT_SUCCESS; /* Really? */ |
| 237 | child->argv++; |
Rob Landley | 7a260f0 | 2006-06-19 03:20:03 +0000 | [diff] [blame] | 238 | while(close_me_list) close((long)llist_pop(&close_me_list)); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 239 | pseudo_exec(child); |
| 240 | /* never returns */ |
Eric Andersen | d2f5677 | 2000-09-21 02:48:07 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 243 | /* built-in 'exit' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 244 | static int builtin_exit(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 245 | { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 246 | if (child->argv[1] == NULL) |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 247 | exit(EXIT_SUCCESS); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 248 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 249 | exit (atoi(child->argv[1])); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* built-in 'fg' and 'bg' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 253 | static int builtin_fg_bg(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 254 | { |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 255 | int i, jobnum; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 256 | struct job *job=NULL; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 257 | |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 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) { |
| 262 | break; |
| 263 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 264 | } |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 265 | if (!job) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 266 | bb_error_msg("%s: no current job", child->argv[0]); |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 267 | return EXIT_FAILURE; |
| 268 | } |
| 269 | } else { |
| 270 | if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) { |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 271 | bb_error_msg(bb_msg_invalid_arg, child->argv[1], child->argv[0]); |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 272 | return EXIT_FAILURE; |
| 273 | } |
| 274 | for (job = child->family->job_list->head; job; job = job->next) { |
| 275 | if (job->jobid == jobnum) { |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | if (!job) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 280 | bb_error_msg("%s: %d: no such job", child->argv[0], jobnum); |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 281 | return EXIT_FAILURE; |
| 282 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 283 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 284 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 285 | if (*child->argv[0] == 'f') { |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 286 | /* Put the job into the foreground. */ |
| 287 | tcsetpgrp(shell_terminal, job->pgrp); |
| 288 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 289 | child->family->job_list->fg = job; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 290 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 291 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 292 | /* Restart the processes in the job */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 293 | for (i = 0; i < job->num_progs; i++) |
| 294 | job->progs[i].is_stopped = 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 295 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 296 | job->stopped_progs = 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 297 | |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 298 | if ( (i=kill(- job->pgrp, SIGCONT)) < 0) { |
| 299 | if (i == ESRCH) { |
| 300 | remove_job(&job_list, job); |
Eric Andersen | 07abfe2 | 2001-06-27 17:29:11 +0000 | [diff] [blame] | 301 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 302 | bb_perror_msg("kill (SIGCONT)"); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 303 | } |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 304 | } |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 305 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 306 | return EXIT_SUCCESS; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* built-in 'help' handler */ |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 310 | static int builtin_help(struct child_prog ATTRIBUTE_UNUSED *dummy) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 311 | { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 312 | struct built_in_command *x; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 313 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 314 | printf("\nBuilt-in commands:\n" |
| 315 | "-------------------\n"); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 316 | for (x = bltins; x->cmd; x++) { |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 317 | if (x->descr==NULL) |
| 318 | continue; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 319 | printf("%s\t%s\n", x->cmd, x->descr); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 320 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 321 | for (x = bltins_forking; x->cmd; x++) { |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 322 | if (x->descr==NULL) |
| 323 | continue; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 324 | printf("%s\t%s\n", x->cmd, x->descr); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 325 | } |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 326 | putchar('\n'); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 327 | return EXIT_SUCCESS; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /* built-in 'jobs' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 331 | static int builtin_jobs(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 332 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 333 | struct job *job; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 334 | char *status_string; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 335 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 336 | for (job = child->family->job_list->head; job; job = job->next) { |
| 337 | if (job->running_progs == job->stopped_progs) |
| 338 | status_string = "Stopped"; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 339 | else |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 340 | status_string = "Running"; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 341 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 342 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 343 | } |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 344 | return EXIT_SUCCESS; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | |
| 348 | /* built-in 'pwd' handler */ |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 349 | static int builtin_pwd(struct child_prog ATTRIBUTE_UNUSED *dummy) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 350 | { |
Eric Andersen | cfa88ec | 2001-05-11 18:08:16 +0000 | [diff] [blame] | 351 | cwd = xgetcwd((char *)cwd); |
Eric Andersen | 5f265b7 | 2001-05-11 16:58:46 +0000 | [diff] [blame] | 352 | if (!cwd) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 353 | cwd = bb_msg_unknown; |
Matt Kraai | 59df6f7 | 2001-05-16 14:21:09 +0000 | [diff] [blame] | 354 | puts(cwd); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 355 | return EXIT_SUCCESS; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 358 | /* built-in 'export VAR=value' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 359 | static int builtin_export(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 360 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 361 | int res; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 362 | char *v = child->argv[1]; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 363 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 364 | if (v == NULL) { |
Eric Andersen | 84e229c | 2001-03-29 22:48:33 +0000 | [diff] [blame] | 365 | char **e; |
| 366 | for (e = environ; *e; e++) { |
Matt Kraai | 59df6f7 | 2001-05-16 14:21:09 +0000 | [diff] [blame] | 367 | puts(*e); |
Eric Andersen | 84e229c | 2001-03-29 22:48:33 +0000 | [diff] [blame] | 368 | } |
Matt Kraai | 2129f97 | 2001-04-04 17:50:04 +0000 | [diff] [blame] | 369 | return 0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 370 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 371 | res = putenv(v); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 372 | if (res) |
Rob Landley | 5483de1 | 2006-06-20 21:35:26 +0000 | [diff] [blame] | 373 | bb_perror_msg("export"); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 374 | #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 375 | if (strncmp(v, "PS1=", 4)==0) |
| 376 | PS1 = getenv("PS1"); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 377 | #endif |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 378 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 379 | #ifdef CONFIG_LOCALE_SUPPORT |
Mark Whitley | 1c6581a | 2001-03-27 16:35:16 +0000 | [diff] [blame] | 380 | if(strncmp(v, "LC_ALL=", 7)==0) |
| 381 | setlocale(LC_ALL, getenv("LC_ALL")); |
Mark Whitley | a82a003 | 2001-03-27 17:07:15 +0000 | [diff] [blame] | 382 | if(strncmp(v, "LC_CTYPE=", 9)==0) |
Mark Whitley | 1c6581a | 2001-03-27 16:35:16 +0000 | [diff] [blame] | 383 | setlocale(LC_CTYPE, getenv("LC_CTYPE")); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 384 | #endif |
Mark Whitley | 1c6581a | 2001-03-27 16:35:16 +0000 | [diff] [blame] | 385 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 386 | return (res); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 389 | /* built-in 'read VAR' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 390 | static int builtin_read(struct child_prog *child) |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 391 | { |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 392 | int res = 0, len; |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 393 | char *s; |
| 394 | char string[MAX_READ]; |
| 395 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 396 | if (child->argv[1]) { |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 397 | /* argument (VAR) given: put "VAR=" into buffer */ |
Eric Andersen | 0d2d1eb | 2002-06-06 13:33:01 +0000 | [diff] [blame] | 398 | safe_strncpy(string, child->argv[1], MAX_READ-1); |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 399 | len = strlen(string); |
| 400 | string[len++] = '='; |
| 401 | string[len] = '\0'; |
| 402 | fgets(&string[len], sizeof(string) - len, stdin); /* read string */ |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 403 | res = strlen(string); |
| 404 | if (res > len) |
| 405 | string[--res] = '\0'; /* chomp trailing newline */ |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 406 | /* |
| 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!) |
| 410 | */ |
| 411 | res = -1; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 412 | if ((s = strdup(string))) |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 413 | res = putenv(s); |
| 414 | if (res) |
Rob Landley | 5483de1 | 2006-06-20 21:35:26 +0000 | [diff] [blame] | 415 | bb_perror_msg("read"); |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 416 | } |
| 417 | else |
| 418 | fgets(string, sizeof(string), stdin); |
| 419 | |
| 420 | return (res); |
| 421 | } |
| 422 | |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 423 | /* Built-in '.' handler (read-in and execute commands from file) */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 424 | static int builtin_source(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 425 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 426 | FILE *input; |
| 427 | int status; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 428 | |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame^] | 429 | input = fopen_or_warn(child->argv[1], "r"); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 430 | if (!input) { |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 431 | return EXIT_FAILURE; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 432 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 433 | |
Rob Landley | 7a260f0 | 2006-06-19 03:20:03 +0000 | [diff] [blame] | 434 | llist_add_to(&close_me_list, (void *)(long)fileno(input)); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 435 | /* Now run the file */ |
| 436 | status = busy_loop(input); |
Matt Kraai | dd450a0 | 2000-09-13 03:43:36 +0000 | [diff] [blame] | 437 | fclose(input); |
Rob Landley | f946379 | 2006-06-13 18:50:20 +0000 | [diff] [blame] | 438 | llist_pop(&close_me_list); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 439 | return (status); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* built-in 'unset VAR' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 443 | static int builtin_unset(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 444 | { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 445 | if (child->argv[1] == NULL) { |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 446 | printf(bb_msg_requires_arg, "unset"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 447 | return EXIT_FAILURE; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 448 | } |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 449 | unsetenv(child->argv[1]); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 450 | return EXIT_SUCCESS; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 453 | #ifdef CONFIG_LASH_JOB_CONTROL |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 454 | /* free up all memory from a job */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 455 | static void free_job(struct job *cmd) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 456 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 457 | int i; |
Mark Whitley | 44a9914 | 2001-03-14 17:26:37 +0000 | [diff] [blame] | 458 | struct jobset *keep; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 459 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 460 | for (i = 0; i < cmd->num_progs; i++) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 461 | free(cmd->progs[i].argv); |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 462 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 463 | if (cmd->progs[i].redirects) |
| 464 | free(cmd->progs[i].redirects); |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 465 | #endif |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 466 | } |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 467 | free(cmd->progs); |
| 468 | free(cmd->text); |
| 469 | free(cmd->cmdbuf); |
Mark Whitley | 44a9914 | 2001-03-14 17:26:37 +0000 | [diff] [blame] | 470 | keep = cmd->job_list; |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 471 | memset(cmd, 0, sizeof(struct job)); |
Mark Whitley | 44a9914 | 2001-03-14 17:26:37 +0000 | [diff] [blame] | 472 | cmd->job_list = keep; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 475 | /* remove a job from a jobset */ |
| 476 | static void remove_job(struct jobset *j_list, struct job *job) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 477 | { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 478 | struct job *prevjob; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 479 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 480 | free_job(job); |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 481 | if (job == j_list->head) { |
| 482 | j_list->head = job->next; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 483 | } else { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 484 | prevjob = j_list->head; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 485 | while (prevjob->next != job) |
| 486 | prevjob = prevjob->next; |
| 487 | prevjob->next = job->next; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 488 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 489 | |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 490 | if (j_list->head) |
| 491 | last_jobid = j_list->head->jobid; |
| 492 | else |
| 493 | last_jobid = 0; |
| 494 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 495 | free(job); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 498 | /* Checks to see if any background processes have exited -- if they |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 499 | have, figure out why and see if a job has completed */ |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 500 | static void checkjobs(struct jobset *j_list) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 501 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 502 | struct job *job; |
| 503 | pid_t childpid; |
| 504 | int status; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 505 | int prognum = 0; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 506 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 507 | while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 508 | for (job = j_list->head; job; job = job->next) { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 509 | prognum = 0; |
| 510 | while (prognum < job->num_progs && |
| 511 | job->progs[prognum].pid != childpid) prognum++; |
| 512 | if (prognum < job->num_progs) |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 513 | break; |
| 514 | } |
| 515 | |
Eric Andersen | a1d187a | 2000-07-17 19:14:41 +0000 | [diff] [blame] | 516 | /* This happens on backticked commands */ |
| 517 | if(job==NULL) |
| 518 | return; |
| 519 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 520 | if (WIFEXITED(status) || WIFSIGNALED(status)) { |
| 521 | /* child exited */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 522 | job->running_progs--; |
| 523 | job->progs[prognum].pid = 0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 524 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 525 | if (!job->running_progs) { |
| 526 | printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text); |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 527 | last_jobid=0; |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 528 | remove_job(j_list, job); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 529 | } |
| 530 | } else { |
| 531 | /* child stopped */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 532 | job->stopped_progs++; |
| 533 | job->progs[prognum].is_stopped = 1; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 534 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 535 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 536 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 537 | if (childpid == -1 && errno != ECHILD) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 538 | bb_perror_msg("waitpid"); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 539 | } |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 540 | #else |
| 541 | static void checkjobs(struct jobset *j_list) |
| 542 | { |
| 543 | } |
| 544 | static void free_job(struct job *cmd) |
| 545 | { |
| 546 | } |
| 547 | static void remove_job(struct jobset *j_list, struct job *job) |
| 548 | { |
| 549 | } |
| 550 | #endif |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 551 | |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 552 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 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[]) |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 556 | { |
| 557 | int i; |
| 558 | int openfd; |
| 559 | int mode = O_RDONLY; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 560 | struct redir_struct *redir = prog->redirects; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 561 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 562 | for (i = 0; i < prog->num_redirects; i++, redir++) { |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 563 | switch (redir->type) { |
| 564 | case REDIRECT_INPUT: |
| 565 | mode = O_RDONLY; |
| 566 | break; |
| 567 | case REDIRECT_OVERWRITE: |
Eric Andersen | 46f0beb | 2000-11-14 21:59:22 +0000 | [diff] [blame] | 568 | mode = O_WRONLY | O_CREAT | O_TRUNC; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 569 | break; |
| 570 | case REDIRECT_APPEND: |
Eric Andersen | 46f0beb | 2000-11-14 21:59:22 +0000 | [diff] [blame] | 571 | mode = O_WRONLY | O_CREAT | O_APPEND; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 572 | break; |
| 573 | } |
| 574 | |
| 575 | openfd = open(redir->filename, mode, 0666); |
| 576 | if (openfd < 0) { |
| 577 | /* this could get lost if stderr has been redirected, but |
| 578 | bash and ash both lose it as well (though zsh doesn't!) */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 579 | bb_perror_msg("error opening %s", redir->filename); |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 580 | return 1; |
| 581 | } |
| 582 | |
| 583 | if (openfd != redir->fd) { |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 584 | if (squirrel && redir->fd < 3) { |
| 585 | squirrel[redir->fd] = dup(redir->fd); |
Eric Andersen | acd647c | 2004-08-16 08:38:34 +0000 | [diff] [blame] | 586 | fcntl (squirrel[redir->fd], F_SETFD, FD_CLOEXEC); |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 587 | } |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 588 | dup2(openfd, redir->fd); |
| 589 | close(openfd); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 596 | static void restore_redirects(int squirrel[]) |
| 597 | { |
| 598 | int i, fd; |
| 599 | for (i=0; i<3; i++) { |
| 600 | fd = squirrel[i]; |
| 601 | if (fd != -1) { |
| 602 | /* No error checking. I sure wouldn't know what |
| 603 | * to do with an error if I found one! */ |
| 604 | dup2(fd, i); |
| 605 | close(fd); |
| 606 | } |
| 607 | } |
| 608 | } |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 609 | #else |
| 610 | static inline int setup_redirects(struct child_prog *prog, int squirrel[]) |
| 611 | { |
| 612 | return 0; |
| 613 | } |
| 614 | static inline void restore_redirects(int squirrel[]) |
| 615 | { |
| 616 | } |
| 617 | #endif |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 618 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 619 | static inline void cmdedit_set_initial_prompt(void) |
Eric Andersen | 22332fd | 2001-01-30 23:40:39 +0000 | [diff] [blame] | 620 | { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 621 | #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 622 | PS1 = NULL; |
Eric Andersen | 22332fd | 2001-01-30 23:40:39 +0000 | [diff] [blame] | 623 | #else |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 624 | PS1 = getenv("PS1"); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 625 | if(PS1==0) |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 626 | PS1 = "\\w \\$ "; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 627 | #endif |
Eric Andersen | 09acc06 | 2001-01-04 11:10:38 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 630 | static inline void setup_prompt_string(char **prompt_str) |
| 631 | { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 632 | #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 633 | /* Set up the prompt */ |
| 634 | if (shell_context == 0) { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 635 | free(PS1); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 636 | PS1=xmalloc(strlen(cwd)+4); |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 637 | sprintf(PS1, "%s %c ", cwd, ( geteuid() != 0 ) ? '$': '#'); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 638 | *prompt_str = PS1; |
| 639 | } else { |
| 640 | *prompt_str = PS2; |
| 641 | } |
| 642 | #else |
| 643 | *prompt_str = (shell_context==0)? PS1 : PS2; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 644 | #endif |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 645 | } |
Eric Andersen | 22332fd | 2001-01-30 23:40:39 +0000 | [diff] [blame] | 646 | |
Eric Andersen | 09acc06 | 2001-01-04 11:10:38 +0000 | [diff] [blame] | 647 | static int get_command(FILE * source, char *command) |
| 648 | { |
| 649 | char *prompt_str; |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 650 | |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 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; |
| 657 | return 0; |
| 658 | } |
| 659 | return 1; |
| 660 | } |
| 661 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 662 | if (source == stdin) { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 663 | setup_prompt_string(&prompt_str); |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 664 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 665 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 666 | /* |
| 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) |
| 671 | */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 672 | cmdedit_read_input(prompt_str, command); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 673 | return 0; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 674 | #else |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 675 | fputs(prompt_str, stdout); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 676 | #endif |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 677 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 678 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 679 | if (!fgets(command, BUFSIZ - 2, source)) { |
| 680 | if (source == stdin) |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 681 | puts(""); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 682 | return 1; |
| 683 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 684 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 685 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 686 | } |
| 687 | |
"Vladimir N. Oleynik" | 264e771 | 2005-09-22 14:35:29 +0000 | [diff] [blame] | 688 | static char * strsep_space( char *string, int * ix) |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 689 | { |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 690 | /* Short circuit the trivial case */ |
| 691 | if ( !string || ! string[*ix]) |
| 692 | return NULL; |
| 693 | |
| 694 | /* Find the end of the token. */ |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 695 | while (string[*ix] && !isspace(string[*ix]) ) { |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 696 | (*ix)++; |
| 697 | } |
| 698 | |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 699 | /* Find the end of any whitespace trailing behind |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 700 | * the token and let that be part of the token */ |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 701 | while (string[*ix] && (isspace)(string[*ix]) ) { |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 702 | (*ix)++; |
| 703 | } |
| 704 | |
Rob Landley | f946379 | 2006-06-13 18:50:20 +0000 | [diff] [blame] | 705 | if (!*ix) { |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 706 | /* Nothing useful was found */ |
| 707 | return NULL; |
| 708 | } |
| 709 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 710 | return xstrndup(string, *ix); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 711 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 712 | |
Eric Andersen | ca60459 | 2001-03-08 17:17:13 +0000 | [diff] [blame] | 713 | static int expand_arguments(char *command) |
| 714 | { |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 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; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 719 | const char * const out_of_space = "out of space during expansion"; |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 720 | int flags = GLOB_NOCHECK |
| 721 | #ifdef GLOB_BRACE |
| 722 | | GLOB_BRACE |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 723 | #endif |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 724 | #ifdef GLOB_TILDE |
| 725 | | GLOB_TILDE |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 726 | #endif |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 727 | ; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 728 | |
Eric Andersen | ca60459 | 2001-03-08 17:17:13 +0000 | [diff] [blame] | 729 | /* get rid of the terminating \n */ |
| 730 | chomp(command); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 731 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 732 | /* Fix up escape sequences to be the Real Thing(tm) */ |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 733 | while( command && command[ix]) { |
| 734 | if (command[ix] == '\\') { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 735 | const char *tmp = command+ix+1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 736 | command[ix] = bb_process_escape_sequence( &tmp ); |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 737 | memmove(command+ix + 1, tmp, strlen(tmp)+1); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 738 | } |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 739 | ix++; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 740 | } |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 741 | /* Use glob and then fixup environment variables and such */ |
| 742 | |
| 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) */ |
| 746 | |
| 747 | /* We need a clean copy, so strsep can mess up the copy while |
| 748 | * we write stuff into the original (in a minute) */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 749 | cmd = cmd_copy = xstrdup(command); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 750 | *command = '\0'; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 751 | for (ix = 0, tmpcmd = cmd; |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 752 | (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) { |
| 753 | if (*tmpcmd == '\0') |
| 754 | break; |
| 755 | /* we need to trim() the result for glob! */ |
| 756 | trim(tmpcmd); |
| 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); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 762 | bb_error_msg(out_of_space); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 763 | return FALSE; |
| 764 | } else if (retval != 0) { |
| 765 | /* Some other error. GLOB_NOMATCH shouldn't |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 766 | * happen because of the GLOB_NOCHECK flag in |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 767 | * the glob call. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 768 | bb_error_msg("syntax error"); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 769 | return FALSE; |
| 770 | } else { |
| 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) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 776 | bb_error_msg(out_of_space); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 777 | return FALSE; |
| 778 | } |
| 779 | strcat(command+total_length, " "); |
| 780 | total_length+=1; |
| 781 | strcat(command+total_length, expand_result.gl_pathv[i]); |
| 782 | total_length+=length; |
| 783 | } |
| 784 | globfree (&expand_result); |
| 785 | } |
| 786 | } |
| 787 | free(cmd_copy); |
| 788 | trim(command); |
| 789 | |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 790 | /* Now do the shell variable substitutions which |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 791 | * wordexp can't do for us, namely $? and $! */ |
| 792 | src = command; |
| 793 | while((dst = strchr(src,'$')) != NULL){ |
| 794 | var = NULL; |
| 795 | switch(*(dst+1)) { |
| 796 | case '?': |
| 797 | var = itoa(last_return_code); |
| 798 | break; |
| 799 | case '!': |
| 800 | if (last_bg_pid==-1) |
| 801 | *(var)='\0'; |
| 802 | else |
| 803 | var = itoa(last_bg_pid); |
| 804 | break; |
| 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 |
| 809 | * now. */ |
| 810 | case '$': |
| 811 | var = itoa(getpid()); |
| 812 | break; |
| 813 | case '#': |
| 814 | var = itoa(argc-1); |
| 815 | break; |
| 816 | case '0':case '1':case '2':case '3':case '4': |
| 817 | case '5':case '6':case '7':case '8':case '9': |
| 818 | { |
Eric Andersen | 7ab93d9 | 2002-11-08 09:40:02 +0000 | [diff] [blame] | 819 | int ixx=*(dst+1)-48+1; |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 820 | if (ixx >= argc) { |
| 821 | var='\0'; |
| 822 | } else { |
| 823 | var = argv[ixx]; |
| 824 | } |
| 825 | } |
| 826 | break; |
| 827 | |
| 828 | } |
| 829 | if (var) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 830 | /* a single character construction was found, and |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 831 | * already handled in the case statement */ |
| 832 | src=dst+2; |
| 833 | } else { |
| 834 | /* Looks like an environment variable */ |
| 835 | char delim_hold; |
| 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, '}'); |
| 841 | num_skip_chars=1; |
| 842 | } else { |
| 843 | src=dst+1; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 844 | while((isalnum)(*src) || *src=='_') src++; |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 845 | } |
| 846 | if (src == NULL) { |
| 847 | src = dst+dstlen; |
| 848 | } |
| 849 | delim_hold=*src; |
| 850 | *src='\0'; /* temporary */ |
| 851 | var = getenv(dst + 1 + num_skip_chars); |
| 852 | *src=delim_hold; |
| 853 | src += num_skip_chars; |
| 854 | } |
| 855 | if (var == NULL) { |
| 856 | /* Seems we got an un-expandable variable. So delete it. */ |
| 857 | var = ""; |
| 858 | } |
| 859 | { |
| 860 | int subst_len = strlen(var); |
| 861 | int trail_len = strlen(src); |
| 862 | if (dst+subst_len+trail_len >= command+BUFSIZ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 863 | bb_error_msg(out_of_space); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 864 | return FALSE; |
| 865 | } |
| 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); |
| 871 | src = dst+subst_len; |
| 872 | } |
| 873 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 874 | |
Eric Andersen | ca60459 | 2001-03-08 17:17:13 +0000 | [diff] [blame] | 875 | return TRUE; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 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 |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 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 |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 882 | present. */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 883 | static int parse_command(char **command_ptr, struct job *job, int *inbg) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 884 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 885 | char *command; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 886 | char *return_command = NULL; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 887 | char *src, *buf; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 888 | int argc_l; |
| 889 | int flag; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 890 | int argv_alloced; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 891 | char quote = '\0'; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 892 | struct child_prog *prog; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 893 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS |
| 894 | int i; |
| 895 | char *chptr; |
| 896 | #endif |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 897 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 898 | /* skip leading white space */ |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 899 | *command_ptr = skip_whitespace(*command_ptr); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 900 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 901 | /* this handles empty lines or leading '#' characters */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 902 | if (!**command_ptr || (**command_ptr == '#')) { |
| 903 | job->num_progs=0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 904 | return 0; |
| 905 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 906 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 907 | *inbg = 0; |
| 908 | job->num_progs = 1; |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 909 | job->progs = xmalloc(sizeof(*job->progs)); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 910 | |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 911 | /* We set the argv elements to point inside of this string. The |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 912 | memory is freed by free_job(). Allocate twice the original |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 913 | length in case we need to quote every single character. |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 914 | |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 915 | Getting clean memory relieves us of the task of NULL |
| 916 | terminating things and makes the rest of this look a bit |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 917 | cleaner (though it is, admittedly, a tad less efficient) */ |
Rob Landley | f946379 | 2006-06-13 18:50:20 +0000 | [diff] [blame] | 918 | job->cmdbuf = command = xzalloc(2*strlen(*command_ptr) + 1); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 919 | job->text = NULL; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 920 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 921 | prog = job->progs; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 922 | prog->num_redirects = 0; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 923 | prog->is_stopped = 0; |
| 924 | prog->family = job; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 925 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS |
| 926 | prog->redirects = NULL; |
| 927 | #endif |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 928 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 929 | argv_alloced = 5; |
| 930 | prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced); |
| 931 | prog->argv[0] = job->cmdbuf; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 932 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 933 | flag = argc_l = 0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 934 | buf = command; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 935 | src = *command_ptr; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 936 | while (*src && !(flag & LASH_OPT_DONE)) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 937 | if (quote == *src) { |
| 938 | quote = '\0'; |
| 939 | } else if (quote) { |
| 940 | if (*src == '\\') { |
| 941 | src++; |
| 942 | if (!*src) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 943 | bb_error_msg("character expected after \\"); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 944 | free_job(job); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 945 | return 1; |
| 946 | } |
| 947 | |
| 948 | /* in shell, "\'" should yield \' */ |
Eric Andersen | b2356f6 | 2000-12-11 19:14:40 +0000 | [diff] [blame] | 949 | if (*src != quote) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 950 | *buf++ = '\\'; |
Eric Andersen | b2356f6 | 2000-12-11 19:14:40 +0000 | [diff] [blame] | 951 | *buf++ = '\\'; |
| 952 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 953 | } else if (*src == '*' || *src == '?' || *src == '[' || |
| 954 | *src == ']') *buf++ = '\\'; |
| 955 | *buf++ = *src; |
| 956 | } else if (isspace(*src)) { |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 957 | if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) { |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 958 | buf++, argc_l++; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 959 | /* +1 here leaves room for the NULL which ends argv */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 960 | if ((argc_l + 1) == argv_alloced) { |
| 961 | argv_alloced += 5; |
Matt Kraai | b890752 | 2000-09-13 02:08:21 +0000 | [diff] [blame] | 962 | prog->argv = xrealloc(prog->argv, |
| 963 | sizeof(*prog->argv) * |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 964 | argv_alloced); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 965 | } |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 966 | prog->argv[argc_l] = buf; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 967 | flag ^= LASH_OPT_SAW_QUOTE; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 968 | } |
| 969 | } else |
| 970 | switch (*src) { |
| 971 | case '"': |
| 972 | case '\'': |
| 973 | quote = *src; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 974 | flag |= LASH_OPT_SAW_QUOTE; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 975 | break; |
| 976 | |
| 977 | case '#': /* comment */ |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 978 | if (*(src-1)== '$') |
| 979 | *buf++ = *src; |
| 980 | else |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 981 | flag |= LASH_OPT_DONE; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 982 | break; |
| 983 | |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 984 | #ifdef CONFIG_LASH_PIPE_N_REDIRECTS |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 985 | case '>': /* redirects */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 986 | case '<': |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 987 | i = prog->num_redirects++; |
| 988 | prog->redirects = xrealloc(prog->redirects, |
| 989 | sizeof(*prog->redirects) * |
Matt Kraai | b890752 | 2000-09-13 02:08:21 +0000 | [diff] [blame] | 990 | (i + 1)); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 991 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 992 | prog->redirects[i].fd = -1; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 993 | if (buf != prog->argv[argc_l]) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 994 | /* the stuff before this character may be the file number |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 995 | being redirected */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 996 | prog->redirects[i].fd = |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 997 | strtol(prog->argv[argc_l], &chptr, 10); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 998 | |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 999 | if (*chptr && *prog->argv[argc_l]) { |
| 1000 | buf++, argc_l++; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1001 | prog->argv[argc_l] = buf; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1005 | if (prog->redirects[i].fd == -1) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1006 | if (*src == '>') |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1007 | prog->redirects[i].fd = 1; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1008 | else |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1009 | prog->redirects[i].fd = 0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | if (*src++ == '>') { |
| 1013 | if (*src == '>') |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1014 | prog->redirects[i].type = |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1015 | REDIRECT_APPEND, src++; |
| 1016 | else |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1017 | prog->redirects[i].type = REDIRECT_OVERWRITE; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1018 | } else { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1019 | prog->redirects[i].type = REDIRECT_INPUT; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | /* This isn't POSIX sh compliant. Oh well. */ |
| 1023 | chptr = src; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1024 | chptr = skip_whitespace(chptr); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1025 | |
| 1026 | if (!*chptr) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1027 | bb_error_msg("file name expected after %c", *(src-1)); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1028 | free_job(job); |
| 1029 | job->num_progs=0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1030 | return 1; |
| 1031 | } |
| 1032 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1033 | prog->redirects[i].filename = buf; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1034 | while (*chptr && !isspace(*chptr)) |
| 1035 | *buf++ = *chptr++; |
| 1036 | |
| 1037 | src = chptr - 1; /* we src++ later */ |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1038 | prog->argv[argc_l] = ++buf; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1039 | break; |
| 1040 | |
| 1041 | case '|': /* pipe */ |
| 1042 | /* finish this command */ |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1043 | if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1044 | argc_l++; |
| 1045 | if (!argc_l) { |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1046 | goto empty_command_in_pipe; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1047 | } |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1048 | prog->argv[argc_l] = NULL; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1049 | |
| 1050 | /* and start the next */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1051 | job->num_progs++; |
Matt Kraai | b890752 | 2000-09-13 02:08:21 +0000 | [diff] [blame] | 1052 | job->progs = xrealloc(job->progs, |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1053 | sizeof(*job->progs) * job->num_progs); |
| 1054 | prog = job->progs + (job->num_progs - 1); |
| 1055 | prog->num_redirects = 0; |
| 1056 | prog->redirects = NULL; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1057 | prog->is_stopped = 0; |
| 1058 | prog->family = job; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1059 | argc_l = 0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1060 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1061 | argv_alloced = 5; |
| 1062 | prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1063 | prog->argv[0] = ++buf; |
| 1064 | |
| 1065 | src++; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1066 | src = skip_whitespace(src); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (!*src) { |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1069 | empty_command_in_pipe: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1070 | bb_error_msg("empty command in pipe"); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1071 | free_job(job); |
| 1072 | job->num_progs=0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1073 | return 1; |
| 1074 | } |
| 1075 | src--; /* we'll ++ it at the end of the loop */ |
| 1076 | |
| 1077 | break; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1078 | #endif |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1079 | |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 1080 | #ifdef CONFIG_LASH_JOB_CONTROL |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1081 | case '&': /* background */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1082 | *inbg = 1; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1083 | /* fallthrough */ |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 1084 | #endif |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1085 | case ';': /* multiple commands */ |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1086 | flag |= LASH_OPT_DONE; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1087 | return_command = *command_ptr + (src - *command_ptr) + 1; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1088 | break; |
| 1089 | |
Matt Kraai | 131241f | 2000-09-14 00:43:20 +0000 | [diff] [blame] | 1090 | case '\\': |
| 1091 | src++; |
| 1092 | if (!*src) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1093 | bb_error_msg("character expected after \\"); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1094 | free_job(job); |
Matt Kraai | 131241f | 2000-09-14 00:43:20 +0000 | [diff] [blame] | 1095 | return 1; |
| 1096 | } |
| 1097 | if (*src == '*' || *src == '[' || *src == ']' |
| 1098 | || *src == '?') *buf++ = '\\'; |
| 1099 | /* fallthrough */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1100 | default: |
| 1101 | *buf++ = *src; |
| 1102 | } |
| 1103 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1104 | src++; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1105 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1106 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1107 | if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) { |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1108 | argc_l++; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1109 | } |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1110 | if (!argc_l) { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1111 | free_job(job); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1112 | return 0; |
| 1113 | } |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1114 | prog->argv[argc_l] = NULL; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1115 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1116 | if (!return_command) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1117 | job->text = xstrdup(*command_ptr); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1118 | } else { |
| 1119 | /* This leaves any trailing spaces, which is a bit sloppy */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1120 | job->text = xstrndup(*command_ptr, return_command - *command_ptr); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1121 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1122 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1123 | *command_ptr = return_command; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1124 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1125 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1128 | /* Run the child_prog, no matter what kind of command it uses. |
| 1129 | */ |
| 1130 | static int pseudo_exec(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1131 | { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1132 | struct built_in_command *x; |
Rob Landley | 6828677 | 2006-06-13 19:01:30 +0000 | [diff] [blame] | 1133 | |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 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. |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1138 | */ |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1139 | for (x = bltins; x->cmd; x++) { |
| 1140 | if (strcmp(child->argv[0], x->cmd) == 0 ) { |
Eric Andersen | 6f2ebca | 2002-09-26 13:59:40 +0000 | [diff] [blame] | 1141 | _exit(x->function(child)); |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | /* Check if the command matches any of the forking builtins. */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1146 | for (x = bltins_forking; x->cmd; x++) { |
| 1147 | if (strcmp(child->argv[0], x->cmd) == 0) { |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 1148 | applet_name=x->cmd; |
Eric Andersen | 6f2ebca | 2002-09-26 13:59:40 +0000 | [diff] [blame] | 1149 | _exit (x->function(child)); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1150 | } |
| 1151 | } |
Rob Landley | 6828677 | 2006-06-13 19:01:30 +0000 | [diff] [blame] | 1152 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1153 | /* Check if the command matches any busybox internal |
| 1154 | * commands ("applets") here. Following discussions from |
Eric Andersen | 2423b12 | 2001-12-08 01:56:15 +0000 | [diff] [blame] | 1155 | * November 2000 on busybox@busybox.net, don't use |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1156 | * bb_get_last_path_component(). This way explicit (with |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 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. |
| 1163 | */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1164 | |
Rob Landley | 6828677 | 2006-06-13 19:01:30 +0000 | [diff] [blame] | 1165 | if (ENABLE_FEATURE_SH_STANDALONE_SHELL) { |
| 1166 | char **argv_l = child->argv; |
Mike Frysinger | dcc40b7 | 2005-03-04 01:33:17 +0000 | [diff] [blame] | 1167 | int argc_l; |
Bernhard Reutner-Fischer | 6255876 | 2006-06-03 10:28:25 +0000 | [diff] [blame] | 1168 | |
Rob Landley | 6828677 | 2006-06-13 19:01:30 +0000 | [diff] [blame] | 1169 | for(argc_l=0; *argv_l; argv_l++, argc_l++); |
Mike Frysinger | dcc40b7 | 2005-03-04 01:33:17 +0000 | [diff] [blame] | 1170 | optind = 1; |
Rob Landley | 6828677 | 2006-06-13 19:01:30 +0000 | [diff] [blame] | 1171 | run_applet_by_name(child->argv[0], argc_l, child->argv); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1172 | } |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1173 | |
| 1174 | execvp(child->argv[0], child->argv); |
Eric Andersen | 6f2ebca | 2002-09-26 13:59:40 +0000 | [diff] [blame] | 1175 | |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1176 | /* Do not use bb_perror_msg_and_die() here, since we must not |
Eric Andersen | 6f2ebca | 2002-09-26 13:59:40 +0000 | [diff] [blame] | 1177 | * call exit() but should call _exit() instead */ |
Rob Landley | 5483de1 | 2006-06-20 21:35:26 +0000 | [diff] [blame] | 1178 | bb_perror_msg("%s", child->argv[0]); |
Eric Andersen | 6f2ebca | 2002-09-26 13:59:40 +0000 | [diff] [blame] | 1179 | _exit(EXIT_FAILURE); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | static void insert_job(struct job *newjob, int inbg) |
| 1183 | { |
| 1184 | struct job *thejob; |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1185 | struct jobset *j_list=newjob->job_list; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1186 | |
| 1187 | /* find the ID for thejob to use */ |
| 1188 | newjob->jobid = 1; |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1189 | for (thejob = j_list->head; thejob; thejob = thejob->next) |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1190 | if (thejob->jobid >= newjob->jobid) |
| 1191 | newjob->jobid = thejob->jobid + 1; |
| 1192 | |
| 1193 | /* add thejob to the list of running jobs */ |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1194 | if (!j_list->head) { |
| 1195 | thejob = j_list->head = xmalloc(sizeof(*thejob)); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1196 | } else { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1197 | for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1198 | thejob->next = xmalloc(sizeof(*thejob)); |
| 1199 | thejob = thejob->next; |
| 1200 | } |
| 1201 | |
| 1202 | *thejob = *newjob; /* physically copy the struct job */ |
| 1203 | thejob->next = NULL; |
| 1204 | thejob->running_progs = thejob->num_progs; |
| 1205 | thejob->stopped_progs = 0; |
| 1206 | |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 1207 | #ifdef CONFIG_LASH_JOB_CONTROL |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1208 | if (inbg) { |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1209 | /* we don't wait for background thejobs to return -- append it |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 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); |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 1213 | last_jobid = newjob->jobid; |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 1214 | last_bg_pid=newjob->progs[newjob->num_progs - 1].pid; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1215 | } else { |
| 1216 | newjob->job_list->fg = thejob; |
| 1217 | |
| 1218 | /* move the new process group into the foreground */ |
Rob Landley | f946379 | 2006-06-13 18:50:20 +0000 | [diff] [blame] | 1219 | /* Ignore errors since child could have already exited */ |
| 1220 | tcsetpgrp(shell_terminal, newjob->pgrp); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1221 | } |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 1222 | #endif |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | static int run_command(struct job *newjob, int inbg, int outpipe[2]) |
| 1226 | { |
| 1227 | /* struct job *thejob; */ |
| 1228 | int i; |
| 1229 | int nextin, nextout; |
| 1230 | int pipefds[2]; /* pipefd[0] is for reading */ |
| 1231 | struct built_in_command *x; |
| 1232 | struct child_prog *child; |
| 1233 | |
Eric Andersen | 6efc48c | 2000-07-18 08:16:39 +0000 | [diff] [blame] | 1234 | nextin = 0, nextout = 1; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1235 | for (i = 0; i < newjob->num_progs; i++) { |
| 1236 | child = & (newjob->progs[i]); |
| 1237 | |
| 1238 | if ((i + 1) < newjob->num_progs) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1239 | if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe"); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1240 | nextout = pipefds[1]; |
| 1241 | } else { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1242 | if (outpipe[1]!=-1) { |
| 1243 | nextout = outpipe[1]; |
Eric Andersen | 6efc48c | 2000-07-18 08:16:39 +0000 | [diff] [blame] | 1244 | } else { |
| 1245 | nextout = 1; |
| 1246 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1249 | |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 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. |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1255 | */ |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1256 | if (newjob->num_progs == 1) { |
Rob Landley | 7a8e491 | 2006-07-01 17:19:02 +0000 | [diff] [blame] | 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); |
| 1261 | } |
| 1262 | |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1263 | for (x = bltins; x->cmd; x++) { |
| 1264 | if (strcmp(child->argv[0], x->cmd) == 0 ) { |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1265 | int rcode; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1266 | int squirrel[] = {-1, -1, -1}; |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1267 | setup_redirects(child, squirrel); |
| 1268 | rcode = x->function(child); |
| 1269 | restore_redirects(squirrel); |
| 1270 | return rcode; |
| 1271 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 1272 | } |
| 1273 | } |
| 1274 | |
Eric Andersen | e3efc92 | 2004-04-12 17:59:24 +0000 | [diff] [blame] | 1275 | #if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__) |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1276 | if (!(child->pid = fork())) |
Eric Andersen | 72f9a42 | 2001-10-28 05:12:20 +0000 | [diff] [blame] | 1277 | #else |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1278 | if (!(child->pid = vfork())) |
Eric Andersen | 72f9a42 | 2001-10-28 05:12:20 +0000 | [diff] [blame] | 1279 | #endif |
| 1280 | { |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 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); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1286 | signal(SIGTTOU, SIG_DFL); |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1287 | signal(SIGCHLD, SIG_DFL); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1288 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1289 | /* Close all open filehandles. */ |
Rob Landley | 7a260f0 | 2006-06-19 03:20:03 +0000 | [diff] [blame] | 1290 | while(close_me_list) close((long)llist_pop(&close_me_list)); |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 1291 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1292 | if (outpipe[1]!=-1) { |
| 1293 | close(outpipe[0]); |
Eric Andersen | 6efc48c | 2000-07-18 08:16:39 +0000 | [diff] [blame] | 1294 | } |
| 1295 | if (nextin != 0) { |
| 1296 | dup2(nextin, 0); |
| 1297 | close(nextin); |
| 1298 | } |
| 1299 | |
| 1300 | if (nextout != 1) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1301 | dup2(nextout, 1); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1302 | dup2(nextout, 2); /* Really? */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1303 | close(nextout); |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1304 | close(pipefds[0]); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1307 | /* explicit redirects override pipes */ |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1308 | setup_redirects(child,NULL); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1309 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1310 | pseudo_exec(child); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1311 | } |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1312 | if (outpipe[1]!=-1) { |
| 1313 | close(outpipe[1]); |
Eric Andersen | a1d187a | 2000-07-17 19:14:41 +0000 | [diff] [blame] | 1314 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1315 | |
| 1316 | /* put our child in the process group whose leader is the |
| 1317 | first process in this pipe */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1318 | setpgid(child->pid, newjob->progs[0].pid); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1319 | if (nextin != 0) |
| 1320 | close(nextin); |
Eric Andersen | 6efc48c | 2000-07-18 08:16:39 +0000 | [diff] [blame] | 1321 | if (nextout != 1) |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1322 | close(nextout); |
| 1323 | |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1324 | /* If there isn't another process, nextin is garbage |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1325 | but it doesn't matter */ |
| 1326 | nextin = pipefds[0]; |
| 1327 | } |
| 1328 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1329 | newjob->pgrp = newjob->progs[0].pid; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1330 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1331 | insert_job(newjob, inbg); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1332 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1333 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1336 | static int busy_loop(FILE * input) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1337 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1338 | char *command; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1339 | char *next_command = NULL; |
| 1340 | struct job newjob; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1341 | int i; |
Eric Andersen | a68ea1c | 2006-01-30 22:48:39 +0000 | [diff] [blame] | 1342 | int inbg = 0; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1343 | int status; |
Glenn L McGrath | c45146c | 2004-03-05 12:55:30 +0000 | [diff] [blame] | 1344 | #ifdef CONFIG_LASH_JOB_CONTROL |
Eric Andersen | d20d375 | 2004-03-12 22:08:42 +0000 | [diff] [blame] | 1345 | pid_t parent_pgrp; |
| 1346 | /* save current owner of TTY so we can restore it on exit */ |
| 1347 | parent_pgrp = tcgetpgrp(shell_terminal); |
Glenn L McGrath | c45146c | 2004-03-05 12:55:30 +0000 | [diff] [blame] | 1348 | #endif |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1349 | newjob.job_list = &job_list; |
| 1350 | newjob.job_context = DEFAULT_CONTEXT; |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1351 | |
Rob Landley | f946379 | 2006-06-13 18:50:20 +0000 | [diff] [blame] | 1352 | command = xzalloc(BUFSIZ); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1353 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1354 | while (1) { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1355 | if (!job_list.fg) { |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1356 | /* no job is in the foreground */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1357 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1358 | /* see if any background processes have exited */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1359 | checkjobs(&job_list); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1360 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1361 | if (!next_command) { |
| 1362 | if (get_command(input, command)) |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1363 | break; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1364 | next_command = command; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1365 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1366 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 1367 | if (! expand_arguments(next_command)) { |
Eric Andersen | ca60459 | 2001-03-08 17:17:13 +0000 | [diff] [blame] | 1368 | free(command); |
Rob Landley | f946379 | 2006-06-13 18:50:20 +0000 | [diff] [blame] | 1369 | command = xzalloc(BUFSIZ); |
Eric Andersen | ca60459 | 2001-03-08 17:17:13 +0000 | [diff] [blame] | 1370 | next_command = NULL; |
| 1371 | continue; |
| 1372 | } |
| 1373 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1374 | if (!parse_command(&next_command, &newjob, &inbg) && |
| 1375 | newjob.num_progs) { |
Eric Andersen | a1d187a | 2000-07-17 19:14:41 +0000 | [diff] [blame] | 1376 | int pipefds[2] = {-1,-1}; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1377 | debug_printf( "job=%p fed to run_command by busy_loop()'\n", |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1378 | &newjob); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1379 | run_command(&newjob, inbg, pipefds); |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 1380 | } |
| 1381 | else { |
Eric Andersen | a1d187a | 2000-07-17 19:14:41 +0000 | [diff] [blame] | 1382 | free(command); |
Rob Landley | f946379 | 2006-06-13 18:50:20 +0000 | [diff] [blame] | 1383 | command = (char *) xzalloc(BUFSIZ); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1384 | next_command = NULL; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1385 | } |
| 1386 | } else { |
| 1387 | /* a job is running in the foreground; wait for it */ |
| 1388 | i = 0; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1389 | while (!job_list.fg->progs[i].pid || |
| 1390 | job_list.fg->progs[i].is_stopped == 1) i++; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1391 | |
Eric Andersen | 7373e48 | 2002-07-31 04:04:47 +0000 | [diff] [blame] | 1392 | if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) { |
| 1393 | if (errno != ECHILD) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1394 | bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid); |
Eric Andersen | 7373e48 | 2002-07-31 04:04:47 +0000 | [diff] [blame] | 1395 | } |
| 1396 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1397 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1398 | if (WIFEXITED(status) || WIFSIGNALED(status)) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1399 | /* the child exited */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1400 | job_list.fg->running_progs--; |
| 1401 | job_list.fg->progs[i].pid = 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1402 | |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 1403 | last_return_code=WEXITSTATUS(status); |
| 1404 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1405 | if (!job_list.fg->running_progs) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1406 | /* child exited */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1407 | remove_job(&job_list, job_list.fg); |
| 1408 | job_list.fg = NULL; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1409 | } |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 1410 | } |
| 1411 | #ifdef CONFIG_LASH_JOB_CONTROL |
| 1412 | else { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1413 | /* the child was stopped */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1414 | job_list.fg->stopped_progs++; |
| 1415 | job_list.fg->progs[i].is_stopped = 1; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1416 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 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); |
| 1420 | job_list.fg = NULL; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1421 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1424 | if (!job_list.fg) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1425 | /* move the shell to the foreground */ |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1426 | /* suppress messages when run from /linuxrc mag@sysgo.de */ |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1427 | if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY) |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1428 | bb_perror_msg("tcsetpgrp"); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1429 | } |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 1430 | #endif |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1431 | } |
| 1432 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1433 | free(command); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1434 | |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 1435 | #ifdef CONFIG_LASH_JOB_CONTROL |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1436 | /* return controlling TTY back to parent process group before exiting */ |
Eric Andersen | 7373e48 | 2002-07-31 04:04:47 +0000 | [diff] [blame] | 1437 | if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1438 | bb_perror_msg("tcsetpgrp"); |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 1439 | #endif |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 1440 | |
| 1441 | /* return exit status if called with "-c" */ |
| 1442 | if (input == NULL && WIFEXITED(status)) |
| 1443 | return WEXITSTATUS(status); |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1444 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1445 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1448 | #ifdef CONFIG_FEATURE_CLEAN_UP |
"Vladimir N. Oleynik" | 264e771 | 2005-09-22 14:35:29 +0000 | [diff] [blame] | 1449 | static void free_memory(void) |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 1450 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1451 | if (cwd && cwd!=bb_msg_unknown) { |
Eric Andersen | 5d60a46 | 2001-08-22 05:32:24 +0000 | [diff] [blame] | 1452 | free((char*)cwd); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 1453 | } |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 1454 | if (local_pending_command) |
| 1455 | free(local_pending_command); |
| 1456 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1457 | if (job_list.fg && !job_list.fg->running_progs) { |
| 1458 | remove_job(&job_list, job_list.fg); |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 1459 | } |
| 1460 | } |
Rob Landley | acf448d | 2006-07-09 17:03:07 +0000 | [diff] [blame] | 1461 | #else |
| 1462 | void free_memory(void); |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 1463 | #endif |
| 1464 | |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 1465 | #ifdef CONFIG_LASH_JOB_CONTROL |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 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 */ |
Eric Andersen | ece8fc2 | 2002-12-11 04:26:28 +0000 | [diff] [blame] | 1469 | static void setup_job_control(void) |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1470 | { |
Eric Andersen | f0a4ac8 | 2001-10-03 11:23:42 +0000 | [diff] [blame] | 1471 | int status; |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 1472 | pid_t shell_pgrp; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1473 | |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1474 | /* Loop until we are in the foreground. */ |
Eric Andersen | f0a4ac8 | 2001-10-03 11:23:42 +0000 | [diff] [blame] | 1475 | while ((status = tcgetpgrp (shell_terminal)) >= 0) { |
| 1476 | if (status == (shell_pgrp = getpgrp ())) { |
| 1477 | break; |
| 1478 | } |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1479 | kill (- shell_pgrp, SIGTTIN); |
Eric Andersen | f0a4ac8 | 2001-10-03 11:23:42 +0000 | [diff] [blame] | 1480 | } |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1481 | |
| 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); |
| 1489 | |
| 1490 | /* Put ourselves in our own process group. */ |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 1491 | setsid(); |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1492 | shell_pgrp = getpid (); |
Rob Landley | acf448d | 2006-07-09 17:03:07 +0000 | [diff] [blame] | 1493 | setpgid(shell_pgrp, shell_pgrp); |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1494 | |
| 1495 | /* Grab control of the terminal. */ |
| 1496 | tcsetpgrp(shell_terminal, shell_pgrp); |
| 1497 | } |
Eric Andersen | 37032b4 | 2004-02-10 01:28:36 +0000 | [diff] [blame] | 1498 | #else |
| 1499 | static inline void setup_job_control(void) |
| 1500 | { |
| 1501 | } |
| 1502 | #endif |
Eric Andersen | 6efc48c | 2000-07-18 08:16:39 +0000 | [diff] [blame] | 1503 | |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 1504 | int lash_main(int argc_l, char **argv_l) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1505 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 1506 | unsigned opt; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1507 | FILE *input = stdin; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1508 | argc = argc_l; |
| 1509 | argv = argv_l; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1510 | |
Eric Andersen | 702ec59 | 2001-03-06 22:17:29 +0000 | [diff] [blame] | 1511 | /* These variables need re-initializing when recursing */ |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 1512 | last_jobid = 0; |
Rob Landley | f946379 | 2006-06-13 18:50:20 +0000 | [diff] [blame] | 1513 | close_me_list = NULL; |
Eric Andersen | b0970d4 | 2001-01-05 19:34:52 +0000 | [diff] [blame] | 1514 | job_list.head = NULL; |
| 1515 | job_list.fg = NULL; |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 1516 | last_return_code=1; |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1517 | |
Eric Andersen | b558e76 | 2000-11-30 22:43:16 +0000 | [diff] [blame] | 1518 | if (argv[0] && argv[0][0] == '-') { |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 1519 | FILE *prof_input; |
| 1520 | prof_input = fopen("/etc/profile", "r"); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 1521 | if (prof_input) { |
Rob Landley | 7a260f0 | 2006-06-19 03:20:03 +0000 | [diff] [blame] | 1522 | llist_add_to(&close_me_list, (void *)(long)fileno(prof_input)); |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 1523 | /* Now run the file */ |
| 1524 | busy_loop(prof_input); |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame^] | 1525 | fclose_if_not_stdin(prof_input); |
Rob Landley | f946379 | 2006-06-13 18:50:20 +0000 | [diff] [blame] | 1526 | llist_pop(&close_me_list); |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 1527 | } |
Eric Andersen | b558e76 | 2000-11-30 22:43:16 +0000 | [diff] [blame] | 1528 | } |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1529 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 1530 | opt = getopt32(argc_l, argv_l, "+ic:", &local_pending_command); |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1531 | #define LASH_OPT_i (1<<0) |
| 1532 | #define LASH_OPT_c (1<<2) |
| 1533 | if (opt & LASH_OPT_c) { |
| 1534 | input = NULL; |
| 1535 | optind++; |
| 1536 | argv += optind; |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1537 | } |
Eric Andersen | 6a4c33c | 2000-07-28 17:08:36 +0000 | [diff] [blame] | 1538 | /* A shell is interactive if the `-i' flag was given, or if all of |
| 1539 | * the following conditions are met: |
| 1540 | * no -c command |
| 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. */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1545 | if (argv[optind]==NULL && input==stdin && |
Rob Landley | acf448d | 2006-07-09 17:03:07 +0000 | [diff] [blame] | 1546 | isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) |
| 1547 | { |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1548 | opt |= LASH_OPT_i; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1549 | } |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1550 | setup_job_control(); |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1551 | if (opt & LASH_OPT_i) { |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1552 | /* Looks like they want an interactive shell */ |
Rob Landley | acf448d | 2006-07-09 17:03:07 +0000 | [diff] [blame] | 1553 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET) { |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 1554 | printf("\n\n%s Built-in shell (lash)\n" |
| 1555 | "Enter 'help' for a list of built-in commands.\n\n", |
| 1556 | BB_BANNER); |
Rob Landley | acf448d | 2006-07-09 17:03:07 +0000 | [diff] [blame] | 1557 | } |
| 1558 | } else if (!local_pending_command && argv[optind]) { |
Eric Andersen | 6f65a3a | 2001-01-20 01:10:07 +0000 | [diff] [blame] | 1559 | //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1560 | input = xfopen(argv[optind], "r"); |
Rob Landley | f946379 | 2006-06-13 18:50:20 +0000 | [diff] [blame] | 1561 | /* be lazy, never mark this closed */ |
Rob Landley | 7a260f0 | 2006-06-19 03:20:03 +0000 | [diff] [blame] | 1562 | llist_add_to(&close_me_list, (void *)(long)fileno(input)); |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Eric Andersen | 6efc48c | 2000-07-18 08:16:39 +0000 | [diff] [blame] | 1565 | /* initialize the cwd -- this is never freed...*/ |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 1566 | cwd = xgetcwd(0); |
Eric Andersen | 5f265b7 | 2001-05-11 16:58:46 +0000 | [diff] [blame] | 1567 | if (!cwd) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1568 | cwd = bb_msg_unknown; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1569 | |
Rob Landley | acf448d | 2006-07-09 17:03:07 +0000 | [diff] [blame] | 1570 | if (ENABLE_FEATURE_CLEAN_UP) atexit(free_memory); |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 1571 | |
Rob Landley | acf448d | 2006-07-09 17:03:07 +0000 | [diff] [blame] | 1572 | if (ENABLE_FEATURE_COMMAND_EDITING) cmdedit_set_initial_prompt(); |
| 1573 | else PS1 = NULL; |
Eric Andersen | ff9ad47 | 2004-02-10 01:07:45 +0000 | [diff] [blame] | 1574 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1575 | return (busy_loop(input)); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1576 | } |