blob: 51ad3cc067d7a996de18f153769f45be5a7914c8 [file] [log] [blame]
Erik Andersen3522eb12000-03-12 23:49:18 +00001/* vi: set sw=4 ts=4: */
2/*
Erik Andersen6acaa402000-03-26 14:03:20 +00003 * lash -- the BusyBox Lame-Ass SHell
Erik Andersen3522eb12000-03-12 23:49:18 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Erik Andersen3522eb12000-03-12 23:49:18 +00006 *
7 * Based in part on ladsh.c by Michael K. Johnson and Erik W. Troan, which is
8 * under the following liberal license: "We have placed this source code in the
9 * public domain. Use it in any project, free or commercial."
10 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000011 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Erik Andersen3522eb12000-03-12 23:49:18 +000012 */
13
Eric Andersenbdfd0d72001-10-24 05:00:29 +000014/* This shell's parsing engine is officially at a dead-end. Future
15 * work shell work should be done using hush, msh, or ash. This is
16 * still a very useful, small shell -- it just don't need any more
17 * features beyond what it already has...
Eric Andersen8ea28be2001-01-05 20:58:22 +000018 */
Eric Andersen8a646dd2001-06-21 16:38:11 +000019
Eric Andersen1e7cea92000-12-06 23:47:38 +000020//For debugging/development on the shell only...
Eric Andersen501c88b2000-07-28 15:14:45 +000021//#define DEBUG_SHELL
Eric Andersena1d187a2000-07-17 19:14:41 +000022
23
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000024#include "busybox.h"
Eric Andersen501c88b2000-07-28 15:14:45 +000025#include <getopt.h>
Eric Andersen13d1fa12001-03-08 23:59:45 +000026#include <glob.h>
27#define expand_t glob_t
Erik Andersen3522eb12000-03-12 23:49:18 +000028
Eric Andersenff9ad472004-02-10 01:07:45 +000029/* Always enable for the moment... */
Eric Andersen95b26252004-02-10 01:30:21 +000030#define CONFIG_LASH_PIPE_N_REDIRECTS
31#define CONFIG_LASH_JOB_CONTROL
Eric Andersen13d1fa12001-03-08 23:59:45 +000032
Denis Vlasenko703e2022007-01-22 14:12:08 +000033enum { MAX_READ = 128 }; /* size of input buffer for 'read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000034#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
35
Erik Andersend75af992000-03-16 08:09:09 +000036
Eric Andersenff9ad472004-02-10 01:07:45 +000037#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000038enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000039 REDIRECT_APPEND
40};
Eric Andersenff9ad472004-02-10 01:07:45 +000041#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000042
Rob Landleybc68cd12006-03-10 19:22:06 +000043enum {
44 DEFAULT_CONTEXT = 0x1,
45 IF_TRUE_CONTEXT = 0x2,
46 IF_FALSE_CONTEXT = 0x4,
47 THEN_EXP_CONTEXT = 0x8,
48 ELSE_EXP_CONTEXT = 0x10
49};
Eric Andersenfad9c112000-07-25 18:06:52 +000050
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +000051#define LASH_OPT_DONE (1)
52#define LASH_OPT_SAW_QUOTE (2)
53
Eric Andersenff9ad472004-02-10 01:07:45 +000054#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000055struct redir_struct {
56 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000057 int fd; /* file descriptor being redirected */
58 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000059};
Eric Andersenff9ad472004-02-10 01:07:45 +000060#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000061
Eric Andersen86349772000-12-18 20:25:50 +000062struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000063 pid_t pid; /* 0 if exited */
64 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000065 int num_redirects; /* elements in redirection array */
Eric Andersen86349772000-12-18 20:25:50 +000066 int is_stopped; /* is the program currently running? */
67 struct job *family; /* pointer back to the child's parent job */
Eric Andersenff9ad472004-02-10 01:07:45 +000068#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
69 struct redir_struct *redirects; /* I/O redirects */
70#endif
71};
72
73struct jobset {
74 struct job *head; /* head of list of running jobs */
75 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000076};
77
78struct job {
Eric Andersen86349772000-12-18 20:25:50 +000079 int jobid; /* job number */
80 int num_progs; /* total number of programs in job */
81 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000082 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +000083 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +000084 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +000085 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +000086 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +000087 int stopped_progs; /* number of programs alive, but stopped */
88 unsigned int job_context; /* bitmask defining current context */
89 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +000090};
91
Eric Andersen86349772000-12-18 20:25:50 +000092struct built_in_command {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +000093 const char *cmd; /* name */
94 const char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +000095 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +000096};
97
Eric Andersen34e19412000-07-10 18:47:24 +000098/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +000099static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000100static int builtin_exec(struct child_prog *cmd);
101static int builtin_exit(struct child_prog *cmd);
102static int builtin_fg_bg(struct child_prog *cmd);
103static int builtin_help(struct child_prog *cmd);
104static int builtin_jobs(struct child_prog *dummy);
105static int builtin_pwd(struct child_prog *dummy);
106static int builtin_export(struct child_prog *cmd);
107static int builtin_source(struct child_prog *cmd);
108static int builtin_unset(struct child_prog *cmd);
109static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000110
Eric Andersen34e19412000-07-10 18:47:24 +0000111
112/* function prototypes for shell stuff */
Eric Andersen86349772000-12-18 20:25:50 +0000113static void checkjobs(struct jobset *job_list);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000114static void remove_job(struct jobset *j_list, struct job *job);
Eric Andersen86349772000-12-18 20:25:50 +0000115static int get_command(FILE * source, char *command);
116static int parse_command(char **command_ptr, struct job *job, int *inbg);
117static int run_command(struct job *newjob, int inbg, int outpipe[2]);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000118static int pseudo_exec(struct child_prog *cmd) ATTRIBUTE_NORETURN;
Erik Andersen3522eb12000-03-12 23:49:18 +0000119static int busy_loop(FILE * input);
120
Erik Andersend75af992000-03-16 08:09:09 +0000121
Mark Whitley37653aa2000-07-12 23:36:17 +0000122/* Table of built-in functions (these are non-forking builtins, meaning they
123 * can change global variables in the parent shell process but they will not
124 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000125static const struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000126 {"bg", "Resume a job in the background", builtin_fg_bg},
127 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000128 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000129 {"exit", "Exit from shell()", builtin_exit},
130 {"fg", "Bring job into the foreground", builtin_fg_bg},
131 {"jobs", "Lists the active jobs", builtin_jobs},
132 {"export", "Set environment variable", builtin_export},
133 {"unset", "Unset environment variable", builtin_unset},
134 {"read", "Input environment variable", builtin_read},
Matt Kraaidd450a02000-09-13 03:43:36 +0000135 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000136 /* to do: add ulimit */
Eric Andersenfad9c112000-07-25 18:06:52 +0000137 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000138};
139
Mark Whitley37653aa2000-07-12 23:36:17 +0000140/* Table of forking built-in functions (things that fork cannot change global
141 * variables in the parent process, such as the current working directory) */
Eric Andersen86349772000-12-18 20:25:50 +0000142static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000143 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000144 {"help", "List shell built-in commands", builtin_help},
145 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000146};
147
Eric Andersen0bcc8132001-01-05 19:37:32 +0000148
Eric Andersen7467c8d2001-07-12 20:26:32 +0000149static int shell_context; /* Type prompt trigger (PS1 or PS2) */
Eric Andersen0bcc8132001-01-05 19:37:32 +0000150
151
152/* Globals that are static to this file */
Eric Andersencfa88ec2001-05-11 18:08:16 +0000153static const char *cwd;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000154static char *local_pending_command;
Eric Andersen86349772000-12-18 20:25:50 +0000155static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000156static int argc;
157static char **argv;
Rob Landleyf9463792006-06-13 18:50:20 +0000158static llist_t *close_me_list;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000159static int last_return_code;
160static int last_bg_pid;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000161static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000162static int shell_terminal;
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000163static const char *PS1;
164static const char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000165
166
Eric Andersenb558e762000-11-30 22:43:16 +0000167#ifdef DEBUG_SHELL
168static inline void debug_printf(const char *format, ...)
169{
170 va_list args;
171 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000172 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000173 va_end(args);
174}
175#else
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000176static inline void debug_printf(const char ATTRIBUTE_UNUSED *format, ...) { }
Eric Andersenb558e762000-11-30 22:43:16 +0000177#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000178
Eric Andersen86349772000-12-18 20:25:50 +0000179/*
180 Most builtins need access to the struct child_prog that has
181 their arguments, previously coded as cmd->progs[0]. That coding
182 can exhibit a bug, if the builtin is not the first command in
183 a pipeline: "echo foo | exec sort" will attempt to exec foo.
184
185builtin previous use notes
186------ ----------------- ---------
187cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000188exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
189exit cmd->progs[0]
190fg_bg cmd->progs[0], job_list->head, job_list->fg
191help 0
192jobs job_list->head
193pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000194export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000195source cmd->progs[0]
196unset cmd->progs[0]
197read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000198
199I added "struct job *family;" to struct child_prog,
200and switched API to builtin_foo(struct child_prog *child);
201So cmd->text becomes child->family->text
202 cmd->job_context becomes child->family->job_context
203 cmd->progs[0] becomes *child
204 job_list becomes child->family->job_list
205 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000206
Erik Andersend75af992000-03-16 08:09:09 +0000207/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000208static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000209{
Erik Andersen161220c2000-03-16 08:12:48 +0000210 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000211
Eric Andersen86349772000-12-18 20:25:50 +0000212 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000213 newdir = getenv("HOME");
214 else
Eric Andersen86349772000-12-18 20:25:50 +0000215 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000216 if (chdir(newdir)) {
Rob Landley5483de12006-06-20 21:35:26 +0000217 bb_perror_msg("cd: %s", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000218 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000219 }
Eric Andersencfa88ec2001-05-11 18:08:16 +0000220 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000221 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000222 cwd = bb_msg_unknown;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000223 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000224}
225
Eric Andersend2f56772000-09-21 02:48:07 +0000226/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000227static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000228{
Eric Andersen86349772000-12-18 20:25:50 +0000229 if (child->argv[1] == NULL)
230 return EXIT_SUCCESS; /* Really? */
231 child->argv++;
Rob Landley7a260f02006-06-19 03:20:03 +0000232 while(close_me_list) close((long)llist_pop(&close_me_list));
Eric Andersen86349772000-12-18 20:25:50 +0000233 pseudo_exec(child);
234 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000235}
236
Erik Andersen3522eb12000-03-12 23:49:18 +0000237/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000238static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000239{
Eric Andersen86349772000-12-18 20:25:50 +0000240 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000241 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000242
Eric Andersen86349772000-12-18 20:25:50 +0000243 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000244}
245
246/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000247static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000248{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000249 int i, jobnum;
Erik Andersen6273f652000-03-17 01:12:41 +0000250 struct job *job=NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000251
Eric Andersen8a646dd2001-06-21 16:38:11 +0000252 /* If they gave us no args, assume they want the last backgrounded task */
253 if (!child->argv[1]) {
254 for (job = child->family->job_list->head; job; job = job->next) {
255 if (job->jobid == last_jobid) {
256 break;
257 }
Erik Andersen161220c2000-03-16 08:12:48 +0000258 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000259 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000260 bb_error_msg("%s: no current job", child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000261 return EXIT_FAILURE;
262 }
263 } else {
264 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000265 bb_error_msg(bb_msg_invalid_arg, child->argv[1], child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000266 return EXIT_FAILURE;
267 }
268 for (job = child->family->job_list->head; job; job = job->next) {
269 if (job->jobid == jobnum) {
270 break;
271 }
272 }
273 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000274 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000275 return EXIT_FAILURE;
276 }
Erik Andersend75af992000-03-16 08:09:09 +0000277 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000278
Eric Andersen86349772000-12-18 20:25:50 +0000279 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000280 /* Put the job into the foreground. */
281 tcsetpgrp(shell_terminal, job->pgrp);
282
Eric Andersen86349772000-12-18 20:25:50 +0000283 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000284 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000285
Erik Andersen161220c2000-03-16 08:12:48 +0000286 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000287 for (i = 0; i < job->num_progs; i++)
288 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000289
Eric Andersen86349772000-12-18 20:25:50 +0000290 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000291
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000292 if ( (i=kill(- job->pgrp, SIGCONT)) < 0) {
293 if (i == ESRCH) {
294 remove_job(&job_list, job);
Eric Andersen07abfe22001-06-27 17:29:11 +0000295 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000296 bb_perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000297 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000298 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000299
Matt Kraai3e856ce2000-12-01 02:55:13 +0000300 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000301}
302
303/* built-in 'help' handler */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000304static int builtin_help(struct child_prog ATTRIBUTE_UNUSED *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000305{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000306 const struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000307
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000308 printf("\nBuilt-in commands:\n"
309 "-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000310 for (x = bltins; x->cmd; x++) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000311 if (x->descr == NULL)
Eric Andersenfad9c112000-07-25 18:06:52 +0000312 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000313 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000314 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000315 for (x = bltins_forking; x->cmd; x++) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000316 if (x->descr == NULL)
Eric Andersenfad9c112000-07-25 18:06:52 +0000317 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000318 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000319 }
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000320 putchar('\n');
Matt Kraai3e856ce2000-12-01 02:55:13 +0000321 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000322}
323
324/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000325static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000326{
Erik Andersen161220c2000-03-16 08:12:48 +0000327 struct job *job;
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000328 const char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000329
Eric Andersen86349772000-12-18 20:25:50 +0000330 for (job = child->family->job_list->head; job; job = job->next) {
331 if (job->running_progs == job->stopped_progs)
332 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000333 else
Eric Andersen86349772000-12-18 20:25:50 +0000334 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000335
Eric Andersen86349772000-12-18 20:25:50 +0000336 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000337 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000338 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000339}
340
341
342/* built-in 'pwd' handler */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000343static int builtin_pwd(struct child_prog ATTRIBUTE_UNUSED *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000344{
Eric Andersencfa88ec2001-05-11 18:08:16 +0000345 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000346 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000347 cwd = bb_msg_unknown;
Matt Kraai59df6f72001-05-16 14:21:09 +0000348 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000349 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000350}
351
Erik Andersen6273f652000-03-17 01:12:41 +0000352/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000353static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000354{
Erik Andersen161220c2000-03-16 08:12:48 +0000355 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000356 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000357
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000358 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000359 char **e;
360 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000361 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000362 }
Matt Kraai2129f972001-04-04 17:50:04 +0000363 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000364 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000365 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000366 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000367 bb_perror_msg("export");
Denis Vlasenko38f63192007-01-22 09:03:07 +0000368#ifdef CONFIG_FEATURE_EDITING_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000369 if (strncmp(v, "PS1=", 4)==0)
370 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000371#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000372
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000373#ifdef CONFIG_LOCALE_SUPPORT
Denis Vlasenkofcdb00f2006-11-21 00:09:37 +0000374 // TODO: why getenv? "" would be just as good...
Mark Whitley1c6581a2001-03-27 16:35:16 +0000375 if(strncmp(v, "LC_ALL=", 7)==0)
376 setlocale(LC_ALL, getenv("LC_ALL"));
Mark Whitleya82a0032001-03-27 17:07:15 +0000377 if(strncmp(v, "LC_CTYPE=", 9)==0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000378 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000379#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000380
Denis Vlasenko097c3242006-11-27 16:59:15 +0000381 return res;
Erik Andersen3522eb12000-03-12 23:49:18 +0000382}
383
Eric Andersenb54833c2000-07-03 23:56:26 +0000384/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000385static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000386{
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000387 int res = 0, len;
Eric Andersenb54833c2000-07-03 23:56:26 +0000388 char *s;
389 char string[MAX_READ];
390
Eric Andersen86349772000-12-18 20:25:50 +0000391 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000392 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen0d2d1eb2002-06-06 13:33:01 +0000393 safe_strncpy(string, child->argv[1], MAX_READ-1);
Eric Andersenb54833c2000-07-03 23:56:26 +0000394 len = strlen(string);
395 string[len++] = '=';
396 string[len] = '\0';
397 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000398 res = strlen(string);
399 if (res > len)
400 string[--res] = '\0'; /* chomp trailing newline */
Eric Andersenb54833c2000-07-03 23:56:26 +0000401 /*
402 ** string should now contain "VAR=<value>"
403 ** copy it (putenv() won't do that, so we must make sure
404 ** the string resides in a static buffer!)
405 */
406 res = -1;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000407 if ((s = strdup(string)))
Eric Andersenb54833c2000-07-03 23:56:26 +0000408 res = putenv(s);
409 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000410 bb_perror_msg("read");
Eric Andersenb54833c2000-07-03 23:56:26 +0000411 }
412 else
413 fgets(string, sizeof(string), stdin);
414
Denis Vlasenko097c3242006-11-27 16:59:15 +0000415 return res;
Eric Andersenb54833c2000-07-03 23:56:26 +0000416}
417
Erik Andersen3522eb12000-03-12 23:49:18 +0000418/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000419static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000420{
Erik Andersen161220c2000-03-16 08:12:48 +0000421 FILE *input;
422 int status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000423
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000424 input = fopen_or_warn(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000425 if (!input) {
Matt Kraai3e856ce2000-12-01 02:55:13 +0000426 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000427 }
Erik Andersend75af992000-03-16 08:09:09 +0000428
Rob Landley7a260f02006-06-19 03:20:03 +0000429 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Erik Andersen161220c2000-03-16 08:12:48 +0000430 /* Now run the file */
431 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000432 fclose(input);
Rob Landleyf9463792006-06-13 18:50:20 +0000433 llist_pop(&close_me_list);
Denis Vlasenko097c3242006-11-27 16:59:15 +0000434 return status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000435}
436
437/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000438static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000439{
Eric Andersen86349772000-12-18 20:25:50 +0000440 if (child->argv[1] == NULL) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000441 printf(bb_msg_requires_arg, "unset");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000442 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000443 }
Eric Andersen86349772000-12-18 20:25:50 +0000444 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000445 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000446}
447
Eric Andersen37032b42004-02-10 01:28:36 +0000448#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen3522eb12000-03-12 23:49:18 +0000449/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000450static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000451{
Erik Andersen161220c2000-03-16 08:12:48 +0000452 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000453 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000454
Eric Andersen86349772000-12-18 20:25:50 +0000455 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000456 free(cmd->progs[i].argv);
Eric Andersenff9ad472004-02-10 01:07:45 +0000457#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000458 if (cmd->progs[i].redirects)
459 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 */
512 if(job==NULL)
513 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);
Eric Andersen8a646dd2001-06-21 16:38:11 +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
Eric Andersenff9ad472004-02-10 01:07:45 +0000547#ifdef CONFIG_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
570 openfd = open(redir->filename, mode, 0666);
571 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!) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000574 bb_perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000575 return 1;
576 }
577
578 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000579 if (squirrel && redir->fd < 3) {
580 squirrel[redir->fd] = dup(redir->fd);
Eric Andersenacd647c2004-08-16 08:38:34 +0000581 fcntl (squirrel[redir->fd], F_SETFD, FD_CLOEXEC);
Eric Andersene9f07fb2000-12-21 18:31:36 +0000582 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000583 dup2(openfd, redir->fd);
584 close(openfd);
585 }
586 }
587
588 return 0;
589}
590
Eric Andersene9f07fb2000-12-21 18:31:36 +0000591static void restore_redirects(int squirrel[])
592{
593 int i, fd;
594 for (i=0; i<3; i++) {
595 fd = squirrel[i];
596 if (fd != -1) {
597 /* No error checking. I sure wouldn't know what
598 * to do with an error if I found one! */
599 dup2(fd, i);
600 close(fd);
601 }
602 }
603}
Eric Andersenff9ad472004-02-10 01:07:45 +0000604#else
605static inline int setup_redirects(struct child_prog *prog, int squirrel[])
606{
607 return 0;
608}
609static inline void restore_redirects(int squirrel[])
610{
611}
612#endif
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000613
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000614static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000615{
Denis Vlasenko38f63192007-01-22 09:03:07 +0000616#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000617 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000618#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000619 PS1 = getenv("PS1");
Eric Andersene5dfced2001-04-09 22:48:12 +0000620 if(PS1==0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000621 PS1 = "\\w \\$ ";
Eric Andersenff9ad472004-02-10 01:07:45 +0000622#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000623}
624
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000625static inline const char* setup_prompt_string(void)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000626{
Denis Vlasenko38f63192007-01-22 09:03:07 +0000627#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000628 /* Set up the prompt */
629 if (shell_context == 0) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000630 char *ns;
631 free((char*)PS1);
632 ns = xmalloc(strlen(cwd)+4);
633 sprintf(ns, "%s %c ", cwd, (geteuid() != 0) ? '$': '#');
634 PS1 = ns;
635 return ns;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000636 } else {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000637 return PS2;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000638 }
639#else
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000640 return (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
Denis Vlasenko38f63192007-01-22 09:03:07 +0000644#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000645static line_input_t *line_input_state;
646#endif
647
Eric Andersen09acc062001-01-04 11:10:38 +0000648static int get_command(FILE * source, char *command)
649{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000650 const char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000651
Eric Andersen1c314ad2000-06-28 16:56:25 +0000652 if (source == NULL) {
653 if (local_pending_command) {
654 /* a command specified (-c option): return it & mark it done */
655 strcpy(command, local_pending_command);
656 free(local_pending_command);
657 local_pending_command = NULL;
658 return 0;
659 }
660 return 1;
661 }
662
Erik Andersen161220c2000-03-16 08:12:48 +0000663 if (source == stdin) {
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000664 prompt_str = setup_prompt_string();
Eric Andersen8ea28be2001-01-05 20:58:22 +0000665
Denis Vlasenko38f63192007-01-22 09:03:07 +0000666#if ENABLE_FEATURE_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000667 /*
668 ** enable command line editing only while a command line
669 ** is actually being read; otherwise, we'll end up bequeathing
670 ** atexit() handlers and other unwanted stuff to our
671 ** child processes (rob@sysgo.de)
672 */
Denis Vlasenko8e1c7152007-01-22 07:21:38 +0000673 read_line_input(prompt_str, command, BUFSIZ, line_input_state);
Erik Andersen161220c2000-03-16 08:12:48 +0000674 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000675#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000676 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000677#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000678 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000679
Erik Andersen161220c2000-03-16 08:12:48 +0000680 if (!fgets(command, BUFSIZ - 2, source)) {
681 if (source == stdin)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000682 puts("");
Erik Andersen161220c2000-03-16 08:12:48 +0000683 return 1;
684 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000685
Erik Andersen161220c2000-03-16 08:12:48 +0000686 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000687}
688
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +0000689static char * strsep_space( char *string, int * ix)
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000690{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000691 /* Short circuit the trivial case */
692 if ( !string || ! string[*ix])
693 return NULL;
694
695 /* Find the end of the token. */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000696 while (string[*ix] && !isspace(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000697 (*ix)++;
698 }
699
Eric Andersenff9ad472004-02-10 01:07:45 +0000700 /* Find the end of any whitespace trailing behind
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000701 * the token and let that be part of the token */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000702 while (string[*ix] && (isspace)(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000703 (*ix)++;
704 }
705
Rob Landleyf9463792006-06-13 18:50:20 +0000706 if (!*ix) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000707 /* Nothing useful was found */
708 return NULL;
709 }
710
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000711 return xstrndup(string, *ix);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000712}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000713
Eric Andersenca604592001-03-08 17:17:13 +0000714static int expand_arguments(char *command)
715{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000716 int total_length=0, length, i, retval, ix = 0;
717 expand_t expand_result;
718 char *tmpcmd, *cmd, *cmd_copy;
719 char *src, *dst, *var;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000720 const char * const out_of_space = "out of space during expansion";
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000721 int flags = GLOB_NOCHECK
722#ifdef GLOB_BRACE
723 | GLOB_BRACE
Eric Andersenff9ad472004-02-10 01:07:45 +0000724#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000725#ifdef GLOB_TILDE
726 | GLOB_TILDE
Eric Andersenff9ad472004-02-10 01:07:45 +0000727#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000728 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000729
Eric Andersenca604592001-03-08 17:17:13 +0000730 /* get rid of the terminating \n */
731 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000732
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000733 /* Fix up escape sequences to be the Real Thing(tm) */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000734 while( command && command[ix]) {
735 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000736 const char *tmp = command+ix+1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000737 command[ix] = bb_process_escape_sequence( &tmp );
Eric Andersen1ca20a72001-03-21 07:34:27 +0000738 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000739 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000740 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000741 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000742 /* Use glob and then fixup environment variables and such */
743
744 /* It turns out that glob is very stupid. We have to feed it one word at a
745 * time since it can't cope with a full string. Here we convert command
746 * (char*) into cmd (char**, one word per string) */
747
748 /* We need a clean copy, so strsep can mess up the copy while
749 * we write stuff into the original (in a minute) */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000750 cmd = cmd_copy = xstrdup(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000751 *command = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000752 for (ix = 0, tmpcmd = cmd;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000753 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
754 if (*tmpcmd == '\0')
755 break;
756 /* we need to trim() the result for glob! */
757 trim(tmpcmd);
758 retval = glob(tmpcmd, flags, NULL, &expand_result);
759 free(tmpcmd); /* Free mem allocated by strsep_space */
760 if (retval == GLOB_NOSPACE) {
761 /* Mem may have been allocated... */
762 globfree (&expand_result);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000763 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000764 return FALSE;
765 } else if (retval != 0) {
766 /* Some other error. GLOB_NOMATCH shouldn't
Eric Andersenff9ad472004-02-10 01:07:45 +0000767 * happen because of the GLOB_NOCHECK flag in
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000768 * the glob call. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000769 bb_error_msg("syntax error");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000770 return FALSE;
771 } else {
772 /* Convert from char** (one word per string) to a simple char*,
773 * but don't overflow command which is BUFSIZ in length */
774 for (i=0; i < expand_result.gl_pathc; i++) {
775 length=strlen(expand_result.gl_pathv[i]);
776 if (total_length+length+1 >= BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000777 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000778 return FALSE;
779 }
780 strcat(command+total_length, " ");
781 total_length+=1;
782 strcat(command+total_length, expand_result.gl_pathv[i]);
783 total_length+=length;
784 }
785 globfree (&expand_result);
786 }
787 }
788 free(cmd_copy);
789 trim(command);
790
Eric Andersenff9ad472004-02-10 01:07:45 +0000791 /* Now do the shell variable substitutions which
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000792 * wordexp can't do for us, namely $? and $! */
793 src = command;
794 while((dst = strchr(src,'$')) != NULL){
795 var = NULL;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000796 switch (*(dst+1)) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000797 case '?':
798 var = itoa(last_return_code);
799 break;
800 case '!':
801 if (last_bg_pid==-1)
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000802 *var = '\0';
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000803 else
804 var = itoa(last_bg_pid);
805 break;
806 /* Everything else like $$, $#, $[0-9], etc. should all be
807 * expanded by wordexp(), so we can in theory skip that stuff
808 * here, but just to be on the safe side (i.e., since uClibc
809 * wordexp doesn't do this stuff yet), lets leave it in for
810 * now. */
811 case '$':
812 var = itoa(getpid());
813 break;
814 case '#':
815 var = itoa(argc-1);
816 break;
817 case '0':case '1':case '2':case '3':case '4':
818 case '5':case '6':case '7':case '8':case '9':
819 {
Eric Andersen7ab93d92002-11-08 09:40:02 +0000820 int ixx=*(dst+1)-48+1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000821 if (ixx >= argc) {
822 var='\0';
823 } else {
824 var = argv[ixx];
825 }
826 }
827 break;
828
829 }
830 if (var) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000831 /* a single character construction was found, and
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000832 * already handled in the case statement */
833 src=dst+2;
834 } else {
835 /* Looks like an environment variable */
836 char delim_hold;
837 int num_skip_chars=0;
838 int dstlen = strlen(dst);
839 /* Is this a ${foo} type variable? */
840 if (dstlen >=2 && *(dst+1) == '{') {
841 src=strchr(dst+1, '}');
842 num_skip_chars=1;
843 } else {
844 src=dst+1;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000845 while((isalnum)(*src) || *src=='_') src++;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000846 }
847 if (src == NULL) {
848 src = dst+dstlen;
849 }
850 delim_hold=*src;
851 *src='\0'; /* temporary */
852 var = getenv(dst + 1 + num_skip_chars);
853 *src=delim_hold;
854 src += num_skip_chars;
855 }
856 if (var == NULL) {
857 /* Seems we got an un-expandable variable. So delete it. */
Denis Vlasenko71d8abf2007-01-30 22:44:07 +0000858 var = (char*)"";
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000859 }
860 {
861 int subst_len = strlen(var);
862 int trail_len = strlen(src);
863 if (dst+subst_len+trail_len >= command+BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000864 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000865 return FALSE;
866 }
867 /* Move stuff to the end of the string to accommodate
868 * filling the created gap with the new stuff */
869 memmove(dst+subst_len, src, trail_len+1);
870 /* Now copy in the new stuff */
871 memcpy(dst, var, subst_len);
872 src = dst+subst_len;
873 }
874 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000875
Eric Andersenca604592001-03-08 17:17:13 +0000876 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000877}
878
Eric Andersen86349772000-12-18 20:25:50 +0000879/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
880 line). If a valid command is found, command_ptr is set to point to
Eric Andersenff9ad472004-02-10 01:07:45 +0000881 the beginning of the next command (if the original command had more
882 then one job associated with it) or NULL if no more commands are
Erik Andersen3522eb12000-03-12 23:49:18 +0000883 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000884static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000885{
Erik Andersen161220c2000-03-16 08:12:48 +0000886 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000887 char *return_command = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +0000888 char *src, *buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000889 int argc_l;
890 int flag;
Eric Andersen86349772000-12-18 20:25:50 +0000891 int argv_alloced;
Erik Andersen161220c2000-03-16 08:12:48 +0000892 char quote = '\0';
Eric Andersen86349772000-12-18 20:25:50 +0000893 struct child_prog *prog;
Eric Andersenff9ad472004-02-10 01:07:45 +0000894#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
895 int i;
896 char *chptr;
897#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000898
Erik Andersen161220c2000-03-16 08:12:48 +0000899 /* skip leading white space */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000900 *command_ptr = skip_whitespace(*command_ptr);
Erik Andersen3522eb12000-03-12 23:49:18 +0000901
Erik Andersen161220c2000-03-16 08:12:48 +0000902 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000903 if (!**command_ptr || (**command_ptr == '#')) {
904 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000905 return 0;
906 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000907
Eric Andersen86349772000-12-18 20:25:50 +0000908 *inbg = 0;
909 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000910 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000911
Eric Andersenff9ad472004-02-10 01:07:45 +0000912 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000913 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000914 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000915
Eric Andersenff9ad472004-02-10 01:07:45 +0000916 Getting clean memory relieves us of the task of NULL
917 terminating things and makes the rest of this look a bit
Erik Andersen161220c2000-03-16 08:12:48 +0000918 cleaner (though it is, admittedly, a tad less efficient) */
Rob Landleyf9463792006-06-13 18:50:20 +0000919 job->cmdbuf = command = xzalloc(2*strlen(*command_ptr) + 1);
Erik Andersen161220c2000-03-16 08:12:48 +0000920 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000921
Erik Andersen161220c2000-03-16 08:12:48 +0000922 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000923 prog->num_redirects = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000924 prog->is_stopped = 0;
925 prog->family = job;
Eric Andersenff9ad472004-02-10 01:07:45 +0000926#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
927 prog->redirects = NULL;
928#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000929
Eric Andersen86349772000-12-18 20:25:50 +0000930 argv_alloced = 5;
931 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
932 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +0000933
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000934 flag = argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000935 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +0000936 src = *command_ptr;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000937 while (*src && !(flag & LASH_OPT_DONE)) {
Erik Andersen161220c2000-03-16 08:12:48 +0000938 if (quote == *src) {
939 quote = '\0';
940 } else if (quote) {
941 if (*src == '\\') {
942 src++;
943 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000944 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +0000945 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +0000946 return 1;
947 }
948
949 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +0000950 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +0000951 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +0000952 *buf++ = '\\';
953 }
Erik Andersen161220c2000-03-16 08:12:48 +0000954 } else if (*src == '*' || *src == '?' || *src == '[' ||
955 *src == ']') *buf++ = '\\';
956 *buf++ = *src;
957 } else if (isspace(*src)) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000958 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000959 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +0000960 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +0000961 if ((argc_l + 1) == argv_alloced) {
962 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +0000963 prog->argv = xrealloc(prog->argv,
964 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +0000965 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +0000966 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000967 prog->argv[argc_l] = buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000968 flag ^= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000969 }
970 } else
971 switch (*src) {
972 case '"':
973 case '\'':
974 quote = *src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000975 flag |= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000976 break;
977
978 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000979 if (*(src-1)== '$')
980 *buf++ = *src;
981 else
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000982 flag |= LASH_OPT_DONE;
Erik Andersen161220c2000-03-16 08:12:48 +0000983 break;
984
Eric Andersenff9ad472004-02-10 01:07:45 +0000985#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000986 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +0000987 case '<':
Eric Andersen86349772000-12-18 20:25:50 +0000988 i = prog->num_redirects++;
989 prog->redirects = xrealloc(prog->redirects,
990 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +0000991 (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +0000992
Eric Andersen86349772000-12-18 20:25:50 +0000993 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000994 if (buf != prog->argv[argc_l]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000995 /* the stuff before this character may be the file number
Erik Andersen161220c2000-03-16 08:12:48 +0000996 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +0000997 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000998 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +0000999
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001000 if (*chptr && *prog->argv[argc_l]) {
1001 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001002 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001003 }
1004 }
1005
Eric Andersen86349772000-12-18 20:25:50 +00001006 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001007 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001008 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001009 else
Eric Andersen86349772000-12-18 20:25:50 +00001010 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001011 }
1012
1013 if (*src++ == '>') {
1014 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001015 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001016 REDIRECT_APPEND, src++;
1017 else
Eric Andersen86349772000-12-18 20:25:50 +00001018 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001019 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001020 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001021 }
1022
1023 /* This isn't POSIX sh compliant. Oh well. */
1024 chptr = src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001025 chptr = skip_whitespace(chptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001026
1027 if (!*chptr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001028 bb_error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001029 free_job(job);
1030 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001031 return 1;
1032 }
1033
Eric Andersen86349772000-12-18 20:25:50 +00001034 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001035 while (*chptr && !isspace(*chptr))
1036 *buf++ = *chptr++;
1037
1038 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001039 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001040 break;
1041
1042 case '|': /* pipe */
1043 /* finish this command */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001044 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001045 argc_l++;
1046 if (!argc_l) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001047 goto empty_command_in_pipe;
Erik Andersen161220c2000-03-16 08:12:48 +00001048 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001049 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001050
1051 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001052 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001053 job->progs = xrealloc(job->progs,
Eric Andersen86349772000-12-18 20:25:50 +00001054 sizeof(*job->progs) * job->num_progs);
1055 prog = job->progs + (job->num_progs - 1);
1056 prog->num_redirects = 0;
1057 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001058 prog->is_stopped = 0;
1059 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001060 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001061
Eric Andersen86349772000-12-18 20:25:50 +00001062 argv_alloced = 5;
1063 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001064 prog->argv[0] = ++buf;
1065
1066 src++;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001067 src = skip_whitespace(src);
Erik Andersen161220c2000-03-16 08:12:48 +00001068
1069 if (!*src) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001070empty_command_in_pipe:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001071 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001072 free_job(job);
1073 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001074 return 1;
1075 }
1076 src--; /* we'll ++ it at the end of the loop */
1077
1078 break;
Eric Andersenff9ad472004-02-10 01:07:45 +00001079#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001080
Eric Andersen37032b42004-02-10 01:28:36 +00001081#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen161220c2000-03-16 08:12:48 +00001082 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001083 *inbg = 1;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001084 /* fallthrough */
Eric Andersen37032b42004-02-10 01:28:36 +00001085#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001086 case ';': /* multiple commands */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001087 flag |= LASH_OPT_DONE;
Eric Andersen86349772000-12-18 20:25:50 +00001088 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001089 break;
1090
Matt Kraai131241f2000-09-14 00:43:20 +00001091 case '\\':
1092 src++;
1093 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001094 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001095 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001096 return 1;
1097 }
1098 if (*src == '*' || *src == '[' || *src == ']'
1099 || *src == '?') *buf++ = '\\';
1100 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001101 default:
1102 *buf++ = *src;
1103 }
1104
Erik Andersend75af992000-03-16 08:09:09 +00001105 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001106 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001107
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001108 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001109 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001110 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001111 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001112 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001113 return 0;
1114 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001115 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001116
Eric Andersen86349772000-12-18 20:25:50 +00001117 if (!return_command) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001118 job->text = xstrdup(*command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001119 } else {
1120 /* This leaves any trailing spaces, which is a bit sloppy */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001121 job->text = xstrndup(*command_ptr, return_command - *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001122 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001123
Eric Andersen86349772000-12-18 20:25:50 +00001124 *command_ptr = return_command;
Eric Andersenff9ad472004-02-10 01:07:45 +00001125
Erik Andersend75af992000-03-16 08:09:09 +00001126 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001127}
1128
Eric Andersen86349772000-12-18 20:25:50 +00001129/* Run the child_prog, no matter what kind of command it uses.
1130 */
1131static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001132{
Denis Vlasenko71d8abf2007-01-30 22:44:07 +00001133 const struct built_in_command *x;
Rob Landley68286772006-06-13 19:01:30 +00001134
Eric Andersene9f07fb2000-12-21 18:31:36 +00001135 /* Check if the command matches any of the non-forking builtins.
1136 * Depending on context, this might be redundant. But it's
1137 * easier to waste a few CPU cycles than it is to figure out
1138 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001139 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001140 for (x = bltins; x->cmd; x++) {
1141 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001142 _exit(x->function(child));
Eric Andersene9f07fb2000-12-21 18:31:36 +00001143 }
1144 }
1145
1146 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001147 for (x = bltins_forking; x->cmd; x++) {
1148 if (strcmp(child->argv[0], x->cmd) == 0) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001149 applet_name=x->cmd;
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001150 _exit (x->function(child));
Eric Andersen86349772000-12-18 20:25:50 +00001151 }
1152 }
Rob Landley68286772006-06-13 19:01:30 +00001153
Eric Andersen86349772000-12-18 20:25:50 +00001154 /* Check if the command matches any busybox internal
1155 * commands ("applets") here. Following discussions from
Eric Andersen2423b122001-12-08 01:56:15 +00001156 * November 2000 on busybox@busybox.net, don't use
Manuel Novoa III cad53642003-03-19 09:13:01 +00001157 * bb_get_last_path_component(). This way explicit (with
Eric Andersen86349772000-12-18 20:25:50 +00001158 * slashes) filenames will never be interpreted as an
1159 * applet, just like with builtins. This way the user can
1160 * override an applet with an explicit filename reference.
1161 * The only downside to this change is that an explicit
1162 * /bin/foo invocation will fork and exec /bin/foo, even if
1163 * /bin/foo is a symlink to busybox.
1164 */
Eric Andersen86349772000-12-18 20:25:50 +00001165
Rob Landley68286772006-06-13 19:01:30 +00001166 if (ENABLE_FEATURE_SH_STANDALONE_SHELL) {
1167 char **argv_l = child->argv;
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001168 int argc_l;
Bernhard Reutner-Fischer62558762006-06-03 10:28:25 +00001169
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001170 for (argc_l=0; *argv_l; argv_l++, argc_l++);
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001171 optind = 1;
Rob Landley68286772006-06-13 19:01:30 +00001172 run_applet_by_name(child->argv[0], argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001173 }
Eric Andersen86349772000-12-18 20:25:50 +00001174
1175 execvp(child->argv[0], child->argv);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001176
Eric Andersenff9ad472004-02-10 01:07:45 +00001177 /* Do not use bb_perror_msg_and_die() here, since we must not
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001178 * call exit() but should call _exit() instead */
Rob Landley5483de12006-06-20 21:35:26 +00001179 bb_perror_msg("%s", child->argv[0]);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001180 _exit(EXIT_FAILURE);
Eric Andersen86349772000-12-18 20:25:50 +00001181}
1182
1183static void insert_job(struct job *newjob, int inbg)
1184{
1185 struct job *thejob;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001186 struct jobset *j_list=newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001187
1188 /* find the ID for thejob to use */
1189 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001190 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001191 if (thejob->jobid >= newjob->jobid)
1192 newjob->jobid = thejob->jobid + 1;
1193
1194 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001195 if (!j_list->head) {
1196 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001197 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001198 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001199 thejob->next = xmalloc(sizeof(*thejob));
1200 thejob = thejob->next;
1201 }
1202
1203 *thejob = *newjob; /* physically copy the struct job */
1204 thejob->next = NULL;
1205 thejob->running_progs = thejob->num_progs;
1206 thejob->stopped_progs = 0;
1207
Eric Andersen37032b42004-02-10 01:28:36 +00001208#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen86349772000-12-18 20:25:50 +00001209 if (inbg) {
Eric Andersenff9ad472004-02-10 01:07:45 +00001210 /* we don't wait for background thejobs to return -- append it
Eric Andersen86349772000-12-18 20:25:50 +00001211 to the list of backgrounded thejobs and leave it alone */
1212 printf("[%d] %d\n", thejob->jobid,
1213 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001214 last_jobid = newjob->jobid;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001215 last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001216 } else {
1217 newjob->job_list->fg = thejob;
1218
1219 /* move the new process group into the foreground */
Rob Landleyf9463792006-06-13 18:50:20 +00001220 /* Ignore errors since child could have already exited */
1221 tcsetpgrp(shell_terminal, newjob->pgrp);
Eric Andersen86349772000-12-18 20:25:50 +00001222 }
Eric Andersen37032b42004-02-10 01:28:36 +00001223#endif
Eric Andersen86349772000-12-18 20:25:50 +00001224}
1225
1226static int run_command(struct job *newjob, int inbg, int outpipe[2])
1227{
1228 /* struct job *thejob; */
1229 int i;
1230 int nextin, nextout;
1231 int pipefds[2]; /* pipefd[0] is for reading */
Denis Vlasenko71d8abf2007-01-30 22:44:07 +00001232 const struct built_in_command *x;
Eric Andersen86349772000-12-18 20:25:50 +00001233 struct child_prog *child;
1234
Eric Andersen6efc48c2000-07-18 08:16:39 +00001235 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001236 for (i = 0; i < newjob->num_progs; i++) {
1237 child = & (newjob->progs[i]);
1238
1239 if ((i + 1) < newjob->num_progs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001240 if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001241 nextout = pipefds[1];
1242 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001243 if (outpipe[1]!=-1) {
1244 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001245 } else {
1246 nextout = 1;
1247 }
Erik Andersen161220c2000-03-16 08:12:48 +00001248 }
1249
Eric Andersen501c88b2000-07-28 15:14:45 +00001250
Eric Andersene9f07fb2000-12-21 18:31:36 +00001251 /* Check if the command matches any non-forking builtins,
1252 * but only if this is a simple command.
1253 * Non-forking builtins within pipes have to fork anyway,
1254 * and are handled in pseudo_exec. "echo foo | read bar"
1255 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001256 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001257 if (newjob->num_progs == 1) {
Rob Landley7a8e4912006-07-01 17:19:02 +00001258 /* Check if the command sets an environment variable. */
1259 if (strchr(child->argv[0], '=') != NULL) {
1260 child->argv[1] = child->argv[0];
1261 return builtin_export(child);
1262 }
1263
Eric Andersene9f07fb2000-12-21 18:31:36 +00001264 for (x = bltins; x->cmd; x++) {
1265 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersene9f07fb2000-12-21 18:31:36 +00001266 int rcode;
Eric Andersenff9ad472004-02-10 01:07:45 +00001267 int squirrel[] = {-1, -1, -1};
Eric Andersene9f07fb2000-12-21 18:31:36 +00001268 setup_redirects(child, squirrel);
1269 rcode = x->function(child);
1270 restore_redirects(squirrel);
1271 return rcode;
1272 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001273 }
1274 }
1275
Eric Andersene3efc922004-04-12 17:59:24 +00001276#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersenff9ad472004-02-10 01:07:45 +00001277 if (!(child->pid = fork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001278#else
Eric Andersenff9ad472004-02-10 01:07:45 +00001279 if (!(child->pid = vfork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001280#endif
1281 {
Eric Andersen2d848a42001-06-25 17:11:54 +00001282 /* Set the handling for job control signals back to the default. */
1283 signal(SIGINT, SIG_DFL);
1284 signal(SIGQUIT, SIG_DFL);
1285 signal(SIGTSTP, SIG_DFL);
1286 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001287 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001288 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001289
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001290 /* Close all open filehandles. */
Rob Landley7a260f02006-06-19 03:20:03 +00001291 while(close_me_list) close((long)llist_pop(&close_me_list));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001292
Eric Andersen86349772000-12-18 20:25:50 +00001293 if (outpipe[1]!=-1) {
1294 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001295 }
1296 if (nextin != 0) {
1297 dup2(nextin, 0);
1298 close(nextin);
1299 }
1300
1301 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001302 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001303 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001304 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001305 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001306 }
1307
Eric Andersen86349772000-12-18 20:25:50 +00001308 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001309 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001310
Eric Andersen86349772000-12-18 20:25:50 +00001311 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001312 }
Eric Andersen86349772000-12-18 20:25:50 +00001313 if (outpipe[1]!=-1) {
1314 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001315 }
Erik Andersen161220c2000-03-16 08:12:48 +00001316
1317 /* put our child in the process group whose leader is the
1318 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001319 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001320 if (nextin != 0)
1321 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001322 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001323 close(nextout);
1324
Eric Andersenff9ad472004-02-10 01:07:45 +00001325 /* If there isn't another process, nextin is garbage
Erik Andersen161220c2000-03-16 08:12:48 +00001326 but it doesn't matter */
1327 nextin = pipefds[0];
1328 }
1329
Eric Andersen86349772000-12-18 20:25:50 +00001330 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001331
Eric Andersen86349772000-12-18 20:25:50 +00001332 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001333
Erik Andersen161220c2000-03-16 08:12:48 +00001334 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001335}
1336
Erik Andersend75af992000-03-16 08:09:09 +00001337static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001338{
Erik Andersen161220c2000-03-16 08:12:48 +00001339 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001340 char *next_command = NULL;
1341 struct job newjob;
Erik Andersen161220c2000-03-16 08:12:48 +00001342 int i;
Eric Andersena68ea1c2006-01-30 22:48:39 +00001343 int inbg = 0;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001344 int status;
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001345#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersend20d3752004-03-12 22:08:42 +00001346 pid_t parent_pgrp;
1347 /* save current owner of TTY so we can restore it on exit */
1348 parent_pgrp = tcgetpgrp(shell_terminal);
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001349#endif
Eric Andersen86349772000-12-18 20:25:50 +00001350 newjob.job_list = &job_list;
1351 newjob.job_context = DEFAULT_CONTEXT;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001352
Rob Landleyf9463792006-06-13 18:50:20 +00001353 command = xzalloc(BUFSIZ);
Erik Andersend75af992000-03-16 08:09:09 +00001354
Erik Andersen161220c2000-03-16 08:12:48 +00001355 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001356 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001357 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001358
Erik Andersend75af992000-03-16 08:09:09 +00001359 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001360 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001361
Eric Andersen86349772000-12-18 20:25:50 +00001362 if (!next_command) {
1363 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001364 break;
Eric Andersen86349772000-12-18 20:25:50 +00001365 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001366 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001367
Matt Kraai1f0c4362001-12-20 23:13:26 +00001368 if (! expand_arguments(next_command)) {
Eric Andersenca604592001-03-08 17:17:13 +00001369 free(command);
Rob Landleyf9463792006-06-13 18:50:20 +00001370 command = xzalloc(BUFSIZ);
Eric Andersenca604592001-03-08 17:17:13 +00001371 next_command = NULL;
1372 continue;
1373 }
1374
Eric Andersen86349772000-12-18 20:25:50 +00001375 if (!parse_command(&next_command, &newjob, &inbg) &&
1376 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001377 int pipefds[2] = {-1,-1};
Eric Andersenff9ad472004-02-10 01:07:45 +00001378 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001379 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001380 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001381 }
1382 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001383 free(command);
Denis Vlasenko4cccc032006-12-22 18:37:07 +00001384 command = xzalloc(BUFSIZ);
Eric Andersen86349772000-12-18 20:25:50 +00001385 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001386 }
1387 } else {
1388 /* a job is running in the foreground; wait for it */
1389 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001390 while (!job_list.fg->progs[i].pid ||
1391 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001392
Eric Andersen7373e482002-07-31 04:04:47 +00001393 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) {
1394 if (errno != ECHILD) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001395 bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
Eric Andersen7373e482002-07-31 04:04:47 +00001396 }
1397 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001398
Erik Andersend75af992000-03-16 08:09:09 +00001399 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001400 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001401 job_list.fg->running_progs--;
1402 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001403
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001404 last_return_code=WEXITSTATUS(status);
1405
Eric Andersen86349772000-12-18 20:25:50 +00001406 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001407 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001408 remove_job(&job_list, job_list.fg);
1409 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001410 }
Eric Andersen37032b42004-02-10 01:28:36 +00001411 }
1412#ifdef CONFIG_LASH_JOB_CONTROL
1413 else {
Erik Andersen161220c2000-03-16 08:12:48 +00001414 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001415 job_list.fg->stopped_progs++;
1416 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001417
Eric Andersen86349772000-12-18 20:25:50 +00001418 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1419 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1420 "Stopped", job_list.fg->text);
1421 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001422 }
Erik Andersend75af992000-03-16 08:09:09 +00001423 }
1424
Eric Andersen86349772000-12-18 20:25:50 +00001425 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001426 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001427 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001428 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Eric Andersenff9ad472004-02-10 01:07:45 +00001429 bb_perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001430 }
Eric Andersen37032b42004-02-10 01:28:36 +00001431#endif
Erik Andersend75af992000-03-16 08:09:09 +00001432 }
1433 }
Erik Andersen161220c2000-03-16 08:12:48 +00001434 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001435
Eric Andersen37032b42004-02-10 01:28:36 +00001436#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen1c314ad2000-06-28 16:56:25 +00001437 /* return controlling TTY back to parent process group before exiting */
Eric Andersen7373e482002-07-31 04:04:47 +00001438 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001439 bb_perror_msg("tcsetpgrp");
Eric Andersen37032b42004-02-10 01:28:36 +00001440#endif
Eric Andersenb54833c2000-07-03 23:56:26 +00001441
1442 /* return exit status if called with "-c" */
1443 if (input == NULL && WIFEXITED(status))
1444 return WEXITSTATUS(status);
Eric Andersenff9ad472004-02-10 01:07:45 +00001445
Erik Andersen161220c2000-03-16 08:12:48 +00001446 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001447}
1448
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001449#ifdef CONFIG_FEATURE_CLEAN_UP
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +00001450static void free_memory(void)
Eric Andersenfad9c112000-07-25 18:06:52 +00001451{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001452 if (cwd && cwd!=bb_msg_unknown) {
Eric Andersen5d60a462001-08-22 05:32:24 +00001453 free((char*)cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001454 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001455 if (local_pending_command)
1456 free(local_pending_command);
1457
Eric Andersen86349772000-12-18 20:25:50 +00001458 if (job_list.fg && !job_list.fg->running_progs) {
1459 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001460 }
1461}
Rob Landleyacf448d2006-07-09 17:03:07 +00001462#else
1463void free_memory(void);
Eric Andersenfad9c112000-07-25 18:06:52 +00001464#endif
1465
Eric Andersen37032b42004-02-10 01:28:36 +00001466#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen2d848a42001-06-25 17:11:54 +00001467/* Make sure we have a controlling tty. If we get started under a job
1468 * aware app (like bash for example), make sure we are now in charge so
1469 * we don't fight over who gets the foreground */
Eric Andersenece8fc22002-12-11 04:26:28 +00001470static void setup_job_control(void)
Eric Andersen2d848a42001-06-25 17:11:54 +00001471{
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001472 int status;
Eric Andersen37032b42004-02-10 01:28:36 +00001473 pid_t shell_pgrp;
Eric Andersenff9ad472004-02-10 01:07:45 +00001474
Eric Andersen2d848a42001-06-25 17:11:54 +00001475 /* Loop until we are in the foreground. */
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001476 while ((status = tcgetpgrp (shell_terminal)) >= 0) {
1477 if (status == (shell_pgrp = getpgrp ())) {
1478 break;
1479 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001480 kill (- shell_pgrp, SIGTTIN);
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001481 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001482
1483 /* Ignore interactive and job-control signals. */
1484 signal(SIGINT, SIG_IGN);
1485 signal(SIGQUIT, SIG_IGN);
1486 signal(SIGTSTP, SIG_IGN);
1487 signal(SIGTTIN, SIG_IGN);
1488 signal(SIGTTOU, SIG_IGN);
1489 signal(SIGCHLD, SIG_IGN);
1490
1491 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001492 setsid();
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +00001493 shell_pgrp = getpid();
Rob Landleyacf448d2006-07-09 17:03:07 +00001494 setpgid(shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001495
1496 /* Grab control of the terminal. */
1497 tcsetpgrp(shell_terminal, shell_pgrp);
1498}
Eric Andersen37032b42004-02-10 01:28:36 +00001499#else
1500static inline void setup_job_control(void)
1501{
1502}
1503#endif
Eric Andersen6efc48c2000-07-18 08:16:39 +00001504
Matt Kraai2d91deb2001-08-01 17:21:35 +00001505int lash_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001506{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001507 unsigned opt;
Erik Andersen161220c2000-03-16 08:12:48 +00001508 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001509 argc = argc_l;
1510 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001511
Denis Vlasenko38f63192007-01-22 09:03:07 +00001512#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001513 line_input_state = new_line_input_t(FOR_SHELL);
1514#endif
1515
Eric Andersen702ec592001-03-06 22:17:29 +00001516 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001517 last_jobid = 0;
Rob Landleyf9463792006-06-13 18:50:20 +00001518 close_me_list = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001519 job_list.head = NULL;
1520 job_list.fg = NULL;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001521 last_return_code=1;
Eric Andersen501c88b2000-07-28 15:14:45 +00001522
Eric Andersenb558e762000-11-30 22:43:16 +00001523 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001524 FILE *prof_input;
1525 prof_input = fopen("/etc/profile", "r");
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001526 if (prof_input) {
Rob Landley7a260f02006-06-19 03:20:03 +00001527 llist_add_to(&close_me_list, (void *)(long)fileno(prof_input));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001528 /* Now run the file */
1529 busy_loop(prof_input);
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00001530 fclose_if_not_stdin(prof_input);
Rob Landleyf9463792006-06-13 18:50:20 +00001531 llist_pop(&close_me_list);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001532 }
Eric Andersenb558e762000-11-30 22:43:16 +00001533 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001534
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001535 opt = getopt32(argc_l, argv_l, "+ic:", &local_pending_command);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001536#define LASH_OPT_i (1<<0)
1537#define LASH_OPT_c (1<<2)
1538 if (opt & LASH_OPT_c) {
1539 input = NULL;
1540 optind++;
1541 argv += optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001542 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001543 /* A shell is interactive if the `-i' flag was given, or if all of
1544 * the following conditions are met:
1545 * no -c command
1546 * no arguments remaining or the -s flag given
1547 * standard input is a terminal
1548 * standard output is a terminal
1549 * Refer to Posix.2, the description of the `sh' utility. */
Eric Andersen86349772000-12-18 20:25:50 +00001550 if (argv[optind]==NULL && input==stdin &&
Rob Landleyacf448d2006-07-09 17:03:07 +00001551 isatty(STDIN_FILENO) && isatty(STDOUT_FILENO))
1552 {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001553 opt |= LASH_OPT_i;
Eric Andersen86349772000-12-18 20:25:50 +00001554 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001555 setup_job_control();
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001556 if (opt & LASH_OPT_i) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001557 /* Looks like they want an interactive shell */
Rob Landleyacf448d2006-07-09 17:03:07 +00001558 if (!ENABLE_FEATURE_SH_EXTRA_QUIET) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001559 printf("\n\n%s Built-in shell (lash)\n"
1560 "Enter 'help' for a list of built-in commands.\n\n",
1561 BB_BANNER);
Rob Landleyacf448d2006-07-09 17:03:07 +00001562 }
1563 } else if (!local_pending_command && argv[optind]) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001564 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Rob Landleyd921b2e2006-08-03 15:41:12 +00001565 input = xfopen(argv[optind], "r");
Rob Landleyf9463792006-06-13 18:50:20 +00001566 /* be lazy, never mark this closed */
Rob Landley7a260f02006-06-19 03:20:03 +00001567 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Eric Andersen501c88b2000-07-28 15:14:45 +00001568 }
1569
Eric Andersen6efc48c2000-07-18 08:16:39 +00001570 /* initialize the cwd -- this is never freed...*/
Eric Andersene5dfced2001-04-09 22:48:12 +00001571 cwd = xgetcwd(0);
Eric Andersen5f265b72001-05-11 16:58:46 +00001572 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001573 cwd = bb_msg_unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001574
Rob Landleyacf448d2006-07-09 17:03:07 +00001575 if (ENABLE_FEATURE_CLEAN_UP) atexit(free_memory);
Eric Andersenfad9c112000-07-25 18:06:52 +00001576
Denis Vlasenko38f63192007-01-22 09:03:07 +00001577 if (ENABLE_FEATURE_EDITING) cmdedit_set_initial_prompt();
Rob Landleyacf448d2006-07-09 17:03:07 +00001578 else PS1 = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +00001579
Erik Andersen161220c2000-03-16 08:12:48 +00001580 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001581}