blob: 5139189627f517d5e2ead9874a09456f452d04b5 [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
Eric Andersen501c88b2000-07-28 15:14:45 +000023#include <getopt.h>
Eric Andersen13d1fa12001-03-08 23:59:45 +000024#include <glob.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000025
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000026#include "libbb.h"
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000027
Eric Andersen13d1fa12001-03-08 23:59:45 +000028#define expand_t glob_t
Erik Andersen3522eb12000-03-12 23:49:18 +000029
Eric Andersenff9ad472004-02-10 01:07:45 +000030/* Always enable for the moment... */
Eric Andersen95b26252004-02-10 01:30:21 +000031#define CONFIG_LASH_PIPE_N_REDIRECTS
32#define CONFIG_LASH_JOB_CONTROL
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +000033#define ENABLE_LASH_PIPE_N_REDIRECTS 1
34#define ENABLE_LASH_JOB_CONTROL 1
Eric Andersen13d1fa12001-03-08 23:59:45 +000035
Denis Vlasenko831a20f2007-04-12 12:27:32 +000036
Denis Vlasenko703e2022007-01-22 14:12:08 +000037enum { MAX_READ = 128 }; /* size of input buffer for 'read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000038#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
39
Erik Andersend75af992000-03-16 08:09:09 +000040
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +000041#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000042enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000043 REDIRECT_APPEND
44};
Eric Andersenff9ad472004-02-10 01:07:45 +000045#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000046
Rob Landleybc68cd12006-03-10 19:22:06 +000047enum {
48 DEFAULT_CONTEXT = 0x1,
49 IF_TRUE_CONTEXT = 0x2,
50 IF_FALSE_CONTEXT = 0x4,
51 THEN_EXP_CONTEXT = 0x8,
52 ELSE_EXP_CONTEXT = 0x10
53};
Eric Andersenfad9c112000-07-25 18:06:52 +000054
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +000055#define LASH_OPT_DONE (1)
56#define LASH_OPT_SAW_QUOTE (2)
57
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +000058#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000059struct redir_struct {
60 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000061 int fd; /* file descriptor being redirected */
62 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000063};
Eric Andersenff9ad472004-02-10 01:07:45 +000064#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000065
Eric Andersen86349772000-12-18 20:25:50 +000066struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000067 pid_t pid; /* 0 if exited */
68 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000069 int num_redirects; /* elements in redirection array */
Eric Andersen86349772000-12-18 20:25:50 +000070 int is_stopped; /* is the program currently running? */
71 struct job *family; /* pointer back to the child's parent job */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +000072#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersenff9ad472004-02-10 01:07:45 +000073 struct redir_struct *redirects; /* I/O redirects */
74#endif
75};
76
77struct jobset {
78 struct job *head; /* head of list of running jobs */
79 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000080};
81
82struct job {
Eric Andersen86349772000-12-18 20:25:50 +000083 int jobid; /* job number */
84 int num_progs; /* total number of programs in job */
85 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000086 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +000087 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +000088 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +000089 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +000090 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +000091 int stopped_progs; /* number of programs alive, but stopped */
92 unsigned int job_context; /* bitmask defining current context */
93 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +000094};
95
Eric Andersen86349772000-12-18 20:25:50 +000096struct built_in_command {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +000097 const char *cmd; /* name */
98 const char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +000099 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +0000100};
101
Eric Andersen34e19412000-07-10 18:47:24 +0000102/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000103static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000104static int builtin_exec(struct child_prog *cmd);
105static int builtin_exit(struct child_prog *cmd);
106static int builtin_fg_bg(struct child_prog *cmd);
107static int builtin_help(struct child_prog *cmd);
108static int builtin_jobs(struct child_prog *dummy);
109static int builtin_pwd(struct child_prog *dummy);
110static int builtin_export(struct child_prog *cmd);
111static int builtin_source(struct child_prog *cmd);
112static int builtin_unset(struct child_prog *cmd);
113static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000114
Eric Andersen34e19412000-07-10 18:47:24 +0000115
116/* function prototypes for shell stuff */
Eric Andersen86349772000-12-18 20:25:50 +0000117static void checkjobs(struct jobset *job_list);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000118static void remove_job(struct jobset *j_list, struct job *job);
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000119static int get_command_bufsiz(FILE *source, char *command);
Eric Andersen86349772000-12-18 20:25:50 +0000120static int parse_command(char **command_ptr, struct job *job, int *inbg);
121static int run_command(struct job *newjob, int inbg, int outpipe[2]);
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000122static int pseudo_exec(struct child_prog *cmd) NORETURN;
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000123static int busy_loop(FILE *input);
Erik Andersen3522eb12000-03-12 23:49:18 +0000124
Erik Andersend75af992000-03-16 08:09:09 +0000125
Mark Whitley37653aa2000-07-12 23:36:17 +0000126/* Table of built-in functions (these are non-forking builtins, meaning they
127 * can change global variables in the parent shell process but they will not
128 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000129static const struct built_in_command bltins[] = {
Denis Vlasenkob69f35e2007-04-12 17:10:45 +0000130 {"bg" , "Resume a job in the background", builtin_fg_bg},
131 {"cd" , "Change working directory", builtin_cd},
132 {"exec" , "Exec command, replacing this shell with the exec'd process", builtin_exec},
133 {"exit" , "Exit from shell()", builtin_exit},
134 {"fg" , "Bring job into the foreground", builtin_fg_bg},
135 {"jobs" , "Lists the active jobs", builtin_jobs},
Eric Andersenfad9c112000-07-25 18:06:52 +0000136 {"export", "Set environment variable", builtin_export},
Denis Vlasenkob69f35e2007-04-12 17:10:45 +0000137 {"unset" , "Unset environment variable", builtin_unset},
138 {"read" , "Input environment variable", builtin_read},
139 {"." , "Source-in and run commands in a file", builtin_source},
140 /* These were "forked applets", but distinction was nuked */
141 /* Original comment retained: */
142 /* Table of forking built-in functions (things that fork cannot change global
143 * variables in the parent process, such as the current working directory) */
144 {"pwd" , "Print current directory", builtin_pwd},
145 {"help" , "List shell built-in commands", builtin_help},
Eric Andersen86349772000-12-18 20:25:50 +0000146 /* to do: add ulimit */
Erik Andersen330fd2b2000-05-19 05:35:19 +0000147};
148
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000149
150#define VEC_LAST(v) v[ARRAY_SIZE(v)-1]
Erik Andersen3522eb12000-03-12 23:49:18 +0000151
Eric Andersen0bcc8132001-01-05 19:37:32 +0000152
Eric Andersen7467c8d2001-07-12 20:26:32 +0000153static int shell_context; /* Type prompt trigger (PS1 or PS2) */
Eric Andersen0bcc8132001-01-05 19:37:32 +0000154
155
156/* Globals that are static to this file */
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000157static char *cwd;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000158static char *local_pending_command;
Eric Andersen86349772000-12-18 20:25:50 +0000159static struct jobset job_list = { NULL, NULL };
Denis Vlasenkoa6899c32007-11-15 17:47:45 +0000160static int global_argc;
161static char **global_argv;
Rob Landleyf9463792006-06-13 18:50:20 +0000162static llist_t *close_me_list;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000163static int last_return_code;
164static int last_bg_pid;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000165static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000166static int shell_terminal;
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000167static const char *PS1;
168static const char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000169
170
Eric Andersenb558e762000-11-30 22:43:16 +0000171#ifdef DEBUG_SHELL
172static inline void debug_printf(const char *format, ...)
173{
174 va_list args;
175 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000176 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000177 va_end(args);
178}
179#else
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000180static inline void debug_printf(const char UNUSED_PARAM *format, ...) { }
Eric Andersenb558e762000-11-30 22:43:16 +0000181#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000182
Eric Andersen86349772000-12-18 20:25:50 +0000183/*
184 Most builtins need access to the struct child_prog that has
185 their arguments, previously coded as cmd->progs[0]. That coding
186 can exhibit a bug, if the builtin is not the first command in
187 a pipeline: "echo foo | exec sort" will attempt to exec foo.
188
189builtin previous use notes
190------ ----------------- ---------
191cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000192exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
193exit cmd->progs[0]
194fg_bg cmd->progs[0], job_list->head, job_list->fg
195help 0
196jobs job_list->head
197pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000198export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000199source cmd->progs[0]
200unset cmd->progs[0]
201read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000202
203I added "struct job *family;" to struct child_prog,
204and switched API to builtin_foo(struct child_prog *child);
205So cmd->text becomes child->family->text
206 cmd->job_context becomes child->family->job_context
207 cmd->progs[0] becomes *child
208 job_list becomes child->family->job_list
209 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000210
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000211
212static void update_cwd(void)
213{
214 cwd = xrealloc_getcwd_or_warn(cwd);
215 if (!cwd)
216 cwd = xstrdup(bb_msg_unknown);
217}
218
Erik Andersend75af992000-03-16 08:09:09 +0000219/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000220static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000221{
Erik Andersen161220c2000-03-16 08:12:48 +0000222 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000223
Eric Andersen86349772000-12-18 20:25:50 +0000224 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000225 newdir = getenv("HOME");
226 else
Eric Andersen86349772000-12-18 20:25:50 +0000227 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000228 if (chdir(newdir)) {
Rob Landley5483de12006-06-20 21:35:26 +0000229 bb_perror_msg("cd: %s", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000230 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000231 }
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000232 update_cwd();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000233 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000234}
235
Eric Andersend2f56772000-09-21 02:48:07 +0000236/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000237static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000238{
Eric Andersen86349772000-12-18 20:25:50 +0000239 if (child->argv[1] == NULL)
240 return EXIT_SUCCESS; /* Really? */
241 child->argv++;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000242 while (close_me_list)
243 close((long)llist_pop(&close_me_list));
Eric Andersen86349772000-12-18 20:25:50 +0000244 pseudo_exec(child);
245 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000246}
247
Erik Andersen3522eb12000-03-12 23:49:18 +0000248/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000249static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000250{
Eric Andersen86349772000-12-18 20:25:50 +0000251 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000252 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000253
Denis Vlasenko6398cf42007-04-11 17:04:29 +0000254 exit(atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000255}
256
257/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000258static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000259{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000260 int i, jobnum;
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000261 struct job *job;
Erik Andersen3522eb12000-03-12 23:49:18 +0000262
Eric Andersen8a646dd2001-06-21 16:38:11 +0000263 /* If they gave us no args, assume they want the last backgrounded task */
264 if (!child->argv[1]) {
265 for (job = child->family->job_list->head; job; job = job->next) {
266 if (job->jobid == last_jobid) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000267 goto found;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000268 }
Erik Andersen161220c2000-03-16 08:12:48 +0000269 }
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000270 bb_error_msg("%s: no current job", child->argv[0]);
271 return EXIT_FAILURE;
272 }
273 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
274 bb_error_msg(bb_msg_invalid_arg, child->argv[1], child->argv[0]);
275 return EXIT_FAILURE;
276 }
277 for (job = child->family->job_list->head; job; job = job->next) {
278 if (job->jobid == jobnum) {
279 goto found;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000280 }
Erik Andersend75af992000-03-16 08:09:09 +0000281 }
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000282 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
283 return EXIT_FAILURE;
284 found:
Eric Andersen86349772000-12-18 20:25:50 +0000285 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000286 /* Put the job into the foreground. */
287 tcsetpgrp(shell_terminal, job->pgrp);
288
Eric Andersen86349772000-12-18 20:25:50 +0000289 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000290 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000291
Erik Andersen161220c2000-03-16 08:12:48 +0000292 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000293 for (i = 0; i < job->num_progs; i++)
294 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000295
Eric Andersen86349772000-12-18 20:25:50 +0000296 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000297
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000298 i = kill(- job->pgrp, SIGCONT);
299 if (i < 0) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000300 if (errno == ESRCH) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000301 remove_job(&job_list, job);
Eric Andersen07abfe22001-06-27 17:29:11 +0000302 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000303 bb_perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000304 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000305 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000306
Matt Kraai3e856ce2000-12-01 02:55:13 +0000307 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000308}
309
310/* built-in 'help' handler */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000311static int builtin_help(struct child_prog UNUSED_PARAM *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000312{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000313 const struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000314
Denys Vlasenkod6b05eb2009-06-06 20:59:55 +0200315 printf(
Denis Vlasenko34d4d892009-04-04 20:24:37 +0000316 "Built-in commands:\n"
317 "------------------\n");
Denis Vlasenkob69f35e2007-04-12 17:10:45 +0000318 for (x = bltins; x <= &VEC_LAST(bltins); x++) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000319 if (x->descr == NULL)
Eric Andersenfad9c112000-07-25 18:06:52 +0000320 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000321 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000322 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000323 bb_putchar('\n');
Matt Kraai3e856ce2000-12-01 02:55:13 +0000324 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000325}
326
327/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000328static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000329{
Erik Andersen161220c2000-03-16 08:12:48 +0000330 struct job *job;
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000331 const char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000332
Eric Andersen86349772000-12-18 20:25:50 +0000333 for (job = child->family->job_list->head; job; job = job->next) {
334 if (job->running_progs == job->stopped_progs)
335 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000336 else
Eric Andersen86349772000-12-18 20:25:50 +0000337 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000338
Eric Andersen86349772000-12-18 20:25:50 +0000339 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000340 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000341 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000342}
343
344
345/* built-in 'pwd' handler */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000346static int builtin_pwd(struct child_prog UNUSED_PARAM *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000347{
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000348 update_cwd();
Matt Kraai59df6f72001-05-16 14:21:09 +0000349 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000350 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000351}
352
Erik Andersen6273f652000-03-17 01:12:41 +0000353/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000354static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000355{
Erik Andersen161220c2000-03-16 08:12:48 +0000356 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000357 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000358
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000359 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000360 char **e;
361 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000362 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000363 }
Matt Kraai2129f972001-04-04 17:50:04 +0000364 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000365 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000366 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000367 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000368 bb_perror_msg("export");
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000369#if ENABLE_FEATURE_EDITING_FANCY_PROMPT
370 if (strncmp(v, "PS1=", 4) == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000371 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000372#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000373
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000374#if ENABLE_LOCALE_SUPPORT
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000375 // TODO: why getenv? "" would be just as good...
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000376 if (strncmp(v, "LC_ALL=", 7) == 0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000377 setlocale(LC_ALL, getenv("LC_ALL"));
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000378 if (strncmp(v, "LC_CTYPE=", 9) == 0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000379 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000380#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000381
Denis Vlasenko097c3242006-11-27 16:59:15 +0000382 return res;
Erik Andersen3522eb12000-03-12 23:49:18 +0000383}
384
Eric Andersenb54833c2000-07-03 23:56:26 +0000385/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000386static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000387{
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000388 int res = 0, len;
Eric Andersenb54833c2000-07-03 23:56:26 +0000389 char *s;
390 char string[MAX_READ];
391
Eric Andersen86349772000-12-18 20:25:50 +0000392 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000393 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen0d2d1eb2002-06-06 13:33:01 +0000394 safe_strncpy(string, child->argv[1], MAX_READ-1);
Eric Andersenb54833c2000-07-03 23:56:26 +0000395 len = strlen(string);
396 string[len++] = '=';
397 string[len] = '\0';
398 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000399 res = strlen(string);
400 if (res > len)
401 string[--res] = '\0'; /* chomp trailing newline */
Eric Andersenb54833c2000-07-03 23:56:26 +0000402 /*
403 ** string should now contain "VAR=<value>"
404 ** copy it (putenv() won't do that, so we must make sure
405 ** the string resides in a static buffer!)
406 */
407 res = -1;
Denis Vlasenko831a20f2007-04-12 12:27:32 +0000408 s = strdup(string);
409 if (s)
Eric Andersenb54833c2000-07-03 23:56:26 +0000410 res = putenv(s);
411 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000412 bb_perror_msg("read");
Denis Vlasenko831a20f2007-04-12 12:27:32 +0000413 } else
Eric Andersenb54833c2000-07-03 23:56:26 +0000414 fgets(string, sizeof(string), stdin);
415
Denis Vlasenko097c3242006-11-27 16:59:15 +0000416 return res;
Eric Andersenb54833c2000-07-03 23:56:26 +0000417}
418
Erik Andersen3522eb12000-03-12 23:49:18 +0000419/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000420static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000421{
Erik Andersen161220c2000-03-16 08:12:48 +0000422 FILE *input;
423 int status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000424
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000425 input = fopen_or_warn(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000426 if (!input) {
Matt Kraai3e856ce2000-12-01 02:55:13 +0000427 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000428 }
Erik Andersend75af992000-03-16 08:09:09 +0000429
Rob Landley7a260f02006-06-19 03:20:03 +0000430 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Erik Andersen161220c2000-03-16 08:12:48 +0000431 /* Now run the file */
432 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000433 fclose(input);
Rob Landleyf9463792006-06-13 18:50:20 +0000434 llist_pop(&close_me_list);
Denis Vlasenko097c3242006-11-27 16:59:15 +0000435 return status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000436}
437
438/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000439static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000440{
Eric Andersen86349772000-12-18 20:25:50 +0000441 if (child->argv[1] == NULL) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000442 printf(bb_msg_requires_arg, "unset");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000443 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000444 }
Eric Andersen86349772000-12-18 20:25:50 +0000445 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000446 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000447}
448
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000449#if ENABLE_LASH_JOB_CONTROL
Erik Andersen3522eb12000-03-12 23:49:18 +0000450/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000451static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000452{
Erik Andersen161220c2000-03-16 08:12:48 +0000453 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000454 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000455
Eric Andersen86349772000-12-18 20:25:50 +0000456 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000457 free(cmd->progs[i].argv);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000458#if ENABLE_LASH_PIPE_N_REDIRECTS
Denis Vlasenko60818682007-09-28 22:07:23 +0000459 free(cmd->progs[i].redirects);
Eric Andersenff9ad472004-02-10 01:07:45 +0000460#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000461 }
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000462 free(cmd->progs);
463 free(cmd->text);
464 free(cmd->cmdbuf);
Mark Whitley44a99142001-03-14 17:26:37 +0000465 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000466 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000467 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000468}
469
Eric Andersen1ca20a72001-03-21 07:34:27 +0000470/* remove a job from a jobset */
471static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000472{
Eric Andersen86349772000-12-18 20:25:50 +0000473 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000474
Eric Andersen86349772000-12-18 20:25:50 +0000475 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000476 if (job == j_list->head) {
477 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000478 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000479 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000480 while (prevjob->next != job)
481 prevjob = prevjob->next;
482 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000483 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000484
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000485 if (j_list->head)
486 last_jobid = j_list->head->jobid;
487 else
488 last_jobid = 0;
489
Erik Andersen161220c2000-03-16 08:12:48 +0000490 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000491}
492
Eric Andersenff9ad472004-02-10 01:07:45 +0000493/* Checks to see if any background processes have exited -- if they
Erik Andersen3522eb12000-03-12 23:49:18 +0000494 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000495static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000496{
Erik Andersen161220c2000-03-16 08:12:48 +0000497 struct job *job;
498 pid_t childpid;
499 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000500 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000501
Erik Andersen161220c2000-03-16 08:12:48 +0000502 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000503 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000504 prognum = 0;
505 while (prognum < job->num_progs &&
506 job->progs[prognum].pid != childpid) prognum++;
507 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000508 break;
509 }
510
Eric Andersena1d187a2000-07-17 19:14:41 +0000511 /* This happens on backticked commands */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000512 if (job == NULL)
Eric Andersena1d187a2000-07-17 19:14:41 +0000513 return;
514
Erik Andersen161220c2000-03-16 08:12:48 +0000515 if (WIFEXITED(status) || WIFSIGNALED(status)) {
516 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000517 job->running_progs--;
518 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000519
Eric Andersen86349772000-12-18 20:25:50 +0000520 if (!job->running_progs) {
521 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000522 last_jobid = 0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000523 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000524 }
525 } else {
526 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000527 job->stopped_progs++;
528 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000529 }
Erik Andersend75af992000-03-16 08:09:09 +0000530 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000531
Erik Andersen161220c2000-03-16 08:12:48 +0000532 if (childpid == -1 && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000533 bb_perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000534}
Eric Andersen37032b42004-02-10 01:28:36 +0000535#else
536static void checkjobs(struct jobset *j_list)
537{
538}
539static void free_job(struct job *cmd)
540{
541}
542static void remove_job(struct jobset *j_list, struct job *job)
543{
544}
545#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000546
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000547#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersene9f07fb2000-12-21 18:31:36 +0000548/* squirrel != NULL means we squirrel away copies of stdin, stdout,
549 * and stderr if they are redirected. */
550static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000551{
552 int i;
553 int openfd;
554 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000555 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000556
Eric Andersen86349772000-12-18 20:25:50 +0000557 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000558 switch (redir->type) {
559 case REDIRECT_INPUT:
560 mode = O_RDONLY;
561 break;
562 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000563 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000564 break;
565 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000566 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000567 break;
568 }
569
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000570 openfd = open_or_warn(redir->filename, mode);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000571 if (openfd < 0) {
572 /* this could get lost if stderr has been redirected, but
573 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000574 return 1;
575 }
576
577 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000578 if (squirrel && redir->fd < 3) {
579 squirrel[redir->fd] = dup(redir->fd);
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000580 close_on_exec_on(squirrel[redir->fd]);
Eric Andersene9f07fb2000-12-21 18:31:36 +0000581 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000582 dup2(openfd, redir->fd);
583 close(openfd);
584 }
585 }
586
587 return 0;
588}
589
Eric Andersene9f07fb2000-12-21 18:31:36 +0000590static void restore_redirects(int squirrel[])
591{
592 int i, fd;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000593 for (i = 0; i < 3; i++) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000594 fd = squirrel[i];
595 if (fd != -1) {
596 /* No error checking. I sure wouldn't know what
597 * to do with an error if I found one! */
598 dup2(fd, i);
599 close(fd);
600 }
601 }
602}
Eric Andersenff9ad472004-02-10 01:07:45 +0000603#else
604static inline int setup_redirects(struct child_prog *prog, int squirrel[])
605{
606 return 0;
607}
608static inline void restore_redirects(int squirrel[])
609{
610}
611#endif
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000612
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000613static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000614{
Denis Vlasenko38f63192007-01-22 09:03:07 +0000615#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000616 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000617#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000618 PS1 = getenv("PS1");
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000619 if (PS1 == 0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000620 PS1 = "\\w \\$ ";
Eric Andersenff9ad472004-02-10 01:07:45 +0000621#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000622}
623
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000624static inline const char* setup_prompt_string(void)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000625{
Denis Vlasenko38f63192007-01-22 09:03:07 +0000626#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000627 /* Set up the prompt */
628 if (shell_context == 0) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000629 char *ns;
630 free((char*)PS1);
631 ns = xmalloc(strlen(cwd)+4);
632 sprintf(ns, "%s %c ", cwd, (geteuid() != 0) ? '$': '#');
633 PS1 = ns;
634 return ns;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000635 } else {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000636 return PS2;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000637 }
638#else
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000639 return (shell_context == 0)? PS1 : PS2;
Eric Andersenff9ad472004-02-10 01:07:45 +0000640#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000641}
Eric Andersen22332fd2001-01-30 23:40:39 +0000642
Denis Vlasenko38f63192007-01-22 09:03:07 +0000643#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000644static line_input_t *line_input_state;
645#endif
646
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000647static int get_command_bufsiz(FILE *source, char *command)
Eric Andersen09acc062001-01-04 11:10:38 +0000648{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000649 const char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000650
Eric Andersen1c314ad2000-06-28 16:56:25 +0000651 if (source == NULL) {
652 if (local_pending_command) {
653 /* a command specified (-c option): return it & mark it done */
Denis Vlasenkof443bff2007-03-31 11:08:50 +0000654 strncpy(command, local_pending_command, BUFSIZ);
Eric Andersen1c314ad2000-06-28 16:56:25 +0000655 local_pending_command = NULL;
656 return 0;
657 }
658 return 1;
659 }
660
Erik Andersen161220c2000-03-16 08:12:48 +0000661 if (source == stdin) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000662 prompt_str = setup_prompt_string();
Eric Andersen8ea28be2001-01-05 20:58:22 +0000663
Denis Vlasenko38f63192007-01-22 09:03:07 +0000664#if ENABLE_FEATURE_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000665 /*
666 ** enable command line editing only while a command line
667 ** is actually being read; otherwise, we'll end up bequeathing
668 ** atexit() handlers and other unwanted stuff to our
669 ** child processes (rob@sysgo.de)
670 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000671 read_line_input(prompt_str, command, BUFSIZ, line_input_state);
Erik Andersen161220c2000-03-16 08:12:48 +0000672 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000673#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000674 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000675#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000676 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000677
Erik Andersen161220c2000-03-16 08:12:48 +0000678 if (!fgets(command, BUFSIZ - 2, source)) {
679 if (source == stdin)
Denis Vlasenko4daad902007-09-27 10:20:47 +0000680 bb_putchar('\n');
Erik Andersen161220c2000-03-16 08:12:48 +0000681 return 1;
682 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000683
Erik Andersen161220c2000-03-16 08:12:48 +0000684 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000685}
686
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000687static char * strsep_space(char *string, int * ix)
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000688{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000689 /* Short circuit the trivial case */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000690 if (!string || ! string[*ix])
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000691 return NULL;
692
693 /* Find the end of the token. */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000694 while (string[*ix] && !isspace(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000695 (*ix)++;
696 }
697
Eric Andersenff9ad472004-02-10 01:07:45 +0000698 /* Find the end of any whitespace trailing behind
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000699 * the token and let that be part of the token */
Denys Vlasenkoc0dab372009-10-22 22:28:08 +0200700 while (string[*ix] && isspace(string[*ix])) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000701 (*ix)++;
702 }
703
Rob Landleyf9463792006-06-13 18:50:20 +0000704 if (!*ix) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000705 /* Nothing useful was found */
706 return NULL;
707 }
708
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000709 return xstrndup(string, *ix);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000710}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000711
Eric Andersenca604592001-03-08 17:17:13 +0000712static int expand_arguments(char *command)
713{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000714 static const char out_of_space[] ALIGN1 = "out of space during expansion";
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000715
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000716 int total_length = 0, length, i, retval, ix = 0;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000717 expand_t expand_result;
718 char *tmpcmd, *cmd, *cmd_copy;
719 char *src, *dst, *var;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000720 int flags = GLOB_NOCHECK
721#ifdef GLOB_BRACE
722 | GLOB_BRACE
Eric Andersenff9ad472004-02-10 01:07:45 +0000723#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000724#ifdef GLOB_TILDE
725 | GLOB_TILDE
Eric Andersenff9ad472004-02-10 01:07:45 +0000726#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000727 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000728
Eric Andersenca604592001-03-08 17:17:13 +0000729 /* get rid of the terminating \n */
730 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000731
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000732 /* Fix up escape sequences to be the Real Thing(tm) */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000733 while (command && command[ix]) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000734 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000735 const char *tmp = command+ix+1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000736 command[ix] = bb_process_escape_sequence( &tmp );
Eric Andersen1ca20a72001-03-21 07:34:27 +0000737 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000738 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000739 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000740 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000741 /* Use glob and then fixup environment variables and such */
742
743 /* It turns out that glob is very stupid. We have to feed it one word at a
744 * time since it can't cope with a full string. Here we convert command
745 * (char*) into cmd (char**, one word per string) */
746
747 /* We need a clean copy, so strsep can mess up the copy while
748 * we write stuff into the original (in a minute) */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000749 cmd = cmd_copy = xstrdup(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000750 *command = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000751 for (ix = 0, tmpcmd = cmd;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000752 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix = 0) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000753 if (*tmpcmd == '\0')
754 break;
755 /* we need to trim() the result for glob! */
756 trim(tmpcmd);
757 retval = glob(tmpcmd, flags, NULL, &expand_result);
758 free(tmpcmd); /* Free mem allocated by strsep_space */
759 if (retval == GLOB_NOSPACE) {
760 /* Mem may have been allocated... */
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000761 globfree(&expand_result);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000762 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000763 return FALSE;
764 } else if (retval != 0) {
765 /* Some other error. GLOB_NOMATCH shouldn't
Eric Andersenff9ad472004-02-10 01:07:45 +0000766 * happen because of the GLOB_NOCHECK flag in
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000767 * the glob call. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000768 bb_error_msg("syntax error");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000769 return FALSE;
770 } else {
771 /* Convert from char** (one word per string) to a simple char*,
772 * but don't overflow command which is BUFSIZ in length */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000773 for (i = 0; i < expand_result.gl_pathc; i++) {
774 length = strlen(expand_result.gl_pathv[i]);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000775 if (total_length+length+1 >= BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000776 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000777 return FALSE;
778 }
779 strcat(command+total_length, " ");
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000780 total_length += 1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000781 strcat(command+total_length, expand_result.gl_pathv[i]);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000782 total_length += length;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000783 }
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000784 globfree(&expand_result);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000785 }
786 }
787 free(cmd_copy);
788 trim(command);
789
Eric Andersenff9ad472004-02-10 01:07:45 +0000790 /* Now do the shell variable substitutions which
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000791 * wordexp can't do for us, namely $? and $! */
792 src = command;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000793 while ((dst = strchr(src,'$')) != NULL) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000794 var = NULL;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000795 switch (*(dst+1)) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000796 case '?':
797 var = itoa(last_return_code);
798 break;
799 case '!':
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000800 if (last_bg_pid == -1)
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000801 *var = '\0';
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000802 else
803 var = itoa(last_bg_pid);
804 break;
805 /* Everything else like $$, $#, $[0-9], etc. should all be
806 * expanded by wordexp(), so we can in theory skip that stuff
807 * here, but just to be on the safe side (i.e., since uClibc
808 * wordexp doesn't do this stuff yet), lets leave it in for
809 * now. */
810 case '$':
811 var = itoa(getpid());
812 break;
813 case '#':
Denis Vlasenkoa6899c32007-11-15 17:47:45 +0000814 var = itoa(global_argc - 1);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000815 break;
816 case '0':case '1':case '2':case '3':case '4':
817 case '5':case '6':case '7':case '8':case '9':
818 {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000819 int ixx = *(dst+1)-48+1;
Denis Vlasenkoa6899c32007-11-15 17:47:45 +0000820 if (ixx >= global_argc) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000821 var = '\0';
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000822 } else {
Denis Vlasenkoa6899c32007-11-15 17:47:45 +0000823 var = global_argv[ixx];
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000824 }
825 }
826 break;
827
828 }
829 if (var) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000830 /* a single character construction was found, and
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000831 * already handled in the case statement */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000832 src = dst + 2;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000833 } else {
834 /* Looks like an environment variable */
835 char delim_hold;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000836 int num_skip_chars = 0;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000837 int dstlen = strlen(dst);
838 /* Is this a ${foo} type variable? */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000839 if (dstlen >= 2 && *(dst+1) == '{') {
840 src = strchr(dst+1, '}');
841 num_skip_chars = 1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000842 } else {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000843 src = dst + 1;
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200844 while (isalnum(*src) || *src == '_') src++;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000845 }
846 if (src == NULL) {
847 src = dst+dstlen;
848 }
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000849 delim_hold = *src;
850 *src = '\0'; /* temporary */
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000851 var = getenv(dst + 1 + num_skip_chars);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000852 *src = delim_hold;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000853 src += num_skip_chars;
854 }
855 if (var == NULL) {
856 /* Seems we got an un-expandable variable. So delete it. */
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000857 var = (char*)"";
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000858 }
859 {
860 int subst_len = strlen(var);
861 int trail_len = strlen(src);
862 if (dst+subst_len+trail_len >= command+BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000863 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000864 return FALSE;
865 }
866 /* Move stuff to the end of the string to accommodate
867 * filling the created gap with the new stuff */
868 memmove(dst+subst_len, src, trail_len+1);
869 /* Now copy in the new stuff */
870 memcpy(dst, var, subst_len);
871 src = dst+subst_len;
872 }
873 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000874
Eric Andersenca604592001-03-08 17:17:13 +0000875 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000876}
877
Eric Andersen86349772000-12-18 20:25:50 +0000878/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
879 line). If a valid command is found, command_ptr is set to point to
Eric Andersenff9ad472004-02-10 01:07:45 +0000880 the beginning of the next command (if the original command had more
881 then one job associated with it) or NULL if no more commands are
Erik Andersen3522eb12000-03-12 23:49:18 +0000882 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000883static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000884{
Erik Andersen161220c2000-03-16 08:12:48 +0000885 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000886 char *return_command = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +0000887 char *src, *buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000888 int argc_l;
889 int flag;
Eric Andersen86349772000-12-18 20:25:50 +0000890 int argv_alloced;
Erik Andersen161220c2000-03-16 08:12:48 +0000891 char quote = '\0';
Eric Andersen86349772000-12-18 20:25:50 +0000892 struct child_prog *prog;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000893#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersenff9ad472004-02-10 01:07:45 +0000894 int i;
895 char *chptr;
896#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000897
Erik Andersen161220c2000-03-16 08:12:48 +0000898 /* skip leading white space */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000899 *command_ptr = skip_whitespace(*command_ptr);
Erik Andersen3522eb12000-03-12 23:49:18 +0000900
Erik Andersen161220c2000-03-16 08:12:48 +0000901 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000902 if (!**command_ptr || (**command_ptr == '#')) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000903 job->num_progs = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000904 return 0;
905 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000906
Eric Andersen86349772000-12-18 20:25:50 +0000907 *inbg = 0;
908 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000909 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000910
Eric Andersenff9ad472004-02-10 01:07:45 +0000911 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000912 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000913 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000914
Eric Andersenff9ad472004-02-10 01:07:45 +0000915 Getting clean memory relieves us of the task of NULL
916 terminating things and makes the rest of this look a bit
Erik Andersen161220c2000-03-16 08:12:48 +0000917 cleaner (though it is, admittedly, a tad less efficient) */
Rob Landleyf9463792006-06-13 18:50:20 +0000918 job->cmdbuf = command = xzalloc(2*strlen(*command_ptr) + 1);
Erik Andersen161220c2000-03-16 08:12:48 +0000919 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000920
Erik Andersen161220c2000-03-16 08:12:48 +0000921 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000922 prog->num_redirects = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000923 prog->is_stopped = 0;
924 prog->family = job;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000925#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersenff9ad472004-02-10 01:07:45 +0000926 prog->redirects = NULL;
927#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000928
Eric Andersen86349772000-12-18 20:25:50 +0000929 argv_alloced = 5;
930 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
931 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +0000932
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000933 flag = argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000934 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +0000935 src = *command_ptr;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000936 while (*src && !(flag & LASH_OPT_DONE)) {
Erik Andersen161220c2000-03-16 08:12:48 +0000937 if (quote == *src) {
938 quote = '\0';
939 } else if (quote) {
940 if (*src == '\\') {
941 src++;
942 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000943 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +0000944 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +0000945 return 1;
946 }
947
948 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +0000949 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +0000950 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +0000951 *buf++ = '\\';
952 }
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100953 } else
954 if (*src == '*' || *src == '?'
955 || *src == '[' || *src == ']'
956 ) {
957 *buf++ = '\\';
958 }
Erik Andersen161220c2000-03-16 08:12:48 +0000959 *buf++ = *src;
960 } else if (isspace(*src)) {
Denis Vlasenkob69f35e2007-04-12 17:10:45 +0000961 if (*prog->argv[argc_l] || (flag & LASH_OPT_SAW_QUOTE)) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000962 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +0000963 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +0000964 if ((argc_l + 1) == argv_alloced) {
965 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +0000966 prog->argv = xrealloc(prog->argv,
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000967 sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +0000968 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000969 prog->argv[argc_l] = buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000970 flag ^= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000971 }
972 } else
973 switch (*src) {
974 case '"':
975 case '\'':
976 quote = *src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000977 flag |= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000978 break;
979
980 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000981 if (*(src-1)== '$')
982 *buf++ = *src;
983 else
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000984 flag |= LASH_OPT_DONE;
Erik Andersen161220c2000-03-16 08:12:48 +0000985 break;
986
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000987#if ENABLE_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000988 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +0000989 case '<':
Eric Andersen86349772000-12-18 20:25:50 +0000990 i = prog->num_redirects++;
991 prog->redirects = xrealloc(prog->redirects,
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +0000992 sizeof(*prog->redirects) * (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +0000993
Eric Andersen86349772000-12-18 20:25:50 +0000994 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000995 if (buf != prog->argv[argc_l]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000996 /* the stuff before this character may be the file number
Erik Andersen161220c2000-03-16 08:12:48 +0000997 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +0000998 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000999 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +00001000
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001001 if (*chptr && *prog->argv[argc_l]) {
1002 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001003 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001004 }
1005 }
1006
Eric Andersen86349772000-12-18 20:25:50 +00001007 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001008 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001009 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001010 else
Eric Andersen86349772000-12-18 20:25:50 +00001011 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001012 }
1013
1014 if (*src++ == '>') {
1015 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001016 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001017 REDIRECT_APPEND, src++;
1018 else
Eric Andersen86349772000-12-18 20:25:50 +00001019 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001020 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001021 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001022 }
1023
1024 /* This isn't POSIX sh compliant. Oh well. */
1025 chptr = src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001026 chptr = skip_whitespace(chptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001027
1028 if (!*chptr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001029 bb_error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001030 free_job(job);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001031 job->num_progs = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001032 return 1;
1033 }
1034
Eric Andersen86349772000-12-18 20:25:50 +00001035 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001036 while (*chptr && !isspace(*chptr))
1037 *buf++ = *chptr++;
1038
1039 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001040 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001041 break;
1042
1043 case '|': /* pipe */
1044 /* finish this command */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001045 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001046 argc_l++;
1047 if (!argc_l) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001048 goto empty_command_in_pipe;
Erik Andersen161220c2000-03-16 08:12:48 +00001049 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001050 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001051
1052 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001053 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001054 job->progs = xrealloc(job->progs,
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001055 sizeof(*job->progs) * job->num_progs);
Eric Andersen86349772000-12-18 20:25:50 +00001056 prog = job->progs + (job->num_progs - 1);
1057 prog->num_redirects = 0;
1058 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001059 prog->is_stopped = 0;
1060 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001061 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001062
Eric Andersen86349772000-12-18 20:25:50 +00001063 argv_alloced = 5;
1064 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001065 prog->argv[0] = ++buf;
1066
1067 src++;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001068 src = skip_whitespace(src);
Erik Andersen161220c2000-03-16 08:12:48 +00001069
1070 if (!*src) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001071empty_command_in_pipe:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001072 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001073 free_job(job);
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001074 job->num_progs = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001075 return 1;
1076 }
1077 src--; /* we'll ++ it at the end of the loop */
1078
1079 break;
Eric Andersenff9ad472004-02-10 01:07:45 +00001080#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001081
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001082#if ENABLE_LASH_JOB_CONTROL
Erik Andersen161220c2000-03-16 08:12:48 +00001083 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001084 *inbg = 1;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001085 /* fallthrough */
Eric Andersen37032b42004-02-10 01:28:36 +00001086#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001087 case ';': /* multiple commands */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001088 flag |= LASH_OPT_DONE;
Eric Andersen86349772000-12-18 20:25:50 +00001089 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001090 break;
1091
Matt Kraai131241f2000-09-14 00:43:20 +00001092 case '\\':
1093 src++;
1094 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001095 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001096 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001097 return 1;
1098 }
1099 if (*src == '*' || *src == '[' || *src == ']'
1100 || *src == '?') *buf++ = '\\';
1101 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001102 default:
1103 *buf++ = *src;
1104 }
1105
Erik Andersend75af992000-03-16 08:09:09 +00001106 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001107 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001108
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001109 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001110 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001111 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001112 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001113 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001114 return 0;
1115 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001116 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001117
Eric Andersen86349772000-12-18 20:25:50 +00001118 if (!return_command) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001119 job->text = xstrdup(*command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001120 } else {
1121 /* This leaves any trailing spaces, which is a bit sloppy */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001122 job->text = xstrndup(*command_ptr, return_command - *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001123 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001124
Eric Andersen86349772000-12-18 20:25:50 +00001125 *command_ptr = return_command;
Eric Andersenff9ad472004-02-10 01:07:45 +00001126
Erik Andersend75af992000-03-16 08:09:09 +00001127 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001128}
1129
Eric Andersen86349772000-12-18 20:25:50 +00001130/* Run the child_prog, no matter what kind of command it uses.
1131 */
1132static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001133{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +00001134 const struct built_in_command *x;
Rob Landley68286772006-06-13 19:01:30 +00001135
Eric Andersene9f07fb2000-12-21 18:31:36 +00001136 /* Check if the command matches any of the non-forking builtins.
1137 * Depending on context, this might be redundant. But it's
1138 * easier to waste a few CPU cycles than it is to figure out
1139 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001140 */
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001141 /* Check if the command matches any of the forking builtins. */
1142 for (x = bltins; x <= &VEC_LAST(bltins); x++) {
Denis Vlasenko219d14d2007-03-24 15:40:16 +00001143 if (strcmp(child->argv[0], x->cmd) == 0) {
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001144 _exit(x->function(child));
Eric Andersene9f07fb2000-12-21 18:31:36 +00001145 }
1146 }
1147
Eric Andersen86349772000-12-18 20:25:50 +00001148 /* Check if the command matches any busybox internal
1149 * commands ("applets") here. Following discussions from
Eric Andersen2423b122001-12-08 01:56:15 +00001150 * November 2000 on busybox@busybox.net, don't use
Denis Vlasenko818322b2007-09-24 18:27:04 +00001151 * bb_get_last_path_component_nostrip(). This way explicit
1152 * (with slashes) filenames will never be interpreted as an
Eric Andersen86349772000-12-18 20:25:50 +00001153 * applet, just like with builtins. This way the user can
1154 * override an applet with an explicit filename reference.
1155 * The only downside to this change is that an explicit
1156 * /bin/foo invocation will fork and exec /bin/foo, even if
1157 * /bin/foo is a symlink to busybox.
1158 */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001159 if (ENABLE_FEATURE_SH_STANDALONE) {
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001160 run_applet_and_exit(child->argv[0], child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001161 }
Eric Andersen86349772000-12-18 20:25:50 +00001162
1163 execvp(child->argv[0], child->argv);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001164
Eric Andersenff9ad472004-02-10 01:07:45 +00001165 /* Do not use bb_perror_msg_and_die() here, since we must not
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001166 * call exit() but should call _exit() instead */
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +00001167 bb_simple_perror_msg(child->argv[0]);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001168 _exit(EXIT_FAILURE);
Eric Andersen86349772000-12-18 20:25:50 +00001169}
1170
1171static void insert_job(struct job *newjob, int inbg)
1172{
1173 struct job *thejob;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001174 struct jobset *j_list = newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001175
1176 /* find the ID for thejob to use */
1177 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001178 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001179 if (thejob->jobid >= newjob->jobid)
1180 newjob->jobid = thejob->jobid + 1;
1181
1182 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001183 if (!j_list->head) {
1184 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001185 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001186 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001187 thejob->next = xmalloc(sizeof(*thejob));
1188 thejob = thejob->next;
1189 }
1190
1191 *thejob = *newjob; /* physically copy the struct job */
1192 thejob->next = NULL;
1193 thejob->running_progs = thejob->num_progs;
1194 thejob->stopped_progs = 0;
1195
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001196#if ENABLE_LASH_JOB_CONTROL
Eric Andersen86349772000-12-18 20:25:50 +00001197 if (inbg) {
Eric Andersenff9ad472004-02-10 01:07:45 +00001198 /* we don't wait for background thejobs to return -- append it
Eric Andersen86349772000-12-18 20:25:50 +00001199 to the list of backgrounded thejobs and leave it alone */
1200 printf("[%d] %d\n", thejob->jobid,
1201 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001202 last_jobid = newjob->jobid;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001203 last_bg_pid = newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001204 } else {
1205 newjob->job_list->fg = thejob;
1206
1207 /* move the new process group into the foreground */
Rob Landleyf9463792006-06-13 18:50:20 +00001208 /* Ignore errors since child could have already exited */
1209 tcsetpgrp(shell_terminal, newjob->pgrp);
Eric Andersen86349772000-12-18 20:25:50 +00001210 }
Eric Andersen37032b42004-02-10 01:28:36 +00001211#endif
Eric Andersen86349772000-12-18 20:25:50 +00001212}
1213
1214static int run_command(struct job *newjob, int inbg, int outpipe[2])
1215{
1216 /* struct job *thejob; */
1217 int i;
1218 int nextin, nextout;
1219 int pipefds[2]; /* pipefd[0] is for reading */
Denis Vlasenko71d8abf2007-01-30 22:44:07 +00001220 const struct built_in_command *x;
Eric Andersen86349772000-12-18 20:25:50 +00001221 struct child_prog *child;
1222
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001223 nextin = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001224 for (i = 0; i < newjob->num_progs; i++) {
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001225 child = &(newjob->progs[i]);
Eric Andersen86349772000-12-18 20:25:50 +00001226
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001227 nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001228 if ((i + 1) < newjob->num_progs) {
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +00001229 xpipe(pipefds);
Erik Andersen161220c2000-03-16 08:12:48 +00001230 nextout = pipefds[1];
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001231 } else if (outpipe[1] != -1) {
1232 nextout = outpipe[1];
Erik Andersen161220c2000-03-16 08:12:48 +00001233 }
1234
Eric Andersene9f07fb2000-12-21 18:31:36 +00001235 /* Check if the command matches any non-forking builtins,
1236 * but only if this is a simple command.
1237 * Non-forking builtins within pipes have to fork anyway,
1238 * and are handled in pseudo_exec. "echo foo | read bar"
1239 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001240 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001241 if (newjob->num_progs == 1) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +00001242 int rcode;
1243 int squirrel[] = {-1, -1, -1};
1244
Rob Landley7a8e4912006-07-01 17:19:02 +00001245 /* Check if the command sets an environment variable. */
1246 if (strchr(child->argv[0], '=') != NULL) {
1247 child->argv[1] = child->argv[0];
1248 return builtin_export(child);
1249 }
1250
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001251 for (x = bltins; x <= &VEC_LAST(bltins); x++) {
Denis Vlasenko219d14d2007-03-24 15:40:16 +00001252 if (strcmp(child->argv[0], x->cmd) == 0) {
Eric Andersene9f07fb2000-12-21 18:31:36 +00001253 setup_redirects(child, squirrel);
1254 rcode = x->function(child);
1255 restore_redirects(squirrel);
1256 return rcode;
1257 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001258 }
Denis Vlasenko8a28e622007-04-14 11:16:29 +00001259#if ENABLE_FEATURE_SH_STANDALONE
1260 {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001261 int a = find_applet_by_name(child->argv[i]);
1262 if (a >= 0 && APPLET_IS_NOFORK(a)) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +00001263 setup_redirects(child, squirrel);
1264 rcode = run_nofork_applet(a, child->argv + i);
1265 restore_redirects(squirrel);
1266 return rcode;
1267 }
1268 }
1269#endif
Erik Andersen330fd2b2000-05-19 05:35:19 +00001270 }
1271
Denis Vlasenkob69f35e2007-04-12 17:10:45 +00001272#if BB_MMU
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001273 child->pid = fork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001274#else
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001275 child->pid = vfork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001276#endif
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001277 if (!child->pid) {
Eric Andersen2d848a42001-06-25 17:11:54 +00001278 /* Set the handling for job control signals back to the default. */
1279 signal(SIGINT, SIG_DFL);
1280 signal(SIGQUIT, SIG_DFL);
1281 signal(SIGTSTP, SIG_DFL);
1282 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001283 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001284 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001285
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001286 /* Close all open filehandles. */
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001287 while (close_me_list)
1288 close((long)llist_pop(&close_me_list));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001289
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001290 if (outpipe[1] != -1) {
Eric Andersen86349772000-12-18 20:25:50 +00001291 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001292 }
1293 if (nextin != 0) {
1294 dup2(nextin, 0);
1295 close(nextin);
1296 }
1297
1298 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001299 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001300 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001301 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001302 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001303 }
1304
Eric Andersen86349772000-12-18 20:25:50 +00001305 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001306 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001307
Eric Andersen86349772000-12-18 20:25:50 +00001308 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001309 }
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001310 if (outpipe[1] != -1) {
Eric Andersen86349772000-12-18 20:25:50 +00001311 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001312 }
Erik Andersen161220c2000-03-16 08:12:48 +00001313
1314 /* put our child in the process group whose leader is the
1315 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001316 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001317 if (nextin != 0)
1318 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001319 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001320 close(nextout);
1321
Eric Andersenff9ad472004-02-10 01:07:45 +00001322 /* If there isn't another process, nextin is garbage
Erik Andersen161220c2000-03-16 08:12:48 +00001323 but it doesn't matter */
1324 nextin = pipefds[0];
1325 }
1326
Eric Andersen86349772000-12-18 20:25:50 +00001327 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001328
Eric Andersen86349772000-12-18 20:25:50 +00001329 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001330
Erik Andersen161220c2000-03-16 08:12:48 +00001331 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001332}
1333
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001334static int busy_loop(FILE *input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001335{
Erik Andersen161220c2000-03-16 08:12:48 +00001336 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001337 char *next_command = NULL;
1338 struct job newjob;
Erik Andersen161220c2000-03-16 08:12:48 +00001339 int i;
Eric Andersena68ea1c2006-01-30 22:48:39 +00001340 int inbg = 0;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001341 int status;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001342#if ENABLE_LASH_JOB_CONTROL
Eric Andersend20d3752004-03-12 22:08:42 +00001343 pid_t parent_pgrp;
1344 /* save current owner of TTY so we can restore it on exit */
1345 parent_pgrp = tcgetpgrp(shell_terminal);
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001346#endif
Eric Andersen86349772000-12-18 20:25:50 +00001347 newjob.job_list = &job_list;
1348 newjob.job_context = DEFAULT_CONTEXT;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001349
Rob Landleyf9463792006-06-13 18:50:20 +00001350 command = xzalloc(BUFSIZ);
Erik Andersend75af992000-03-16 08:09:09 +00001351
Erik Andersen161220c2000-03-16 08:12:48 +00001352 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001353 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001354 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001355
Erik Andersend75af992000-03-16 08:09:09 +00001356 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001357 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001358
Eric Andersen86349772000-12-18 20:25:50 +00001359 if (!next_command) {
Denis Vlasenkof443bff2007-03-31 11:08:50 +00001360 if (get_command_bufsiz(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001361 break;
Eric Andersen86349772000-12-18 20:25:50 +00001362 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001363 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001364
Denis Vlasenkof443bff2007-03-31 11:08:50 +00001365 if (!expand_arguments(next_command)) {
Eric Andersenca604592001-03-08 17:17:13 +00001366 free(command);
Rob Landleyf9463792006-06-13 18:50:20 +00001367 command = xzalloc(BUFSIZ);
Eric Andersenca604592001-03-08 17:17:13 +00001368 next_command = NULL;
1369 continue;
1370 }
1371
Denys Vlasenko6b9f1632010-01-28 02:24:24 +01001372 if (!parse_command(&next_command, &newjob, &inbg)
1373 && newjob.num_progs
1374 ) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001375 int pipefds[2] = { -1, -1 };
1376 debug_printf("job=%p fed to run_command by busy_loop()'\n",
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001377 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001378 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001379 }
1380 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001381 free(command);
Denis Vlasenko4cccc032006-12-22 18:37:07 +00001382 command = xzalloc(BUFSIZ);
Eric Andersen86349772000-12-18 20:25:50 +00001383 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001384 }
1385 } else {
1386 /* a job is running in the foreground; wait for it */
1387 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001388 while (!job_list.fg->progs[i].pid ||
1389 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001390
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001391 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED) < 0) {
Eric Andersen7373e482002-07-31 04:04:47 +00001392 if (errno != ECHILD) {
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001393 bb_perror_msg_and_die("waitpid(%d)", job_list.fg->progs[i].pid);
Eric Andersen7373e482002-07-31 04:04:47 +00001394 }
1395 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001396
Erik Andersend75af992000-03-16 08:09:09 +00001397 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001398 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001399 job_list.fg->running_progs--;
1400 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001401
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001402 last_return_code = WEXITSTATUS(status);
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001403
Eric Andersen86349772000-12-18 20:25:50 +00001404 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001405 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001406 remove_job(&job_list, job_list.fg);
1407 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001408 }
Eric Andersen37032b42004-02-10 01:28:36 +00001409 }
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001410#if ENABLE_LASH_JOB_CONTROL
Eric Andersen37032b42004-02-10 01:28:36 +00001411 else {
Erik Andersen161220c2000-03-16 08:12:48 +00001412 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001413 job_list.fg->stopped_progs++;
1414 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001415
Eric Andersen86349772000-12-18 20:25:50 +00001416 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1417 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1418 "Stopped", job_list.fg->text);
1419 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001420 }
Erik Andersend75af992000-03-16 08:09:09 +00001421 }
1422
Eric Andersen86349772000-12-18 20:25:50 +00001423 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001424 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001425 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001426 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Eric Andersenff9ad472004-02-10 01:07:45 +00001427 bb_perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001428 }
Eric Andersen37032b42004-02-10 01:28:36 +00001429#endif
Erik Andersend75af992000-03-16 08:09:09 +00001430 }
1431 }
Erik Andersen161220c2000-03-16 08:12:48 +00001432 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001433
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001434#if ENABLE_LASH_JOB_CONTROL
Eric Andersen1c314ad2000-06-28 16:56:25 +00001435 /* return controlling TTY back to parent process group before exiting */
Eric Andersen7373e482002-07-31 04:04:47 +00001436 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001437 bb_perror_msg("tcsetpgrp");
Eric Andersen37032b42004-02-10 01:28:36 +00001438#endif
Eric Andersenb54833c2000-07-03 23:56:26 +00001439
1440 /* return exit status if called with "-c" */
1441 if (input == NULL && WIFEXITED(status))
1442 return WEXITSTATUS(status);
Eric Andersenff9ad472004-02-10 01:07:45 +00001443
Erik Andersen161220c2000-03-16 08:12:48 +00001444 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001445}
1446
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001447#if ENABLE_FEATURE_CLEAN_UP
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +00001448static void free_memory(void)
Eric Andersenfad9c112000-07-25 18:06:52 +00001449{
Denis Vlasenko7cced6e2007-04-12 17:08:53 +00001450 free(cwd);
Eric Andersenfad9c112000-07-25 18:06:52 +00001451
Eric Andersen86349772000-12-18 20:25:50 +00001452 if (job_list.fg && !job_list.fg->running_progs) {
1453 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001454 }
1455}
Rob Landleyacf448d2006-07-09 17:03:07 +00001456#else
1457void free_memory(void);
Eric Andersenfad9c112000-07-25 18:06:52 +00001458#endif
1459
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001460#if ENABLE_LASH_JOB_CONTROL
Eric Andersen2d848a42001-06-25 17:11:54 +00001461/* Make sure we have a controlling tty. If we get started under a job
1462 * aware app (like bash for example), make sure we are now in charge so
1463 * we don't fight over who gets the foreground */
Eric Andersenece8fc22002-12-11 04:26:28 +00001464static void setup_job_control(void)
Eric Andersen2d848a42001-06-25 17:11:54 +00001465{
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001466 int status;
Eric Andersen37032b42004-02-10 01:28:36 +00001467 pid_t shell_pgrp;
Eric Andersenff9ad472004-02-10 01:07:45 +00001468
Eric Andersen2d848a42001-06-25 17:11:54 +00001469 /* Loop until we are in the foreground. */
Denis Vlasenko831a20f2007-04-12 12:27:32 +00001470 while ((status = tcgetpgrp(shell_terminal)) >= 0) {
1471 shell_pgrp = getpgrp();
1472 if (status == shell_pgrp) {
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001473 break;
1474 }
Denis Vlasenkoff131b92007-04-10 15:42:06 +00001475 kill(- shell_pgrp, SIGTTIN);
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001476 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001477
1478 /* Ignore interactive and job-control signals. */
1479 signal(SIGINT, SIG_IGN);
1480 signal(SIGQUIT, SIG_IGN);
1481 signal(SIGTSTP, SIG_IGN);
1482 signal(SIGTTIN, SIG_IGN);
1483 signal(SIGTTOU, SIG_IGN);
1484 signal(SIGCHLD, SIG_IGN);
1485
1486 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001487 setsid();
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +00001488 shell_pgrp = getpid();
Rob Landleyacf448d2006-07-09 17:03:07 +00001489 setpgid(shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001490
1491 /* Grab control of the terminal. */
1492 tcsetpgrp(shell_terminal, shell_pgrp);
1493}
Eric Andersen37032b42004-02-10 01:28:36 +00001494#else
1495static inline void setup_job_control(void)
1496{
1497}
1498#endif
Eric Andersen6efc48c2000-07-18 08:16:39 +00001499
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001500int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa6899c32007-11-15 17:47:45 +00001501int lash_main(int argc, char **argv)
Erik Andersen3522eb12000-03-12 23:49:18 +00001502{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001503 unsigned opt;
Erik Andersen161220c2000-03-16 08:12:48 +00001504 FILE *input = stdin;
Denis Vlasenkoa6899c32007-11-15 17:47:45 +00001505
1506 global_argc = argc;
1507 global_argv = argv;
Erik Andersen3522eb12000-03-12 23:49:18 +00001508
Denis Vlasenko38f63192007-01-22 09:03:07 +00001509#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001510 line_input_state = new_line_input_t(FOR_SHELL);
1511#endif
1512
Eric Andersen702ec592001-03-06 22:17:29 +00001513 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001514 last_jobid = 0;
Rob Landleyf9463792006-06-13 18:50:20 +00001515 close_me_list = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001516 job_list.head = NULL;
1517 job_list.fg = NULL;
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001518 last_return_code = 1;
Eric Andersen501c88b2000-07-28 15:14:45 +00001519
Denis Vlasenkoa6899c32007-11-15 17:47:45 +00001520 if (global_argv[0] && global_argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001521 FILE *prof_input;
Denis Vlasenko5415c852008-07-21 23:05:26 +00001522 prof_input = fopen_for_read("/etc/profile");
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001523 if (prof_input) {
Rob Landley7a260f02006-06-19 03:20:03 +00001524 llist_add_to(&close_me_list, (void *)(long)fileno(prof_input));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001525 /* Now run the file */
1526 busy_loop(prof_input);
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00001527 fclose_if_not_stdin(prof_input);
Rob Landleyf9463792006-06-13 18:50:20 +00001528 llist_pop(&close_me_list);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001529 }
Eric Andersenb558e762000-11-30 22:43:16 +00001530 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001531
Denis Vlasenkoa6899c32007-11-15 17:47:45 +00001532 opt = getopt32(argv, "+ic:", &local_pending_command);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001533#define LASH_OPT_i (1<<0)
Denis Vlasenkof443bff2007-03-31 11:08:50 +00001534#define LASH_OPT_c (1<<1)
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001535 if (opt & LASH_OPT_c) {
1536 input = NULL;
1537 optind++;
Denis Vlasenkoa6899c32007-11-15 17:47:45 +00001538 global_argv += optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001539 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001540 /* A shell is interactive if the `-i' flag was given, or if all of
1541 * the following conditions are met:
1542 * no -c command
1543 * no arguments remaining or the -s flag given
1544 * standard input is a terminal
1545 * standard output is a terminal
1546 * Refer to Posix.2, the description of the `sh' utility. */
Denis Vlasenkoa6899c32007-11-15 17:47:45 +00001547 if (global_argv[optind] == NULL && input == stdin
Denis Vlasenko6fa8b8e2007-03-31 11:07:30 +00001548 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
1549 ) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001550 opt |= LASH_OPT_i;
Eric Andersen86349772000-12-18 20:25:50 +00001551 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001552 setup_job_control();
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001553 if (opt & LASH_OPT_i) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001554 /* Looks like they want an interactive shell */
Rob Landleyacf448d2006-07-09 17:03:07 +00001555 if (!ENABLE_FEATURE_SH_EXTRA_QUIET) {
Denis Vlasenkoca525b42007-06-13 12:27:17 +00001556 printf("\n\n%s built-in shell (lash)\n"
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001557 "Enter 'help' for a list of built-in commands.\n\n",
Denis Vlasenkoca525b42007-06-13 12:27:17 +00001558 bb_banner);
Rob Landleyacf448d2006-07-09 17:03:07 +00001559 }
Denis Vlasenkoa6899c32007-11-15 17:47:45 +00001560 } else if (!local_pending_command && global_argv[optind]) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001561 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Denis Vlasenko5415c852008-07-21 23:05:26 +00001562 input = xfopen_for_read(global_argv[optind]);
Rob Landleyf9463792006-06-13 18:50:20 +00001563 /* be lazy, never mark this closed */
Rob Landley7a260f02006-06-19 03:20:03 +00001564 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Eric Andersen501c88b2000-07-28 15:14:45 +00001565 }
1566
Eric Andersen6efc48c2000-07-18 08:16:39 +00001567 /* initialize the cwd -- this is never freed...*/
Denis Vlasenko7cced6e2007-04-12 17:08:53 +00001568 update_cwd();
Erik Andersen3522eb12000-03-12 23:49:18 +00001569
Rob Landleyacf448d2006-07-09 17:03:07 +00001570 if (ENABLE_FEATURE_CLEAN_UP) atexit(free_memory);
Eric Andersenfad9c112000-07-25 18:06:52 +00001571
Denis Vlasenko38f63192007-01-22 09:03:07 +00001572 if (ENABLE_FEATURE_EDITING) cmdedit_set_initial_prompt();
Rob Landleyacf448d2006-07-09 17:03:07 +00001573 else PS1 = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +00001574
Denis Vlasenko7cced6e2007-04-12 17:08:53 +00001575 return busy_loop(input);
Erik Andersen3522eb12000-03-12 23:49:18 +00001576}