blob: f9d9deb62118dad67e38800e6e38d732650ba8a2 [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 Vlasenko703e2022007-01-22 14:12:08 +000035enum { MAX_READ = 128 }; /* size of input buffer for 'read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000036#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
37
Erik Andersend75af992000-03-16 08:09:09 +000038
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +000039#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000040enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000041 REDIRECT_APPEND
42};
Eric Andersenff9ad472004-02-10 01:07:45 +000043#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000044
Rob Landleybc68cd12006-03-10 19:22:06 +000045enum {
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 Andersenfad9c112000-07-25 18:06:52 +000052
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +000053#define LASH_OPT_DONE (1)
54#define LASH_OPT_SAW_QUOTE (2)
55
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +000056#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000057struct redir_struct {
58 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000059 int fd; /* file descriptor being redirected */
60 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000061};
Eric Andersenff9ad472004-02-10 01:07:45 +000062#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000063
Eric Andersen86349772000-12-18 20:25:50 +000064struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000065 pid_t pid; /* 0 if exited */
66 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000067 int num_redirects; /* elements in redirection array */
Eric Andersen86349772000-12-18 20:25:50 +000068 int is_stopped; /* is the program currently running? */
69 struct job *family; /* pointer back to the child's parent job */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +000070#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersenff9ad472004-02-10 01:07:45 +000071 struct redir_struct *redirects; /* I/O redirects */
72#endif
73};
74
75struct jobset {
76 struct job *head; /* head of list of running jobs */
77 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000078};
79
80struct job {
Eric Andersen86349772000-12-18 20:25:50 +000081 int jobid; /* job number */
82 int num_progs; /* total number of programs in job */
83 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000084 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +000085 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +000086 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +000087 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +000088 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +000089 int stopped_progs; /* number of programs alive, but stopped */
90 unsigned int job_context; /* bitmask defining current context */
91 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +000092};
93
Eric Andersen86349772000-12-18 20:25:50 +000094struct built_in_command {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +000095 const char *cmd; /* name */
96 const char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +000097 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +000098};
99
Eric Andersen34e19412000-07-10 18:47:24 +0000100/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000101static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000102static int builtin_exec(struct child_prog *cmd);
103static int builtin_exit(struct child_prog *cmd);
104static int builtin_fg_bg(struct child_prog *cmd);
105static int builtin_help(struct child_prog *cmd);
106static int builtin_jobs(struct child_prog *dummy);
107static int builtin_pwd(struct child_prog *dummy);
108static int builtin_export(struct child_prog *cmd);
109static int builtin_source(struct child_prog *cmd);
110static int builtin_unset(struct child_prog *cmd);
111static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000112
Eric Andersen34e19412000-07-10 18:47:24 +0000113
114/* function prototypes for shell stuff */
Eric Andersen86349772000-12-18 20:25:50 +0000115static void checkjobs(struct jobset *job_list);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000116static void remove_job(struct jobset *j_list, struct job *job);
Denis Vlasenkof443bff2007-03-31 11:08:50 +0000117static int get_command_bufsiz(FILE * source, char *command);
Eric Andersen86349772000-12-18 20:25:50 +0000118static int parse_command(char **command_ptr, struct job *job, int *inbg);
119static int run_command(struct job *newjob, int inbg, int outpipe[2]);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000120static int pseudo_exec(struct child_prog *cmd) ATTRIBUTE_NORETURN;
Erik Andersen3522eb12000-03-12 23:49:18 +0000121static int busy_loop(FILE * input);
122
Erik Andersend75af992000-03-16 08:09:09 +0000123
Mark Whitley37653aa2000-07-12 23:36:17 +0000124/* 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 Vlasenko71d8abf2007-01-30 22:44:07 +0000127static const struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000128 {"bg", "Resume a job in the background", builtin_fg_bg},
129 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000130 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000131 {"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 Kraaidd450a02000-09-13 03:43:36 +0000137 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000138 /* to do: add ulimit */
Eric Andersenfad9c112000-07-25 18:06:52 +0000139 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000140};
141
Mark Whitley37653aa2000-07-12 23:36:17 +0000142/* 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 Andersen86349772000-12-18 20:25:50 +0000144static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000145 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000146 {"help", "List shell built-in commands", builtin_help},
147 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000148};
149
Eric Andersen0bcc8132001-01-05 19:37:32 +0000150
Eric Andersen7467c8d2001-07-12 20:26:32 +0000151static int shell_context; /* Type prompt trigger (PS1 or PS2) */
Eric Andersen0bcc8132001-01-05 19:37:32 +0000152
153
154/* Globals that are static to this file */
Eric Andersencfa88ec2001-05-11 18:08:16 +0000155static const char *cwd;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000156static char *local_pending_command;
Eric Andersen86349772000-12-18 20:25:50 +0000157static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000158static int argc;
159static char **argv;
Rob Landleyf9463792006-06-13 18:50:20 +0000160static llist_t *close_me_list;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000161static int last_return_code;
162static int last_bg_pid;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000163static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000164static int shell_terminal;
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000165static const char *PS1;
166static const char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000167
168
Eric Andersenb558e762000-11-30 22:43:16 +0000169#ifdef DEBUG_SHELL
170static inline void debug_printf(const char *format, ...)
171{
172 va_list args;
173 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000174 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000175 va_end(args);
176}
177#else
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000178static inline void debug_printf(const char ATTRIBUTE_UNUSED *format, ...) { }
Eric Andersenb558e762000-11-30 22:43:16 +0000179#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000180
Eric Andersen86349772000-12-18 20:25:50 +0000181/*
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
187builtin previous use notes
188------ ----------------- ---------
189cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000190exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
191exit cmd->progs[0]
192fg_bg cmd->progs[0], job_list->head, job_list->fg
193help 0
194jobs job_list->head
195pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000196export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000197source cmd->progs[0]
198unset cmd->progs[0]
199read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000200
201I added "struct job *family;" to struct child_prog,
202and switched API to builtin_foo(struct child_prog *child);
203So 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 Andersen3522eb12000-03-12 23:49:18 +0000208
Erik Andersend75af992000-03-16 08:09:09 +0000209/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000210static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000211{
Erik Andersen161220c2000-03-16 08:12:48 +0000212 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000213
Eric Andersen86349772000-12-18 20:25:50 +0000214 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000215 newdir = getenv("HOME");
216 else
Eric Andersen86349772000-12-18 20:25:50 +0000217 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000218 if (chdir(newdir)) {
Rob Landley5483de12006-06-20 21:35:26 +0000219 bb_perror_msg("cd: %s", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000220 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000221 }
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000222 cwd = xrealloc_getcwd_or_warn((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000223 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000224 cwd = bb_msg_unknown;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000225 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000226}
227
Eric Andersend2f56772000-09-21 02:48:07 +0000228/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000229static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000230{
Eric Andersen86349772000-12-18 20:25:50 +0000231 if (child->argv[1] == NULL)
232 return EXIT_SUCCESS; /* Really? */
233 child->argv++;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000234 while (close_me_list)
235 close((long)llist_pop(&close_me_list));
Eric Andersen86349772000-12-18 20:25:50 +0000236 pseudo_exec(child);
237 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000238}
239
Erik Andersen3522eb12000-03-12 23:49:18 +0000240/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000241static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000242{
Eric Andersen86349772000-12-18 20:25:50 +0000243 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000244 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000245
Eric Andersen86349772000-12-18 20:25:50 +0000246 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000247}
248
249/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000250static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000251{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000252 int i, jobnum;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000253 struct job *job = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000254
Eric Andersen8a646dd2001-06-21 16:38:11 +0000255 /* 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 Andersen161220c2000-03-16 08:12:48 +0000261 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000262 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000263 bb_error_msg("%s: no current job", child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000264 return EXIT_FAILURE;
265 }
266 } else {
267 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000268 bb_error_msg(bb_msg_invalid_arg, child->argv[1], child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000269 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 cad53642003-03-19 09:13:01 +0000277 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000278 return EXIT_FAILURE;
279 }
Erik Andersend75af992000-03-16 08:09:09 +0000280 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000281
Eric Andersen86349772000-12-18 20:25:50 +0000282 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000283 /* Put the job into the foreground. */
284 tcsetpgrp(shell_terminal, job->pgrp);
285
Eric Andersen86349772000-12-18 20:25:50 +0000286 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000287 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000288
Erik Andersen161220c2000-03-16 08:12:48 +0000289 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000290 for (i = 0; i < job->num_progs; i++)
291 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000292
Eric Andersen86349772000-12-18 20:25:50 +0000293 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000294
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000295 i = kill(- job->pgrp, SIGCONT);
296 if (i < 0) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000297 if (i == ESRCH) {
298 remove_job(&job_list, job);
Eric Andersen07abfe22001-06-27 17:29:11 +0000299 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000300 bb_perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000301 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000302 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000303
Matt Kraai3e856ce2000-12-01 02:55:13 +0000304 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000305}
306
307/* built-in 'help' handler */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000308static int builtin_help(struct child_prog ATTRIBUTE_UNUSED *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000309{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000310 const struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000311
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000312 printf("\nBuilt-in commands:\n"
313 "-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000314 for (x = bltins; x->cmd; x++) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000315 if (x->descr == NULL)
Eric Andersenfad9c112000-07-25 18:06:52 +0000316 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000317 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000318 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000319 for (x = bltins_forking; x->cmd; x++) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000320 if (x->descr == NULL)
Eric Andersenfad9c112000-07-25 18:06:52 +0000321 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000322 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000323 }
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000324 putchar('\n');
Matt Kraai3e856ce2000-12-01 02:55:13 +0000325 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000326}
327
328/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000329static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000330{
Erik Andersen161220c2000-03-16 08:12:48 +0000331 struct job *job;
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000332 const char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000333
Eric Andersen86349772000-12-18 20:25:50 +0000334 for (job = child->family->job_list->head; job; job = job->next) {
335 if (job->running_progs == job->stopped_progs)
336 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000337 else
Eric Andersen86349772000-12-18 20:25:50 +0000338 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000339
Eric Andersen86349772000-12-18 20:25:50 +0000340 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000341 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000342 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000343}
344
345
346/* built-in 'pwd' handler */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000347static int builtin_pwd(struct child_prog ATTRIBUTE_UNUSED *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000348{
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000349 cwd = xrealloc_getcwd_or_warn((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000350 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000351 cwd = bb_msg_unknown;
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;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000411 if ((s = strdup(string)))
Eric Andersenb54833c2000-07-03 23:56:26 +0000412 res = putenv(s);
413 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000414 bb_perror_msg("read");
Eric Andersenb54833c2000-07-03 23:56:26 +0000415 }
416 else
417 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
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 cad53642003-03-19 09:13:01 +0000578 bb_perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000579 return 1;
580 }
581
582 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000583 if (squirrel && redir->fd < 3) {
584 squirrel[redir->fd] = dup(redir->fd);
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000585 fcntl(squirrel[redir->fd], F_SETFD, FD_CLOEXEC);
Eric Andersene9f07fb2000-12-21 18:31:36 +0000586 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000587 dup2(openfd, redir->fd);
588 close(openfd);
589 }
590 }
591
592 return 0;
593}
594
Eric Andersene9f07fb2000-12-21 18:31:36 +0000595static void restore_redirects(int squirrel[])
596{
597 int i, fd;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000598 for (i = 0; i < 3; i++) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000599 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 Andersenff9ad472004-02-10 01:07:45 +0000608#else
609static inline int setup_redirects(struct child_prog *prog, int squirrel[])
610{
611 return 0;
612}
613static inline void restore_redirects(int squirrel[])
614{
615}
616#endif
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000617
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000618static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000619{
Denis Vlasenko38f63192007-01-22 09:03:07 +0000620#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000621 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000622#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000623 PS1 = getenv("PS1");
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000624 if (PS1 == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000625 PS1 = "\\w \\$ ";
Eric Andersenff9ad472004-02-10 01:07:45 +0000626#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000627}
628
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000629static inline const char* setup_prompt_string(void)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000630{
Denis Vlasenko38f63192007-01-22 09:03:07 +0000631#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000632 /* Set up the prompt */
633 if (shell_context == 0) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000634 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 Andersenb3d6e2d2001-03-13 22:57:56 +0000640 } else {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000641 return PS2;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000642 }
643#else
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000644 return (shell_context == 0)? PS1 : PS2;
Eric Andersenff9ad472004-02-10 01:07:45 +0000645#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000646}
Eric Andersen22332fd2001-01-30 23:40:39 +0000647
Denis Vlasenko38f63192007-01-22 09:03:07 +0000648#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000649static line_input_t *line_input_state;
650#endif
651
Denis Vlasenkof443bff2007-03-31 11:08:50 +0000652static int get_command_bufsiz(FILE * source, char *command)
Eric Andersen09acc062001-01-04 11:10:38 +0000653{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000654 const char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000655
Eric Andersen1c314ad2000-06-28 16:56:25 +0000656 if (source == NULL) {
657 if (local_pending_command) {
658 /* a command specified (-c option): return it & mark it done */
Denis Vlasenkof443bff2007-03-31 11:08:50 +0000659 strncpy(command, local_pending_command, BUFSIZ);
Eric Andersen1c314ad2000-06-28 16:56:25 +0000660 local_pending_command = NULL;
661 return 0;
662 }
663 return 1;
664 }
665
Erik Andersen161220c2000-03-16 08:12:48 +0000666 if (source == stdin) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000667 prompt_str = setup_prompt_string();
Eric Andersen8ea28be2001-01-05 20:58:22 +0000668
Denis Vlasenko38f63192007-01-22 09:03:07 +0000669#if ENABLE_FEATURE_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000670 /*
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 Vlasenko8e1c7152007-01-22 07:21:38 +0000676 read_line_input(prompt_str, command, BUFSIZ, line_input_state);
Erik Andersen161220c2000-03-16 08:12:48 +0000677 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000678#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000679 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000680#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000681 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000682
Erik Andersen161220c2000-03-16 08:12:48 +0000683 if (!fgets(command, BUFSIZ - 2, source)) {
684 if (source == stdin)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000685 puts("");
Erik Andersen161220c2000-03-16 08:12:48 +0000686 return 1;
687 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000688
Erik Andersen161220c2000-03-16 08:12:48 +0000689 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000690}
691
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000692static char * strsep_space(char *string, int * ix)
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000693{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000694 /* Short circuit the trivial case */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000695 if (!string || ! string[*ix])
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000696 return NULL;
697
698 /* Find the end of the token. */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000699 while (string[*ix] && !isspace(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000700 (*ix)++;
701 }
702
Eric Andersenff9ad472004-02-10 01:07:45 +0000703 /* Find the end of any whitespace trailing behind
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000704 * the token and let that be part of the token */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000705 while (string[*ix] && (isspace)(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000706 (*ix)++;
707 }
708
Rob Landleyf9463792006-06-13 18:50:20 +0000709 if (!*ix) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000710 /* Nothing useful was found */
711 return NULL;
712 }
713
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000714 return xstrndup(string, *ix);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000715}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000716
Eric Andersenca604592001-03-08 17:17:13 +0000717static int expand_arguments(char *command)
718{
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000719 static const char out_of_space[] = "out of space during expansion";
720
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000721 int total_length = 0, length, i, retval, ix = 0;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000722 expand_t expand_result;
723 char *tmpcmd, *cmd, *cmd_copy;
724 char *src, *dst, *var;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000725 int flags = GLOB_NOCHECK
726#ifdef GLOB_BRACE
727 | GLOB_BRACE
Eric Andersenff9ad472004-02-10 01:07:45 +0000728#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000729#ifdef GLOB_TILDE
730 | GLOB_TILDE
Eric Andersenff9ad472004-02-10 01:07:45 +0000731#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000732 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000733
Eric Andersenca604592001-03-08 17:17:13 +0000734 /* get rid of the terminating \n */
735 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000736
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000737 /* Fix up escape sequences to be the Real Thing(tm) */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000738 while (command && command[ix]) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000739 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000740 const char *tmp = command+ix+1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000741 command[ix] = bb_process_escape_sequence( &tmp );
Eric Andersen1ca20a72001-03-21 07:34:27 +0000742 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000743 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000744 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000745 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000746 /* 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 Landleyd921b2e2006-08-03 15:41:12 +0000754 cmd = cmd_copy = xstrdup(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000755 *command = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000756 for (ix = 0, tmpcmd = cmd;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000757 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix = 0) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000758 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 Vlasenkoff131b92007-04-10 15:42:06 +0000766 globfree(&expand_result);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000767 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000768 return FALSE;
769 } else if (retval != 0) {
770 /* Some other error. GLOB_NOMATCH shouldn't
Eric Andersenff9ad472004-02-10 01:07:45 +0000771 * happen because of the GLOB_NOCHECK flag in
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000772 * the glob call. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000773 bb_error_msg("syntax error");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000774 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 Vlasenko6fa8b8e2007-03-31 11:07:30 +0000778 for (i = 0; i < expand_result.gl_pathc; i++) {
779 length = strlen(expand_result.gl_pathv[i]);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000780 if (total_length+length+1 >= BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000781 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000782 return FALSE;
783 }
784 strcat(command+total_length, " ");
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000785 total_length += 1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000786 strcat(command+total_length, expand_result.gl_pathv[i]);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000787 total_length += length;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000788 }
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000789 globfree(&expand_result);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000790 }
791 }
792 free(cmd_copy);
793 trim(command);
794
Eric Andersenff9ad472004-02-10 01:07:45 +0000795 /* Now do the shell variable substitutions which
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000796 * wordexp can't do for us, namely $? and $! */
797 src = command;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000798 while ((dst = strchr(src,'$')) != NULL) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000799 var = NULL;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000800 switch (*(dst+1)) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000801 case '?':
802 var = itoa(last_return_code);
803 break;
804 case '!':
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000805 if (last_bg_pid == -1)
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000806 *var = '\0';
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000807 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 Vlasenko6fa8b8e2007-03-31 11:07:30 +0000824 int ixx = *(dst+1)-48+1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000825 if (ixx >= argc) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000826 var = '\0';
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000827 } else {
828 var = argv[ixx];
829 }
830 }
831 break;
832
833 }
834 if (var) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000835 /* a single character construction was found, and
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000836 * already handled in the case statement */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000837 src = dst + 2;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000838 } else {
839 /* Looks like an environment variable */
840 char delim_hold;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000841 int num_skip_chars = 0;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000842 int dstlen = strlen(dst);
843 /* Is this a ${foo} type variable? */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000844 if (dstlen >= 2 && *(dst+1) == '{') {
845 src = strchr(dst+1, '}');
846 num_skip_chars = 1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000847 } else {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000848 src = dst + 1;
849 while ((isalnum)(*src) || *src == '_') src++;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000850 }
851 if (src == NULL) {
852 src = dst+dstlen;
853 }
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000854 delim_hold = *src;
855 *src = '\0'; /* temporary */
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000856 var = getenv(dst + 1 + num_skip_chars);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000857 *src = delim_hold;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000858 src += num_skip_chars;
859 }
860 if (var == NULL) {
861 /* Seems we got an un-expandable variable. So delete it. */
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000862 var = (char*)"";
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000863 }
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 cad53642003-03-19 09:13:01 +0000868 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000869 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 Andersen3522eb12000-03-12 23:49:18 +0000879
Eric Andersenca604592001-03-08 17:17:13 +0000880 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000881}
882
Eric Andersen86349772000-12-18 20:25:50 +0000883/* 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 Andersenff9ad472004-02-10 01:07:45 +0000885 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 Andersen3522eb12000-03-12 23:49:18 +0000887 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000888static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000889{
Erik Andersen161220c2000-03-16 08:12:48 +0000890 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000891 char *return_command = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +0000892 char *src, *buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000893 int argc_l;
894 int flag;
Eric Andersen86349772000-12-18 20:25:50 +0000895 int argv_alloced;
Erik Andersen161220c2000-03-16 08:12:48 +0000896 char quote = '\0';
Eric Andersen86349772000-12-18 20:25:50 +0000897 struct child_prog *prog;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000898#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersenff9ad472004-02-10 01:07:45 +0000899 int i;
900 char *chptr;
901#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000902
Erik Andersen161220c2000-03-16 08:12:48 +0000903 /* skip leading white space */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000904 *command_ptr = skip_whitespace(*command_ptr);
Erik Andersen3522eb12000-03-12 23:49:18 +0000905
Erik Andersen161220c2000-03-16 08:12:48 +0000906 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000907 if (!**command_ptr || (**command_ptr == '#')) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000908 job->num_progs = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000909 return 0;
910 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000911
Eric Andersen86349772000-12-18 20:25:50 +0000912 *inbg = 0;
913 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000914 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000915
Eric Andersenff9ad472004-02-10 01:07:45 +0000916 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000917 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000918 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000919
Eric Andersenff9ad472004-02-10 01:07:45 +0000920 Getting clean memory relieves us of the task of NULL
921 terminating things and makes the rest of this look a bit
Erik Andersen161220c2000-03-16 08:12:48 +0000922 cleaner (though it is, admittedly, a tad less efficient) */
Rob Landleyf9463792006-06-13 18:50:20 +0000923 job->cmdbuf = command = xzalloc(2*strlen(*command_ptr) + 1);
Erik Andersen161220c2000-03-16 08:12:48 +0000924 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000925
Erik Andersen161220c2000-03-16 08:12:48 +0000926 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000927 prog->num_redirects = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000928 prog->is_stopped = 0;
929 prog->family = job;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000930#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersenff9ad472004-02-10 01:07:45 +0000931 prog->redirects = NULL;
932#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000933
Eric Andersen86349772000-12-18 20:25:50 +0000934 argv_alloced = 5;
935 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
936 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +0000937
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000938 flag = argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000939 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +0000940 src = *command_ptr;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000941 while (*src && !(flag & LASH_OPT_DONE)) {
Erik Andersen161220c2000-03-16 08:12:48 +0000942 if (quote == *src) {
943 quote = '\0';
944 } else if (quote) {
945 if (*src == '\\') {
946 src++;
947 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000948 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +0000949 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +0000950 return 1;
951 }
952
953 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +0000954 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +0000955 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +0000956 *buf++ = '\\';
957 }
Erik Andersen161220c2000-03-16 08:12:48 +0000958 } else if (*src == '*' || *src == '?' || *src == '[' ||
959 *src == ']') *buf++ = '\\';
960 *buf++ = *src;
961 } else if (isspace(*src)) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000962 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000963 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +0000964 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +0000965 if ((argc_l + 1) == argv_alloced) {
966 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +0000967 prog->argv = xrealloc(prog->argv,
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000968 sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +0000969 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000970 prog->argv[argc_l] = buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000971 flag ^= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000972 }
973 } else
974 switch (*src) {
975 case '"':
976 case '\'':
977 quote = *src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000978 flag |= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000979 break;
980
981 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000982 if (*(src-1)== '$')
983 *buf++ = *src;
984 else
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000985 flag |= LASH_OPT_DONE;
Erik Andersen161220c2000-03-16 08:12:48 +0000986 break;
987
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000988#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000989 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +0000990 case '<':
Eric Andersen86349772000-12-18 20:25:50 +0000991 i = prog->num_redirects++;
992 prog->redirects = xrealloc(prog->redirects,
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000993 sizeof(*prog->redirects) * (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +0000994
Eric Andersen86349772000-12-18 20:25:50 +0000995 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000996 if (buf != prog->argv[argc_l]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000997 /* the stuff before this character may be the file number
Erik Andersen161220c2000-03-16 08:12:48 +0000998 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +0000999 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001000 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +00001001
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001002 if (*chptr && *prog->argv[argc_l]) {
1003 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001004 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001005 }
1006 }
1007
Eric Andersen86349772000-12-18 20:25:50 +00001008 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001009 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001010 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001011 else
Eric Andersen86349772000-12-18 20:25:50 +00001012 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001013 }
1014
1015 if (*src++ == '>') {
1016 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001017 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001018 REDIRECT_APPEND, src++;
1019 else
Eric Andersen86349772000-12-18 20:25:50 +00001020 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001021 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001022 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001023 }
1024
1025 /* This isn't POSIX sh compliant. Oh well. */
1026 chptr = src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001027 chptr = skip_whitespace(chptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001028
1029 if (!*chptr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001030 bb_error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001031 free_job(job);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001032 job->num_progs = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001033 return 1;
1034 }
1035
Eric Andersen86349772000-12-18 20:25:50 +00001036 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001037 while (*chptr && !isspace(*chptr))
1038 *buf++ = *chptr++;
1039
1040 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001041 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001042 break;
1043
1044 case '|': /* pipe */
1045 /* finish this command */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001046 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001047 argc_l++;
1048 if (!argc_l) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001049 goto empty_command_in_pipe;
Erik Andersen161220c2000-03-16 08:12:48 +00001050 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001051 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001052
1053 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001054 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001055 job->progs = xrealloc(job->progs,
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001056 sizeof(*job->progs) * job->num_progs);
Eric Andersen86349772000-12-18 20:25:50 +00001057 prog = job->progs + (job->num_progs - 1);
1058 prog->num_redirects = 0;
1059 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001060 prog->is_stopped = 0;
1061 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001062 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001063
Eric Andersen86349772000-12-18 20:25:50 +00001064 argv_alloced = 5;
1065 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001066 prog->argv[0] = ++buf;
1067
1068 src++;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001069 src = skip_whitespace(src);
Erik Andersen161220c2000-03-16 08:12:48 +00001070
1071 if (!*src) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001072empty_command_in_pipe:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001073 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001074 free_job(job);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001075 job->num_progs = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001076 return 1;
1077 }
1078 src--; /* we'll ++ it at the end of the loop */
1079
1080 break;
Eric Andersenff9ad472004-02-10 01:07:45 +00001081#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001082
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001083#if ENABLE_LASH_JOB_CONTROL
Erik Andersen161220c2000-03-16 08:12:48 +00001084 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001085 *inbg = 1;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001086 /* fallthrough */
Eric Andersen37032b42004-02-10 01:28:36 +00001087#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001088 case ';': /* multiple commands */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001089 flag |= LASH_OPT_DONE;
Eric Andersen86349772000-12-18 20:25:50 +00001090 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001091 break;
1092
Matt Kraai131241f2000-09-14 00:43:20 +00001093 case '\\':
1094 src++;
1095 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001096 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001097 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001098 return 1;
1099 }
1100 if (*src == '*' || *src == '[' || *src == ']'
1101 || *src == '?') *buf++ = '\\';
1102 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001103 default:
1104 *buf++ = *src;
1105 }
1106
Erik Andersend75af992000-03-16 08:09:09 +00001107 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001108 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001109
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001110 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001111 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001112 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001113 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001114 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001115 return 0;
1116 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001117 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001118
Eric Andersen86349772000-12-18 20:25:50 +00001119 if (!return_command) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001120 job->text = xstrdup(*command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001121 } else {
1122 /* This leaves any trailing spaces, which is a bit sloppy */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001123 job->text = xstrndup(*command_ptr, return_command - *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001124 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001125
Eric Andersen86349772000-12-18 20:25:50 +00001126 *command_ptr = return_command;
Eric Andersenff9ad472004-02-10 01:07:45 +00001127
Erik Andersend75af992000-03-16 08:09:09 +00001128 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001129}
1130
Eric Andersen86349772000-12-18 20:25:50 +00001131/* Run the child_prog, no matter what kind of command it uses.
1132 */
1133static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001134{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +00001135 const struct built_in_command *x;
Rob Landley68286772006-06-13 19:01:30 +00001136
Eric Andersene9f07fb2000-12-21 18:31:36 +00001137 /* 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 Andersen86349772000-12-18 20:25:50 +00001141 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001142 for (x = bltins; x->cmd; 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
1148 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001149 for (x = bltins_forking; x->cmd; x++) {
1150 if (strcmp(child->argv[0], x->cmd) == 0) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001151 applet_name = x->cmd;
1152 _exit(x->function(child));
Eric Andersen86349772000-12-18 20:25:50 +00001153 }
1154 }
Rob Landley68286772006-06-13 19:01:30 +00001155
Eric Andersen86349772000-12-18 20:25:50 +00001156 /* Check if the command matches any busybox internal
1157 * commands ("applets") here. Following discussions from
Eric Andersen2423b122001-12-08 01:56:15 +00001158 * November 2000 on busybox@busybox.net, don't use
Manuel Novoa III cad53642003-03-19 09:13:01 +00001159 * bb_get_last_path_component(). This way explicit (with
Eric Andersen86349772000-12-18 20:25:50 +00001160 * 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 Landley68286772006-06-13 19:01:30 +00001167 if (ENABLE_FEATURE_SH_STANDALONE_SHELL) {
1168 char **argv_l = child->argv;
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001169 int argc_l;
Bernhard Reutner-Fischer62558762006-06-03 10:28:25 +00001170
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001171 for (argc_l = 0; *argv_l; argv_l++, argc_l++);
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001172 optind = 1;
Rob Landley68286772006-06-13 19:01:30 +00001173 run_applet_by_name(child->argv[0], argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001174 }
Eric Andersen86349772000-12-18 20:25:50 +00001175
1176 execvp(child->argv[0], child->argv);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001177
Eric Andersenff9ad472004-02-10 01:07:45 +00001178 /* Do not use bb_perror_msg_and_die() here, since we must not
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001179 * call exit() but should call _exit() instead */
Rob Landley5483de12006-06-20 21:35:26 +00001180 bb_perror_msg("%s", child->argv[0]);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001181 _exit(EXIT_FAILURE);
Eric Andersen86349772000-12-18 20:25:50 +00001182}
1183
1184static void insert_job(struct job *newjob, int inbg)
1185{
1186 struct job *thejob;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001187 struct jobset *j_list = newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001188
1189 /* find the ID for thejob to use */
1190 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001191 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001192 if (thejob->jobid >= newjob->jobid)
1193 newjob->jobid = thejob->jobid + 1;
1194
1195 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001196 if (!j_list->head) {
1197 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001198 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001199 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001200 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 Vlasenko6fa8b8e2007-03-31 11:07:30 +00001209#if ENABLE_LASH_JOB_CONTROL
Eric Andersen86349772000-12-18 20:25:50 +00001210 if (inbg) {
Eric Andersenff9ad472004-02-10 01:07:45 +00001211 /* we don't wait for background thejobs to return -- append it
Eric Andersen86349772000-12-18 20:25:50 +00001212 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 Andersen8a646dd2001-06-21 16:38:11 +00001215 last_jobid = newjob->jobid;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001216 last_bg_pid = newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001217 } else {
1218 newjob->job_list->fg = thejob;
1219
1220 /* move the new process group into the foreground */
Rob Landleyf9463792006-06-13 18:50:20 +00001221 /* Ignore errors since child could have already exited */
1222 tcsetpgrp(shell_terminal, newjob->pgrp);
Eric Andersen86349772000-12-18 20:25:50 +00001223 }
Eric Andersen37032b42004-02-10 01:28:36 +00001224#endif
Eric Andersen86349772000-12-18 20:25:50 +00001225}
1226
1227static 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 Vlasenko71d8abf2007-01-30 22:44:07 +00001233 const struct built_in_command *x;
Eric Andersen86349772000-12-18 20:25:50 +00001234 struct child_prog *child;
1235
Eric Andersen6efc48c2000-07-18 08:16:39 +00001236 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001237 for (i = 0; i < newjob->num_progs; i++) {
1238 child = & (newjob->progs[i]);
1239
1240 if ((i + 1) < newjob->num_progs) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001241 if (pipe(pipefds) < 0)
1242 bb_perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001243 nextout = pipefds[1];
1244 } else {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001245 if (outpipe[1] != -1) {
Eric Andersen86349772000-12-18 20:25:50 +00001246 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001247 } else {
1248 nextout = 1;
1249 }
Erik Andersen161220c2000-03-16 08:12:48 +00001250 }
1251
Eric Andersen501c88b2000-07-28 15:14:45 +00001252
Eric Andersene9f07fb2000-12-21 18:31:36 +00001253 /* 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 Andersen86349772000-12-18 20:25:50 +00001258 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001259 if (newjob->num_progs == 1) {
Rob Landley7a8e4912006-07-01 17:19:02 +00001260 /* 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 Andersene9f07fb2000-12-21 18:31:36 +00001266 for (x = bltins; x->cmd; x++) {
Denis Vlasenko219d14d2007-03-24 15:40:16 +00001267 if (strcmp(child->argv[0], x->cmd) == 0) {
Eric Andersene9f07fb2000-12-21 18:31:36 +00001268 int rcode;
Eric Andersenff9ad472004-02-10 01:07:45 +00001269 int squirrel[] = {-1, -1, -1};
Eric Andersene9f07fb2000-12-21 18:31:36 +00001270 setup_redirects(child, squirrel);
1271 rcode = x->function(child);
1272 restore_redirects(squirrel);
1273 return rcode;
1274 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001275 }
1276 }
1277
Eric Andersene3efc922004-04-12 17:59:24 +00001278#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersenff9ad472004-02-10 01:07:45 +00001279 if (!(child->pid = fork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001280#else
Eric Andersenff9ad472004-02-10 01:07:45 +00001281 if (!(child->pid = vfork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001282#endif
1283 {
Eric Andersen2d848a42001-06-25 17:11:54 +00001284 /* 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 Andersen161220c2000-03-16 08:12:48 +00001289 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001290 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001291
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001292 /* Close all open filehandles. */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001293 while (close_me_list)
1294 close((long)llist_pop(&close_me_list));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001295
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001296 if (outpipe[1] != -1) {
Eric Andersen86349772000-12-18 20:25:50 +00001297 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001298 }
1299 if (nextin != 0) {
1300 dup2(nextin, 0);
1301 close(nextin);
1302 }
1303
1304 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001305 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001306 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001307 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001308 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001309 }
1310
Eric Andersen86349772000-12-18 20:25:50 +00001311 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001312 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001313
Eric Andersen86349772000-12-18 20:25:50 +00001314 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001315 }
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001316 if (outpipe[1] != -1) {
Eric Andersen86349772000-12-18 20:25:50 +00001317 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001318 }
Erik Andersen161220c2000-03-16 08:12:48 +00001319
1320 /* put our child in the process group whose leader is the
1321 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001322 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001323 if (nextin != 0)
1324 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001325 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001326 close(nextout);
1327
Eric Andersenff9ad472004-02-10 01:07:45 +00001328 /* If there isn't another process, nextin is garbage
Erik Andersen161220c2000-03-16 08:12:48 +00001329 but it doesn't matter */
1330 nextin = pipefds[0];
1331 }
1332
Eric Andersen86349772000-12-18 20:25:50 +00001333 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001334
Eric Andersen86349772000-12-18 20:25:50 +00001335 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001336
Erik Andersen161220c2000-03-16 08:12:48 +00001337 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001338}
1339
Erik Andersend75af992000-03-16 08:09:09 +00001340static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001341{
Erik Andersen161220c2000-03-16 08:12:48 +00001342 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001343 char *next_command = NULL;
1344 struct job newjob;
Erik Andersen161220c2000-03-16 08:12:48 +00001345 int i;
Eric Andersena68ea1c2006-01-30 22:48:39 +00001346 int inbg = 0;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001347 int status;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001348#if ENABLE_LASH_JOB_CONTROL
Eric Andersend20d3752004-03-12 22:08:42 +00001349 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 McGrathc45146c2004-03-05 12:55:30 +00001352#endif
Eric Andersen86349772000-12-18 20:25:50 +00001353 newjob.job_list = &job_list;
1354 newjob.job_context = DEFAULT_CONTEXT;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001355
Rob Landleyf9463792006-06-13 18:50:20 +00001356 command = xzalloc(BUFSIZ);
Erik Andersend75af992000-03-16 08:09:09 +00001357
Erik Andersen161220c2000-03-16 08:12:48 +00001358 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001359 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001360 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001361
Erik Andersend75af992000-03-16 08:09:09 +00001362 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001363 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001364
Eric Andersen86349772000-12-18 20:25:50 +00001365 if (!next_command) {
Denis Vlasenkof443bff2007-03-31 11:08:50 +00001366 if (get_command_bufsiz(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001367 break;
Eric Andersen86349772000-12-18 20:25:50 +00001368 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001369 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001370
Denis Vlasenkof443bff2007-03-31 11:08:50 +00001371 if (!expand_arguments(next_command)) {
Eric Andersenca604592001-03-08 17:17:13 +00001372 free(command);
Rob Landleyf9463792006-06-13 18:50:20 +00001373 command = xzalloc(BUFSIZ);
Eric Andersenca604592001-03-08 17:17:13 +00001374 next_command = NULL;
1375 continue;
1376 }
1377
Eric Andersen86349772000-12-18 20:25:50 +00001378 if (!parse_command(&next_command, &newjob, &inbg) &&
1379 newjob.num_progs) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001380 int pipefds[2] = { -1, -1 };
1381 debug_printf("job=%p fed to run_command by busy_loop()'\n",
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001382 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001383 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001384 }
1385 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001386 free(command);
Denis Vlasenko4cccc032006-12-22 18:37:07 +00001387 command = xzalloc(BUFSIZ);
Eric Andersen86349772000-12-18 20:25:50 +00001388 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001389 }
1390 } else {
1391 /* a job is running in the foreground; wait for it */
1392 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001393 while (!job_list.fg->progs[i].pid ||
1394 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001395
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001396 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED) < 0) {
Eric Andersen7373e482002-07-31 04:04:47 +00001397 if (errno != ECHILD) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001398 bb_perror_msg_and_die("waitpid(%d)", job_list.fg->progs[i].pid);
Eric Andersen7373e482002-07-31 04:04:47 +00001399 }
1400 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001401
Erik Andersend75af992000-03-16 08:09:09 +00001402 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001403 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001404 job_list.fg->running_progs--;
1405 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001406
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001407 last_return_code = WEXITSTATUS(status);
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001408
Eric Andersen86349772000-12-18 20:25:50 +00001409 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001410 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001411 remove_job(&job_list, job_list.fg);
1412 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001413 }
Eric Andersen37032b42004-02-10 01:28:36 +00001414 }
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001415#if ENABLE_LASH_JOB_CONTROL
Eric Andersen37032b42004-02-10 01:28:36 +00001416 else {
Erik Andersen161220c2000-03-16 08:12:48 +00001417 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001418 job_list.fg->stopped_progs++;
1419 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001420
Eric Andersen86349772000-12-18 20:25:50 +00001421 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 Andersen161220c2000-03-16 08:12:48 +00001425 }
Erik Andersend75af992000-03-16 08:09:09 +00001426 }
1427
Eric Andersen86349772000-12-18 20:25:50 +00001428 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001429 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001430 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001431 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Eric Andersenff9ad472004-02-10 01:07:45 +00001432 bb_perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001433 }
Eric Andersen37032b42004-02-10 01:28:36 +00001434#endif
Erik Andersend75af992000-03-16 08:09:09 +00001435 }
1436 }
Erik Andersen161220c2000-03-16 08:12:48 +00001437 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001438
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001439#if ENABLE_LASH_JOB_CONTROL
Eric Andersen1c314ad2000-06-28 16:56:25 +00001440 /* return controlling TTY back to parent process group before exiting */
Eric Andersen7373e482002-07-31 04:04:47 +00001441 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001442 bb_perror_msg("tcsetpgrp");
Eric Andersen37032b42004-02-10 01:28:36 +00001443#endif
Eric Andersenb54833c2000-07-03 23:56:26 +00001444
1445 /* return exit status if called with "-c" */
1446 if (input == NULL && WIFEXITED(status))
1447 return WEXITSTATUS(status);
Eric Andersenff9ad472004-02-10 01:07:45 +00001448
Erik Andersen161220c2000-03-16 08:12:48 +00001449 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001450}
1451
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001452#if ENABLE_FEATURE_CLEAN_UP
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +00001453static void free_memory(void)
Eric Andersenfad9c112000-07-25 18:06:52 +00001454{
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001455 if (cwd && cwd != bb_msg_unknown) {
Eric Andersen5d60a462001-08-22 05:32:24 +00001456 free((char*)cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001457 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001458
Eric Andersen86349772000-12-18 20:25:50 +00001459 if (job_list.fg && !job_list.fg->running_progs) {
1460 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001461 }
1462}
Rob Landleyacf448d2006-07-09 17:03:07 +00001463#else
1464void free_memory(void);
Eric Andersenfad9c112000-07-25 18:06:52 +00001465#endif
1466
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001467#if ENABLE_LASH_JOB_CONTROL
Eric Andersen2d848a42001-06-25 17:11:54 +00001468/* 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 Andersenece8fc22002-12-11 04:26:28 +00001471static void setup_job_control(void)
Eric Andersen2d848a42001-06-25 17:11:54 +00001472{
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001473 int status;
Eric Andersen37032b42004-02-10 01:28:36 +00001474 pid_t shell_pgrp;
Eric Andersenff9ad472004-02-10 01:07:45 +00001475
Eric Andersen2d848a42001-06-25 17:11:54 +00001476 /* Loop until we are in the foreground. */
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001477 while ((status = tcgetpgrp (shell_terminal)) >= 0) {
1478 if (status == (shell_pgrp = getpgrp ())) {
1479 break;
1480 }
Denis Vlasenkoff131b92007-04-10 15:42:06 +00001481 kill(- shell_pgrp, SIGTTIN);
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001482 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001483
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 Andersen4b6b5e42001-06-27 04:30:11 +00001493 setsid();
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +00001494 shell_pgrp = getpid();
Rob Landleyacf448d2006-07-09 17:03:07 +00001495 setpgid(shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001496
1497 /* Grab control of the terminal. */
1498 tcsetpgrp(shell_terminal, shell_pgrp);
1499}
Eric Andersen37032b42004-02-10 01:28:36 +00001500#else
1501static inline void setup_job_control(void)
1502{
1503}
1504#endif
Eric Andersen6efc48c2000-07-18 08:16:39 +00001505
Denis Vlasenko06af2162007-02-03 17:28:39 +00001506int lash_main(int argc_l, char **argv_l);
Matt Kraai2d91deb2001-08-01 17:21:35 +00001507int lash_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001508{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001509 unsigned opt;
Erik Andersen161220c2000-03-16 08:12:48 +00001510 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001511 argc = argc_l;
1512 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001513
Denis Vlasenko38f63192007-01-22 09:03:07 +00001514#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001515 line_input_state = new_line_input_t(FOR_SHELL);
1516#endif
1517
Eric Andersen702ec592001-03-06 22:17:29 +00001518 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001519 last_jobid = 0;
Rob Landleyf9463792006-06-13 18:50:20 +00001520 close_me_list = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001521 job_list.head = NULL;
1522 job_list.fg = NULL;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001523 last_return_code = 1;
Eric Andersen501c88b2000-07-28 15:14:45 +00001524
Eric Andersenb558e762000-11-30 22:43:16 +00001525 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001526 FILE *prof_input;
1527 prof_input = fopen("/etc/profile", "r");
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001528 if (prof_input) {
Rob Landley7a260f02006-06-19 03:20:03 +00001529 llist_add_to(&close_me_list, (void *)(long)fileno(prof_input));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001530 /* Now run the file */
1531 busy_loop(prof_input);
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00001532 fclose_if_not_stdin(prof_input);
Rob Landleyf9463792006-06-13 18:50:20 +00001533 llist_pop(&close_me_list);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001534 }
Eric Andersenb558e762000-11-30 22:43:16 +00001535 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001536
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001537 opt = getopt32(argc_l, argv_l, "+ic:", &local_pending_command);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001538#define LASH_OPT_i (1<<0)
Denis Vlasenkof443bff2007-03-31 11:08:50 +00001539#define LASH_OPT_c (1<<1)
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001540 if (opt & LASH_OPT_c) {
1541 input = NULL;
1542 optind++;
1543 argv += optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001544 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001545 /* 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 Vlasenko6fa8b8e2007-03-31 11:07:30 +00001552 if (argv[optind] == NULL && input == stdin
1553 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
1554 ) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001555 opt |= LASH_OPT_i;
Eric Andersen86349772000-12-18 20:25:50 +00001556 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001557 setup_job_control();
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001558 if (opt & LASH_OPT_i) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001559 /* Looks like they want an interactive shell */
Rob Landleyacf448d2006-07-09 17:03:07 +00001560 if (!ENABLE_FEATURE_SH_EXTRA_QUIET) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001561 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 Landleyacf448d2006-07-09 17:03:07 +00001564 }
1565 } else if (!local_pending_command && argv[optind]) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001566 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Rob Landleyd921b2e2006-08-03 15:41:12 +00001567 input = xfopen(argv[optind], "r");
Rob Landleyf9463792006-06-13 18:50:20 +00001568 /* be lazy, never mark this closed */
Rob Landley7a260f02006-06-19 03:20:03 +00001569 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Eric Andersen501c88b2000-07-28 15:14:45 +00001570 }
1571
Eric Andersen6efc48c2000-07-18 08:16:39 +00001572 /* initialize the cwd -- this is never freed...*/
Denis Vlasenko6ca04442007-02-11 16:19:28 +00001573 cwd = xrealloc_getcwd_or_warn(NULL);
Eric Andersen5f265b72001-05-11 16:58:46 +00001574 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001575 cwd = bb_msg_unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001576
Rob Landleyacf448d2006-07-09 17:03:07 +00001577 if (ENABLE_FEATURE_CLEAN_UP) atexit(free_memory);
Eric Andersenfad9c112000-07-25 18:06:52 +00001578
Denis Vlasenko38f63192007-01-22 09:03:07 +00001579 if (ENABLE_FEATURE_EDITING) cmdedit_set_initial_prompt();
Rob Landleyacf448d2006-07-09 17:03:07 +00001580 else PS1 = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +00001581
Erik Andersen161220c2000-03-16 08:12:48 +00001582 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001583}