blob: c74684bf70245edff6c50bc9ca30c315fff06c8f [file] [log] [blame]
Erik Andersen3522eb12000-03-12 23:49:18 +00001/* vi: set sw=4 ts=4: */
2/*
Erik Andersen6acaa402000-03-26 14:03:20 +00003 * lash -- the BusyBox Lame-Ass SHell
Erik Andersen3522eb12000-03-12 23:49:18 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Erik Andersen3522eb12000-03-12 23:49:18 +00006 *
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-Fischer86f5c992006-01-22 22:55:11 +000011 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Erik Andersen3522eb12000-03-12 23:49:18 +000012 */
13
Eric Andersenbdfd0d72001-10-24 05:00:29 +000014/* 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 Andersen8ea28be2001-01-05 20:58:22 +000018 */
Eric Andersen8a646dd2001-06-21 16:38:11 +000019
Eric Andersen1e7cea92000-12-06 23:47:38 +000020//For debugging/development on the shell only...
Eric Andersen501c88b2000-07-28 15:14:45 +000021//#define DEBUG_SHELL
Eric Andersena1d187a2000-07-17 19:14:41 +000022
23
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000024#include "busybox.h"
Eric Andersen501c88b2000-07-28 15:14:45 +000025#include <getopt.h>
Eric Andersen13d1fa12001-03-08 23:59:45 +000026#include <glob.h>
27#define expand_t glob_t
Erik Andersen3522eb12000-03-12 23:49:18 +000028
Eric Andersenff9ad472004-02-10 01:07:45 +000029/* Always enable for the moment... */
Eric Andersen95b26252004-02-10 01:30:21 +000030#define CONFIG_LASH_PIPE_N_REDIRECTS
31#define CONFIG_LASH_JOB_CONTROL
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +000032#define ENABLE_LASH_PIPE_N_REDIRECTS 1
33#define ENABLE_LASH_JOB_CONTROL 1
Eric Andersen13d1fa12001-03-08 23:59:45 +000034
Denis Vlasenko831a20f2007-04-12 12:27:32 +000035
Denis Vlasenko703e2022007-01-22 14:12:08 +000036enum { MAX_READ = 128 }; /* size of input buffer for 'read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000037#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
38
Erik Andersend75af992000-03-16 08:09:09 +000039
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +000040#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000041enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000042 REDIRECT_APPEND
43};
Eric Andersenff9ad472004-02-10 01:07:45 +000044#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000045
Rob Landleybc68cd12006-03-10 19:22:06 +000046enum {
47 DEFAULT_CONTEXT = 0x1,
48 IF_TRUE_CONTEXT = 0x2,
49 IF_FALSE_CONTEXT = 0x4,
50 THEN_EXP_CONTEXT = 0x8,
51 ELSE_EXP_CONTEXT = 0x10
52};
Eric Andersenfad9c112000-07-25 18:06:52 +000053
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +000054#define LASH_OPT_DONE (1)
55#define LASH_OPT_SAW_QUOTE (2)
56
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +000057#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000058struct redir_struct {
59 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000060 int fd; /* file descriptor being redirected */
61 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000062};
Eric Andersenff9ad472004-02-10 01:07:45 +000063#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000064
Eric Andersen86349772000-12-18 20:25:50 +000065struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000066 pid_t pid; /* 0 if exited */
67 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000068 int num_redirects; /* elements in redirection array */
Eric Andersen86349772000-12-18 20:25:50 +000069 int is_stopped; /* is the program currently running? */
70 struct job *family; /* pointer back to the child's parent job */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +000071#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersenff9ad472004-02-10 01:07:45 +000072 struct redir_struct *redirects; /* I/O redirects */
73#endif
74};
75
76struct jobset {
77 struct job *head; /* head of list of running jobs */
78 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000079};
80
81struct job {
Eric Andersen86349772000-12-18 20:25:50 +000082 int jobid; /* job number */
83 int num_progs; /* total number of programs in job */
84 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000085 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +000086 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +000087 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +000088 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +000089 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +000090 int stopped_progs; /* number of programs alive, but stopped */
91 unsigned int job_context; /* bitmask defining current context */
92 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +000093};
94
Eric Andersen86349772000-12-18 20:25:50 +000095struct built_in_command {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +000096 const char *cmd; /* name */
97 const char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +000098 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +000099};
100
Eric Andersen34e19412000-07-10 18:47:24 +0000101/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000102static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000103static int builtin_exec(struct child_prog *cmd);
104static int builtin_exit(struct child_prog *cmd);
105static int builtin_fg_bg(struct child_prog *cmd);
106static int builtin_help(struct child_prog *cmd);
107static int builtin_jobs(struct child_prog *dummy);
108static int builtin_pwd(struct child_prog *dummy);
109static int builtin_export(struct child_prog *cmd);
110static int builtin_source(struct child_prog *cmd);
111static int builtin_unset(struct child_prog *cmd);
112static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000113
Eric Andersen34e19412000-07-10 18:47:24 +0000114
115/* function prototypes for shell stuff */
Eric Andersen86349772000-12-18 20:25:50 +0000116static void checkjobs(struct jobset *job_list);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000117static void remove_job(struct jobset *j_list, struct job *job);
Denis Vlasenkof443bff2007-03-31 11:08:50 +0000118static int get_command_bufsiz(FILE * source, char *command);
Eric Andersen86349772000-12-18 20:25:50 +0000119static int parse_command(char **command_ptr, struct job *job, int *inbg);
120static int run_command(struct job *newjob, int inbg, int outpipe[2]);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000121static int pseudo_exec(struct child_prog *cmd) ATTRIBUTE_NORETURN;
Erik Andersen3522eb12000-03-12 23:49:18 +0000122static int busy_loop(FILE * input);
123
Erik Andersend75af992000-03-16 08:09:09 +0000124
Mark Whitley37653aa2000-07-12 23:36:17 +0000125/* Table of built-in functions (these are non-forking builtins, meaning they
126 * can change global variables in the parent shell process but they will not
127 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000128static const struct built_in_command bltins[] = {
Denis Vlasenkob69f35e2007-04-12 17:10:45 +0000129 {"bg" , "Resume a job in the background", builtin_fg_bg},
130 {"cd" , "Change working directory", builtin_cd},
131 {"exec" , "Exec command, replacing this shell with the exec'd process", builtin_exec},
132 {"exit" , "Exit from shell()", builtin_exit},
133 {"fg" , "Bring job into the foreground", builtin_fg_bg},
134 {"jobs" , "Lists the active jobs", builtin_jobs},
Eric Andersenfad9c112000-07-25 18:06:52 +0000135 {"export", "Set environment variable", builtin_export},
Denis Vlasenkob69f35e2007-04-12 17:10:45 +0000136 {"unset" , "Unset environment variable", builtin_unset},
137 {"read" , "Input environment variable", builtin_read},
138 {"." , "Source-in and run commands in a file", builtin_source},
139 /* These were "forked applets", but distinction was nuked */
140 /* Original comment retained: */
141 /* Table of forking built-in functions (things that fork cannot change global
142 * variables in the parent process, such as the current working directory) */
143 {"pwd" , "Print current directory", builtin_pwd},
144 {"help" , "List shell built-in commands", builtin_help},
Eric Andersen86349772000-12-18 20:25:50 +0000145 /* to do: add ulimit */
Erik Andersen330fd2b2000-05-19 05:35:19 +0000146};
147
Denis Vlasenkob69f35e2007-04-12 17:10:45 +0000148#define VEC_SIZE(v) (sizeof(v)/sizeof(v[0]))
149#define VEC_LAST(v) v[VEC_SIZE(v)-1]
Erik Andersen3522eb12000-03-12 23:49:18 +0000150
Eric Andersen0bcc8132001-01-05 19:37:32 +0000151
Eric Andersen7467c8d2001-07-12 20:26:32 +0000152static int shell_context; /* Type prompt trigger (PS1 or PS2) */
Eric Andersen0bcc8132001-01-05 19:37:32 +0000153
154
155/* Globals that are static to this file */
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000156static char *cwd;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000157static char *local_pending_command;
Eric Andersen86349772000-12-18 20:25:50 +0000158static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000159static int argc;
160static char **argv;
Rob Landleyf9463792006-06-13 18:50:20 +0000161static llist_t *close_me_list;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000162static int last_return_code;
163static int last_bg_pid;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000164static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000165static int shell_terminal;
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000166static const char *PS1;
167static const char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000168
169
Eric Andersenb558e762000-11-30 22:43:16 +0000170#ifdef DEBUG_SHELL
171static inline void debug_printf(const char *format, ...)
172{
173 va_list args;
174 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000175 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000176 va_end(args);
177}
178#else
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000179static inline void debug_printf(const char ATTRIBUTE_UNUSED *format, ...) { }
Eric Andersenb558e762000-11-30 22:43:16 +0000180#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000181
Eric Andersen86349772000-12-18 20:25:50 +0000182/*
183 Most builtins need access to the struct child_prog that has
184 their arguments, previously coded as cmd->progs[0]. That coding
185 can exhibit a bug, if the builtin is not the first command in
186 a pipeline: "echo foo | exec sort" will attempt to exec foo.
187
188builtin previous use notes
189------ ----------------- ---------
190cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000191exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
192exit cmd->progs[0]
193fg_bg cmd->progs[0], job_list->head, job_list->fg
194help 0
195jobs job_list->head
196pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000197export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000198source cmd->progs[0]
199unset cmd->progs[0]
200read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000201
202I added "struct job *family;" to struct child_prog,
203and switched API to builtin_foo(struct child_prog *child);
204So cmd->text becomes child->family->text
205 cmd->job_context becomes child->family->job_context
206 cmd->progs[0] becomes *child
207 job_list becomes child->family->job_list
208 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000209
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000210
211static void update_cwd(void)
212{
213 cwd = xrealloc_getcwd_or_warn(cwd);
214 if (!cwd)
215 cwd = xstrdup(bb_msg_unknown);
216}
217
Erik Andersend75af992000-03-16 08:09:09 +0000218/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000219static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000220{
Erik Andersen161220c2000-03-16 08:12:48 +0000221 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000222
Eric Andersen86349772000-12-18 20:25:50 +0000223 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000224 newdir = getenv("HOME");
225 else
Eric Andersen86349772000-12-18 20:25:50 +0000226 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000227 if (chdir(newdir)) {
Rob Landley5483de12006-06-20 21:35:26 +0000228 bb_perror_msg("cd: %s", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000229 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000230 }
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000231 update_cwd();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000232 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000233}
234
Eric Andersend2f56772000-09-21 02:48:07 +0000235/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000236static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000237{
Eric Andersen86349772000-12-18 20:25:50 +0000238 if (child->argv[1] == NULL)
239 return EXIT_SUCCESS; /* Really? */
240 child->argv++;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000241 while (close_me_list)
242 close((long)llist_pop(&close_me_list));
Eric Andersen86349772000-12-18 20:25:50 +0000243 pseudo_exec(child);
244 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000245}
246
Erik Andersen3522eb12000-03-12 23:49:18 +0000247/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000248static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000249{
Eric Andersen86349772000-12-18 20:25:50 +0000250 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000251 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000252
Denis Vlasenko6398cf42007-04-11 17:04:29 +0000253 exit(atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000254}
255
256/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000257static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000258{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000259 int i, jobnum;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000260 struct job *job = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000261
Eric Andersen8a646dd2001-06-21 16:38:11 +0000262 /* If they gave us no args, assume they want the last backgrounded task */
263 if (!child->argv[1]) {
264 for (job = child->family->job_list->head; job; job = job->next) {
265 if (job->jobid == last_jobid) {
266 break;
267 }
Erik Andersen161220c2000-03-16 08:12:48 +0000268 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000269 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000270 bb_error_msg("%s: no current job", child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000271 return EXIT_FAILURE;
272 }
273 } else {
274 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000275 bb_error_msg(bb_msg_invalid_arg, child->argv[1], child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000276 return EXIT_FAILURE;
277 }
278 for (job = child->family->job_list->head; job; job = job->next) {
279 if (job->jobid == jobnum) {
280 break;
281 }
282 }
283 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000284 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000285 return EXIT_FAILURE;
286 }
Erik Andersend75af992000-03-16 08:09:09 +0000287 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000288
Eric Andersen86349772000-12-18 20:25:50 +0000289 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000290 /* Put the job into the foreground. */
291 tcsetpgrp(shell_terminal, job->pgrp);
292
Eric Andersen86349772000-12-18 20:25:50 +0000293 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000294 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000295
Erik Andersen161220c2000-03-16 08:12:48 +0000296 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000297 for (i = 0; i < job->num_progs; i++)
298 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000299
Eric Andersen86349772000-12-18 20:25:50 +0000300 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000301
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000302 i = kill(- job->pgrp, SIGCONT);
303 if (i < 0) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000304 if (i == ESRCH) {
305 remove_job(&job_list, job);
Eric Andersen07abfe22001-06-27 17:29:11 +0000306 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000307 bb_perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000308 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000309 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000310
Matt Kraai3e856ce2000-12-01 02:55:13 +0000311 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000312}
313
314/* built-in 'help' handler */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000315static int builtin_help(struct child_prog ATTRIBUTE_UNUSED *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000316{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000317 const struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000318
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000319 printf("\nBuilt-in commands:\n"
Denis Vlasenko831a20f2007-04-12 12:27:32 +0000320 "-------------------\n");
Denis Vlasenkob69f35e2007-04-12 17:10:45 +0000321 for (x = bltins; x <= &VEC_LAST(bltins); x++) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000322 if (x->descr == NULL)
Eric Andersenfad9c112000-07-25 18:06:52 +0000323 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000324 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000325 }
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000326 putchar('\n');
Matt Kraai3e856ce2000-12-01 02:55:13 +0000327 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000328}
329
330/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000331static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000332{
Erik Andersen161220c2000-03-16 08:12:48 +0000333 struct job *job;
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000334 const char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000335
Eric Andersen86349772000-12-18 20:25:50 +0000336 for (job = child->family->job_list->head; job; job = job->next) {
337 if (job->running_progs == job->stopped_progs)
338 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000339 else
Eric Andersen86349772000-12-18 20:25:50 +0000340 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000341
Eric Andersen86349772000-12-18 20:25:50 +0000342 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000343 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000344 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000345}
346
347
348/* built-in 'pwd' handler */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000349static int builtin_pwd(struct child_prog ATTRIBUTE_UNUSED *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000350{
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000351 update_cwd();
Matt Kraai59df6f72001-05-16 14:21:09 +0000352 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000353 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000354}
355
Erik Andersen6273f652000-03-17 01:12:41 +0000356/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000357static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000358{
Erik Andersen161220c2000-03-16 08:12:48 +0000359 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000360 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000361
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000362 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000363 char **e;
364 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000365 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000366 }
Matt Kraai2129f972001-04-04 17:50:04 +0000367 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000368 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000369 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000370 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000371 bb_perror_msg("export");
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000372#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
373 if (strncmp(v, "PS1=", 4) == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000374 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000375#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000376
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000377#if ENABLE_LOCALE_SUPPORT
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000378 // TODO: why getenv? "" would be just as good...
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000379 if (strncmp(v, "LC_ALL=", 7) == 0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000380 setlocale(LC_ALL, getenv("LC_ALL"));
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000381 if (strncmp(v, "LC_CTYPE=", 9) == 0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000382 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000383#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000384
Denis Vlasenko097c3242006-11-27 16:59:15 +0000385 return res;
Erik Andersen3522eb12000-03-12 23:49:18 +0000386}
387
Eric Andersenb54833c2000-07-03 23:56:26 +0000388/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000389static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000390{
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000391 int res = 0, len;
Eric Andersenb54833c2000-07-03 23:56:26 +0000392 char *s;
393 char string[MAX_READ];
394
Eric Andersen86349772000-12-18 20:25:50 +0000395 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000396 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen0d2d1eb2002-06-06 13:33:01 +0000397 safe_strncpy(string, child->argv[1], MAX_READ-1);
Eric Andersenb54833c2000-07-03 23:56:26 +0000398 len = strlen(string);
399 string[len++] = '=';
400 string[len] = '\0';
401 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000402 res = strlen(string);
403 if (res > len)
404 string[--res] = '\0'; /* chomp trailing newline */
Eric Andersenb54833c2000-07-03 23:56:26 +0000405 /*
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;
Denis Vlasenko831a20f2007-04-12 12:27:32 +0000411 s = strdup(string);
412 if (s)
Eric Andersenb54833c2000-07-03 23:56:26 +0000413 res = putenv(s);
414 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000415 bb_perror_msg("read");
Denis Vlasenko831a20f2007-04-12 12:27:32 +0000416 } else
Eric Andersenb54833c2000-07-03 23:56:26 +0000417 fgets(string, sizeof(string), stdin);
418
Denis Vlasenko097c3242006-11-27 16:59:15 +0000419 return res;
Eric Andersenb54833c2000-07-03 23:56:26 +0000420}
421
Erik Andersen3522eb12000-03-12 23:49:18 +0000422/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000423static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000424{
Erik Andersen161220c2000-03-16 08:12:48 +0000425 FILE *input;
426 int status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000427
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000428 input = fopen_or_warn(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000429 if (!input) {
Matt Kraai3e856ce2000-12-01 02:55:13 +0000430 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000431 }
Erik Andersend75af992000-03-16 08:09:09 +0000432
Rob Landley7a260f02006-06-19 03:20:03 +0000433 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Erik Andersen161220c2000-03-16 08:12:48 +0000434 /* Now run the file */
435 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000436 fclose(input);
Rob Landleyf9463792006-06-13 18:50:20 +0000437 llist_pop(&close_me_list);
Denis Vlasenko097c3242006-11-27 16:59:15 +0000438 return status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000439}
440
441/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000442static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000443{
Eric Andersen86349772000-12-18 20:25:50 +0000444 if (child->argv[1] == NULL) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000445 printf(bb_msg_requires_arg, "unset");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000446 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000447 }
Eric Andersen86349772000-12-18 20:25:50 +0000448 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000449 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000450}
451
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000452#if ENABLE_LASH_JOB_CONTROL
Erik Andersen3522eb12000-03-12 23:49:18 +0000453/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000454static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000455{
Erik Andersen161220c2000-03-16 08:12:48 +0000456 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000457 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000458
Eric Andersen86349772000-12-18 20:25:50 +0000459 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000460 free(cmd->progs[i].argv);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000461#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000462 if (cmd->progs[i].redirects)
463 free(cmd->progs[i].redirects);
Eric Andersenff9ad472004-02-10 01:07:45 +0000464#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000465 }
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000466 free(cmd->progs);
467 free(cmd->text);
468 free(cmd->cmdbuf);
Mark Whitley44a99142001-03-14 17:26:37 +0000469 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000470 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000471 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000472}
473
Eric Andersen1ca20a72001-03-21 07:34:27 +0000474/* remove a job from a jobset */
475static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000476{
Eric Andersen86349772000-12-18 20:25:50 +0000477 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000478
Eric Andersen86349772000-12-18 20:25:50 +0000479 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000480 if (job == j_list->head) {
481 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000482 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000483 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000484 while (prevjob->next != job)
485 prevjob = prevjob->next;
486 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000487 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000488
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000489 if (j_list->head)
490 last_jobid = j_list->head->jobid;
491 else
492 last_jobid = 0;
493
Erik Andersen161220c2000-03-16 08:12:48 +0000494 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000495}
496
Eric Andersenff9ad472004-02-10 01:07:45 +0000497/* Checks to see if any background processes have exited -- if they
Erik Andersen3522eb12000-03-12 23:49:18 +0000498 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000499static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000500{
Erik Andersen161220c2000-03-16 08:12:48 +0000501 struct job *job;
502 pid_t childpid;
503 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000504 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000505
Erik Andersen161220c2000-03-16 08:12:48 +0000506 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000507 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000508 prognum = 0;
509 while (prognum < job->num_progs &&
510 job->progs[prognum].pid != childpid) prognum++;
511 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000512 break;
513 }
514
Eric Andersena1d187a2000-07-17 19:14:41 +0000515 /* This happens on backticked commands */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000516 if (job == NULL)
Eric Andersena1d187a2000-07-17 19:14:41 +0000517 return;
518
Erik Andersen161220c2000-03-16 08:12:48 +0000519 if (WIFEXITED(status) || WIFSIGNALED(status)) {
520 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000521 job->running_progs--;
522 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000523
Eric Andersen86349772000-12-18 20:25:50 +0000524 if (!job->running_progs) {
525 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000526 last_jobid = 0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000527 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000528 }
529 } else {
530 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000531 job->stopped_progs++;
532 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000533 }
Erik Andersend75af992000-03-16 08:09:09 +0000534 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000535
Erik Andersen161220c2000-03-16 08:12:48 +0000536 if (childpid == -1 && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000537 bb_perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000538}
Eric Andersen37032b42004-02-10 01:28:36 +0000539#else
540static void checkjobs(struct jobset *j_list)
541{
542}
543static void free_job(struct job *cmd)
544{
545}
546static void remove_job(struct jobset *j_list, struct job *job)
547{
548}
549#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000550
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000551#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersene9f07fb2000-12-21 18:31:36 +0000552/* squirrel != NULL means we squirrel away copies of stdin, stdout,
553 * and stderr if they are redirected. */
554static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000555{
556 int i;
557 int openfd;
558 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000559 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000560
Eric Andersen86349772000-12-18 20:25:50 +0000561 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000562 switch (redir->type) {
563 case REDIRECT_INPUT:
564 mode = O_RDONLY;
565 break;
566 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000567 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000568 break;
569 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000570 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000571 break;
572 }
573
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000574 openfd = open3_or_warn(redir->filename, mode, 0666);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000575 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!) */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000578 return 1;
579 }
580
581 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000582 if (squirrel && redir->fd < 3) {
583 squirrel[redir->fd] = dup(redir->fd);
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000584 fcntl(squirrel[redir->fd], F_SETFD, FD_CLOEXEC);
Eric Andersene9f07fb2000-12-21 18:31:36 +0000585 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000586 dup2(openfd, redir->fd);
587 close(openfd);
588 }
589 }
590
591 return 0;
592}
593
Eric Andersene9f07fb2000-12-21 18:31:36 +0000594static void restore_redirects(int squirrel[])
595{
596 int i, fd;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000597 for (i = 0; i < 3; i++) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000598 fd = squirrel[i];
599 if (fd != -1) {
600 /* No error checking. I sure wouldn't know what
601 * to do with an error if I found one! */
602 dup2(fd, i);
603 close(fd);
604 }
605 }
606}
Eric Andersenff9ad472004-02-10 01:07:45 +0000607#else
608static inline int setup_redirects(struct child_prog *prog, int squirrel[])
609{
610 return 0;
611}
612static inline void restore_redirects(int squirrel[])
613{
614}
615#endif
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000616
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000617static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000618{
Denis Vlasenko38f63192007-01-22 09:03:07 +0000619#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000620 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000621#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000622 PS1 = getenv("PS1");
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000623 if (PS1 == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000624 PS1 = "\\w \\$ ";
Eric Andersenff9ad472004-02-10 01:07:45 +0000625#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000626}
627
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000628static inline const char* setup_prompt_string(void)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000629{
Denis Vlasenko38f63192007-01-22 09:03:07 +0000630#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000631 /* Set up the prompt */
632 if (shell_context == 0) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000633 char *ns;
634 free((char*)PS1);
635 ns = xmalloc(strlen(cwd)+4);
636 sprintf(ns, "%s %c ", cwd, (geteuid() != 0) ? '$': '#');
637 PS1 = ns;
638 return ns;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000639 } else {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000640 return PS2;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000641 }
642#else
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000643 return (shell_context == 0)? PS1 : PS2;
Eric Andersenff9ad472004-02-10 01:07:45 +0000644#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000645}
Eric Andersen22332fd2001-01-30 23:40:39 +0000646
Denis Vlasenko38f63192007-01-22 09:03:07 +0000647#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000648static line_input_t *line_input_state;
649#endif
650
Denis Vlasenkof443bff2007-03-31 11:08:50 +0000651static int get_command_bufsiz(FILE * source, char *command)
Eric Andersen09acc062001-01-04 11:10:38 +0000652{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000653 const char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000654
Eric Andersen1c314ad2000-06-28 16:56:25 +0000655 if (source == NULL) {
656 if (local_pending_command) {
657 /* a command specified (-c option): return it & mark it done */
Denis Vlasenkof443bff2007-03-31 11:08:50 +0000658 strncpy(command, local_pending_command, BUFSIZ);
Eric Andersen1c314ad2000-06-28 16:56:25 +0000659 local_pending_command = NULL;
660 return 0;
661 }
662 return 1;
663 }
664
Erik Andersen161220c2000-03-16 08:12:48 +0000665 if (source == stdin) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000666 prompt_str = setup_prompt_string();
Eric Andersen8ea28be2001-01-05 20:58:22 +0000667
Denis Vlasenko38f63192007-01-22 09:03:07 +0000668#if ENABLE_FEATURE_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000669 /*
670 ** enable command line editing only while a command line
671 ** is actually being read; otherwise, we'll end up bequeathing
672 ** atexit() handlers and other unwanted stuff to our
673 ** child processes (rob@sysgo.de)
674 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000675 read_line_input(prompt_str, command, BUFSIZ, line_input_state);
Erik Andersen161220c2000-03-16 08:12:48 +0000676 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000677#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000678 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000679#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000680 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000681
Erik Andersen161220c2000-03-16 08:12:48 +0000682 if (!fgets(command, BUFSIZ - 2, source)) {
683 if (source == stdin)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000684 puts("");
Erik Andersen161220c2000-03-16 08:12:48 +0000685 return 1;
686 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000687
Erik Andersen161220c2000-03-16 08:12:48 +0000688 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000689}
690
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000691static char * strsep_space(char *string, int * ix)
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000692{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000693 /* Short circuit the trivial case */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000694 if (!string || ! string[*ix])
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000695 return NULL;
696
697 /* Find the end of the token. */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000698 while (string[*ix] && !isspace(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000699 (*ix)++;
700 }
701
Eric Andersenff9ad472004-02-10 01:07:45 +0000702 /* Find the end of any whitespace trailing behind
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000703 * the token and let that be part of the token */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000704 while (string[*ix] && (isspace)(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000705 (*ix)++;
706 }
707
Rob Landleyf9463792006-06-13 18:50:20 +0000708 if (!*ix) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000709 /* Nothing useful was found */
710 return NULL;
711 }
712
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000713 return xstrndup(string, *ix);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000714}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000715
Eric Andersenca604592001-03-08 17:17:13 +0000716static int expand_arguments(char *command)
717{
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000718 static const char out_of_space[] = "out of space during expansion";
719
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000720 int total_length = 0, length, i, retval, ix = 0;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000721 expand_t expand_result;
722 char *tmpcmd, *cmd, *cmd_copy;
723 char *src, *dst, *var;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000724 int flags = GLOB_NOCHECK
725#ifdef GLOB_BRACE
726 | GLOB_BRACE
Eric Andersenff9ad472004-02-10 01:07:45 +0000727#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000728#ifdef GLOB_TILDE
729 | GLOB_TILDE
Eric Andersenff9ad472004-02-10 01:07:45 +0000730#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000731 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000732
Eric Andersenca604592001-03-08 17:17:13 +0000733 /* get rid of the terminating \n */
734 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000735
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000736 /* Fix up escape sequences to be the Real Thing(tm) */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000737 while (command && command[ix]) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000738 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000739 const char *tmp = command+ix+1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000740 command[ix] = bb_process_escape_sequence( &tmp );
Eric Andersen1ca20a72001-03-21 07:34:27 +0000741 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000742 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000743 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000744 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000745 /* Use glob and then fixup environment variables and such */
746
747 /* It turns out that glob is very stupid. We have to feed it one word at a
748 * time since it can't cope with a full string. Here we convert command
749 * (char*) into cmd (char**, one word per string) */
750
751 /* We need a clean copy, so strsep can mess up the copy while
752 * we write stuff into the original (in a minute) */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000753 cmd = cmd_copy = xstrdup(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000754 *command = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000755 for (ix = 0, tmpcmd = cmd;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000756 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix = 0) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000757 if (*tmpcmd == '\0')
758 break;
759 /* we need to trim() the result for glob! */
760 trim(tmpcmd);
761 retval = glob(tmpcmd, flags, NULL, &expand_result);
762 free(tmpcmd); /* Free mem allocated by strsep_space */
763 if (retval == GLOB_NOSPACE) {
764 /* Mem may have been allocated... */
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000765 globfree(&expand_result);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000766 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000767 return FALSE;
768 } else if (retval != 0) {
769 /* Some other error. GLOB_NOMATCH shouldn't
Eric Andersenff9ad472004-02-10 01:07:45 +0000770 * happen because of the GLOB_NOCHECK flag in
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000771 * the glob call. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000772 bb_error_msg("syntax error");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000773 return FALSE;
774 } else {
775 /* Convert from char** (one word per string) to a simple char*,
776 * but don't overflow command which is BUFSIZ in length */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000777 for (i = 0; i < expand_result.gl_pathc; i++) {
778 length = strlen(expand_result.gl_pathv[i]);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000779 if (total_length+length+1 >= BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000780 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000781 return FALSE;
782 }
783 strcat(command+total_length, " ");
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000784 total_length += 1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000785 strcat(command+total_length, expand_result.gl_pathv[i]);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000786 total_length += length;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000787 }
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000788 globfree(&expand_result);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000789 }
790 }
791 free(cmd_copy);
792 trim(command);
793
Eric Andersenff9ad472004-02-10 01:07:45 +0000794 /* Now do the shell variable substitutions which
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000795 * wordexp can't do for us, namely $? and $! */
796 src = command;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000797 while ((dst = strchr(src,'$')) != NULL) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000798 var = NULL;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000799 switch (*(dst+1)) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000800 case '?':
801 var = itoa(last_return_code);
802 break;
803 case '!':
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000804 if (last_bg_pid == -1)
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000805 *var = '\0';
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000806 else
807 var = itoa(last_bg_pid);
808 break;
809 /* Everything else like $$, $#, $[0-9], etc. should all be
810 * expanded by wordexp(), so we can in theory skip that stuff
811 * here, but just to be on the safe side (i.e., since uClibc
812 * wordexp doesn't do this stuff yet), lets leave it in for
813 * now. */
814 case '$':
815 var = itoa(getpid());
816 break;
817 case '#':
818 var = itoa(argc-1);
819 break;
820 case '0':case '1':case '2':case '3':case '4':
821 case '5':case '6':case '7':case '8':case '9':
822 {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000823 int ixx = *(dst+1)-48+1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000824 if (ixx >= argc) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000825 var = '\0';
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000826 } else {
827 var = argv[ixx];
828 }
829 }
830 break;
831
832 }
833 if (var) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000834 /* a single character construction was found, and
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000835 * already handled in the case statement */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000836 src = dst + 2;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000837 } else {
838 /* Looks like an environment variable */
839 char delim_hold;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000840 int num_skip_chars = 0;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000841 int dstlen = strlen(dst);
842 /* Is this a ${foo} type variable? */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000843 if (dstlen >= 2 && *(dst+1) == '{') {
844 src = strchr(dst+1, '}');
845 num_skip_chars = 1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000846 } else {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000847 src = dst + 1;
848 while ((isalnum)(*src) || *src == '_') src++;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000849 }
850 if (src == NULL) {
851 src = dst+dstlen;
852 }
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000853 delim_hold = *src;
854 *src = '\0'; /* temporary */
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000855 var = getenv(dst + 1 + num_skip_chars);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000856 *src = delim_hold;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000857 src += num_skip_chars;
858 }
859 if (var == NULL) {
860 /* Seems we got an un-expandable variable. So delete it. */
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000861 var = (char*)"";
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000862 }
863 {
864 int subst_len = strlen(var);
865 int trail_len = strlen(src);
866 if (dst+subst_len+trail_len >= command+BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000867 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000868 return FALSE;
869 }
870 /* Move stuff to the end of the string to accommodate
871 * filling the created gap with the new stuff */
872 memmove(dst+subst_len, src, trail_len+1);
873 /* Now copy in the new stuff */
874 memcpy(dst, var, subst_len);
875 src = dst+subst_len;
876 }
877 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000878
Eric Andersenca604592001-03-08 17:17:13 +0000879 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000880}
881
Eric Andersen86349772000-12-18 20:25:50 +0000882/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
883 line). If a valid command is found, command_ptr is set to point to
Eric Andersenff9ad472004-02-10 01:07:45 +0000884 the beginning of the next command (if the original command had more
885 then one job associated with it) or NULL if no more commands are
Erik Andersen3522eb12000-03-12 23:49:18 +0000886 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000887static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000888{
Erik Andersen161220c2000-03-16 08:12:48 +0000889 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000890 char *return_command = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +0000891 char *src, *buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000892 int argc_l;
893 int flag;
Eric Andersen86349772000-12-18 20:25:50 +0000894 int argv_alloced;
Erik Andersen161220c2000-03-16 08:12:48 +0000895 char quote = '\0';
Eric Andersen86349772000-12-18 20:25:50 +0000896 struct child_prog *prog;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000897#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersenff9ad472004-02-10 01:07:45 +0000898 int i;
899 char *chptr;
900#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000901
Erik Andersen161220c2000-03-16 08:12:48 +0000902 /* skip leading white space */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000903 *command_ptr = skip_whitespace(*command_ptr);
Erik Andersen3522eb12000-03-12 23:49:18 +0000904
Erik Andersen161220c2000-03-16 08:12:48 +0000905 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000906 if (!**command_ptr || (**command_ptr == '#')) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000907 job->num_progs = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000908 return 0;
909 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000910
Eric Andersen86349772000-12-18 20:25:50 +0000911 *inbg = 0;
912 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000913 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000914
Eric Andersenff9ad472004-02-10 01:07:45 +0000915 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000916 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000917 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000918
Eric Andersenff9ad472004-02-10 01:07:45 +0000919 Getting clean memory relieves us of the task of NULL
920 terminating things and makes the rest of this look a bit
Erik Andersen161220c2000-03-16 08:12:48 +0000921 cleaner (though it is, admittedly, a tad less efficient) */
Rob Landleyf9463792006-06-13 18:50:20 +0000922 job->cmdbuf = command = xzalloc(2*strlen(*command_ptr) + 1);
Erik Andersen161220c2000-03-16 08:12:48 +0000923 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000924
Erik Andersen161220c2000-03-16 08:12:48 +0000925 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000926 prog->num_redirects = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000927 prog->is_stopped = 0;
928 prog->family = job;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000929#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersenff9ad472004-02-10 01:07:45 +0000930 prog->redirects = NULL;
931#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000932
Eric Andersen86349772000-12-18 20:25:50 +0000933 argv_alloced = 5;
934 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
935 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +0000936
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000937 flag = argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000938 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +0000939 src = *command_ptr;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000940 while (*src && !(flag & LASH_OPT_DONE)) {
Erik Andersen161220c2000-03-16 08:12:48 +0000941 if (quote == *src) {
942 quote = '\0';
943 } else if (quote) {
944 if (*src == '\\') {
945 src++;
946 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000947 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +0000948 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +0000949 return 1;
950 }
951
952 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +0000953 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +0000954 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +0000955 *buf++ = '\\';
956 }
Erik Andersen161220c2000-03-16 08:12:48 +0000957 } else if (*src == '*' || *src == '?' || *src == '[' ||
958 *src == ']') *buf++ = '\\';
959 *buf++ = *src;
960 } else if (isspace(*src)) {
Denis Vlasenkob69f35e2007-04-12 17:10:45 +0000961 if (*prog->argv[argc_l] || (flag & LASH_OPT_SAW_QUOTE)) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000962 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +0000963 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +0000964 if ((argc_l + 1) == argv_alloced) {
965 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +0000966 prog->argv = xrealloc(prog->argv,
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000967 sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +0000968 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000969 prog->argv[argc_l] = buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000970 flag ^= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000971 }
972 } else
973 switch (*src) {
974 case '"':
975 case '\'':
976 quote = *src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000977 flag |= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000978 break;
979
980 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000981 if (*(src-1)== '$')
982 *buf++ = *src;
983 else
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000984 flag |= LASH_OPT_DONE;
Erik Andersen161220c2000-03-16 08:12:48 +0000985 break;
986
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000987#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000988 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +0000989 case '<':
Eric Andersen86349772000-12-18 20:25:50 +0000990 i = prog->num_redirects++;
991 prog->redirects = xrealloc(prog->redirects,
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000992 sizeof(*prog->redirects) * (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +0000993
Eric Andersen86349772000-12-18 20:25:50 +0000994 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000995 if (buf != prog->argv[argc_l]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000996 /* the stuff before this character may be the file number
Erik Andersen161220c2000-03-16 08:12:48 +0000997 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +0000998 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000999 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +00001000
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001001 if (*chptr && *prog->argv[argc_l]) {
1002 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001003 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001004 }
1005 }
1006
Eric Andersen86349772000-12-18 20:25:50 +00001007 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001008 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001009 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001010 else
Eric Andersen86349772000-12-18 20:25:50 +00001011 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001012 }
1013
1014 if (*src++ == '>') {
1015 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001016 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001017 REDIRECT_APPEND, src++;
1018 else
Eric Andersen86349772000-12-18 20:25:50 +00001019 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001020 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001021 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001022 }
1023
1024 /* This isn't POSIX sh compliant. Oh well. */
1025 chptr = src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001026 chptr = skip_whitespace(chptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001027
1028 if (!*chptr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001029 bb_error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001030 free_job(job);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001031 job->num_progs = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001032 return 1;
1033 }
1034
Eric Andersen86349772000-12-18 20:25:50 +00001035 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001036 while (*chptr && !isspace(*chptr))
1037 *buf++ = *chptr++;
1038
1039 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001040 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001041 break;
1042
1043 case '|': /* pipe */
1044 /* finish this command */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001045 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001046 argc_l++;
1047 if (!argc_l) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001048 goto empty_command_in_pipe;
Erik Andersen161220c2000-03-16 08:12:48 +00001049 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001050 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001051
1052 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001053 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001054 job->progs = xrealloc(job->progs,
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001055 sizeof(*job->progs) * job->num_progs);
Eric Andersen86349772000-12-18 20:25:50 +00001056 prog = job->progs + (job->num_progs - 1);
1057 prog->num_redirects = 0;
1058 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001059 prog->is_stopped = 0;
1060 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001061 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001062
Eric Andersen86349772000-12-18 20:25:50 +00001063 argv_alloced = 5;
1064 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001065 prog->argv[0] = ++buf;
1066
1067 src++;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001068 src = skip_whitespace(src);
Erik Andersen161220c2000-03-16 08:12:48 +00001069
1070 if (!*src) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001071empty_command_in_pipe:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001072 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001073 free_job(job);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001074 job->num_progs = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001075 return 1;
1076 }
1077 src--; /* we'll ++ it at the end of the loop */
1078
1079 break;
Eric Andersenff9ad472004-02-10 01:07:45 +00001080#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001081
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001082#if ENABLE_LASH_JOB_CONTROL
Erik Andersen161220c2000-03-16 08:12:48 +00001083 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001084 *inbg = 1;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001085 /* fallthrough */
Eric Andersen37032b42004-02-10 01:28:36 +00001086#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001087 case ';': /* multiple commands */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001088 flag |= LASH_OPT_DONE;
Eric Andersen86349772000-12-18 20:25:50 +00001089 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001090 break;
1091
Matt Kraai131241f2000-09-14 00:43:20 +00001092 case '\\':
1093 src++;
1094 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001095 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001096 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001097 return 1;
1098 }
1099 if (*src == '*' || *src == '[' || *src == ']'
1100 || *src == '?') *buf++ = '\\';
1101 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001102 default:
1103 *buf++ = *src;
1104 }
1105
Erik Andersend75af992000-03-16 08:09:09 +00001106 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001107 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001108
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001109 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001110 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001111 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001112 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001113 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001114 return 0;
1115 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001116 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001117
Eric Andersen86349772000-12-18 20:25:50 +00001118 if (!return_command) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001119 job->text = xstrdup(*command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001120 } else {
1121 /* This leaves any trailing spaces, which is a bit sloppy */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001122 job->text = xstrndup(*command_ptr, return_command - *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001123 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001124
Eric Andersen86349772000-12-18 20:25:50 +00001125 *command_ptr = return_command;
Eric Andersenff9ad472004-02-10 01:07:45 +00001126
Erik Andersend75af992000-03-16 08:09:09 +00001127 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001128}
1129
Eric Andersen86349772000-12-18 20:25:50 +00001130/* Run the child_prog, no matter what kind of command it uses.
1131 */
1132static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001133{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +00001134 const struct built_in_command *x;
Rob Landley68286772006-06-13 19:01:30 +00001135
Eric Andersene9f07fb2000-12-21 18:31:36 +00001136 /* Check if the command matches any of the non-forking builtins.
1137 * Depending on context, this might be redundant. But it's
1138 * easier to waste a few CPU cycles than it is to figure out
1139 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001140 */
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001141 /* Check if the command matches any of the forking builtins. */
1142 for (x = bltins; x <= &VEC_LAST(bltins); x++) {
Denis Vlasenko219d14d2007-03-24 15:40:16 +00001143 if (strcmp(child->argv[0], x->cmd) == 0) {
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001144 _exit(x->function(child));
Eric Andersene9f07fb2000-12-21 18:31:36 +00001145 }
1146 }
1147
Rob Landley68286772006-06-13 19:01:30 +00001148
Eric Andersen86349772000-12-18 20:25:50 +00001149 /* Check if the command matches any busybox internal
1150 * commands ("applets") here. Following discussions from
Eric Andersen2423b122001-12-08 01:56:15 +00001151 * November 2000 on busybox@busybox.net, don't use
Manuel Novoa III cad53642003-03-19 09:13:01 +00001152 * bb_get_last_path_component(). This way explicit (with
Eric Andersen86349772000-12-18 20:25:50 +00001153 * slashes) filenames will never be interpreted as an
1154 * applet, just like with builtins. This way the user can
1155 * override an applet with an explicit filename reference.
1156 * The only downside to this change is that an explicit
1157 * /bin/foo invocation will fork and exec /bin/foo, even if
1158 * /bin/foo is a symlink to busybox.
1159 */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001160 if (ENABLE_FEATURE_SH_STANDALONE) {
Rob Landley68286772006-06-13 19:01:30 +00001161 char **argv_l = child->argv;
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001162 int argc_l;
Bernhard Reutner-Fischer62558762006-06-03 10:28:25 +00001163
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001164 for (argc_l = 0; *argv_l; argv_l++, argc_l++)
1165 continue;
Denis Vlasenkoe4f2d062007-04-11 17:03:19 +00001166 run_applet_and_exit(child->argv[0], argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001167 }
Eric Andersen86349772000-12-18 20:25:50 +00001168
1169 execvp(child->argv[0], child->argv);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001170
Eric Andersenff9ad472004-02-10 01:07:45 +00001171 /* Do not use bb_perror_msg_and_die() here, since we must not
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001172 * call exit() but should call _exit() instead */
Rob Landley5483de12006-06-20 21:35:26 +00001173 bb_perror_msg("%s", child->argv[0]);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001174 _exit(EXIT_FAILURE);
Eric Andersen86349772000-12-18 20:25:50 +00001175}
1176
1177static void insert_job(struct job *newjob, int inbg)
1178{
1179 struct job *thejob;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001180 struct jobset *j_list = newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001181
1182 /* find the ID for thejob to use */
1183 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001184 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001185 if (thejob->jobid >= newjob->jobid)
1186 newjob->jobid = thejob->jobid + 1;
1187
1188 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001189 if (!j_list->head) {
1190 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001191 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001192 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001193 thejob->next = xmalloc(sizeof(*thejob));
1194 thejob = thejob->next;
1195 }
1196
1197 *thejob = *newjob; /* physically copy the struct job */
1198 thejob->next = NULL;
1199 thejob->running_progs = thejob->num_progs;
1200 thejob->stopped_progs = 0;
1201
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001202#if ENABLE_LASH_JOB_CONTROL
Eric Andersen86349772000-12-18 20:25:50 +00001203 if (inbg) {
Eric Andersenff9ad472004-02-10 01:07:45 +00001204 /* we don't wait for background thejobs to return -- append it
Eric Andersen86349772000-12-18 20:25:50 +00001205 to the list of backgrounded thejobs and leave it alone */
1206 printf("[%d] %d\n", thejob->jobid,
1207 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001208 last_jobid = newjob->jobid;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001209 last_bg_pid = newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001210 } else {
1211 newjob->job_list->fg = thejob;
1212
1213 /* move the new process group into the foreground */
Rob Landleyf9463792006-06-13 18:50:20 +00001214 /* Ignore errors since child could have already exited */
1215 tcsetpgrp(shell_terminal, newjob->pgrp);
Eric Andersen86349772000-12-18 20:25:50 +00001216 }
Eric Andersen37032b42004-02-10 01:28:36 +00001217#endif
Eric Andersen86349772000-12-18 20:25:50 +00001218}
1219
1220static int run_command(struct job *newjob, int inbg, int outpipe[2])
1221{
1222 /* struct job *thejob; */
1223 int i;
1224 int nextin, nextout;
1225 int pipefds[2]; /* pipefd[0] is for reading */
Denis Vlasenko71d8abf2007-01-30 22:44:07 +00001226 const struct built_in_command *x;
Eric Andersen86349772000-12-18 20:25:50 +00001227 struct child_prog *child;
1228
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001229 nextin = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001230 for (i = 0; i < newjob->num_progs; i++) {
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001231 child = &(newjob->progs[i]);
Eric Andersen86349772000-12-18 20:25:50 +00001232
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001233 nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001234 if ((i + 1) < newjob->num_progs) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001235 if (pipe(pipefds) < 0)
1236 bb_perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001237 nextout = pipefds[1];
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001238 } else if (outpipe[1] != -1) {
1239 nextout = outpipe[1];
Erik Andersen161220c2000-03-16 08:12:48 +00001240 }
1241
Eric Andersene9f07fb2000-12-21 18:31:36 +00001242 /* Check if the command matches any non-forking builtins,
1243 * but only if this is a simple command.
1244 * Non-forking builtins within pipes have to fork anyway,
1245 * and are handled in pseudo_exec. "echo foo | read bar"
1246 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001247 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001248 if (newjob->num_progs == 1) {
Rob Landley7a8e4912006-07-01 17:19:02 +00001249 /* Check if the command sets an environment variable. */
1250 if (strchr(child->argv[0], '=') != NULL) {
1251 child->argv[1] = child->argv[0];
1252 return builtin_export(child);
1253 }
1254
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001255 for (x = bltins; x <= &VEC_LAST(bltins); x++) {
Denis Vlasenko219d14d2007-03-24 15:40:16 +00001256 if (strcmp(child->argv[0], x->cmd) == 0) {
Eric Andersene9f07fb2000-12-21 18:31:36 +00001257 int rcode;
Eric Andersenff9ad472004-02-10 01:07:45 +00001258 int squirrel[] = {-1, -1, -1};
Eric Andersene9f07fb2000-12-21 18:31:36 +00001259 setup_redirects(child, squirrel);
1260 rcode = x->function(child);
1261 restore_redirects(squirrel);
1262 return rcode;
1263 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001264 }
1265 }
1266
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001267#if BB_MMU
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001268 child->pid = fork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001269#else
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001270 child->pid = vfork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001271#endif
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001272 if (!child->pid) {
Eric Andersen2d848a42001-06-25 17:11:54 +00001273 /* Set the handling for job control signals back to the default. */
1274 signal(SIGINT, SIG_DFL);
1275 signal(SIGQUIT, SIG_DFL);
1276 signal(SIGTSTP, SIG_DFL);
1277 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001278 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001279 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001280
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001281 /* Close all open filehandles. */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001282 while (close_me_list)
1283 close((long)llist_pop(&close_me_list));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001284
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001285 if (outpipe[1] != -1) {
Eric Andersen86349772000-12-18 20:25:50 +00001286 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001287 }
1288 if (nextin != 0) {
1289 dup2(nextin, 0);
1290 close(nextin);
1291 }
1292
1293 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001294 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001295 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001296 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001297 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001298 }
1299
Eric Andersen86349772000-12-18 20:25:50 +00001300 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001301 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001302
Eric Andersen86349772000-12-18 20:25:50 +00001303 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001304 }
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001305 if (outpipe[1] != -1) {
Eric Andersen86349772000-12-18 20:25:50 +00001306 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001307 }
Erik Andersen161220c2000-03-16 08:12:48 +00001308
1309 /* put our child in the process group whose leader is the
1310 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001311 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001312 if (nextin != 0)
1313 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001314 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001315 close(nextout);
1316
Eric Andersenff9ad472004-02-10 01:07:45 +00001317 /* If there isn't another process, nextin is garbage
Erik Andersen161220c2000-03-16 08:12:48 +00001318 but it doesn't matter */
1319 nextin = pipefds[0];
1320 }
1321
Eric Andersen86349772000-12-18 20:25:50 +00001322 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001323
Eric Andersen86349772000-12-18 20:25:50 +00001324 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001325
Erik Andersen161220c2000-03-16 08:12:48 +00001326 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001327}
1328
Erik Andersend75af992000-03-16 08:09:09 +00001329static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001330{
Erik Andersen161220c2000-03-16 08:12:48 +00001331 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001332 char *next_command = NULL;
1333 struct job newjob;
Erik Andersen161220c2000-03-16 08:12:48 +00001334 int i;
Eric Andersena68ea1c2006-01-30 22:48:39 +00001335 int inbg = 0;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001336 int status;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001337#if ENABLE_LASH_JOB_CONTROL
Eric Andersend20d3752004-03-12 22:08:42 +00001338 pid_t parent_pgrp;
1339 /* save current owner of TTY so we can restore it on exit */
1340 parent_pgrp = tcgetpgrp(shell_terminal);
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001341#endif
Eric Andersen86349772000-12-18 20:25:50 +00001342 newjob.job_list = &job_list;
1343 newjob.job_context = DEFAULT_CONTEXT;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001344
Rob Landleyf9463792006-06-13 18:50:20 +00001345 command = xzalloc(BUFSIZ);
Erik Andersend75af992000-03-16 08:09:09 +00001346
Erik Andersen161220c2000-03-16 08:12:48 +00001347 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001348 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001349 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001350
Erik Andersend75af992000-03-16 08:09:09 +00001351 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001352 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001353
Eric Andersen86349772000-12-18 20:25:50 +00001354 if (!next_command) {
Denis Vlasenkof443bff2007-03-31 11:08:50 +00001355 if (get_command_bufsiz(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001356 break;
Eric Andersen86349772000-12-18 20:25:50 +00001357 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001358 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001359
Denis Vlasenkof443bff2007-03-31 11:08:50 +00001360 if (!expand_arguments(next_command)) {
Eric Andersenca604592001-03-08 17:17:13 +00001361 free(command);
Rob Landleyf9463792006-06-13 18:50:20 +00001362 command = xzalloc(BUFSIZ);
Eric Andersenca604592001-03-08 17:17:13 +00001363 next_command = NULL;
1364 continue;
1365 }
1366
Eric Andersen86349772000-12-18 20:25:50 +00001367 if (!parse_command(&next_command, &newjob, &inbg) &&
1368 newjob.num_progs) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001369 int pipefds[2] = { -1, -1 };
1370 debug_printf("job=%p fed to run_command by busy_loop()'\n",
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001371 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001372 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001373 }
1374 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001375 free(command);
Denis Vlasenko4cccc032006-12-22 18:37:07 +00001376 command = xzalloc(BUFSIZ);
Eric Andersen86349772000-12-18 20:25:50 +00001377 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001378 }
1379 } else {
1380 /* a job is running in the foreground; wait for it */
1381 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001382 while (!job_list.fg->progs[i].pid ||
1383 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001384
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001385 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED) < 0) {
Eric Andersen7373e482002-07-31 04:04:47 +00001386 if (errno != ECHILD) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001387 bb_perror_msg_and_die("waitpid(%d)", job_list.fg->progs[i].pid);
Eric Andersen7373e482002-07-31 04:04:47 +00001388 }
1389 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001390
Erik Andersend75af992000-03-16 08:09:09 +00001391 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001392 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001393 job_list.fg->running_progs--;
1394 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001395
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001396 last_return_code = WEXITSTATUS(status);
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001397
Eric Andersen86349772000-12-18 20:25:50 +00001398 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001399 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001400 remove_job(&job_list, job_list.fg);
1401 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001402 }
Eric Andersen37032b42004-02-10 01:28:36 +00001403 }
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001404#if ENABLE_LASH_JOB_CONTROL
Eric Andersen37032b42004-02-10 01:28:36 +00001405 else {
Erik Andersen161220c2000-03-16 08:12:48 +00001406 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001407 job_list.fg->stopped_progs++;
1408 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001409
Eric Andersen86349772000-12-18 20:25:50 +00001410 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1411 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1412 "Stopped", job_list.fg->text);
1413 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001414 }
Erik Andersend75af992000-03-16 08:09:09 +00001415 }
1416
Eric Andersen86349772000-12-18 20:25:50 +00001417 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001418 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001419 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001420 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Eric Andersenff9ad472004-02-10 01:07:45 +00001421 bb_perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001422 }
Eric Andersen37032b42004-02-10 01:28:36 +00001423#endif
Erik Andersend75af992000-03-16 08:09:09 +00001424 }
1425 }
Erik Andersen161220c2000-03-16 08:12:48 +00001426 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001427
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001428#if ENABLE_LASH_JOB_CONTROL
Eric Andersen1c314ad2000-06-28 16:56:25 +00001429 /* return controlling TTY back to parent process group before exiting */
Eric Andersen7373e482002-07-31 04:04:47 +00001430 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001431 bb_perror_msg("tcsetpgrp");
Eric Andersen37032b42004-02-10 01:28:36 +00001432#endif
Eric Andersenb54833c2000-07-03 23:56:26 +00001433
1434 /* return exit status if called with "-c" */
1435 if (input == NULL && WIFEXITED(status))
1436 return WEXITSTATUS(status);
Eric Andersenff9ad472004-02-10 01:07:45 +00001437
Erik Andersen161220c2000-03-16 08:12:48 +00001438 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001439}
1440
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001441#if ENABLE_FEATURE_CLEAN_UP
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +00001442static void free_memory(void)
Eric Andersenfad9c112000-07-25 18:06:52 +00001443{
Denis Vlasenko7cced6e2007-04-12 17:08:53 +00001444 free(cwd);
Eric Andersenfad9c112000-07-25 18:06:52 +00001445
Eric Andersen86349772000-12-18 20:25:50 +00001446 if (job_list.fg && !job_list.fg->running_progs) {
1447 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001448 }
1449}
Rob Landleyacf448d2006-07-09 17:03:07 +00001450#else
1451void free_memory(void);
Eric Andersenfad9c112000-07-25 18:06:52 +00001452#endif
1453
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001454#if ENABLE_LASH_JOB_CONTROL
Eric Andersen2d848a42001-06-25 17:11:54 +00001455/* Make sure we have a controlling tty. If we get started under a job
1456 * aware app (like bash for example), make sure we are now in charge so
1457 * we don't fight over who gets the foreground */
Eric Andersenece8fc22002-12-11 04:26:28 +00001458static void setup_job_control(void)
Eric Andersen2d848a42001-06-25 17:11:54 +00001459{
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001460 int status;
Eric Andersen37032b42004-02-10 01:28:36 +00001461 pid_t shell_pgrp;
Eric Andersenff9ad472004-02-10 01:07:45 +00001462
Eric Andersen2d848a42001-06-25 17:11:54 +00001463 /* Loop until we are in the foreground. */
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001464 while ((status = tcgetpgrp(shell_terminal)) >= 0) {
1465 shell_pgrp = getpgrp();
1466 if (status == shell_pgrp) {
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001467 break;
1468 }
Denis Vlasenkoff131b92007-04-10 15:42:06 +00001469 kill(- shell_pgrp, SIGTTIN);
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001470 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001471
1472 /* Ignore interactive and job-control signals. */
1473 signal(SIGINT, SIG_IGN);
1474 signal(SIGQUIT, SIG_IGN);
1475 signal(SIGTSTP, SIG_IGN);
1476 signal(SIGTTIN, SIG_IGN);
1477 signal(SIGTTOU, SIG_IGN);
1478 signal(SIGCHLD, SIG_IGN);
1479
1480 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001481 setsid();
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +00001482 shell_pgrp = getpid();
Rob Landleyacf448d2006-07-09 17:03:07 +00001483 setpgid(shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001484
1485 /* Grab control of the terminal. */
1486 tcsetpgrp(shell_terminal, shell_pgrp);
1487}
Eric Andersen37032b42004-02-10 01:28:36 +00001488#else
1489static inline void setup_job_control(void)
1490{
1491}
1492#endif
Eric Andersen6efc48c2000-07-18 08:16:39 +00001493
Denis Vlasenko06af2162007-02-03 17:28:39 +00001494int lash_main(int argc_l, char **argv_l);
Matt Kraai2d91deb2001-08-01 17:21:35 +00001495int lash_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001496{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001497 unsigned opt;
Erik Andersen161220c2000-03-16 08:12:48 +00001498 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001499 argc = argc_l;
1500 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001501
Denis Vlasenko38f63192007-01-22 09:03:07 +00001502#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001503 line_input_state = new_line_input_t(FOR_SHELL);
1504#endif
1505
Eric Andersen702ec592001-03-06 22:17:29 +00001506 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001507 last_jobid = 0;
Rob Landleyf9463792006-06-13 18:50:20 +00001508 close_me_list = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001509 job_list.head = NULL;
1510 job_list.fg = NULL;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001511 last_return_code = 1;
Eric Andersen501c88b2000-07-28 15:14:45 +00001512
Eric Andersenb558e762000-11-30 22:43:16 +00001513 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001514 FILE *prof_input;
1515 prof_input = fopen("/etc/profile", "r");
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001516 if (prof_input) {
Rob Landley7a260f02006-06-19 03:20:03 +00001517 llist_add_to(&close_me_list, (void *)(long)fileno(prof_input));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001518 /* Now run the file */
1519 busy_loop(prof_input);
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00001520 fclose_if_not_stdin(prof_input);
Rob Landleyf9463792006-06-13 18:50:20 +00001521 llist_pop(&close_me_list);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001522 }
Eric Andersenb558e762000-11-30 22:43:16 +00001523 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001524
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001525 opt = getopt32(argc_l, argv_l, "+ic:", &local_pending_command);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001526#define LASH_OPT_i (1<<0)
Denis Vlasenkof443bff2007-03-31 11:08:50 +00001527#define LASH_OPT_c (1<<1)
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001528 if (opt & LASH_OPT_c) {
1529 input = NULL;
1530 optind++;
1531 argv += optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001532 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001533 /* A shell is interactive if the `-i' flag was given, or if all of
1534 * the following conditions are met:
1535 * no -c command
1536 * no arguments remaining or the -s flag given
1537 * standard input is a terminal
1538 * standard output is a terminal
1539 * Refer to Posix.2, the description of the `sh' utility. */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001540 if (argv[optind] == NULL && input == stdin
1541 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
1542 ) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001543 opt |= LASH_OPT_i;
Eric Andersen86349772000-12-18 20:25:50 +00001544 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001545 setup_job_control();
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001546 if (opt & LASH_OPT_i) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001547 /* Looks like they want an interactive shell */
Rob Landleyacf448d2006-07-09 17:03:07 +00001548 if (!ENABLE_FEATURE_SH_EXTRA_QUIET) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001549 printf("\n\n%s Built-in shell (lash)\n"
1550 "Enter 'help' for a list of built-in commands.\n\n",
1551 BB_BANNER);
Rob Landleyacf448d2006-07-09 17:03:07 +00001552 }
1553 } else if (!local_pending_command && argv[optind]) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001554 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Rob Landleyd921b2e2006-08-03 15:41:12 +00001555 input = xfopen(argv[optind], "r");
Rob Landleyf9463792006-06-13 18:50:20 +00001556 /* be lazy, never mark this closed */
Rob Landley7a260f02006-06-19 03:20:03 +00001557 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Eric Andersen501c88b2000-07-28 15:14:45 +00001558 }
1559
Eric Andersen6efc48c2000-07-18 08:16:39 +00001560 /* initialize the cwd -- this is never freed...*/
Denis Vlasenko7cced6e2007-04-12 17:08:53 +00001561 update_cwd();
Erik Andersen3522eb12000-03-12 23:49:18 +00001562
Rob Landleyacf448d2006-07-09 17:03:07 +00001563 if (ENABLE_FEATURE_CLEAN_UP) atexit(free_memory);
Eric Andersenfad9c112000-07-25 18:06:52 +00001564
Denis Vlasenko38f63192007-01-22 09:03:07 +00001565 if (ENABLE_FEATURE_EDITING) cmdedit_set_initial_prompt();
Rob Landleyacf448d2006-07-09 17:03:07 +00001566 else PS1 = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +00001567
Denis Vlasenko7cced6e2007-04-12 17:08:53 +00001568 return busy_loop(input);
Erik Andersen3522eb12000-03-12 23:49:18 +00001569}