blob: 24e48c337d61fb83c6f4c2bc4803bd3c2da19af1 [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 Vlasenko8a28e622007-04-14 11:16:29 +0000260 struct job *job;
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) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000266 goto found;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000267 }
Erik Andersen161220c2000-03-16 08:12:48 +0000268 }
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000269 bb_error_msg("%s: no current job", child->argv[0]);
270 return EXIT_FAILURE;
271 }
272 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
273 bb_error_msg(bb_msg_invalid_arg, child->argv[1], child->argv[0]);
274 return EXIT_FAILURE;
275 }
276 for (job = child->family->job_list->head; job; job = job->next) {
277 if (job->jobid == jobnum) {
278 goto found;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000279 }
Erik Andersend75af992000-03-16 08:09:09 +0000280 }
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000281 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
282 return EXIT_FAILURE;
283 found:
Eric Andersen86349772000-12-18 20:25:50 +0000284 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000285 /* Put the job into the foreground. */
286 tcsetpgrp(shell_terminal, job->pgrp);
287
Eric Andersen86349772000-12-18 20:25:50 +0000288 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000289 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000290
Erik Andersen161220c2000-03-16 08:12:48 +0000291 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000292 for (i = 0; i < job->num_progs; i++)
293 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000294
Eric Andersen86349772000-12-18 20:25:50 +0000295 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000296
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000297 i = kill(- job->pgrp, SIGCONT);
298 if (i < 0) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000299 if (errno == ESRCH) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000300 remove_job(&job_list, job);
Eric Andersen07abfe22001-06-27 17:29:11 +0000301 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000302 bb_perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000303 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000304 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000305
Matt Kraai3e856ce2000-12-01 02:55:13 +0000306 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000307}
308
309/* built-in 'help' handler */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000310static int builtin_help(struct child_prog ATTRIBUTE_UNUSED *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000311{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000312 const struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000313
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000314 printf("\nBuilt-in commands:\n"
Denis Vlasenko831a20f2007-04-12 12:27:32 +0000315 "-------------------\n");
Denis Vlasenkob69f35e2007-04-12 17:10:45 +0000316 for (x = bltins; x <= &VEC_LAST(bltins); x++) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000317 if (x->descr == NULL)
Eric Andersenfad9c112000-07-25 18:06:52 +0000318 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000319 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000320 }
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000321 putchar('\n');
Matt Kraai3e856ce2000-12-01 02:55:13 +0000322 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000323}
324
325/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000326static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000327{
Erik Andersen161220c2000-03-16 08:12:48 +0000328 struct job *job;
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000329 const char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000330
Eric Andersen86349772000-12-18 20:25:50 +0000331 for (job = child->family->job_list->head; job; job = job->next) {
332 if (job->running_progs == job->stopped_progs)
333 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000334 else
Eric Andersen86349772000-12-18 20:25:50 +0000335 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000336
Eric Andersen86349772000-12-18 20:25:50 +0000337 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000338 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000339 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000340}
341
342
343/* built-in 'pwd' handler */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000344static int builtin_pwd(struct child_prog ATTRIBUTE_UNUSED *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000345{
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000346 update_cwd();
Matt Kraai59df6f72001-05-16 14:21:09 +0000347 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000348 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000349}
350
Erik Andersen6273f652000-03-17 01:12:41 +0000351/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000352static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000353{
Erik Andersen161220c2000-03-16 08:12:48 +0000354 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000355 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000356
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000357 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000358 char **e;
359 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000360 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000361 }
Matt Kraai2129f972001-04-04 17:50:04 +0000362 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000363 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000364 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000365 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000366 bb_perror_msg("export");
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000367#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
368 if (strncmp(v, "PS1=", 4) == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000369 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000370#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000371
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000372#if ENABLE_LOCALE_SUPPORT
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000373 // TODO: why getenv? "" would be just as good...
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000374 if (strncmp(v, "LC_ALL=", 7) == 0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000375 setlocale(LC_ALL, getenv("LC_ALL"));
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000376 if (strncmp(v, "LC_CTYPE=", 9) == 0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000377 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000378#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000379
Denis Vlasenko097c3242006-11-27 16:59:15 +0000380 return res;
Erik Andersen3522eb12000-03-12 23:49:18 +0000381}
382
Eric Andersenb54833c2000-07-03 23:56:26 +0000383/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000384static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000385{
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000386 int res = 0, len;
Eric Andersenb54833c2000-07-03 23:56:26 +0000387 char *s;
388 char string[MAX_READ];
389
Eric Andersen86349772000-12-18 20:25:50 +0000390 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000391 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen0d2d1eb2002-06-06 13:33:01 +0000392 safe_strncpy(string, child->argv[1], MAX_READ-1);
Eric Andersenb54833c2000-07-03 23:56:26 +0000393 len = strlen(string);
394 string[len++] = '=';
395 string[len] = '\0';
396 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000397 res = strlen(string);
398 if (res > len)
399 string[--res] = '\0'; /* chomp trailing newline */
Eric Andersenb54833c2000-07-03 23:56:26 +0000400 /*
401 ** string should now contain "VAR=<value>"
402 ** copy it (putenv() won't do that, so we must make sure
403 ** the string resides in a static buffer!)
404 */
405 res = -1;
Denis Vlasenko831a20f2007-04-12 12:27:32 +0000406 s = strdup(string);
407 if (s)
Eric Andersenb54833c2000-07-03 23:56:26 +0000408 res = putenv(s);
409 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000410 bb_perror_msg("read");
Denis Vlasenko831a20f2007-04-12 12:27:32 +0000411 } else
Eric Andersenb54833c2000-07-03 23:56:26 +0000412 fgets(string, sizeof(string), stdin);
413
Denis Vlasenko097c3242006-11-27 16:59:15 +0000414 return res;
Eric Andersenb54833c2000-07-03 23:56:26 +0000415}
416
Erik Andersen3522eb12000-03-12 23:49:18 +0000417/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000418static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000419{
Erik Andersen161220c2000-03-16 08:12:48 +0000420 FILE *input;
421 int status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000422
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000423 input = fopen_or_warn(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000424 if (!input) {
Matt Kraai3e856ce2000-12-01 02:55:13 +0000425 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000426 }
Erik Andersend75af992000-03-16 08:09:09 +0000427
Rob Landley7a260f02006-06-19 03:20:03 +0000428 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Erik Andersen161220c2000-03-16 08:12:48 +0000429 /* Now run the file */
430 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000431 fclose(input);
Rob Landleyf9463792006-06-13 18:50:20 +0000432 llist_pop(&close_me_list);
Denis Vlasenko097c3242006-11-27 16:59:15 +0000433 return status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000434}
435
436/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000437static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000438{
Eric Andersen86349772000-12-18 20:25:50 +0000439 if (child->argv[1] == NULL) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000440 printf(bb_msg_requires_arg, "unset");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000441 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000442 }
Eric Andersen86349772000-12-18 20:25:50 +0000443 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000444 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000445}
446
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000447#if ENABLE_LASH_JOB_CONTROL
Erik Andersen3522eb12000-03-12 23:49:18 +0000448/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000449static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000450{
Erik Andersen161220c2000-03-16 08:12:48 +0000451 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000452 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000453
Eric Andersen86349772000-12-18 20:25:50 +0000454 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000455 free(cmd->progs[i].argv);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000456#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000457 if (cmd->progs[i].redirects)
458 free(cmd->progs[i].redirects);
Eric Andersenff9ad472004-02-10 01:07:45 +0000459#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000460 }
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000461 free(cmd->progs);
462 free(cmd->text);
463 free(cmd->cmdbuf);
Mark Whitley44a99142001-03-14 17:26:37 +0000464 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000465 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000466 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000467}
468
Eric Andersen1ca20a72001-03-21 07:34:27 +0000469/* remove a job from a jobset */
470static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000471{
Eric Andersen86349772000-12-18 20:25:50 +0000472 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000473
Eric Andersen86349772000-12-18 20:25:50 +0000474 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000475 if (job == j_list->head) {
476 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000477 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000478 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000479 while (prevjob->next != job)
480 prevjob = prevjob->next;
481 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000482 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000483
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000484 if (j_list->head)
485 last_jobid = j_list->head->jobid;
486 else
487 last_jobid = 0;
488
Erik Andersen161220c2000-03-16 08:12:48 +0000489 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000490}
491
Eric Andersenff9ad472004-02-10 01:07:45 +0000492/* Checks to see if any background processes have exited -- if they
Erik Andersen3522eb12000-03-12 23:49:18 +0000493 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000494static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000495{
Erik Andersen161220c2000-03-16 08:12:48 +0000496 struct job *job;
497 pid_t childpid;
498 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000499 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000500
Erik Andersen161220c2000-03-16 08:12:48 +0000501 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000502 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000503 prognum = 0;
504 while (prognum < job->num_progs &&
505 job->progs[prognum].pid != childpid) prognum++;
506 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000507 break;
508 }
509
Eric Andersena1d187a2000-07-17 19:14:41 +0000510 /* This happens on backticked commands */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000511 if (job == NULL)
Eric Andersena1d187a2000-07-17 19:14:41 +0000512 return;
513
Erik Andersen161220c2000-03-16 08:12:48 +0000514 if (WIFEXITED(status) || WIFSIGNALED(status)) {
515 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000516 job->running_progs--;
517 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000518
Eric Andersen86349772000-12-18 20:25:50 +0000519 if (!job->running_progs) {
520 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000521 last_jobid = 0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000522 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000523 }
524 } else {
525 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000526 job->stopped_progs++;
527 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000528 }
Erik Andersend75af992000-03-16 08:09:09 +0000529 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000530
Erik Andersen161220c2000-03-16 08:12:48 +0000531 if (childpid == -1 && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000532 bb_perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000533}
Eric Andersen37032b42004-02-10 01:28:36 +0000534#else
535static void checkjobs(struct jobset *j_list)
536{
537}
538static void free_job(struct job *cmd)
539{
540}
541static void remove_job(struct jobset *j_list, struct job *job)
542{
543}
544#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000545
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000546#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersene9f07fb2000-12-21 18:31:36 +0000547/* squirrel != NULL means we squirrel away copies of stdin, stdout,
548 * and stderr if they are redirected. */
549static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000550{
551 int i;
552 int openfd;
553 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000554 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000555
Eric Andersen86349772000-12-18 20:25:50 +0000556 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000557 switch (redir->type) {
558 case REDIRECT_INPUT:
559 mode = O_RDONLY;
560 break;
561 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000562 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000563 break;
564 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000565 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000566 break;
567 }
568
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000569 openfd = open3_or_warn(redir->filename, mode, 0666);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000570 if (openfd < 0) {
571 /* this could get lost if stderr has been redirected, but
572 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000573 return 1;
574 }
575
576 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000577 if (squirrel && redir->fd < 3) {
578 squirrel[redir->fd] = dup(redir->fd);
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000579 fcntl(squirrel[redir->fd], F_SETFD, FD_CLOEXEC);
Eric Andersene9f07fb2000-12-21 18:31:36 +0000580 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000581 dup2(openfd, redir->fd);
582 close(openfd);
583 }
584 }
585
586 return 0;
587}
588
Eric Andersene9f07fb2000-12-21 18:31:36 +0000589static void restore_redirects(int squirrel[])
590{
591 int i, fd;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000592 for (i = 0; i < 3; i++) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000593 fd = squirrel[i];
594 if (fd != -1) {
595 /* No error checking. I sure wouldn't know what
596 * to do with an error if I found one! */
597 dup2(fd, i);
598 close(fd);
599 }
600 }
601}
Eric Andersenff9ad472004-02-10 01:07:45 +0000602#else
603static inline int setup_redirects(struct child_prog *prog, int squirrel[])
604{
605 return 0;
606}
607static inline void restore_redirects(int squirrel[])
608{
609}
610#endif
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000611
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000612static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000613{
Denis Vlasenko38f63192007-01-22 09:03:07 +0000614#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000615 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000616#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000617 PS1 = getenv("PS1");
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000618 if (PS1 == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000619 PS1 = "\\w \\$ ";
Eric Andersenff9ad472004-02-10 01:07:45 +0000620#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000621}
622
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000623static inline const char* setup_prompt_string(void)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000624{
Denis Vlasenko38f63192007-01-22 09:03:07 +0000625#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000626 /* Set up the prompt */
627 if (shell_context == 0) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000628 char *ns;
629 free((char*)PS1);
630 ns = xmalloc(strlen(cwd)+4);
631 sprintf(ns, "%s %c ", cwd, (geteuid() != 0) ? '$': '#');
632 PS1 = ns;
633 return ns;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000634 } else {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000635 return PS2;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000636 }
637#else
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000638 return (shell_context == 0)? PS1 : PS2;
Eric Andersenff9ad472004-02-10 01:07:45 +0000639#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000640}
Eric Andersen22332fd2001-01-30 23:40:39 +0000641
Denis Vlasenko38f63192007-01-22 09:03:07 +0000642#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000643static line_input_t *line_input_state;
644#endif
645
Denis Vlasenkof443bff2007-03-31 11:08:50 +0000646static int get_command_bufsiz(FILE * source, char *command)
Eric Andersen09acc062001-01-04 11:10:38 +0000647{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000648 const char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000649
Eric Andersen1c314ad2000-06-28 16:56:25 +0000650 if (source == NULL) {
651 if (local_pending_command) {
652 /* a command specified (-c option): return it & mark it done */
Denis Vlasenkof443bff2007-03-31 11:08:50 +0000653 strncpy(command, local_pending_command, BUFSIZ);
Eric Andersen1c314ad2000-06-28 16:56:25 +0000654 local_pending_command = NULL;
655 return 0;
656 }
657 return 1;
658 }
659
Erik Andersen161220c2000-03-16 08:12:48 +0000660 if (source == stdin) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000661 prompt_str = setup_prompt_string();
Eric Andersen8ea28be2001-01-05 20:58:22 +0000662
Denis Vlasenko38f63192007-01-22 09:03:07 +0000663#if ENABLE_FEATURE_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000664 /*
665 ** enable command line editing only while a command line
666 ** is actually being read; otherwise, we'll end up bequeathing
667 ** atexit() handlers and other unwanted stuff to our
668 ** child processes (rob@sysgo.de)
669 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000670 read_line_input(prompt_str, command, BUFSIZ, line_input_state);
Erik Andersen161220c2000-03-16 08:12:48 +0000671 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000672#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000673 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000674#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000675 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000676
Erik Andersen161220c2000-03-16 08:12:48 +0000677 if (!fgets(command, BUFSIZ - 2, source)) {
678 if (source == stdin)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000679 puts("");
Erik Andersen161220c2000-03-16 08:12:48 +0000680 return 1;
681 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000682
Erik Andersen161220c2000-03-16 08:12:48 +0000683 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000684}
685
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000686static char * strsep_space(char *string, int * ix)
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000687{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000688 /* Short circuit the trivial case */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000689 if (!string || ! string[*ix])
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000690 return NULL;
691
692 /* Find the end of the token. */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000693 while (string[*ix] && !isspace(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000694 (*ix)++;
695 }
696
Eric Andersenff9ad472004-02-10 01:07:45 +0000697 /* Find the end of any whitespace trailing behind
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000698 * the token and let that be part 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
Rob Landleyf9463792006-06-13 18:50:20 +0000703 if (!*ix) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000704 /* Nothing useful was found */
705 return NULL;
706 }
707
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000708 return xstrndup(string, *ix);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000709}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000710
Eric Andersenca604592001-03-08 17:17:13 +0000711static int expand_arguments(char *command)
712{
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000713 static const char out_of_space[] = "out of space during expansion";
714
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000715 int total_length = 0, length, i, retval, ix = 0;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000716 expand_t expand_result;
717 char *tmpcmd, *cmd, *cmd_copy;
718 char *src, *dst, *var;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000719 int flags = GLOB_NOCHECK
720#ifdef GLOB_BRACE
721 | GLOB_BRACE
Eric Andersenff9ad472004-02-10 01:07:45 +0000722#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000723#ifdef GLOB_TILDE
724 | GLOB_TILDE
Eric Andersenff9ad472004-02-10 01:07:45 +0000725#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000726 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000727
Eric Andersenca604592001-03-08 17:17:13 +0000728 /* get rid of the terminating \n */
729 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000730
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000731 /* Fix up escape sequences to be the Real Thing(tm) */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000732 while (command && command[ix]) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000733 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000734 const char *tmp = command+ix+1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000735 command[ix] = bb_process_escape_sequence( &tmp );
Eric Andersen1ca20a72001-03-21 07:34:27 +0000736 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000737 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000738 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000739 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000740 /* Use glob and then fixup environment variables and such */
741
742 /* It turns out that glob is very stupid. We have to feed it one word at a
743 * time since it can't cope with a full string. Here we convert command
744 * (char*) into cmd (char**, one word per string) */
745
746 /* We need a clean copy, so strsep can mess up the copy while
747 * we write stuff into the original (in a minute) */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000748 cmd = cmd_copy = xstrdup(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000749 *command = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000750 for (ix = 0, tmpcmd = cmd;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000751 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix = 0) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000752 if (*tmpcmd == '\0')
753 break;
754 /* we need to trim() the result for glob! */
755 trim(tmpcmd);
756 retval = glob(tmpcmd, flags, NULL, &expand_result);
757 free(tmpcmd); /* Free mem allocated by strsep_space */
758 if (retval == GLOB_NOSPACE) {
759 /* Mem may have been allocated... */
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000760 globfree(&expand_result);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000761 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000762 return FALSE;
763 } else if (retval != 0) {
764 /* Some other error. GLOB_NOMATCH shouldn't
Eric Andersenff9ad472004-02-10 01:07:45 +0000765 * happen because of the GLOB_NOCHECK flag in
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000766 * the glob call. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000767 bb_error_msg("syntax error");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000768 return FALSE;
769 } else {
770 /* Convert from char** (one word per string) to a simple char*,
771 * but don't overflow command which is BUFSIZ in length */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000772 for (i = 0; i < expand_result.gl_pathc; i++) {
773 length = strlen(expand_result.gl_pathv[i]);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000774 if (total_length+length+1 >= BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000775 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000776 return FALSE;
777 }
778 strcat(command+total_length, " ");
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000779 total_length += 1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000780 strcat(command+total_length, expand_result.gl_pathv[i]);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000781 total_length += length;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000782 }
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000783 globfree(&expand_result);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000784 }
785 }
786 free(cmd_copy);
787 trim(command);
788
Eric Andersenff9ad472004-02-10 01:07:45 +0000789 /* Now do the shell variable substitutions which
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000790 * wordexp can't do for us, namely $? and $! */
791 src = command;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000792 while ((dst = strchr(src,'$')) != NULL) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000793 var = NULL;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000794 switch (*(dst+1)) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000795 case '?':
796 var = itoa(last_return_code);
797 break;
798 case '!':
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000799 if (last_bg_pid == -1)
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000800 *var = '\0';
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000801 else
802 var = itoa(last_bg_pid);
803 break;
804 /* Everything else like $$, $#, $[0-9], etc. should all be
805 * expanded by wordexp(), so we can in theory skip that stuff
806 * here, but just to be on the safe side (i.e., since uClibc
807 * wordexp doesn't do this stuff yet), lets leave it in for
808 * now. */
809 case '$':
810 var = itoa(getpid());
811 break;
812 case '#':
813 var = itoa(argc-1);
814 break;
815 case '0':case '1':case '2':case '3':case '4':
816 case '5':case '6':case '7':case '8':case '9':
817 {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000818 int ixx = *(dst+1)-48+1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000819 if (ixx >= argc) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000820 var = '\0';
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000821 } else {
822 var = argv[ixx];
823 }
824 }
825 break;
826
827 }
828 if (var) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000829 /* a single character construction was found, and
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000830 * already handled in the case statement */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000831 src = dst + 2;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000832 } else {
833 /* Looks like an environment variable */
834 char delim_hold;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000835 int num_skip_chars = 0;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000836 int dstlen = strlen(dst);
837 /* Is this a ${foo} type variable? */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000838 if (dstlen >= 2 && *(dst+1) == '{') {
839 src = strchr(dst+1, '}');
840 num_skip_chars = 1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000841 } else {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000842 src = dst + 1;
843 while ((isalnum)(*src) || *src == '_') src++;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000844 }
845 if (src == NULL) {
846 src = dst+dstlen;
847 }
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000848 delim_hold = *src;
849 *src = '\0'; /* temporary */
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000850 var = getenv(dst + 1 + num_skip_chars);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000851 *src = delim_hold;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000852 src += num_skip_chars;
853 }
854 if (var == NULL) {
855 /* Seems we got an un-expandable variable. So delete it. */
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000856 var = (char*)"";
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000857 }
858 {
859 int subst_len = strlen(var);
860 int trail_len = strlen(src);
861 if (dst+subst_len+trail_len >= command+BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000862 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000863 return FALSE;
864 }
865 /* Move stuff to the end of the string to accommodate
866 * filling the created gap with the new stuff */
867 memmove(dst+subst_len, src, trail_len+1);
868 /* Now copy in the new stuff */
869 memcpy(dst, var, subst_len);
870 src = dst+subst_len;
871 }
872 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000873
Eric Andersenca604592001-03-08 17:17:13 +0000874 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000875}
876
Eric Andersen86349772000-12-18 20:25:50 +0000877/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
878 line). If a valid command is found, command_ptr is set to point to
Eric Andersenff9ad472004-02-10 01:07:45 +0000879 the beginning of the next command (if the original command had more
880 then one job associated with it) or NULL if no more commands are
Erik Andersen3522eb12000-03-12 23:49:18 +0000881 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000882static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000883{
Erik Andersen161220c2000-03-16 08:12:48 +0000884 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000885 char *return_command = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +0000886 char *src, *buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000887 int argc_l;
888 int flag;
Eric Andersen86349772000-12-18 20:25:50 +0000889 int argv_alloced;
Erik Andersen161220c2000-03-16 08:12:48 +0000890 char quote = '\0';
Eric Andersen86349772000-12-18 20:25:50 +0000891 struct child_prog *prog;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000892#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersenff9ad472004-02-10 01:07:45 +0000893 int i;
894 char *chptr;
895#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000896
Erik Andersen161220c2000-03-16 08:12:48 +0000897 /* skip leading white space */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000898 *command_ptr = skip_whitespace(*command_ptr);
Erik Andersen3522eb12000-03-12 23:49:18 +0000899
Erik Andersen161220c2000-03-16 08:12:48 +0000900 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000901 if (!**command_ptr || (**command_ptr == '#')) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000902 job->num_progs = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000903 return 0;
904 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000905
Eric Andersen86349772000-12-18 20:25:50 +0000906 *inbg = 0;
907 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000908 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000909
Eric Andersenff9ad472004-02-10 01:07:45 +0000910 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000911 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000912 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000913
Eric Andersenff9ad472004-02-10 01:07:45 +0000914 Getting clean memory relieves us of the task of NULL
915 terminating things and makes the rest of this look a bit
Erik Andersen161220c2000-03-16 08:12:48 +0000916 cleaner (though it is, admittedly, a tad less efficient) */
Rob Landleyf9463792006-06-13 18:50:20 +0000917 job->cmdbuf = command = xzalloc(2*strlen(*command_ptr) + 1);
Erik Andersen161220c2000-03-16 08:12:48 +0000918 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000919
Erik Andersen161220c2000-03-16 08:12:48 +0000920 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000921 prog->num_redirects = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000922 prog->is_stopped = 0;
923 prog->family = job;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000924#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersenff9ad472004-02-10 01:07:45 +0000925 prog->redirects = NULL;
926#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000927
Eric Andersen86349772000-12-18 20:25:50 +0000928 argv_alloced = 5;
929 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
930 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +0000931
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000932 flag = argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000933 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +0000934 src = *command_ptr;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000935 while (*src && !(flag & LASH_OPT_DONE)) {
Erik Andersen161220c2000-03-16 08:12:48 +0000936 if (quote == *src) {
937 quote = '\0';
938 } else if (quote) {
939 if (*src == '\\') {
940 src++;
941 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000942 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +0000943 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +0000944 return 1;
945 }
946
947 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +0000948 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +0000949 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +0000950 *buf++ = '\\';
951 }
Erik Andersen161220c2000-03-16 08:12:48 +0000952 } else if (*src == '*' || *src == '?' || *src == '[' ||
953 *src == ']') *buf++ = '\\';
954 *buf++ = *src;
955 } else if (isspace(*src)) {
Denis Vlasenkob69f35e2007-04-12 17:10:45 +0000956 if (*prog->argv[argc_l] || (flag & LASH_OPT_SAW_QUOTE)) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000957 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +0000958 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +0000959 if ((argc_l + 1) == argv_alloced) {
960 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +0000961 prog->argv = xrealloc(prog->argv,
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000962 sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +0000963 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000964 prog->argv[argc_l] = buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000965 flag ^= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000966 }
967 } else
968 switch (*src) {
969 case '"':
970 case '\'':
971 quote = *src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000972 flag |= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000973 break;
974
975 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000976 if (*(src-1)== '$')
977 *buf++ = *src;
978 else
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000979 flag |= LASH_OPT_DONE;
Erik Andersen161220c2000-03-16 08:12:48 +0000980 break;
981
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000982#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000983 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +0000984 case '<':
Eric Andersen86349772000-12-18 20:25:50 +0000985 i = prog->num_redirects++;
986 prog->redirects = xrealloc(prog->redirects,
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000987 sizeof(*prog->redirects) * (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +0000988
Eric Andersen86349772000-12-18 20:25:50 +0000989 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000990 if (buf != prog->argv[argc_l]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000991 /* the stuff before this character may be the file number
Erik Andersen161220c2000-03-16 08:12:48 +0000992 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +0000993 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000994 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +0000995
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000996 if (*chptr && *prog->argv[argc_l]) {
997 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000998 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +0000999 }
1000 }
1001
Eric Andersen86349772000-12-18 20:25:50 +00001002 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001003 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001004 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001005 else
Eric Andersen86349772000-12-18 20:25:50 +00001006 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001007 }
1008
1009 if (*src++ == '>') {
1010 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001011 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001012 REDIRECT_APPEND, src++;
1013 else
Eric Andersen86349772000-12-18 20:25:50 +00001014 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001015 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001016 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001017 }
1018
1019 /* This isn't POSIX sh compliant. Oh well. */
1020 chptr = src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001021 chptr = skip_whitespace(chptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001022
1023 if (!*chptr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001024 bb_error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001025 free_job(job);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001026 job->num_progs = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001027 return 1;
1028 }
1029
Eric Andersen86349772000-12-18 20:25:50 +00001030 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001031 while (*chptr && !isspace(*chptr))
1032 *buf++ = *chptr++;
1033
1034 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001035 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001036 break;
1037
1038 case '|': /* pipe */
1039 /* finish this command */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001040 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001041 argc_l++;
1042 if (!argc_l) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001043 goto empty_command_in_pipe;
Erik Andersen161220c2000-03-16 08:12:48 +00001044 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001045 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001046
1047 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001048 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001049 job->progs = xrealloc(job->progs,
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001050 sizeof(*job->progs) * job->num_progs);
Eric Andersen86349772000-12-18 20:25:50 +00001051 prog = job->progs + (job->num_progs - 1);
1052 prog->num_redirects = 0;
1053 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001054 prog->is_stopped = 0;
1055 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001056 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001057
Eric Andersen86349772000-12-18 20:25:50 +00001058 argv_alloced = 5;
1059 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001060 prog->argv[0] = ++buf;
1061
1062 src++;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001063 src = skip_whitespace(src);
Erik Andersen161220c2000-03-16 08:12:48 +00001064
1065 if (!*src) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001066empty_command_in_pipe:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001067 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001068 free_job(job);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001069 job->num_progs = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001070 return 1;
1071 }
1072 src--; /* we'll ++ it at the end of the loop */
1073
1074 break;
Eric Andersenff9ad472004-02-10 01:07:45 +00001075#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001076
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001077#if ENABLE_LASH_JOB_CONTROL
Erik Andersen161220c2000-03-16 08:12:48 +00001078 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001079 *inbg = 1;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001080 /* fallthrough */
Eric Andersen37032b42004-02-10 01:28:36 +00001081#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001082 case ';': /* multiple commands */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001083 flag |= LASH_OPT_DONE;
Eric Andersen86349772000-12-18 20:25:50 +00001084 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001085 break;
1086
Matt Kraai131241f2000-09-14 00:43:20 +00001087 case '\\':
1088 src++;
1089 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001090 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001091 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001092 return 1;
1093 }
1094 if (*src == '*' || *src == '[' || *src == ']'
1095 || *src == '?') *buf++ = '\\';
1096 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001097 default:
1098 *buf++ = *src;
1099 }
1100
Erik Andersend75af992000-03-16 08:09:09 +00001101 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001102 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001103
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001104 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001105 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001106 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001107 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001108 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001109 return 0;
1110 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001111 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001112
Eric Andersen86349772000-12-18 20:25:50 +00001113 if (!return_command) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001114 job->text = xstrdup(*command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001115 } else {
1116 /* This leaves any trailing spaces, which is a bit sloppy */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001117 job->text = xstrndup(*command_ptr, return_command - *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001118 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001119
Eric Andersen86349772000-12-18 20:25:50 +00001120 *command_ptr = return_command;
Eric Andersenff9ad472004-02-10 01:07:45 +00001121
Erik Andersend75af992000-03-16 08:09:09 +00001122 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001123}
1124
Eric Andersen86349772000-12-18 20:25:50 +00001125/* Run the child_prog, no matter what kind of command it uses.
1126 */
1127static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001128{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +00001129 const struct built_in_command *x;
Rob Landley68286772006-06-13 19:01:30 +00001130
Eric Andersene9f07fb2000-12-21 18:31:36 +00001131 /* Check if the command matches any of the non-forking builtins.
1132 * Depending on context, this might be redundant. But it's
1133 * easier to waste a few CPU cycles than it is to figure out
1134 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001135 */
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001136 /* Check if the command matches any of the forking builtins. */
1137 for (x = bltins; x <= &VEC_LAST(bltins); x++) {
Denis Vlasenko219d14d2007-03-24 15:40:16 +00001138 if (strcmp(child->argv[0], x->cmd) == 0) {
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001139 _exit(x->function(child));
Eric Andersene9f07fb2000-12-21 18:31:36 +00001140 }
1141 }
1142
Rob Landley68286772006-06-13 19:01:30 +00001143
Eric Andersen86349772000-12-18 20:25:50 +00001144 /* Check if the command matches any busybox internal
1145 * commands ("applets") here. Following discussions from
Eric Andersen2423b122001-12-08 01:56:15 +00001146 * November 2000 on busybox@busybox.net, don't use
Manuel Novoa III cad53642003-03-19 09:13:01 +00001147 * bb_get_last_path_component(). This way explicit (with
Eric Andersen86349772000-12-18 20:25:50 +00001148 * slashes) filenames will never be interpreted as an
1149 * applet, just like with builtins. This way the user can
1150 * override an applet with an explicit filename reference.
1151 * The only downside to this change is that an explicit
1152 * /bin/foo invocation will fork and exec /bin/foo, even if
1153 * /bin/foo is a symlink to busybox.
1154 */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001155 if (ENABLE_FEATURE_SH_STANDALONE) {
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001156 run_applet_and_exit(child->argv[0], child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001157 }
Eric Andersen86349772000-12-18 20:25:50 +00001158
1159 execvp(child->argv[0], child->argv);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001160
Eric Andersenff9ad472004-02-10 01:07:45 +00001161 /* Do not use bb_perror_msg_and_die() here, since we must not
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001162 * call exit() but should call _exit() instead */
Rob Landley5483de12006-06-20 21:35:26 +00001163 bb_perror_msg("%s", child->argv[0]);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001164 _exit(EXIT_FAILURE);
Eric Andersen86349772000-12-18 20:25:50 +00001165}
1166
1167static void insert_job(struct job *newjob, int inbg)
1168{
1169 struct job *thejob;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001170 struct jobset *j_list = newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001171
1172 /* find the ID for thejob to use */
1173 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001174 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001175 if (thejob->jobid >= newjob->jobid)
1176 newjob->jobid = thejob->jobid + 1;
1177
1178 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001179 if (!j_list->head) {
1180 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001181 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001182 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001183 thejob->next = xmalloc(sizeof(*thejob));
1184 thejob = thejob->next;
1185 }
1186
1187 *thejob = *newjob; /* physically copy the struct job */
1188 thejob->next = NULL;
1189 thejob->running_progs = thejob->num_progs;
1190 thejob->stopped_progs = 0;
1191
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001192#if ENABLE_LASH_JOB_CONTROL
Eric Andersen86349772000-12-18 20:25:50 +00001193 if (inbg) {
Eric Andersenff9ad472004-02-10 01:07:45 +00001194 /* we don't wait for background thejobs to return -- append it
Eric Andersen86349772000-12-18 20:25:50 +00001195 to the list of backgrounded thejobs and leave it alone */
1196 printf("[%d] %d\n", thejob->jobid,
1197 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001198 last_jobid = newjob->jobid;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001199 last_bg_pid = newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001200 } else {
1201 newjob->job_list->fg = thejob;
1202
1203 /* move the new process group into the foreground */
Rob Landleyf9463792006-06-13 18:50:20 +00001204 /* Ignore errors since child could have already exited */
1205 tcsetpgrp(shell_terminal, newjob->pgrp);
Eric Andersen86349772000-12-18 20:25:50 +00001206 }
Eric Andersen37032b42004-02-10 01:28:36 +00001207#endif
Eric Andersen86349772000-12-18 20:25:50 +00001208}
1209
1210static int run_command(struct job *newjob, int inbg, int outpipe[2])
1211{
1212 /* struct job *thejob; */
1213 int i;
1214 int nextin, nextout;
1215 int pipefds[2]; /* pipefd[0] is for reading */
Denis Vlasenko71d8abf2007-01-30 22:44:07 +00001216 const struct built_in_command *x;
Eric Andersen86349772000-12-18 20:25:50 +00001217 struct child_prog *child;
1218
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001219 nextin = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001220 for (i = 0; i < newjob->num_progs; i++) {
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001221 child = &(newjob->progs[i]);
Eric Andersen86349772000-12-18 20:25:50 +00001222
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001223 nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001224 if ((i + 1) < newjob->num_progs) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001225 if (pipe(pipefds) < 0)
1226 bb_perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001227 nextout = pipefds[1];
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001228 } else if (outpipe[1] != -1) {
1229 nextout = outpipe[1];
Erik Andersen161220c2000-03-16 08:12:48 +00001230 }
1231
Eric Andersene9f07fb2000-12-21 18:31:36 +00001232 /* Check if the command matches any non-forking builtins,
1233 * but only if this is a simple command.
1234 * Non-forking builtins within pipes have to fork anyway,
1235 * and are handled in pseudo_exec. "echo foo | read bar"
1236 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001237 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001238 if (newjob->num_progs == 1) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +00001239 int rcode;
1240 int squirrel[] = {-1, -1, -1};
1241
Rob Landley7a8e4912006-07-01 17:19:02 +00001242 /* Check if the command sets an environment variable. */
1243 if (strchr(child->argv[0], '=') != NULL) {
1244 child->argv[1] = child->argv[0];
1245 return builtin_export(child);
1246 }
1247
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001248 for (x = bltins; x <= &VEC_LAST(bltins); x++) {
Denis Vlasenko219d14d2007-03-24 15:40:16 +00001249 if (strcmp(child->argv[0], x->cmd) == 0) {
Eric Andersene9f07fb2000-12-21 18:31:36 +00001250 setup_redirects(child, squirrel);
1251 rcode = x->function(child);
1252 restore_redirects(squirrel);
1253 return rcode;
1254 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001255 }
Denis Vlasenko8a28e622007-04-14 11:16:29 +00001256#if ENABLE_FEATURE_SH_STANDALONE
1257 {
1258 const struct bb_applet *a = find_applet_by_name(child->argv[i]);
1259 if (a && a->nofork) {
1260 setup_redirects(child, squirrel);
1261 rcode = run_nofork_applet(a, child->argv + i);
1262 restore_redirects(squirrel);
1263 return rcode;
1264 }
1265 }
1266#endif
Erik Andersen330fd2b2000-05-19 05:35:19 +00001267 }
1268
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001269#if BB_MMU
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001270 child->pid = fork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001271#else
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001272 child->pid = vfork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001273#endif
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001274 if (!child->pid) {
Eric Andersen2d848a42001-06-25 17:11:54 +00001275 /* Set the handling for job control signals back to the default. */
1276 signal(SIGINT, SIG_DFL);
1277 signal(SIGQUIT, SIG_DFL);
1278 signal(SIGTSTP, SIG_DFL);
1279 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001280 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001281 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001282
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001283 /* Close all open filehandles. */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001284 while (close_me_list)
1285 close((long)llist_pop(&close_me_list));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001286
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001287 if (outpipe[1] != -1) {
Eric Andersen86349772000-12-18 20:25:50 +00001288 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001289 }
1290 if (nextin != 0) {
1291 dup2(nextin, 0);
1292 close(nextin);
1293 }
1294
1295 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001296 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001297 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001298 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001299 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001300 }
1301
Eric Andersen86349772000-12-18 20:25:50 +00001302 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001303 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001304
Eric Andersen86349772000-12-18 20:25:50 +00001305 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001306 }
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001307 if (outpipe[1] != -1) {
Eric Andersen86349772000-12-18 20:25:50 +00001308 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001309 }
Erik Andersen161220c2000-03-16 08:12:48 +00001310
1311 /* put our child in the process group whose leader is the
1312 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001313 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001314 if (nextin != 0)
1315 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001316 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001317 close(nextout);
1318
Eric Andersenff9ad472004-02-10 01:07:45 +00001319 /* If there isn't another process, nextin is garbage
Erik Andersen161220c2000-03-16 08:12:48 +00001320 but it doesn't matter */
1321 nextin = pipefds[0];
1322 }
1323
Eric Andersen86349772000-12-18 20:25:50 +00001324 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001325
Eric Andersen86349772000-12-18 20:25:50 +00001326 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001327
Erik Andersen161220c2000-03-16 08:12:48 +00001328 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001329}
1330
Erik Andersend75af992000-03-16 08:09:09 +00001331static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001332{
Erik Andersen161220c2000-03-16 08:12:48 +00001333 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001334 char *next_command = NULL;
1335 struct job newjob;
Erik Andersen161220c2000-03-16 08:12:48 +00001336 int i;
Eric Andersena68ea1c2006-01-30 22:48:39 +00001337 int inbg = 0;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001338 int status;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001339#if ENABLE_LASH_JOB_CONTROL
Eric Andersend20d3752004-03-12 22:08:42 +00001340 pid_t parent_pgrp;
1341 /* save current owner of TTY so we can restore it on exit */
1342 parent_pgrp = tcgetpgrp(shell_terminal);
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001343#endif
Eric Andersen86349772000-12-18 20:25:50 +00001344 newjob.job_list = &job_list;
1345 newjob.job_context = DEFAULT_CONTEXT;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001346
Rob Landleyf9463792006-06-13 18:50:20 +00001347 command = xzalloc(BUFSIZ);
Erik Andersend75af992000-03-16 08:09:09 +00001348
Erik Andersen161220c2000-03-16 08:12:48 +00001349 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001350 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001351 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001352
Erik Andersend75af992000-03-16 08:09:09 +00001353 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001354 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001355
Eric Andersen86349772000-12-18 20:25:50 +00001356 if (!next_command) {
Denis Vlasenkof443bff2007-03-31 11:08:50 +00001357 if (get_command_bufsiz(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001358 break;
Eric Andersen86349772000-12-18 20:25:50 +00001359 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001360 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001361
Denis Vlasenkof443bff2007-03-31 11:08:50 +00001362 if (!expand_arguments(next_command)) {
Eric Andersenca604592001-03-08 17:17:13 +00001363 free(command);
Rob Landleyf9463792006-06-13 18:50:20 +00001364 command = xzalloc(BUFSIZ);
Eric Andersenca604592001-03-08 17:17:13 +00001365 next_command = NULL;
1366 continue;
1367 }
1368
Eric Andersen86349772000-12-18 20:25:50 +00001369 if (!parse_command(&next_command, &newjob, &inbg) &&
1370 newjob.num_progs) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001371 int pipefds[2] = { -1, -1 };
1372 debug_printf("job=%p fed to run_command by busy_loop()'\n",
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001373 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001374 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001375 }
1376 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001377 free(command);
Denis Vlasenko4cccc032006-12-22 18:37:07 +00001378 command = xzalloc(BUFSIZ);
Eric Andersen86349772000-12-18 20:25:50 +00001379 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001380 }
1381 } else {
1382 /* a job is running in the foreground; wait for it */
1383 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001384 while (!job_list.fg->progs[i].pid ||
1385 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001386
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001387 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED) < 0) {
Eric Andersen7373e482002-07-31 04:04:47 +00001388 if (errno != ECHILD) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001389 bb_perror_msg_and_die("waitpid(%d)", job_list.fg->progs[i].pid);
Eric Andersen7373e482002-07-31 04:04:47 +00001390 }
1391 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001392
Erik Andersend75af992000-03-16 08:09:09 +00001393 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001394 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001395 job_list.fg->running_progs--;
1396 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001397
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001398 last_return_code = WEXITSTATUS(status);
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001399
Eric Andersen86349772000-12-18 20:25:50 +00001400 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001401 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001402 remove_job(&job_list, job_list.fg);
1403 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001404 }
Eric Andersen37032b42004-02-10 01:28:36 +00001405 }
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001406#if ENABLE_LASH_JOB_CONTROL
Eric Andersen37032b42004-02-10 01:28:36 +00001407 else {
Erik Andersen161220c2000-03-16 08:12:48 +00001408 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001409 job_list.fg->stopped_progs++;
1410 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001411
Eric Andersen86349772000-12-18 20:25:50 +00001412 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1413 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1414 "Stopped", job_list.fg->text);
1415 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001416 }
Erik Andersend75af992000-03-16 08:09:09 +00001417 }
1418
Eric Andersen86349772000-12-18 20:25:50 +00001419 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001420 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001421 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001422 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Eric Andersenff9ad472004-02-10 01:07:45 +00001423 bb_perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001424 }
Eric Andersen37032b42004-02-10 01:28:36 +00001425#endif
Erik Andersend75af992000-03-16 08:09:09 +00001426 }
1427 }
Erik Andersen161220c2000-03-16 08:12:48 +00001428 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001429
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001430#if ENABLE_LASH_JOB_CONTROL
Eric Andersen1c314ad2000-06-28 16:56:25 +00001431 /* return controlling TTY back to parent process group before exiting */
Eric Andersen7373e482002-07-31 04:04:47 +00001432 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001433 bb_perror_msg("tcsetpgrp");
Eric Andersen37032b42004-02-10 01:28:36 +00001434#endif
Eric Andersenb54833c2000-07-03 23:56:26 +00001435
1436 /* return exit status if called with "-c" */
1437 if (input == NULL && WIFEXITED(status))
1438 return WEXITSTATUS(status);
Eric Andersenff9ad472004-02-10 01:07:45 +00001439
Erik Andersen161220c2000-03-16 08:12:48 +00001440 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001441}
1442
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001443#if ENABLE_FEATURE_CLEAN_UP
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +00001444static void free_memory(void)
Eric Andersenfad9c112000-07-25 18:06:52 +00001445{
Denis Vlasenko7cced6e2007-04-12 17:08:53 +00001446 free(cwd);
Eric Andersenfad9c112000-07-25 18:06:52 +00001447
Eric Andersen86349772000-12-18 20:25:50 +00001448 if (job_list.fg && !job_list.fg->running_progs) {
1449 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001450 }
1451}
Rob Landleyacf448d2006-07-09 17:03:07 +00001452#else
1453void free_memory(void);
Eric Andersenfad9c112000-07-25 18:06:52 +00001454#endif
1455
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001456#if ENABLE_LASH_JOB_CONTROL
Eric Andersen2d848a42001-06-25 17:11:54 +00001457/* Make sure we have a controlling tty. If we get started under a job
1458 * aware app (like bash for example), make sure we are now in charge so
1459 * we don't fight over who gets the foreground */
Eric Andersenece8fc22002-12-11 04:26:28 +00001460static void setup_job_control(void)
Eric Andersen2d848a42001-06-25 17:11:54 +00001461{
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001462 int status;
Eric Andersen37032b42004-02-10 01:28:36 +00001463 pid_t shell_pgrp;
Eric Andersenff9ad472004-02-10 01:07:45 +00001464
Eric Andersen2d848a42001-06-25 17:11:54 +00001465 /* Loop until we are in the foreground. */
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001466 while ((status = tcgetpgrp(shell_terminal)) >= 0) {
1467 shell_pgrp = getpgrp();
1468 if (status == shell_pgrp) {
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001469 break;
1470 }
Denis Vlasenkoff131b92007-04-10 15:42:06 +00001471 kill(- shell_pgrp, SIGTTIN);
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001472 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001473
1474 /* Ignore interactive and job-control signals. */
1475 signal(SIGINT, SIG_IGN);
1476 signal(SIGQUIT, SIG_IGN);
1477 signal(SIGTSTP, SIG_IGN);
1478 signal(SIGTTIN, SIG_IGN);
1479 signal(SIGTTOU, SIG_IGN);
1480 signal(SIGCHLD, SIG_IGN);
1481
1482 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001483 setsid();
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +00001484 shell_pgrp = getpid();
Rob Landleyacf448d2006-07-09 17:03:07 +00001485 setpgid(shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001486
1487 /* Grab control of the terminal. */
1488 tcsetpgrp(shell_terminal, shell_pgrp);
1489}
Eric Andersen37032b42004-02-10 01:28:36 +00001490#else
1491static inline void setup_job_control(void)
1492{
1493}
1494#endif
Eric Andersen6efc48c2000-07-18 08:16:39 +00001495
Denis Vlasenko06af2162007-02-03 17:28:39 +00001496int lash_main(int argc_l, char **argv_l);
Matt Kraai2d91deb2001-08-01 17:21:35 +00001497int lash_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001498{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001499 unsigned opt;
Erik Andersen161220c2000-03-16 08:12:48 +00001500 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001501 argc = argc_l;
1502 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001503
Denis Vlasenko38f63192007-01-22 09:03:07 +00001504#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001505 line_input_state = new_line_input_t(FOR_SHELL);
1506#endif
1507
Eric Andersen702ec592001-03-06 22:17:29 +00001508 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001509 last_jobid = 0;
Rob Landleyf9463792006-06-13 18:50:20 +00001510 close_me_list = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001511 job_list.head = NULL;
1512 job_list.fg = NULL;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001513 last_return_code = 1;
Eric Andersen501c88b2000-07-28 15:14:45 +00001514
Eric Andersenb558e762000-11-30 22:43:16 +00001515 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001516 FILE *prof_input;
1517 prof_input = fopen("/etc/profile", "r");
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001518 if (prof_input) {
Rob Landley7a260f02006-06-19 03:20:03 +00001519 llist_add_to(&close_me_list, (void *)(long)fileno(prof_input));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001520 /* Now run the file */
1521 busy_loop(prof_input);
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00001522 fclose_if_not_stdin(prof_input);
Rob Landleyf9463792006-06-13 18:50:20 +00001523 llist_pop(&close_me_list);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001524 }
Eric Andersenb558e762000-11-30 22:43:16 +00001525 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001526
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001527 opt = getopt32(argc_l, argv_l, "+ic:", &local_pending_command);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001528#define LASH_OPT_i (1<<0)
Denis Vlasenkof443bff2007-03-31 11:08:50 +00001529#define LASH_OPT_c (1<<1)
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001530 if (opt & LASH_OPT_c) {
1531 input = NULL;
1532 optind++;
1533 argv += optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001534 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001535 /* A shell is interactive if the `-i' flag was given, or if all of
1536 * the following conditions are met:
1537 * no -c command
1538 * no arguments remaining or the -s flag given
1539 * standard input is a terminal
1540 * standard output is a terminal
1541 * Refer to Posix.2, the description of the `sh' utility. */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001542 if (argv[optind] == NULL && input == stdin
1543 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
1544 ) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001545 opt |= LASH_OPT_i;
Eric Andersen86349772000-12-18 20:25:50 +00001546 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001547 setup_job_control();
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001548 if (opt & LASH_OPT_i) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001549 /* Looks like they want an interactive shell */
Rob Landleyacf448d2006-07-09 17:03:07 +00001550 if (!ENABLE_FEATURE_SH_EXTRA_QUIET) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001551 printf("\n\n%s Built-in shell (lash)\n"
1552 "Enter 'help' for a list of built-in commands.\n\n",
1553 BB_BANNER);
Rob Landleyacf448d2006-07-09 17:03:07 +00001554 }
1555 } else if (!local_pending_command && argv[optind]) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001556 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Rob Landleyd921b2e2006-08-03 15:41:12 +00001557 input = xfopen(argv[optind], "r");
Rob Landleyf9463792006-06-13 18:50:20 +00001558 /* be lazy, never mark this closed */
Rob Landley7a260f02006-06-19 03:20:03 +00001559 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Eric Andersen501c88b2000-07-28 15:14:45 +00001560 }
1561
Eric Andersen6efc48c2000-07-18 08:16:39 +00001562 /* initialize the cwd -- this is never freed...*/
Denis Vlasenko7cced6e2007-04-12 17:08:53 +00001563 update_cwd();
Erik Andersen3522eb12000-03-12 23:49:18 +00001564
Rob Landleyacf448d2006-07-09 17:03:07 +00001565 if (ENABLE_FEATURE_CLEAN_UP) atexit(free_memory);
Eric Andersenfad9c112000-07-25 18:06:52 +00001566
Denis Vlasenko38f63192007-01-22 09:03:07 +00001567 if (ENABLE_FEATURE_EDITING) cmdedit_set_initial_prompt();
Rob Landleyacf448d2006-07-09 17:03:07 +00001568 else PS1 = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +00001569
Denis Vlasenko7cced6e2007-04-12 17:08:53 +00001570 return busy_loop(input);
Erik Andersen3522eb12000-03-12 23:49:18 +00001571}