blob: 52b1174319d0ccd031a9b87be5b3dc77e95beece [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>
Mark Whitley4b541a82001-04-25 17:10:30 +000026#include "cmdedit.h"
Eric Andersene5dfced2001-04-09 22:48:12 +000027
Eric Andersen13d1fa12001-03-08 23:59:45 +000028#include <glob.h>
29#define expand_t glob_t
Erik Andersen3522eb12000-03-12 23:49:18 +000030
Eric Andersenff9ad472004-02-10 01:07:45 +000031/* Always enable for the moment... */
Eric Andersen95b26252004-02-10 01:30:21 +000032#define CONFIG_LASH_PIPE_N_REDIRECTS
33#define CONFIG_LASH_JOB_CONTROL
Eric Andersen13d1fa12001-03-08 23:59:45 +000034
Mark Whitley59ab0252001-01-23 22:30:04 +000035static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000036#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
37
Erik Andersend75af992000-03-16 08:09:09 +000038
Eric Andersenff9ad472004-02-10 01:07:45 +000039#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000040enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000041 REDIRECT_APPEND
42};
Eric Andersenff9ad472004-02-10 01:07:45 +000043#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000044
Rob Landleybc68cd12006-03-10 19:22:06 +000045enum {
46 DEFAULT_CONTEXT = 0x1,
47 IF_TRUE_CONTEXT = 0x2,
48 IF_FALSE_CONTEXT = 0x4,
49 THEN_EXP_CONTEXT = 0x8,
50 ELSE_EXP_CONTEXT = 0x10
51};
Eric Andersenfad9c112000-07-25 18:06:52 +000052
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +000053#define LASH_OPT_DONE (1)
54#define LASH_OPT_SAW_QUOTE (2)
55
Eric Andersenff9ad472004-02-10 01:07:45 +000056#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000057struct redir_struct {
58 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000059 int fd; /* file descriptor being redirected */
60 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000061};
Eric Andersenff9ad472004-02-10 01:07:45 +000062#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000063
Eric Andersen86349772000-12-18 20:25:50 +000064struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000065 pid_t pid; /* 0 if exited */
66 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000067 int num_redirects; /* elements in redirection array */
Eric Andersen86349772000-12-18 20:25:50 +000068 int is_stopped; /* is the program currently running? */
69 struct job *family; /* pointer back to the child's parent job */
Eric Andersenff9ad472004-02-10 01:07:45 +000070#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
71 struct redir_struct *redirects; /* I/O redirects */
72#endif
73};
74
75struct jobset {
76 struct job *head; /* head of list of running jobs */
77 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000078};
79
80struct job {
Eric Andersen86349772000-12-18 20:25:50 +000081 int jobid; /* job number */
82 int num_progs; /* total number of programs in job */
83 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000084 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +000085 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +000086 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +000087 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +000088 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +000089 int stopped_progs; /* number of programs alive, but stopped */
90 unsigned int job_context; /* bitmask defining current context */
91 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +000092};
93
Eric Andersen86349772000-12-18 20:25:50 +000094struct built_in_command {
Erik Andersen161220c2000-03-16 08:12:48 +000095 char *cmd; /* name */
96 char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +000097 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +000098};
99
Eric Andersen34e19412000-07-10 18:47:24 +0000100/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000101static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000102static int builtin_exec(struct child_prog *cmd);
103static int builtin_exit(struct child_prog *cmd);
104static int builtin_fg_bg(struct child_prog *cmd);
105static int builtin_help(struct child_prog *cmd);
106static int builtin_jobs(struct child_prog *dummy);
107static int builtin_pwd(struct child_prog *dummy);
108static int builtin_export(struct child_prog *cmd);
109static int builtin_source(struct child_prog *cmd);
110static int builtin_unset(struct child_prog *cmd);
111static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000112
Eric Andersen34e19412000-07-10 18:47:24 +0000113
114/* function prototypes for shell stuff */
Eric Andersen86349772000-12-18 20:25:50 +0000115static void checkjobs(struct jobset *job_list);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000116static void remove_job(struct jobset *j_list, struct job *job);
Eric Andersen86349772000-12-18 20:25:50 +0000117static int get_command(FILE * source, char *command);
118static int parse_command(char **command_ptr, struct job *job, int *inbg);
119static int run_command(struct job *newjob, int inbg, int outpipe[2]);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000120static int pseudo_exec(struct child_prog *cmd) ATTRIBUTE_NORETURN;
Erik Andersen3522eb12000-03-12 23:49:18 +0000121static int busy_loop(FILE * input);
122
Erik Andersend75af992000-03-16 08:09:09 +0000123
Mark Whitley37653aa2000-07-12 23:36:17 +0000124/* Table of built-in functions (these are non-forking builtins, meaning they
125 * can change global variables in the parent shell process but they will not
126 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Eric Andersen86349772000-12-18 20:25:50 +0000127static struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000128 {"bg", "Resume a job in the background", builtin_fg_bg},
129 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000130 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000131 {"exit", "Exit from shell()", builtin_exit},
132 {"fg", "Bring job into the foreground", builtin_fg_bg},
133 {"jobs", "Lists the active jobs", builtin_jobs},
134 {"export", "Set environment variable", builtin_export},
135 {"unset", "Unset environment variable", builtin_unset},
136 {"read", "Input environment variable", builtin_read},
Matt Kraaidd450a02000-09-13 03:43:36 +0000137 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000138 /* to do: add ulimit */
Eric Andersenfad9c112000-07-25 18:06:52 +0000139 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000140};
141
Mark Whitley37653aa2000-07-12 23:36:17 +0000142/* Table of forking built-in functions (things that fork cannot change global
143 * variables in the parent process, such as the current working directory) */
Eric Andersen86349772000-12-18 20:25:50 +0000144static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000145 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000146 {"help", "List shell built-in commands", builtin_help},
147 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000148};
149
Eric Andersen0bcc8132001-01-05 19:37:32 +0000150
Eric Andersen7467c8d2001-07-12 20:26:32 +0000151static int shell_context; /* Type prompt trigger (PS1 or PS2) */
Eric Andersen0bcc8132001-01-05 19:37:32 +0000152
153
154/* Globals that are static to this file */
Eric Andersencfa88ec2001-05-11 18:08:16 +0000155static const char *cwd;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000156static char *local_pending_command;
Eric Andersen86349772000-12-18 20:25:50 +0000157static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000158static int argc;
159static char **argv;
Rob Landleyf9463792006-06-13 18:50:20 +0000160static llist_t *close_me_list;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000161static int last_return_code;
162static int last_bg_pid;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000163static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000164static int shell_terminal;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000165static char *PS1;
Eric Andersene5dfced2001-04-09 22:48:12 +0000166static char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000167
168
Eric Andersenb558e762000-11-30 22:43:16 +0000169#ifdef DEBUG_SHELL
170static inline void debug_printf(const char *format, ...)
171{
172 va_list args;
173 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000174 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000175 va_end(args);
176}
177#else
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000178static inline void debug_printf(const char ATTRIBUTE_UNUSED *format, ...) { }
Eric Andersenb558e762000-11-30 22:43:16 +0000179#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000180
Eric Andersen86349772000-12-18 20:25:50 +0000181/*
182 Most builtins need access to the struct child_prog that has
183 their arguments, previously coded as cmd->progs[0]. That coding
184 can exhibit a bug, if the builtin is not the first command in
185 a pipeline: "echo foo | exec sort" will attempt to exec foo.
186
187builtin previous use notes
188------ ----------------- ---------
189cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000190exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
191exit cmd->progs[0]
192fg_bg cmd->progs[0], job_list->head, job_list->fg
193help 0
194jobs job_list->head
195pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000196export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000197source cmd->progs[0]
198unset cmd->progs[0]
199read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000200
201I added "struct job *family;" to struct child_prog,
202and switched API to builtin_foo(struct child_prog *child);
203So cmd->text becomes child->family->text
204 cmd->job_context becomes child->family->job_context
205 cmd->progs[0] becomes *child
206 job_list becomes child->family->job_list
207 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000208
Erik Andersend75af992000-03-16 08:09:09 +0000209/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000210static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000211{
Erik Andersen161220c2000-03-16 08:12:48 +0000212 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000213
Eric Andersen86349772000-12-18 20:25:50 +0000214 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000215 newdir = getenv("HOME");
216 else
Eric Andersen86349772000-12-18 20:25:50 +0000217 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000218 if (chdir(newdir)) {
Rob Landley5483de12006-06-20 21:35:26 +0000219 bb_perror_msg("cd: %s", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000220 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000221 }
Eric Andersencfa88ec2001-05-11 18:08:16 +0000222 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000223 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000224 cwd = bb_msg_unknown;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000225 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000226}
227
Eric Andersend2f56772000-09-21 02:48:07 +0000228/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000229static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000230{
Eric Andersen86349772000-12-18 20:25:50 +0000231 if (child->argv[1] == NULL)
232 return EXIT_SUCCESS; /* Really? */
233 child->argv++;
Rob Landley7a260f02006-06-19 03:20:03 +0000234 while(close_me_list) close((long)llist_pop(&close_me_list));
Eric Andersen86349772000-12-18 20:25:50 +0000235 pseudo_exec(child);
236 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000237}
238
Erik Andersen3522eb12000-03-12 23:49:18 +0000239/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000240static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000241{
Eric Andersen86349772000-12-18 20:25:50 +0000242 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000243 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000244
Eric Andersen86349772000-12-18 20:25:50 +0000245 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000246}
247
248/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000249static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000250{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000251 int i, jobnum;
Erik Andersen6273f652000-03-17 01:12:41 +0000252 struct job *job=NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000253
Eric Andersen8a646dd2001-06-21 16:38:11 +0000254 /* If they gave us no args, assume they want the last backgrounded task */
255 if (!child->argv[1]) {
256 for (job = child->family->job_list->head; job; job = job->next) {
257 if (job->jobid == last_jobid) {
258 break;
259 }
Erik Andersen161220c2000-03-16 08:12:48 +0000260 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000261 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000262 bb_error_msg("%s: no current job", child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000263 return EXIT_FAILURE;
264 }
265 } else {
266 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000267 bb_error_msg(bb_msg_invalid_arg, child->argv[1], child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000268 return EXIT_FAILURE;
269 }
270 for (job = child->family->job_list->head; job; job = job->next) {
271 if (job->jobid == jobnum) {
272 break;
273 }
274 }
275 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000276 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000277 return EXIT_FAILURE;
278 }
Erik Andersend75af992000-03-16 08:09:09 +0000279 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000280
Eric Andersen86349772000-12-18 20:25:50 +0000281 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000282 /* Put the job into the foreground. */
283 tcsetpgrp(shell_terminal, job->pgrp);
284
Eric Andersen86349772000-12-18 20:25:50 +0000285 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000286 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000287
Erik Andersen161220c2000-03-16 08:12:48 +0000288 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000289 for (i = 0; i < job->num_progs; i++)
290 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000291
Eric Andersen86349772000-12-18 20:25:50 +0000292 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000293
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000294 if ( (i=kill(- job->pgrp, SIGCONT)) < 0) {
295 if (i == ESRCH) {
296 remove_job(&job_list, job);
Eric Andersen07abfe22001-06-27 17:29:11 +0000297 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000298 bb_perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000299 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000300 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000301
Matt Kraai3e856ce2000-12-01 02:55:13 +0000302 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000303}
304
305/* built-in 'help' handler */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000306static int builtin_help(struct child_prog ATTRIBUTE_UNUSED *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000307{
Eric Andersen86349772000-12-18 20:25:50 +0000308 struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000309
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000310 printf("\nBuilt-in commands:\n"
311 "-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000312 for (x = bltins; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000313 if (x->descr==NULL)
314 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000315 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000316 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000317 for (x = bltins_forking; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000318 if (x->descr==NULL)
319 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000320 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000321 }
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000322 putchar('\n');
Matt Kraai3e856ce2000-12-01 02:55:13 +0000323 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000324}
325
326/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000327static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000328{
Erik Andersen161220c2000-03-16 08:12:48 +0000329 struct job *job;
Eric Andersen86349772000-12-18 20:25:50 +0000330 char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000331
Eric Andersen86349772000-12-18 20:25:50 +0000332 for (job = child->family->job_list->head; job; job = job->next) {
333 if (job->running_progs == job->stopped_progs)
334 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000335 else
Eric Andersen86349772000-12-18 20:25:50 +0000336 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000337
Eric Andersen86349772000-12-18 20:25:50 +0000338 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000339 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000340 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000341}
342
343
344/* built-in 'pwd' handler */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000345static int builtin_pwd(struct child_prog ATTRIBUTE_UNUSED *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000346{
Eric Andersencfa88ec2001-05-11 18:08:16 +0000347 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000348 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000349 cwd = bb_msg_unknown;
Matt Kraai59df6f72001-05-16 14:21:09 +0000350 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000351 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000352}
353
Erik Andersen6273f652000-03-17 01:12:41 +0000354/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000355static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000356{
Erik Andersen161220c2000-03-16 08:12:48 +0000357 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000358 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000359
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000360 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000361 char **e;
362 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000363 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000364 }
Matt Kraai2129f972001-04-04 17:50:04 +0000365 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000366 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000367 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000368 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000369 bb_perror_msg("export");
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000370#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000371 if (strncmp(v, "PS1=", 4)==0)
372 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000373#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000374
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000375#ifdef CONFIG_LOCALE_SUPPORT
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000376 // TODO: why getenv? "" would be just as good...
Mark Whitley1c6581a2001-03-27 16:35:16 +0000377 if(strncmp(v, "LC_ALL=", 7)==0)
378 setlocale(LC_ALL, getenv("LC_ALL"));
Mark Whitleya82a0032001-03-27 17:07:15 +0000379 if(strncmp(v, "LC_CTYPE=", 9)==0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000380 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000381#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000382
Denis Vlasenko097c3242006-11-27 16:59:15 +0000383 return res;
Erik Andersen3522eb12000-03-12 23:49:18 +0000384}
385
Eric Andersenb54833c2000-07-03 23:56:26 +0000386/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000387static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000388{
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000389 int res = 0, len;
Eric Andersenb54833c2000-07-03 23:56:26 +0000390 char *s;
391 char string[MAX_READ];
392
Eric Andersen86349772000-12-18 20:25:50 +0000393 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000394 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen0d2d1eb2002-06-06 13:33:01 +0000395 safe_strncpy(string, child->argv[1], MAX_READ-1);
Eric Andersenb54833c2000-07-03 23:56:26 +0000396 len = strlen(string);
397 string[len++] = '=';
398 string[len] = '\0';
399 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000400 res = strlen(string);
401 if (res > len)
402 string[--res] = '\0'; /* chomp trailing newline */
Eric Andersenb54833c2000-07-03 23:56:26 +0000403 /*
404 ** string should now contain "VAR=<value>"
405 ** copy it (putenv() won't do that, so we must make sure
406 ** the string resides in a static buffer!)
407 */
408 res = -1;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000409 if ((s = strdup(string)))
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");
Eric Andersenb54833c2000-07-03 23:56:26 +0000413 }
414 else
415 fgets(string, sizeof(string), stdin);
416
Denis Vlasenko097c3242006-11-27 16:59:15 +0000417 return res;
Eric Andersenb54833c2000-07-03 23:56:26 +0000418}
419
Erik Andersen3522eb12000-03-12 23:49:18 +0000420/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000421static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000422{
Erik Andersen161220c2000-03-16 08:12:48 +0000423 FILE *input;
424 int status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000425
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000426 input = fopen_or_warn(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000427 if (!input) {
Matt Kraai3e856ce2000-12-01 02:55:13 +0000428 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000429 }
Erik Andersend75af992000-03-16 08:09:09 +0000430
Rob Landley7a260f02006-06-19 03:20:03 +0000431 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Erik Andersen161220c2000-03-16 08:12:48 +0000432 /* Now run the file */
433 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000434 fclose(input);
Rob Landleyf9463792006-06-13 18:50:20 +0000435 llist_pop(&close_me_list);
Denis Vlasenko097c3242006-11-27 16:59:15 +0000436 return status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000437}
438
439/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000440static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000441{
Eric Andersen86349772000-12-18 20:25:50 +0000442 if (child->argv[1] == NULL) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000443 printf(bb_msg_requires_arg, "unset");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000444 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000445 }
Eric Andersen86349772000-12-18 20:25:50 +0000446 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000447 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000448}
449
Eric Andersen37032b42004-02-10 01:28:36 +0000450#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen3522eb12000-03-12 23:49:18 +0000451/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000452static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000453{
Erik Andersen161220c2000-03-16 08:12:48 +0000454 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000455 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000456
Eric Andersen86349772000-12-18 20:25:50 +0000457 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000458 free(cmd->progs[i].argv);
Eric Andersenff9ad472004-02-10 01:07:45 +0000459#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000460 if (cmd->progs[i].redirects)
461 free(cmd->progs[i].redirects);
Eric Andersenff9ad472004-02-10 01:07:45 +0000462#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000463 }
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000464 free(cmd->progs);
465 free(cmd->text);
466 free(cmd->cmdbuf);
Mark Whitley44a99142001-03-14 17:26:37 +0000467 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000468 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000469 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000470}
471
Eric Andersen1ca20a72001-03-21 07:34:27 +0000472/* remove a job from a jobset */
473static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000474{
Eric Andersen86349772000-12-18 20:25:50 +0000475 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000476
Eric Andersen86349772000-12-18 20:25:50 +0000477 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000478 if (job == j_list->head) {
479 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000480 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000481 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000482 while (prevjob->next != job)
483 prevjob = prevjob->next;
484 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000485 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000486
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000487 if (j_list->head)
488 last_jobid = j_list->head->jobid;
489 else
490 last_jobid = 0;
491
Erik Andersen161220c2000-03-16 08:12:48 +0000492 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000493}
494
Eric Andersenff9ad472004-02-10 01:07:45 +0000495/* Checks to see if any background processes have exited -- if they
Erik Andersen3522eb12000-03-12 23:49:18 +0000496 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000497static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000498{
Erik Andersen161220c2000-03-16 08:12:48 +0000499 struct job *job;
500 pid_t childpid;
501 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000502 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000503
Erik Andersen161220c2000-03-16 08:12:48 +0000504 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000505 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000506 prognum = 0;
507 while (prognum < job->num_progs &&
508 job->progs[prognum].pid != childpid) prognum++;
509 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000510 break;
511 }
512
Eric Andersena1d187a2000-07-17 19:14:41 +0000513 /* This happens on backticked commands */
514 if(job==NULL)
515 return;
516
Erik Andersen161220c2000-03-16 08:12:48 +0000517 if (WIFEXITED(status) || WIFSIGNALED(status)) {
518 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000519 job->running_progs--;
520 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000521
Eric Andersen86349772000-12-18 20:25:50 +0000522 if (!job->running_progs) {
523 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000524 last_jobid=0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000525 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000526 }
527 } else {
528 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000529 job->stopped_progs++;
530 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000531 }
Erik Andersend75af992000-03-16 08:09:09 +0000532 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000533
Erik Andersen161220c2000-03-16 08:12:48 +0000534 if (childpid == -1 && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000535 bb_perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000536}
Eric Andersen37032b42004-02-10 01:28:36 +0000537#else
538static void checkjobs(struct jobset *j_list)
539{
540}
541static void free_job(struct job *cmd)
542{
543}
544static void remove_job(struct jobset *j_list, struct job *job)
545{
546}
547#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000548
Eric Andersenff9ad472004-02-10 01:07:45 +0000549#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersene9f07fb2000-12-21 18:31:36 +0000550/* squirrel != NULL means we squirrel away copies of stdin, stdout,
551 * and stderr if they are redirected. */
552static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000553{
554 int i;
555 int openfd;
556 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000557 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000558
Eric Andersen86349772000-12-18 20:25:50 +0000559 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000560 switch (redir->type) {
561 case REDIRECT_INPUT:
562 mode = O_RDONLY;
563 break;
564 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000565 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000566 break;
567 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000568 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000569 break;
570 }
571
572 openfd = open(redir->filename, mode, 0666);
573 if (openfd < 0) {
574 /* this could get lost if stderr has been redirected, but
575 bash and ash both lose it as well (though zsh doesn't!) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000576 bb_perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000577 return 1;
578 }
579
580 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000581 if (squirrel && redir->fd < 3) {
582 squirrel[redir->fd] = dup(redir->fd);
Eric Andersenacd647c2004-08-16 08:38:34 +0000583 fcntl (squirrel[redir->fd], F_SETFD, FD_CLOEXEC);
Eric Andersene9f07fb2000-12-21 18:31:36 +0000584 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000585 dup2(openfd, redir->fd);
586 close(openfd);
587 }
588 }
589
590 return 0;
591}
592
Eric Andersene9f07fb2000-12-21 18:31:36 +0000593static void restore_redirects(int squirrel[])
594{
595 int i, fd;
596 for (i=0; i<3; i++) {
597 fd = squirrel[i];
598 if (fd != -1) {
599 /* No error checking. I sure wouldn't know what
600 * to do with an error if I found one! */
601 dup2(fd, i);
602 close(fd);
603 }
604 }
605}
Eric Andersenff9ad472004-02-10 01:07:45 +0000606#else
607static inline int setup_redirects(struct child_prog *prog, int squirrel[])
608{
609 return 0;
610}
611static inline void restore_redirects(int squirrel[])
612{
613}
614#endif
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000615
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000616static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000617{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000618#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000619 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000620#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000621 PS1 = getenv("PS1");
Eric Andersene5dfced2001-04-09 22:48:12 +0000622 if(PS1==0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000623 PS1 = "\\w \\$ ";
Eric Andersenff9ad472004-02-10 01:07:45 +0000624#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000625}
626
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000627static inline void setup_prompt_string(char **prompt_str)
628{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000629#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000630 /* Set up the prompt */
631 if (shell_context == 0) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000632 free(PS1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000633 PS1=xmalloc(strlen(cwd)+4);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000634 sprintf(PS1, "%s %c ", cwd, ( geteuid() != 0 ) ? '$': '#');
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000635 *prompt_str = PS1;
636 } else {
637 *prompt_str = PS2;
638 }
639#else
640 *prompt_str = (shell_context==0)? PS1 : PS2;
Eric Andersenff9ad472004-02-10 01:07:45 +0000641#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000642}
Eric Andersen22332fd2001-01-30 23:40:39 +0000643
Eric Andersen09acc062001-01-04 11:10:38 +0000644static int get_command(FILE * source, char *command)
645{
646 char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000647
Eric Andersen1c314ad2000-06-28 16:56:25 +0000648 if (source == NULL) {
649 if (local_pending_command) {
650 /* a command specified (-c option): return it & mark it done */
651 strcpy(command, local_pending_command);
652 free(local_pending_command);
653 local_pending_command = NULL;
654 return 0;
655 }
656 return 1;
657 }
658
Erik Andersen161220c2000-03-16 08:12:48 +0000659 if (source == stdin) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000660 setup_prompt_string(&prompt_str);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000661
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000662#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000663 /*
664 ** enable command line editing only while a command line
665 ** is actually being read; otherwise, we'll end up bequeathing
666 ** atexit() handlers and other unwanted stuff to our
667 ** child processes (rob@sysgo.de)
668 */
Eric Andersen86349772000-12-18 20:25:50 +0000669 cmdedit_read_input(prompt_str, command);
Erik Andersen161220c2000-03-16 08:12:48 +0000670 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000671#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000672 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000673#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000674 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000675
Erik Andersen161220c2000-03-16 08:12:48 +0000676 if (!fgets(command, BUFSIZ - 2, source)) {
677 if (source == stdin)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000678 puts("");
Erik Andersen161220c2000-03-16 08:12:48 +0000679 return 1;
680 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000681
Erik Andersen161220c2000-03-16 08:12:48 +0000682 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000683}
684
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +0000685static char * strsep_space( char *string, int * ix)
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000686{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000687 /* Short circuit the trivial case */
688 if ( !string || ! string[*ix])
689 return NULL;
690
691 /* Find the end of the token. */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000692 while (string[*ix] && !isspace(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000693 (*ix)++;
694 }
695
Eric Andersenff9ad472004-02-10 01:07:45 +0000696 /* Find the end of any whitespace trailing behind
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000697 * the token and let that be part of the token */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000698 while (string[*ix] && (isspace)(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000699 (*ix)++;
700 }
701
Rob Landleyf9463792006-06-13 18:50:20 +0000702 if (!*ix) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000703 /* Nothing useful was found */
704 return NULL;
705 }
706
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000707 return xstrndup(string, *ix);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000708}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000709
Eric Andersenca604592001-03-08 17:17:13 +0000710static int expand_arguments(char *command)
711{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000712 int total_length=0, length, i, retval, ix = 0;
713 expand_t expand_result;
714 char *tmpcmd, *cmd, *cmd_copy;
715 char *src, *dst, *var;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000716 const char * const out_of_space = "out of space during expansion";
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000717 int flags = GLOB_NOCHECK
718#ifdef GLOB_BRACE
719 | GLOB_BRACE
Eric Andersenff9ad472004-02-10 01:07:45 +0000720#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000721#ifdef GLOB_TILDE
722 | GLOB_TILDE
Eric Andersenff9ad472004-02-10 01:07:45 +0000723#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000724 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000725
Eric Andersenca604592001-03-08 17:17:13 +0000726 /* get rid of the terminating \n */
727 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000728
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000729 /* Fix up escape sequences to be the Real Thing(tm) */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000730 while( command && command[ix]) {
731 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000732 const char *tmp = command+ix+1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000733 command[ix] = bb_process_escape_sequence( &tmp );
Eric Andersen1ca20a72001-03-21 07:34:27 +0000734 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000735 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000736 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000737 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000738 /* Use glob and then fixup environment variables and such */
739
740 /* It turns out that glob is very stupid. We have to feed it one word at a
741 * time since it can't cope with a full string. Here we convert command
742 * (char*) into cmd (char**, one word per string) */
743
744 /* We need a clean copy, so strsep can mess up the copy while
745 * we write stuff into the original (in a minute) */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000746 cmd = cmd_copy = xstrdup(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000747 *command = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000748 for (ix = 0, tmpcmd = cmd;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000749 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
750 if (*tmpcmd == '\0')
751 break;
752 /* we need to trim() the result for glob! */
753 trim(tmpcmd);
754 retval = glob(tmpcmd, flags, NULL, &expand_result);
755 free(tmpcmd); /* Free mem allocated by strsep_space */
756 if (retval == GLOB_NOSPACE) {
757 /* Mem may have been allocated... */
758 globfree (&expand_result);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000759 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000760 return FALSE;
761 } else if (retval != 0) {
762 /* Some other error. GLOB_NOMATCH shouldn't
Eric Andersenff9ad472004-02-10 01:07:45 +0000763 * happen because of the GLOB_NOCHECK flag in
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000764 * the glob call. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000765 bb_error_msg("syntax error");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000766 return FALSE;
767 } else {
768 /* Convert from char** (one word per string) to a simple char*,
769 * but don't overflow command which is BUFSIZ in length */
770 for (i=0; i < expand_result.gl_pathc; i++) {
771 length=strlen(expand_result.gl_pathv[i]);
772 if (total_length+length+1 >= BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000773 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000774 return FALSE;
775 }
776 strcat(command+total_length, " ");
777 total_length+=1;
778 strcat(command+total_length, expand_result.gl_pathv[i]);
779 total_length+=length;
780 }
781 globfree (&expand_result);
782 }
783 }
784 free(cmd_copy);
785 trim(command);
786
Eric Andersenff9ad472004-02-10 01:07:45 +0000787 /* Now do the shell variable substitutions which
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000788 * wordexp can't do for us, namely $? and $! */
789 src = command;
790 while((dst = strchr(src,'$')) != NULL){
791 var = NULL;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000792 switch (*(dst+1)) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000793 case '?':
794 var = itoa(last_return_code);
795 break;
796 case '!':
797 if (last_bg_pid==-1)
798 *(var)='\0';
799 else
800 var = itoa(last_bg_pid);
801 break;
802 /* Everything else like $$, $#, $[0-9], etc. should all be
803 * expanded by wordexp(), so we can in theory skip that stuff
804 * here, but just to be on the safe side (i.e., since uClibc
805 * wordexp doesn't do this stuff yet), lets leave it in for
806 * now. */
807 case '$':
808 var = itoa(getpid());
809 break;
810 case '#':
811 var = itoa(argc-1);
812 break;
813 case '0':case '1':case '2':case '3':case '4':
814 case '5':case '6':case '7':case '8':case '9':
815 {
Eric Andersen7ab93d92002-11-08 09:40:02 +0000816 int ixx=*(dst+1)-48+1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000817 if (ixx >= argc) {
818 var='\0';
819 } else {
820 var = argv[ixx];
821 }
822 }
823 break;
824
825 }
826 if (var) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000827 /* a single character construction was found, and
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000828 * already handled in the case statement */
829 src=dst+2;
830 } else {
831 /* Looks like an environment variable */
832 char delim_hold;
833 int num_skip_chars=0;
834 int dstlen = strlen(dst);
835 /* Is this a ${foo} type variable? */
836 if (dstlen >=2 && *(dst+1) == '{') {
837 src=strchr(dst+1, '}');
838 num_skip_chars=1;
839 } else {
840 src=dst+1;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000841 while((isalnum)(*src) || *src=='_') src++;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000842 }
843 if (src == NULL) {
844 src = dst+dstlen;
845 }
846 delim_hold=*src;
847 *src='\0'; /* temporary */
848 var = getenv(dst + 1 + num_skip_chars);
849 *src=delim_hold;
850 src += num_skip_chars;
851 }
852 if (var == NULL) {
853 /* Seems we got an un-expandable variable. So delete it. */
854 var = "";
855 }
856 {
857 int subst_len = strlen(var);
858 int trail_len = strlen(src);
859 if (dst+subst_len+trail_len >= command+BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000860 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000861 return FALSE;
862 }
863 /* Move stuff to the end of the string to accommodate
864 * filling the created gap with the new stuff */
865 memmove(dst+subst_len, src, trail_len+1);
866 /* Now copy in the new stuff */
867 memcpy(dst, var, subst_len);
868 src = dst+subst_len;
869 }
870 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000871
Eric Andersenca604592001-03-08 17:17:13 +0000872 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000873}
874
Eric Andersen86349772000-12-18 20:25:50 +0000875/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
876 line). If a valid command is found, command_ptr is set to point to
Eric Andersenff9ad472004-02-10 01:07:45 +0000877 the beginning of the next command (if the original command had more
878 then one job associated with it) or NULL if no more commands are
Erik Andersen3522eb12000-03-12 23:49:18 +0000879 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000880static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000881{
Erik Andersen161220c2000-03-16 08:12:48 +0000882 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000883 char *return_command = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +0000884 char *src, *buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000885 int argc_l;
886 int flag;
Eric Andersen86349772000-12-18 20:25:50 +0000887 int argv_alloced;
Erik Andersen161220c2000-03-16 08:12:48 +0000888 char quote = '\0';
Eric Andersen86349772000-12-18 20:25:50 +0000889 struct child_prog *prog;
Eric Andersenff9ad472004-02-10 01:07:45 +0000890#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
891 int i;
892 char *chptr;
893#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000894
Erik Andersen161220c2000-03-16 08:12:48 +0000895 /* skip leading white space */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000896 *command_ptr = skip_whitespace(*command_ptr);
Erik Andersen3522eb12000-03-12 23:49:18 +0000897
Erik Andersen161220c2000-03-16 08:12:48 +0000898 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000899 if (!**command_ptr || (**command_ptr == '#')) {
900 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000901 return 0;
902 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000903
Eric Andersen86349772000-12-18 20:25:50 +0000904 *inbg = 0;
905 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000906 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000907
Eric Andersenff9ad472004-02-10 01:07:45 +0000908 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000909 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000910 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000911
Eric Andersenff9ad472004-02-10 01:07:45 +0000912 Getting clean memory relieves us of the task of NULL
913 terminating things and makes the rest of this look a bit
Erik Andersen161220c2000-03-16 08:12:48 +0000914 cleaner (though it is, admittedly, a tad less efficient) */
Rob Landleyf9463792006-06-13 18:50:20 +0000915 job->cmdbuf = command = xzalloc(2*strlen(*command_ptr) + 1);
Erik Andersen161220c2000-03-16 08:12:48 +0000916 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000917
Erik Andersen161220c2000-03-16 08:12:48 +0000918 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000919 prog->num_redirects = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000920 prog->is_stopped = 0;
921 prog->family = job;
Eric Andersenff9ad472004-02-10 01:07:45 +0000922#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
923 prog->redirects = NULL;
924#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000925
Eric Andersen86349772000-12-18 20:25:50 +0000926 argv_alloced = 5;
927 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
928 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +0000929
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000930 flag = argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000931 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +0000932 src = *command_ptr;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000933 while (*src && !(flag & LASH_OPT_DONE)) {
Erik Andersen161220c2000-03-16 08:12:48 +0000934 if (quote == *src) {
935 quote = '\0';
936 } else if (quote) {
937 if (*src == '\\') {
938 src++;
939 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000940 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +0000941 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +0000942 return 1;
943 }
944
945 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +0000946 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +0000947 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +0000948 *buf++ = '\\';
949 }
Erik Andersen161220c2000-03-16 08:12:48 +0000950 } else if (*src == '*' || *src == '?' || *src == '[' ||
951 *src == ']') *buf++ = '\\';
952 *buf++ = *src;
953 } else if (isspace(*src)) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000954 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000955 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +0000956 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +0000957 if ((argc_l + 1) == argv_alloced) {
958 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +0000959 prog->argv = xrealloc(prog->argv,
960 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +0000961 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +0000962 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000963 prog->argv[argc_l] = buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000964 flag ^= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000965 }
966 } else
967 switch (*src) {
968 case '"':
969 case '\'':
970 quote = *src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000971 flag |= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000972 break;
973
974 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000975 if (*(src-1)== '$')
976 *buf++ = *src;
977 else
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000978 flag |= LASH_OPT_DONE;
Erik Andersen161220c2000-03-16 08:12:48 +0000979 break;
980
Eric Andersenff9ad472004-02-10 01:07:45 +0000981#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000982 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +0000983 case '<':
Eric Andersen86349772000-12-18 20:25:50 +0000984 i = prog->num_redirects++;
985 prog->redirects = xrealloc(prog->redirects,
986 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +0000987 (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);
1026 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,
Eric Andersen86349772000-12-18 20:25:50 +00001050 sizeof(*job->progs) * job->num_progs);
1051 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);
1069 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
Eric Andersen37032b42004-02-10 01:28:36 +00001077#ifdef CONFIG_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{
Eric Andersen86349772000-12-18 20:25:50 +00001129 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 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001136 for (x = bltins; x->cmd; x++) {
1137 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001138 _exit(x->function(child));
Eric Andersene9f07fb2000-12-21 18:31:36 +00001139 }
1140 }
1141
1142 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001143 for (x = bltins_forking; x->cmd; x++) {
1144 if (strcmp(child->argv[0], x->cmd) == 0) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001145 applet_name=x->cmd;
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001146 _exit (x->function(child));
Eric Andersen86349772000-12-18 20:25:50 +00001147 }
1148 }
Rob Landley68286772006-06-13 19:01:30 +00001149
Eric Andersen86349772000-12-18 20:25:50 +00001150 /* Check if the command matches any busybox internal
1151 * commands ("applets") here. Following discussions from
Eric Andersen2423b122001-12-08 01:56:15 +00001152 * November 2000 on busybox@busybox.net, don't use
Manuel Novoa III cad53642003-03-19 09:13:01 +00001153 * bb_get_last_path_component(). This way explicit (with
Eric Andersen86349772000-12-18 20:25:50 +00001154 * slashes) filenames will never be interpreted as an
1155 * applet, just like with builtins. This way the user can
1156 * override an applet with an explicit filename reference.
1157 * The only downside to this change is that an explicit
1158 * /bin/foo invocation will fork and exec /bin/foo, even if
1159 * /bin/foo is a symlink to busybox.
1160 */
Eric Andersen86349772000-12-18 20:25:50 +00001161
Rob Landley68286772006-06-13 19:01:30 +00001162 if (ENABLE_FEATURE_SH_STANDALONE_SHELL) {
1163 char **argv_l = child->argv;
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001164 int argc_l;
Bernhard Reutner-Fischer62558762006-06-03 10:28:25 +00001165
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001166 for (argc_l=0; *argv_l; argv_l++, argc_l++);
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001167 optind = 1;
Rob Landley68286772006-06-13 19:01:30 +00001168 run_applet_by_name(child->argv[0], argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001169 }
Eric Andersen86349772000-12-18 20:25:50 +00001170
1171 execvp(child->argv[0], child->argv);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001172
Eric Andersenff9ad472004-02-10 01:07:45 +00001173 /* Do not use bb_perror_msg_and_die() here, since we must not
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001174 * call exit() but should call _exit() instead */
Rob Landley5483de12006-06-20 21:35:26 +00001175 bb_perror_msg("%s", child->argv[0]);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001176 _exit(EXIT_FAILURE);
Eric Andersen86349772000-12-18 20:25:50 +00001177}
1178
1179static void insert_job(struct job *newjob, int inbg)
1180{
1181 struct job *thejob;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001182 struct jobset *j_list=newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001183
1184 /* find the ID for thejob to use */
1185 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001186 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001187 if (thejob->jobid >= newjob->jobid)
1188 newjob->jobid = thejob->jobid + 1;
1189
1190 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001191 if (!j_list->head) {
1192 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001193 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001194 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001195 thejob->next = xmalloc(sizeof(*thejob));
1196 thejob = thejob->next;
1197 }
1198
1199 *thejob = *newjob; /* physically copy the struct job */
1200 thejob->next = NULL;
1201 thejob->running_progs = thejob->num_progs;
1202 thejob->stopped_progs = 0;
1203
Eric Andersen37032b42004-02-10 01:28:36 +00001204#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen86349772000-12-18 20:25:50 +00001205 if (inbg) {
Eric Andersenff9ad472004-02-10 01:07:45 +00001206 /* we don't wait for background thejobs to return -- append it
Eric Andersen86349772000-12-18 20:25:50 +00001207 to the list of backgrounded thejobs and leave it alone */
1208 printf("[%d] %d\n", thejob->jobid,
1209 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001210 last_jobid = newjob->jobid;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001211 last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001212 } else {
1213 newjob->job_list->fg = thejob;
1214
1215 /* move the new process group into the foreground */
Rob Landleyf9463792006-06-13 18:50:20 +00001216 /* Ignore errors since child could have already exited */
1217 tcsetpgrp(shell_terminal, newjob->pgrp);
Eric Andersen86349772000-12-18 20:25:50 +00001218 }
Eric Andersen37032b42004-02-10 01:28:36 +00001219#endif
Eric Andersen86349772000-12-18 20:25:50 +00001220}
1221
1222static int run_command(struct job *newjob, int inbg, int outpipe[2])
1223{
1224 /* struct job *thejob; */
1225 int i;
1226 int nextin, nextout;
1227 int pipefds[2]; /* pipefd[0] is for reading */
1228 struct built_in_command *x;
1229 struct child_prog *child;
1230
Eric Andersen6efc48c2000-07-18 08:16:39 +00001231 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001232 for (i = 0; i < newjob->num_progs; i++) {
1233 child = & (newjob->progs[i]);
1234
1235 if ((i + 1) < newjob->num_progs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001236 if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001237 nextout = pipefds[1];
1238 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001239 if (outpipe[1]!=-1) {
1240 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001241 } else {
1242 nextout = 1;
1243 }
Erik Andersen161220c2000-03-16 08:12:48 +00001244 }
1245
Eric Andersen501c88b2000-07-28 15:14:45 +00001246
Eric Andersene9f07fb2000-12-21 18:31:36 +00001247 /* Check if the command matches any non-forking builtins,
1248 * but only if this is a simple command.
1249 * Non-forking builtins within pipes have to fork anyway,
1250 * and are handled in pseudo_exec. "echo foo | read bar"
1251 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001252 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001253 if (newjob->num_progs == 1) {
Rob Landley7a8e4912006-07-01 17:19:02 +00001254 /* Check if the command sets an environment variable. */
1255 if (strchr(child->argv[0], '=') != NULL) {
1256 child->argv[1] = child->argv[0];
1257 return builtin_export(child);
1258 }
1259
Eric Andersene9f07fb2000-12-21 18:31:36 +00001260 for (x = bltins; x->cmd; x++) {
1261 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersene9f07fb2000-12-21 18:31:36 +00001262 int rcode;
Eric Andersenff9ad472004-02-10 01:07:45 +00001263 int squirrel[] = {-1, -1, -1};
Eric Andersene9f07fb2000-12-21 18:31:36 +00001264 setup_redirects(child, squirrel);
1265 rcode = x->function(child);
1266 restore_redirects(squirrel);
1267 return rcode;
1268 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001269 }
1270 }
1271
Eric Andersene3efc922004-04-12 17:59:24 +00001272#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersenff9ad472004-02-10 01:07:45 +00001273 if (!(child->pid = fork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001274#else
Eric Andersenff9ad472004-02-10 01:07:45 +00001275 if (!(child->pid = vfork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001276#endif
1277 {
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. */
Rob Landley7a260f02006-06-19 03:20:03 +00001287 while(close_me_list) close((long)llist_pop(&close_me_list));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001288
Eric Andersen86349772000-12-18 20:25:50 +00001289 if (outpipe[1]!=-1) {
1290 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001291 }
1292 if (nextin != 0) {
1293 dup2(nextin, 0);
1294 close(nextin);
1295 }
1296
1297 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001298 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001299 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001300 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001301 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001302 }
1303
Eric Andersen86349772000-12-18 20:25:50 +00001304 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001305 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001306
Eric Andersen86349772000-12-18 20:25:50 +00001307 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001308 }
Eric Andersen86349772000-12-18 20:25:50 +00001309 if (outpipe[1]!=-1) {
1310 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001311 }
Erik Andersen161220c2000-03-16 08:12:48 +00001312
1313 /* put our child in the process group whose leader is the
1314 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001315 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001316 if (nextin != 0)
1317 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001318 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001319 close(nextout);
1320
Eric Andersenff9ad472004-02-10 01:07:45 +00001321 /* If there isn't another process, nextin is garbage
Erik Andersen161220c2000-03-16 08:12:48 +00001322 but it doesn't matter */
1323 nextin = pipefds[0];
1324 }
1325
Eric Andersen86349772000-12-18 20:25:50 +00001326 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001327
Eric Andersen86349772000-12-18 20:25:50 +00001328 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001329
Erik Andersen161220c2000-03-16 08:12:48 +00001330 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001331}
1332
Erik Andersend75af992000-03-16 08:09:09 +00001333static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001334{
Erik Andersen161220c2000-03-16 08:12:48 +00001335 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001336 char *next_command = NULL;
1337 struct job newjob;
Erik Andersen161220c2000-03-16 08:12:48 +00001338 int i;
Eric Andersena68ea1c2006-01-30 22:48:39 +00001339 int inbg = 0;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001340 int status;
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001341#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersend20d3752004-03-12 22:08:42 +00001342 pid_t parent_pgrp;
1343 /* save current owner of TTY so we can restore it on exit */
1344 parent_pgrp = tcgetpgrp(shell_terminal);
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001345#endif
Eric Andersen86349772000-12-18 20:25:50 +00001346 newjob.job_list = &job_list;
1347 newjob.job_context = DEFAULT_CONTEXT;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001348
Rob Landleyf9463792006-06-13 18:50:20 +00001349 command = xzalloc(BUFSIZ);
Erik Andersend75af992000-03-16 08:09:09 +00001350
Erik Andersen161220c2000-03-16 08:12:48 +00001351 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001352 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001353 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001354
Erik Andersend75af992000-03-16 08:09:09 +00001355 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001356 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001357
Eric Andersen86349772000-12-18 20:25:50 +00001358 if (!next_command) {
1359 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001360 break;
Eric Andersen86349772000-12-18 20:25:50 +00001361 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001362 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001363
Matt Kraai1f0c4362001-12-20 23:13:26 +00001364 if (! expand_arguments(next_command)) {
Eric Andersenca604592001-03-08 17:17:13 +00001365 free(command);
Rob Landleyf9463792006-06-13 18:50:20 +00001366 command = xzalloc(BUFSIZ);
Eric Andersenca604592001-03-08 17:17:13 +00001367 next_command = NULL;
1368 continue;
1369 }
1370
Eric Andersen86349772000-12-18 20:25:50 +00001371 if (!parse_command(&next_command, &newjob, &inbg) &&
1372 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001373 int pipefds[2] = {-1,-1};
Eric Andersenff9ad472004-02-10 01:07:45 +00001374 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001375 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001376 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001377 }
1378 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001379 free(command);
Denis Vlasenko4cccc032006-12-22 18:37:07 +00001380 command = xzalloc(BUFSIZ);
Eric Andersen86349772000-12-18 20:25:50 +00001381 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001382 }
1383 } else {
1384 /* a job is running in the foreground; wait for it */
1385 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001386 while (!job_list.fg->progs[i].pid ||
1387 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001388
Eric Andersen7373e482002-07-31 04:04:47 +00001389 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) {
1390 if (errno != ECHILD) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001391 bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
Eric Andersen7373e482002-07-31 04:04:47 +00001392 }
1393 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001394
Erik Andersend75af992000-03-16 08:09:09 +00001395 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001396 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001397 job_list.fg->running_progs--;
1398 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001399
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001400 last_return_code=WEXITSTATUS(status);
1401
Eric Andersen86349772000-12-18 20:25:50 +00001402 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001403 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001404 remove_job(&job_list, job_list.fg);
1405 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001406 }
Eric Andersen37032b42004-02-10 01:28:36 +00001407 }
1408#ifdef CONFIG_LASH_JOB_CONTROL
1409 else {
Erik Andersen161220c2000-03-16 08:12:48 +00001410 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001411 job_list.fg->stopped_progs++;
1412 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001413
Eric Andersen86349772000-12-18 20:25:50 +00001414 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1415 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1416 "Stopped", job_list.fg->text);
1417 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001418 }
Erik Andersend75af992000-03-16 08:09:09 +00001419 }
1420
Eric Andersen86349772000-12-18 20:25:50 +00001421 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001422 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001423 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001424 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Eric Andersenff9ad472004-02-10 01:07:45 +00001425 bb_perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001426 }
Eric Andersen37032b42004-02-10 01:28:36 +00001427#endif
Erik Andersend75af992000-03-16 08:09:09 +00001428 }
1429 }
Erik Andersen161220c2000-03-16 08:12:48 +00001430 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001431
Eric Andersen37032b42004-02-10 01:28:36 +00001432#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen1c314ad2000-06-28 16:56:25 +00001433 /* return controlling TTY back to parent process group before exiting */
Eric Andersen7373e482002-07-31 04:04:47 +00001434 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001435 bb_perror_msg("tcsetpgrp");
Eric Andersen37032b42004-02-10 01:28:36 +00001436#endif
Eric Andersenb54833c2000-07-03 23:56:26 +00001437
1438 /* return exit status if called with "-c" */
1439 if (input == NULL && WIFEXITED(status))
1440 return WEXITSTATUS(status);
Eric Andersenff9ad472004-02-10 01:07:45 +00001441
Erik Andersen161220c2000-03-16 08:12:48 +00001442 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001443}
1444
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001445#ifdef CONFIG_FEATURE_CLEAN_UP
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +00001446static void free_memory(void)
Eric Andersenfad9c112000-07-25 18:06:52 +00001447{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001448 if (cwd && cwd!=bb_msg_unknown) {
Eric Andersen5d60a462001-08-22 05:32:24 +00001449 free((char*)cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001450 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001451 if (local_pending_command)
1452 free(local_pending_command);
1453
Eric Andersen86349772000-12-18 20:25:50 +00001454 if (job_list.fg && !job_list.fg->running_progs) {
1455 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001456 }
1457}
Rob Landleyacf448d2006-07-09 17:03:07 +00001458#else
1459void free_memory(void);
Eric Andersenfad9c112000-07-25 18:06:52 +00001460#endif
1461
Eric Andersen37032b42004-02-10 01:28:36 +00001462#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen2d848a42001-06-25 17:11:54 +00001463/* Make sure we have a controlling tty. If we get started under a job
1464 * aware app (like bash for example), make sure we are now in charge so
1465 * we don't fight over who gets the foreground */
Eric Andersenece8fc22002-12-11 04:26:28 +00001466static void setup_job_control(void)
Eric Andersen2d848a42001-06-25 17:11:54 +00001467{
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001468 int status;
Eric Andersen37032b42004-02-10 01:28:36 +00001469 pid_t shell_pgrp;
Eric Andersenff9ad472004-02-10 01:07:45 +00001470
Eric Andersen2d848a42001-06-25 17:11:54 +00001471 /* Loop until we are in the foreground. */
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001472 while ((status = tcgetpgrp (shell_terminal)) >= 0) {
1473 if (status == (shell_pgrp = getpgrp ())) {
1474 break;
1475 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001476 kill (- shell_pgrp, SIGTTIN);
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001477 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001478
1479 /* Ignore interactive and job-control signals. */
1480 signal(SIGINT, SIG_IGN);
1481 signal(SIGQUIT, SIG_IGN);
1482 signal(SIGTSTP, SIG_IGN);
1483 signal(SIGTTIN, SIG_IGN);
1484 signal(SIGTTOU, SIG_IGN);
1485 signal(SIGCHLD, SIG_IGN);
1486
1487 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001488 setsid();
Eric Andersen2d848a42001-06-25 17:11:54 +00001489 shell_pgrp = getpid ();
Rob Landleyacf448d2006-07-09 17:03:07 +00001490 setpgid(shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001491
1492 /* Grab control of the terminal. */
1493 tcsetpgrp(shell_terminal, shell_pgrp);
1494}
Eric Andersen37032b42004-02-10 01:28:36 +00001495#else
1496static inline void setup_job_control(void)
1497{
1498}
1499#endif
Eric Andersen6efc48c2000-07-18 08:16:39 +00001500
Matt Kraai2d91deb2001-08-01 17:21:35 +00001501int lash_main(int argc_l, char **argv_l)
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;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001505 argc = argc_l;
1506 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001507
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;
Eric Andersen4b6b5e42001-06-27 04:30:11 +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)
1529#define LASH_OPT_c (1<<2)
1530 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. */
Eric Andersen86349772000-12-18 20:25:50 +00001542 if (argv[optind]==NULL && input==stdin &&
Rob Landleyacf448d2006-07-09 17:03:07 +00001543 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...*/
Eric Andersene5dfced2001-04-09 22:48:12 +00001563 cwd = xgetcwd(0);
Eric Andersen5f265b72001-05-11 16:58:46 +00001564 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001565 cwd = bb_msg_unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001566
Rob Landleyacf448d2006-07-09 17:03:07 +00001567 if (ENABLE_FEATURE_CLEAN_UP) atexit(free_memory);
Eric Andersenfad9c112000-07-25 18:06:52 +00001568
Rob Landleyacf448d2006-07-09 17:03:07 +00001569 if (ENABLE_FEATURE_COMMAND_EDITING) cmdedit_set_initial_prompt();
1570 else PS1 = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +00001571
Erik Andersen161220c2000-03-16 08:12:48 +00001572 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001573}