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