blob: 4067bc6bc853f2bb6622a4658477286bc3101df3 [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 Andersenbdfd0d72001-10-24 05:00:29 +000028#ifdef CONFIG_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +000029#include <locale.h>
Eric Andersene5dfced2001-04-09 22:48:12 +000030#endif
Eric Andersen13d1fa12001-03-08 23:59:45 +000031
Eric Andersen13d1fa12001-03-08 23:59:45 +000032#include <glob.h>
33#define expand_t glob_t
Erik Andersen3522eb12000-03-12 23:49:18 +000034
Eric Andersenff9ad472004-02-10 01:07:45 +000035/* Always enable for the moment... */
Eric Andersen95b26252004-02-10 01:30:21 +000036#define CONFIG_LASH_PIPE_N_REDIRECTS
37#define CONFIG_LASH_JOB_CONTROL
Eric Andersen13d1fa12001-03-08 23:59:45 +000038
Mark Whitley59ab0252001-01-23 22:30:04 +000039static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000040#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
41
Erik Andersend75af992000-03-16 08:09:09 +000042
Eric Andersenff9ad472004-02-10 01:07:45 +000043#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000044enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000045 REDIRECT_APPEND
46};
Eric Andersenff9ad472004-02-10 01:07:45 +000047#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000048
Rob Landleybc68cd12006-03-10 19:22:06 +000049enum {
50 DEFAULT_CONTEXT = 0x1,
51 IF_TRUE_CONTEXT = 0x2,
52 IF_FALSE_CONTEXT = 0x4,
53 THEN_EXP_CONTEXT = 0x8,
54 ELSE_EXP_CONTEXT = 0x10
55};
Eric Andersenfad9c112000-07-25 18:06:52 +000056
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +000057#define LASH_OPT_DONE (1)
58#define LASH_OPT_SAW_QUOTE (2)
59
Eric Andersenff9ad472004-02-10 01:07:45 +000060#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000061struct redir_struct {
62 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000063 int fd; /* file descriptor being redirected */
64 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000065};
Eric Andersenff9ad472004-02-10 01:07:45 +000066#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000067
Eric Andersen86349772000-12-18 20:25:50 +000068struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000069 pid_t pid; /* 0 if exited */
70 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000071 int num_redirects; /* elements in redirection array */
Eric Andersen86349772000-12-18 20:25:50 +000072 int is_stopped; /* is the program currently running? */
73 struct job *family; /* pointer back to the child's parent job */
Eric Andersenff9ad472004-02-10 01:07:45 +000074#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
75 struct redir_struct *redirects; /* I/O redirects */
76#endif
77};
78
79struct jobset {
80 struct job *head; /* head of list of running jobs */
81 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000082};
83
84struct job {
Eric Andersen86349772000-12-18 20:25:50 +000085 int jobid; /* job number */
86 int num_progs; /* total number of programs in job */
87 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000088 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +000089 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +000090 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +000091 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +000092 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +000093 int stopped_progs; /* number of programs alive, but stopped */
94 unsigned int job_context; /* bitmask defining current context */
95 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +000096};
97
Eric Andersen86349772000-12-18 20:25:50 +000098struct built_in_command {
Erik Andersen161220c2000-03-16 08:12:48 +000099 char *cmd; /* name */
100 char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +0000101 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +0000102};
103
Eric Andersen34e19412000-07-10 18:47:24 +0000104/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000105static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000106static int builtin_exec(struct child_prog *cmd);
107static int builtin_exit(struct child_prog *cmd);
108static int builtin_fg_bg(struct child_prog *cmd);
109static int builtin_help(struct child_prog *cmd);
110static int builtin_jobs(struct child_prog *dummy);
111static int builtin_pwd(struct child_prog *dummy);
112static int builtin_export(struct child_prog *cmd);
113static int builtin_source(struct child_prog *cmd);
114static int builtin_unset(struct child_prog *cmd);
115static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000116
Eric Andersen34e19412000-07-10 18:47:24 +0000117
118/* function prototypes for shell stuff */
Eric Andersen86349772000-12-18 20:25:50 +0000119static void checkjobs(struct jobset *job_list);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000120static void remove_job(struct jobset *j_list, struct job *job);
Eric Andersen86349772000-12-18 20:25:50 +0000121static int get_command(FILE * source, char *command);
122static int parse_command(char **command_ptr, struct job *job, int *inbg);
123static int run_command(struct job *newjob, int inbg, int outpipe[2]);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000124static int pseudo_exec(struct child_prog *cmd) ATTRIBUTE_NORETURN;
Erik Andersen3522eb12000-03-12 23:49:18 +0000125static int busy_loop(FILE * input);
126
Erik Andersend75af992000-03-16 08:09:09 +0000127
Mark Whitley37653aa2000-07-12 23:36:17 +0000128/* Table of built-in functions (these are non-forking builtins, meaning they
129 * can change global variables in the parent shell process but they will not
130 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Eric Andersen86349772000-12-18 20:25:50 +0000131static struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000132 {"bg", "Resume a job in the background", builtin_fg_bg},
133 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000134 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000135 {"exit", "Exit from shell()", builtin_exit},
136 {"fg", "Bring job into the foreground", builtin_fg_bg},
137 {"jobs", "Lists the active jobs", builtin_jobs},
138 {"export", "Set environment variable", builtin_export},
139 {"unset", "Unset environment variable", builtin_unset},
140 {"read", "Input environment variable", builtin_read},
Matt Kraaidd450a02000-09-13 03:43:36 +0000141 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000142 /* to do: add ulimit */
Eric Andersenfad9c112000-07-25 18:06:52 +0000143 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000144};
145
Mark Whitley37653aa2000-07-12 23:36:17 +0000146/* Table of forking built-in functions (things that fork cannot change global
147 * variables in the parent process, such as the current working directory) */
Eric Andersen86349772000-12-18 20:25:50 +0000148static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000149 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000150 {"help", "List shell built-in commands", builtin_help},
151 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000152};
153
Eric Andersen0bcc8132001-01-05 19:37:32 +0000154
Eric Andersen7467c8d2001-07-12 20:26:32 +0000155static int shell_context; /* Type prompt trigger (PS1 or PS2) */
Eric Andersen0bcc8132001-01-05 19:37:32 +0000156
157
158/* Globals that are static to this file */
Eric Andersencfa88ec2001-05-11 18:08:16 +0000159static const char *cwd;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000160static char *local_pending_command;
Eric Andersen86349772000-12-18 20:25:50 +0000161static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000162static int argc;
163static char **argv;
Rob Landleyf9463792006-06-13 18:50:20 +0000164static llist_t *close_me_list;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000165static int last_return_code;
166static int last_bg_pid;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000167static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000168static int shell_terminal;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000169static char *PS1;
Eric Andersene5dfced2001-04-09 22:48:12 +0000170static char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000171
172
Eric Andersenb558e762000-11-30 22:43:16 +0000173#ifdef DEBUG_SHELL
174static inline void debug_printf(const char *format, ...)
175{
176 va_list args;
177 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000178 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000179 va_end(args);
180}
181#else
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000182static inline void debug_printf(const char ATTRIBUTE_UNUSED *format, ...) { }
Eric Andersenb558e762000-11-30 22:43:16 +0000183#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000184
Eric Andersen86349772000-12-18 20:25:50 +0000185/*
186 Most builtins need access to the struct child_prog that has
187 their arguments, previously coded as cmd->progs[0]. That coding
188 can exhibit a bug, if the builtin is not the first command in
189 a pipeline: "echo foo | exec sort" will attempt to exec foo.
190
191builtin previous use notes
192------ ----------------- ---------
193cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000194exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
195exit cmd->progs[0]
196fg_bg cmd->progs[0], job_list->head, job_list->fg
197help 0
198jobs job_list->head
199pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000200export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000201source cmd->progs[0]
202unset cmd->progs[0]
203read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000204
205I added "struct job *family;" to struct child_prog,
206and switched API to builtin_foo(struct child_prog *child);
207So cmd->text becomes child->family->text
208 cmd->job_context becomes child->family->job_context
209 cmd->progs[0] becomes *child
210 job_list becomes child->family->job_list
211 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000212
Erik Andersend75af992000-03-16 08:09:09 +0000213/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000214static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000215{
Erik Andersen161220c2000-03-16 08:12:48 +0000216 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000217
Eric Andersen86349772000-12-18 20:25:50 +0000218 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000219 newdir = getenv("HOME");
220 else
Eric Andersen86349772000-12-18 20:25:50 +0000221 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000222 if (chdir(newdir)) {
Rob Landley5483de12006-06-20 21:35:26 +0000223 bb_perror_msg("cd: %s", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000224 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000225 }
Eric Andersencfa88ec2001-05-11 18:08:16 +0000226 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000227 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000228 cwd = bb_msg_unknown;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000229 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000230}
231
Eric Andersend2f56772000-09-21 02:48:07 +0000232/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000233static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000234{
Eric Andersen86349772000-12-18 20:25:50 +0000235 if (child->argv[1] == NULL)
236 return EXIT_SUCCESS; /* Really? */
237 child->argv++;
Rob Landley7a260f02006-06-19 03:20:03 +0000238 while(close_me_list) close((long)llist_pop(&close_me_list));
Eric Andersen86349772000-12-18 20:25:50 +0000239 pseudo_exec(child);
240 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000241}
242
Erik Andersen3522eb12000-03-12 23:49:18 +0000243/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000244static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000245{
Eric Andersen86349772000-12-18 20:25:50 +0000246 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000247 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000248
Eric Andersen86349772000-12-18 20:25:50 +0000249 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000250}
251
252/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000253static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000254{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000255 int i, jobnum;
Erik Andersen6273f652000-03-17 01:12:41 +0000256 struct job *job=NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000257
Eric Andersen8a646dd2001-06-21 16:38:11 +0000258 /* If they gave us no args, assume they want the last backgrounded task */
259 if (!child->argv[1]) {
260 for (job = child->family->job_list->head; job; job = job->next) {
261 if (job->jobid == last_jobid) {
262 break;
263 }
Erik Andersen161220c2000-03-16 08:12:48 +0000264 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000265 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000266 bb_error_msg("%s: no current job", child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000267 return EXIT_FAILURE;
268 }
269 } else {
270 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000271 bb_error_msg(bb_msg_invalid_arg, child->argv[1], child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000272 return EXIT_FAILURE;
273 }
274 for (job = child->family->job_list->head; job; job = job->next) {
275 if (job->jobid == jobnum) {
276 break;
277 }
278 }
279 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000280 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000281 return EXIT_FAILURE;
282 }
Erik Andersend75af992000-03-16 08:09:09 +0000283 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000284
Eric Andersen86349772000-12-18 20:25:50 +0000285 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000286 /* Put the job into the foreground. */
287 tcsetpgrp(shell_terminal, job->pgrp);
288
Eric Andersen86349772000-12-18 20:25:50 +0000289 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000290 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000291
Erik Andersen161220c2000-03-16 08:12:48 +0000292 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000293 for (i = 0; i < job->num_progs; i++)
294 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000295
Eric Andersen86349772000-12-18 20:25:50 +0000296 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000297
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000298 if ( (i=kill(- job->pgrp, SIGCONT)) < 0) {
299 if (i == ESRCH) {
300 remove_job(&job_list, job);
Eric Andersen07abfe22001-06-27 17:29:11 +0000301 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000302 bb_perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000303 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000304 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000305
Matt Kraai3e856ce2000-12-01 02:55:13 +0000306 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000307}
308
309/* built-in 'help' handler */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000310static int builtin_help(struct child_prog ATTRIBUTE_UNUSED *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000311{
Eric Andersen86349772000-12-18 20:25:50 +0000312 struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000313
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000314 printf("\nBuilt-in commands:\n"
315 "-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000316 for (x = bltins; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000317 if (x->descr==NULL)
318 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000319 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000320 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000321 for (x = bltins_forking; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000322 if (x->descr==NULL)
323 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000324 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000325 }
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000326 putchar('\n');
Matt Kraai3e856ce2000-12-01 02:55:13 +0000327 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000328}
329
330/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000331static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000332{
Erik Andersen161220c2000-03-16 08:12:48 +0000333 struct job *job;
Eric Andersen86349772000-12-18 20:25:50 +0000334 char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000335
Eric Andersen86349772000-12-18 20:25:50 +0000336 for (job = child->family->job_list->head; job; job = job->next) {
337 if (job->running_progs == job->stopped_progs)
338 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000339 else
Eric Andersen86349772000-12-18 20:25:50 +0000340 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000341
Eric Andersen86349772000-12-18 20:25:50 +0000342 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000343 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000344 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000345}
346
347
348/* built-in 'pwd' handler */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000349static int builtin_pwd(struct child_prog ATTRIBUTE_UNUSED *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000350{
Eric Andersencfa88ec2001-05-11 18:08:16 +0000351 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000352 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000353 cwd = bb_msg_unknown;
Matt Kraai59df6f72001-05-16 14:21:09 +0000354 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000355 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000356}
357
Erik Andersen6273f652000-03-17 01:12:41 +0000358/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000359static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000360{
Erik Andersen161220c2000-03-16 08:12:48 +0000361 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000362 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000363
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000364 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000365 char **e;
366 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000367 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000368 }
Matt Kraai2129f972001-04-04 17:50:04 +0000369 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000370 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000371 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000372 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000373 bb_perror_msg("export");
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000374#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000375 if (strncmp(v, "PS1=", 4)==0)
376 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000377#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000378
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000379#ifdef CONFIG_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +0000380 if(strncmp(v, "LC_ALL=", 7)==0)
381 setlocale(LC_ALL, getenv("LC_ALL"));
Mark Whitleya82a0032001-03-27 17:07:15 +0000382 if(strncmp(v, "LC_CTYPE=", 9)==0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000383 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000384#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000385
Erik Andersen161220c2000-03-16 08:12:48 +0000386 return (res);
Erik Andersen3522eb12000-03-12 23:49:18 +0000387}
388
Eric Andersenb54833c2000-07-03 23:56:26 +0000389/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000390static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000391{
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000392 int res = 0, len;
Eric Andersenb54833c2000-07-03 23:56:26 +0000393 char *s;
394 char string[MAX_READ];
395
Eric Andersen86349772000-12-18 20:25:50 +0000396 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000397 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen0d2d1eb2002-06-06 13:33:01 +0000398 safe_strncpy(string, child->argv[1], MAX_READ-1);
Eric Andersenb54833c2000-07-03 23:56:26 +0000399 len = strlen(string);
400 string[len++] = '=';
401 string[len] = '\0';
402 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000403 res = strlen(string);
404 if (res > len)
405 string[--res] = '\0'; /* chomp trailing newline */
Eric Andersenb54833c2000-07-03 23:56:26 +0000406 /*
407 ** string should now contain "VAR=<value>"
408 ** copy it (putenv() won't do that, so we must make sure
409 ** the string resides in a static buffer!)
410 */
411 res = -1;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000412 if ((s = strdup(string)))
Eric Andersenb54833c2000-07-03 23:56:26 +0000413 res = putenv(s);
414 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000415 bb_perror_msg("read");
Eric Andersenb54833c2000-07-03 23:56:26 +0000416 }
417 else
418 fgets(string, sizeof(string), stdin);
419
420 return (res);
421}
422
Erik Andersen3522eb12000-03-12 23:49:18 +0000423/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000424static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000425{
Erik Andersen161220c2000-03-16 08:12:48 +0000426 FILE *input;
427 int status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000428
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000429 input = fopen_or_warn(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000430 if (!input) {
Matt Kraai3e856ce2000-12-01 02:55:13 +0000431 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000432 }
Erik Andersend75af992000-03-16 08:09:09 +0000433
Rob Landley7a260f02006-06-19 03:20:03 +0000434 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Erik Andersen161220c2000-03-16 08:12:48 +0000435 /* Now run the file */
436 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000437 fclose(input);
Rob Landleyf9463792006-06-13 18:50:20 +0000438 llist_pop(&close_me_list);
Erik Andersen161220c2000-03-16 08:12:48 +0000439 return (status);
Erik Andersen3522eb12000-03-12 23:49:18 +0000440}
441
442/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000443static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000444{
Eric Andersen86349772000-12-18 20:25:50 +0000445 if (child->argv[1] == NULL) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000446 printf(bb_msg_requires_arg, "unset");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000447 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000448 }
Eric Andersen86349772000-12-18 20:25:50 +0000449 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000450 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000451}
452
Eric Andersen37032b42004-02-10 01:28:36 +0000453#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen3522eb12000-03-12 23:49:18 +0000454/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000455static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000456{
Erik Andersen161220c2000-03-16 08:12:48 +0000457 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000458 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000459
Eric Andersen86349772000-12-18 20:25:50 +0000460 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000461 free(cmd->progs[i].argv);
Eric Andersenff9ad472004-02-10 01:07:45 +0000462#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000463 if (cmd->progs[i].redirects)
464 free(cmd->progs[i].redirects);
Eric Andersenff9ad472004-02-10 01:07:45 +0000465#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000466 }
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000467 free(cmd->progs);
468 free(cmd->text);
469 free(cmd->cmdbuf);
Mark Whitley44a99142001-03-14 17:26:37 +0000470 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000471 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000472 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000473}
474
Eric Andersen1ca20a72001-03-21 07:34:27 +0000475/* remove a job from a jobset */
476static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000477{
Eric Andersen86349772000-12-18 20:25:50 +0000478 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000479
Eric Andersen86349772000-12-18 20:25:50 +0000480 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000481 if (job == j_list->head) {
482 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000483 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000484 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000485 while (prevjob->next != job)
486 prevjob = prevjob->next;
487 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000488 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000489
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000490 if (j_list->head)
491 last_jobid = j_list->head->jobid;
492 else
493 last_jobid = 0;
494
Erik Andersen161220c2000-03-16 08:12:48 +0000495 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000496}
497
Eric Andersenff9ad472004-02-10 01:07:45 +0000498/* Checks to see if any background processes have exited -- if they
Erik Andersen3522eb12000-03-12 23:49:18 +0000499 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000500static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000501{
Erik Andersen161220c2000-03-16 08:12:48 +0000502 struct job *job;
503 pid_t childpid;
504 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000505 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000506
Erik Andersen161220c2000-03-16 08:12:48 +0000507 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000508 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000509 prognum = 0;
510 while (prognum < job->num_progs &&
511 job->progs[prognum].pid != childpid) prognum++;
512 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000513 break;
514 }
515
Eric Andersena1d187a2000-07-17 19:14:41 +0000516 /* This happens on backticked commands */
517 if(job==NULL)
518 return;
519
Erik Andersen161220c2000-03-16 08:12:48 +0000520 if (WIFEXITED(status) || WIFSIGNALED(status)) {
521 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000522 job->running_progs--;
523 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000524
Eric Andersen86349772000-12-18 20:25:50 +0000525 if (!job->running_progs) {
526 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000527 last_jobid=0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000528 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000529 }
530 } else {
531 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000532 job->stopped_progs++;
533 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000534 }
Erik Andersend75af992000-03-16 08:09:09 +0000535 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000536
Erik Andersen161220c2000-03-16 08:12:48 +0000537 if (childpid == -1 && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000538 bb_perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000539}
Eric Andersen37032b42004-02-10 01:28:36 +0000540#else
541static void checkjobs(struct jobset *j_list)
542{
543}
544static void free_job(struct job *cmd)
545{
546}
547static void remove_job(struct jobset *j_list, struct job *job)
548{
549}
550#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000551
Eric Andersenff9ad472004-02-10 01:07:45 +0000552#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersene9f07fb2000-12-21 18:31:36 +0000553/* squirrel != NULL means we squirrel away copies of stdin, stdout,
554 * and stderr if they are redirected. */
555static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000556{
557 int i;
558 int openfd;
559 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000560 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000561
Eric Andersen86349772000-12-18 20:25:50 +0000562 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000563 switch (redir->type) {
564 case REDIRECT_INPUT:
565 mode = O_RDONLY;
566 break;
567 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000568 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000569 break;
570 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000571 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000572 break;
573 }
574
575 openfd = open(redir->filename, mode, 0666);
576 if (openfd < 0) {
577 /* this could get lost if stderr has been redirected, but
578 bash and ash both lose it as well (though zsh doesn't!) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000579 bb_perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000580 return 1;
581 }
582
583 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000584 if (squirrel && redir->fd < 3) {
585 squirrel[redir->fd] = dup(redir->fd);
Eric Andersenacd647c2004-08-16 08:38:34 +0000586 fcntl (squirrel[redir->fd], F_SETFD, FD_CLOEXEC);
Eric Andersene9f07fb2000-12-21 18:31:36 +0000587 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000588 dup2(openfd, redir->fd);
589 close(openfd);
590 }
591 }
592
593 return 0;
594}
595
Eric Andersene9f07fb2000-12-21 18:31:36 +0000596static void restore_redirects(int squirrel[])
597{
598 int i, fd;
599 for (i=0; i<3; i++) {
600 fd = squirrel[i];
601 if (fd != -1) {
602 /* No error checking. I sure wouldn't know what
603 * to do with an error if I found one! */
604 dup2(fd, i);
605 close(fd);
606 }
607 }
608}
Eric Andersenff9ad472004-02-10 01:07:45 +0000609#else
610static inline int setup_redirects(struct child_prog *prog, int squirrel[])
611{
612 return 0;
613}
614static inline void restore_redirects(int squirrel[])
615{
616}
617#endif
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000618
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000619static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000620{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000621#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000622 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000623#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000624 PS1 = getenv("PS1");
Eric Andersene5dfced2001-04-09 22:48:12 +0000625 if(PS1==0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000626 PS1 = "\\w \\$ ";
Eric Andersenff9ad472004-02-10 01:07:45 +0000627#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000628}
629
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000630static inline void setup_prompt_string(char **prompt_str)
631{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000632#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000633 /* Set up the prompt */
634 if (shell_context == 0) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000635 free(PS1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000636 PS1=xmalloc(strlen(cwd)+4);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000637 sprintf(PS1, "%s %c ", cwd, ( geteuid() != 0 ) ? '$': '#');
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000638 *prompt_str = PS1;
639 } else {
640 *prompt_str = PS2;
641 }
642#else
643 *prompt_str = (shell_context==0)? PS1 : PS2;
Eric Andersenff9ad472004-02-10 01:07:45 +0000644#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000645}
Eric Andersen22332fd2001-01-30 23:40:39 +0000646
Eric Andersen09acc062001-01-04 11:10:38 +0000647static int get_command(FILE * source, char *command)
648{
649 char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000650
Eric Andersen1c314ad2000-06-28 16:56:25 +0000651 if (source == NULL) {
652 if (local_pending_command) {
653 /* a command specified (-c option): return it & mark it done */
654 strcpy(command, local_pending_command);
655 free(local_pending_command);
656 local_pending_command = NULL;
657 return 0;
658 }
659 return 1;
660 }
661
Erik Andersen161220c2000-03-16 08:12:48 +0000662 if (source == stdin) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000663 setup_prompt_string(&prompt_str);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000664
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000665#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000666 /*
667 ** enable command line editing only while a command line
668 ** is actually being read; otherwise, we'll end up bequeathing
669 ** atexit() handlers and other unwanted stuff to our
670 ** child processes (rob@sysgo.de)
671 */
Eric Andersen86349772000-12-18 20:25:50 +0000672 cmdedit_read_input(prompt_str, command);
Erik Andersen161220c2000-03-16 08:12:48 +0000673 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000674#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000675 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000676#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000677 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000678
Erik Andersen161220c2000-03-16 08:12:48 +0000679 if (!fgets(command, BUFSIZ - 2, source)) {
680 if (source == stdin)
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000681 puts("");
Erik Andersen161220c2000-03-16 08:12:48 +0000682 return 1;
683 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000684
Erik Andersen161220c2000-03-16 08:12:48 +0000685 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000686}
687
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +0000688static char * strsep_space( char *string, int * ix)
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000689{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000690 /* Short circuit the trivial case */
691 if ( !string || ! string[*ix])
692 return NULL;
693
694 /* Find the end of the token. */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000695 while (string[*ix] && !isspace(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000696 (*ix)++;
697 }
698
Eric Andersenff9ad472004-02-10 01:07:45 +0000699 /* Find the end of any whitespace trailing behind
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000700 * the token and let that be part of the token */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000701 while (string[*ix] && (isspace)(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000702 (*ix)++;
703 }
704
Rob Landleyf9463792006-06-13 18:50:20 +0000705 if (!*ix) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000706 /* Nothing useful was found */
707 return NULL;
708 }
709
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000710 return xstrndup(string, *ix);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000711}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000712
Eric Andersenca604592001-03-08 17:17:13 +0000713static int expand_arguments(char *command)
714{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000715 int total_length=0, length, i, retval, ix = 0;
716 expand_t expand_result;
717 char *tmpcmd, *cmd, *cmd_copy;
718 char *src, *dst, *var;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000719 const char * const out_of_space = "out of space during expansion";
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000720 int flags = GLOB_NOCHECK
721#ifdef GLOB_BRACE
722 | GLOB_BRACE
Eric Andersenff9ad472004-02-10 01:07:45 +0000723#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000724#ifdef GLOB_TILDE
725 | GLOB_TILDE
Eric Andersenff9ad472004-02-10 01:07:45 +0000726#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000727 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000728
Eric Andersenca604592001-03-08 17:17:13 +0000729 /* get rid of the terminating \n */
730 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000731
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000732 /* Fix up escape sequences to be the Real Thing(tm) */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000733 while( command && command[ix]) {
734 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000735 const char *tmp = command+ix+1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000736 command[ix] = bb_process_escape_sequence( &tmp );
Eric Andersen1ca20a72001-03-21 07:34:27 +0000737 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000738 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000739 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000740 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000741 /* Use glob and then fixup environment variables and such */
742
743 /* It turns out that glob is very stupid. We have to feed it one word at a
744 * time since it can't cope with a full string. Here we convert command
745 * (char*) into cmd (char**, one word per string) */
746
747 /* We need a clean copy, so strsep can mess up the copy while
748 * we write stuff into the original (in a minute) */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000749 cmd = cmd_copy = xstrdup(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000750 *command = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000751 for (ix = 0, tmpcmd = cmd;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000752 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
753 if (*tmpcmd == '\0')
754 break;
755 /* we need to trim() the result for glob! */
756 trim(tmpcmd);
757 retval = glob(tmpcmd, flags, NULL, &expand_result);
758 free(tmpcmd); /* Free mem allocated by strsep_space */
759 if (retval == GLOB_NOSPACE) {
760 /* Mem may have been allocated... */
761 globfree (&expand_result);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000762 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000763 return FALSE;
764 } else if (retval != 0) {
765 /* Some other error. GLOB_NOMATCH shouldn't
Eric Andersenff9ad472004-02-10 01:07:45 +0000766 * happen because of the GLOB_NOCHECK flag in
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000767 * the glob call. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000768 bb_error_msg("syntax error");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000769 return FALSE;
770 } else {
771 /* Convert from char** (one word per string) to a simple char*,
772 * but don't overflow command which is BUFSIZ in length */
773 for (i=0; i < expand_result.gl_pathc; i++) {
774 length=strlen(expand_result.gl_pathv[i]);
775 if (total_length+length+1 >= BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000776 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000777 return FALSE;
778 }
779 strcat(command+total_length, " ");
780 total_length+=1;
781 strcat(command+total_length, expand_result.gl_pathv[i]);
782 total_length+=length;
783 }
784 globfree (&expand_result);
785 }
786 }
787 free(cmd_copy);
788 trim(command);
789
Eric Andersenff9ad472004-02-10 01:07:45 +0000790 /* Now do the shell variable substitutions which
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000791 * wordexp can't do for us, namely $? and $! */
792 src = command;
793 while((dst = strchr(src,'$')) != NULL){
794 var = NULL;
795 switch(*(dst+1)) {
796 case '?':
797 var = itoa(last_return_code);
798 break;
799 case '!':
800 if (last_bg_pid==-1)
801 *(var)='\0';
802 else
803 var = itoa(last_bg_pid);
804 break;
805 /* Everything else like $$, $#, $[0-9], etc. should all be
806 * expanded by wordexp(), so we can in theory skip that stuff
807 * here, but just to be on the safe side (i.e., since uClibc
808 * wordexp doesn't do this stuff yet), lets leave it in for
809 * now. */
810 case '$':
811 var = itoa(getpid());
812 break;
813 case '#':
814 var = itoa(argc-1);
815 break;
816 case '0':case '1':case '2':case '3':case '4':
817 case '5':case '6':case '7':case '8':case '9':
818 {
Eric Andersen7ab93d92002-11-08 09:40:02 +0000819 int ixx=*(dst+1)-48+1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000820 if (ixx >= argc) {
821 var='\0';
822 } else {
823 var = argv[ixx];
824 }
825 }
826 break;
827
828 }
829 if (var) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000830 /* a single character construction was found, and
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000831 * already handled in the case statement */
832 src=dst+2;
833 } else {
834 /* Looks like an environment variable */
835 char delim_hold;
836 int num_skip_chars=0;
837 int dstlen = strlen(dst);
838 /* Is this a ${foo} type variable? */
839 if (dstlen >=2 && *(dst+1) == '{') {
840 src=strchr(dst+1, '}');
841 num_skip_chars=1;
842 } else {
843 src=dst+1;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000844 while((isalnum)(*src) || *src=='_') src++;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000845 }
846 if (src == NULL) {
847 src = dst+dstlen;
848 }
849 delim_hold=*src;
850 *src='\0'; /* temporary */
851 var = getenv(dst + 1 + num_skip_chars);
852 *src=delim_hold;
853 src += num_skip_chars;
854 }
855 if (var == NULL) {
856 /* Seems we got an un-expandable variable. So delete it. */
857 var = "";
858 }
859 {
860 int subst_len = strlen(var);
861 int trail_len = strlen(src);
862 if (dst+subst_len+trail_len >= command+BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000863 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000864 return FALSE;
865 }
866 /* Move stuff to the end of the string to accommodate
867 * filling the created gap with the new stuff */
868 memmove(dst+subst_len, src, trail_len+1);
869 /* Now copy in the new stuff */
870 memcpy(dst, var, subst_len);
871 src = dst+subst_len;
872 }
873 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000874
Eric Andersenca604592001-03-08 17:17:13 +0000875 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000876}
877
Eric Andersen86349772000-12-18 20:25:50 +0000878/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
879 line). If a valid command is found, command_ptr is set to point to
Eric Andersenff9ad472004-02-10 01:07:45 +0000880 the beginning of the next command (if the original command had more
881 then one job associated with it) or NULL if no more commands are
Erik Andersen3522eb12000-03-12 23:49:18 +0000882 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000883static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000884{
Erik Andersen161220c2000-03-16 08:12:48 +0000885 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000886 char *return_command = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +0000887 char *src, *buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000888 int argc_l;
889 int flag;
Eric Andersen86349772000-12-18 20:25:50 +0000890 int argv_alloced;
Erik Andersen161220c2000-03-16 08:12:48 +0000891 char quote = '\0';
Eric Andersen86349772000-12-18 20:25:50 +0000892 struct child_prog *prog;
Eric Andersenff9ad472004-02-10 01:07:45 +0000893#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
894 int i;
895 char *chptr;
896#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000897
Erik Andersen161220c2000-03-16 08:12:48 +0000898 /* skip leading white space */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000899 *command_ptr = skip_whitespace(*command_ptr);
Erik Andersen3522eb12000-03-12 23:49:18 +0000900
Erik Andersen161220c2000-03-16 08:12:48 +0000901 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000902 if (!**command_ptr || (**command_ptr == '#')) {
903 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000904 return 0;
905 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000906
Eric Andersen86349772000-12-18 20:25:50 +0000907 *inbg = 0;
908 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000909 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000910
Eric Andersenff9ad472004-02-10 01:07:45 +0000911 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000912 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000913 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000914
Eric Andersenff9ad472004-02-10 01:07:45 +0000915 Getting clean memory relieves us of the task of NULL
916 terminating things and makes the rest of this look a bit
Erik Andersen161220c2000-03-16 08:12:48 +0000917 cleaner (though it is, admittedly, a tad less efficient) */
Rob Landleyf9463792006-06-13 18:50:20 +0000918 job->cmdbuf = command = xzalloc(2*strlen(*command_ptr) + 1);
Erik Andersen161220c2000-03-16 08:12:48 +0000919 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000920
Erik Andersen161220c2000-03-16 08:12:48 +0000921 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000922 prog->num_redirects = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000923 prog->is_stopped = 0;
924 prog->family = job;
Eric Andersenff9ad472004-02-10 01:07:45 +0000925#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
926 prog->redirects = NULL;
927#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000928
Eric Andersen86349772000-12-18 20:25:50 +0000929 argv_alloced = 5;
930 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
931 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +0000932
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000933 flag = argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000934 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +0000935 src = *command_ptr;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000936 while (*src && !(flag & LASH_OPT_DONE)) {
Erik Andersen161220c2000-03-16 08:12:48 +0000937 if (quote == *src) {
938 quote = '\0';
939 } else if (quote) {
940 if (*src == '\\') {
941 src++;
942 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000943 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +0000944 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +0000945 return 1;
946 }
947
948 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +0000949 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +0000950 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +0000951 *buf++ = '\\';
952 }
Erik Andersen161220c2000-03-16 08:12:48 +0000953 } else if (*src == '*' || *src == '?' || *src == '[' ||
954 *src == ']') *buf++ = '\\';
955 *buf++ = *src;
956 } else if (isspace(*src)) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000957 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000958 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +0000959 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +0000960 if ((argc_l + 1) == argv_alloced) {
961 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +0000962 prog->argv = xrealloc(prog->argv,
963 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +0000964 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +0000965 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000966 prog->argv[argc_l] = buf;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000967 flag ^= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000968 }
969 } else
970 switch (*src) {
971 case '"':
972 case '\'':
973 quote = *src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000974 flag |= LASH_OPT_SAW_QUOTE;
Erik Andersen161220c2000-03-16 08:12:48 +0000975 break;
976
977 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000978 if (*(src-1)== '$')
979 *buf++ = *src;
980 else
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000981 flag |= LASH_OPT_DONE;
Erik Andersen161220c2000-03-16 08:12:48 +0000982 break;
983
Eric Andersenff9ad472004-02-10 01:07:45 +0000984#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000985 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +0000986 case '<':
Eric Andersen86349772000-12-18 20:25:50 +0000987 i = prog->num_redirects++;
988 prog->redirects = xrealloc(prog->redirects,
989 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +0000990 (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +0000991
Eric Andersen86349772000-12-18 20:25:50 +0000992 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000993 if (buf != prog->argv[argc_l]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000994 /* the stuff before this character may be the file number
Erik Andersen161220c2000-03-16 08:12:48 +0000995 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +0000996 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000997 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +0000998
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000999 if (*chptr && *prog->argv[argc_l]) {
1000 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001001 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001002 }
1003 }
1004
Eric Andersen86349772000-12-18 20:25:50 +00001005 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001006 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001007 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001008 else
Eric Andersen86349772000-12-18 20:25:50 +00001009 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001010 }
1011
1012 if (*src++ == '>') {
1013 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001014 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001015 REDIRECT_APPEND, src++;
1016 else
Eric Andersen86349772000-12-18 20:25:50 +00001017 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001018 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001019 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001020 }
1021
1022 /* This isn't POSIX sh compliant. Oh well. */
1023 chptr = src;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001024 chptr = skip_whitespace(chptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001025
1026 if (!*chptr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001027 bb_error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001028 free_job(job);
1029 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001030 return 1;
1031 }
1032
Eric Andersen86349772000-12-18 20:25:50 +00001033 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001034 while (*chptr && !isspace(*chptr))
1035 *buf++ = *chptr++;
1036
1037 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001038 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001039 break;
1040
1041 case '|': /* pipe */
1042 /* finish this command */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001043 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001044 argc_l++;
1045 if (!argc_l) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001046 goto empty_command_in_pipe;
Erik Andersen161220c2000-03-16 08:12:48 +00001047 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001048 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001049
1050 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001051 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001052 job->progs = xrealloc(job->progs,
Eric Andersen86349772000-12-18 20:25:50 +00001053 sizeof(*job->progs) * job->num_progs);
1054 prog = job->progs + (job->num_progs - 1);
1055 prog->num_redirects = 0;
1056 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001057 prog->is_stopped = 0;
1058 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001059 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001060
Eric Andersen86349772000-12-18 20:25:50 +00001061 argv_alloced = 5;
1062 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001063 prog->argv[0] = ++buf;
1064
1065 src++;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001066 src = skip_whitespace(src);
Erik Andersen161220c2000-03-16 08:12:48 +00001067
1068 if (!*src) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001069empty_command_in_pipe:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001070 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001071 free_job(job);
1072 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001073 return 1;
1074 }
1075 src--; /* we'll ++ it at the end of the loop */
1076
1077 break;
Eric Andersenff9ad472004-02-10 01:07:45 +00001078#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001079
Eric Andersen37032b42004-02-10 01:28:36 +00001080#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen161220c2000-03-16 08:12:48 +00001081 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001082 *inbg = 1;
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001083 /* fallthrough */
Eric Andersen37032b42004-02-10 01:28:36 +00001084#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001085 case ';': /* multiple commands */
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001086 flag |= LASH_OPT_DONE;
Eric Andersen86349772000-12-18 20:25:50 +00001087 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001088 break;
1089
Matt Kraai131241f2000-09-14 00:43:20 +00001090 case '\\':
1091 src++;
1092 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001093 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001094 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001095 return 1;
1096 }
1097 if (*src == '*' || *src == '[' || *src == ']'
1098 || *src == '?') *buf++ = '\\';
1099 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001100 default:
1101 *buf++ = *src;
1102 }
1103
Erik Andersend75af992000-03-16 08:09:09 +00001104 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001105 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001106
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001107 if (*prog->argv[argc_l] || flag & LASH_OPT_SAW_QUOTE) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001108 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001109 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001110 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001111 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001112 return 0;
1113 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001114 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001115
Eric Andersen86349772000-12-18 20:25:50 +00001116 if (!return_command) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001117 job->text = xstrdup(*command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001118 } else {
1119 /* This leaves any trailing spaces, which is a bit sloppy */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001120 job->text = xstrndup(*command_ptr, return_command - *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001121 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001122
Eric Andersen86349772000-12-18 20:25:50 +00001123 *command_ptr = return_command;
Eric Andersenff9ad472004-02-10 01:07:45 +00001124
Erik Andersend75af992000-03-16 08:09:09 +00001125 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001126}
1127
Eric Andersen86349772000-12-18 20:25:50 +00001128/* Run the child_prog, no matter what kind of command it uses.
1129 */
1130static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001131{
Eric Andersen86349772000-12-18 20:25:50 +00001132 struct built_in_command *x;
Rob Landley68286772006-06-13 19:01:30 +00001133
Eric Andersene9f07fb2000-12-21 18:31:36 +00001134 /* Check if the command matches any of the non-forking builtins.
1135 * Depending on context, this might be redundant. But it's
1136 * easier to waste a few CPU cycles than it is to figure out
1137 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001138 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001139 for (x = bltins; x->cmd; x++) {
1140 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001141 _exit(x->function(child));
Eric Andersene9f07fb2000-12-21 18:31:36 +00001142 }
1143 }
1144
1145 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001146 for (x = bltins_forking; x->cmd; x++) {
1147 if (strcmp(child->argv[0], x->cmd) == 0) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001148 applet_name=x->cmd;
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001149 _exit (x->function(child));
Eric Andersen86349772000-12-18 20:25:50 +00001150 }
1151 }
Rob Landley68286772006-06-13 19:01:30 +00001152
Eric Andersen86349772000-12-18 20:25:50 +00001153 /* Check if the command matches any busybox internal
1154 * commands ("applets") here. Following discussions from
Eric Andersen2423b122001-12-08 01:56:15 +00001155 * November 2000 on busybox@busybox.net, don't use
Manuel Novoa III cad53642003-03-19 09:13:01 +00001156 * bb_get_last_path_component(). This way explicit (with
Eric Andersen86349772000-12-18 20:25:50 +00001157 * slashes) filenames will never be interpreted as an
1158 * applet, just like with builtins. This way the user can
1159 * override an applet with an explicit filename reference.
1160 * The only downside to this change is that an explicit
1161 * /bin/foo invocation will fork and exec /bin/foo, even if
1162 * /bin/foo is a symlink to busybox.
1163 */
Eric Andersen86349772000-12-18 20:25:50 +00001164
Rob Landley68286772006-06-13 19:01:30 +00001165 if (ENABLE_FEATURE_SH_STANDALONE_SHELL) {
1166 char **argv_l = child->argv;
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001167 int argc_l;
Bernhard Reutner-Fischer62558762006-06-03 10:28:25 +00001168
Rob Landley68286772006-06-13 19:01:30 +00001169 for(argc_l=0; *argv_l; argv_l++, argc_l++);
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001170 optind = 1;
Rob Landley68286772006-06-13 19:01:30 +00001171 run_applet_by_name(child->argv[0], argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001172 }
Eric Andersen86349772000-12-18 20:25:50 +00001173
1174 execvp(child->argv[0], child->argv);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001175
Eric Andersenff9ad472004-02-10 01:07:45 +00001176 /* Do not use bb_perror_msg_and_die() here, since we must not
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001177 * call exit() but should call _exit() instead */
Rob Landley5483de12006-06-20 21:35:26 +00001178 bb_perror_msg("%s", child->argv[0]);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001179 _exit(EXIT_FAILURE);
Eric Andersen86349772000-12-18 20:25:50 +00001180}
1181
1182static void insert_job(struct job *newjob, int inbg)
1183{
1184 struct job *thejob;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001185 struct jobset *j_list=newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001186
1187 /* find the ID for thejob to use */
1188 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001189 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001190 if (thejob->jobid >= newjob->jobid)
1191 newjob->jobid = thejob->jobid + 1;
1192
1193 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001194 if (!j_list->head) {
1195 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001196 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001197 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001198 thejob->next = xmalloc(sizeof(*thejob));
1199 thejob = thejob->next;
1200 }
1201
1202 *thejob = *newjob; /* physically copy the struct job */
1203 thejob->next = NULL;
1204 thejob->running_progs = thejob->num_progs;
1205 thejob->stopped_progs = 0;
1206
Eric Andersen37032b42004-02-10 01:28:36 +00001207#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen86349772000-12-18 20:25:50 +00001208 if (inbg) {
Eric Andersenff9ad472004-02-10 01:07:45 +00001209 /* we don't wait for background thejobs to return -- append it
Eric Andersen86349772000-12-18 20:25:50 +00001210 to the list of backgrounded thejobs and leave it alone */
1211 printf("[%d] %d\n", thejob->jobid,
1212 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001213 last_jobid = newjob->jobid;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001214 last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001215 } else {
1216 newjob->job_list->fg = thejob;
1217
1218 /* move the new process group into the foreground */
Rob Landleyf9463792006-06-13 18:50:20 +00001219 /* Ignore errors since child could have already exited */
1220 tcsetpgrp(shell_terminal, newjob->pgrp);
Eric Andersen86349772000-12-18 20:25:50 +00001221 }
Eric Andersen37032b42004-02-10 01:28:36 +00001222#endif
Eric Andersen86349772000-12-18 20:25:50 +00001223}
1224
1225static int run_command(struct job *newjob, int inbg, int outpipe[2])
1226{
1227 /* struct job *thejob; */
1228 int i;
1229 int nextin, nextout;
1230 int pipefds[2]; /* pipefd[0] is for reading */
1231 struct built_in_command *x;
1232 struct child_prog *child;
1233
Eric Andersen6efc48c2000-07-18 08:16:39 +00001234 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001235 for (i = 0; i < newjob->num_progs; i++) {
1236 child = & (newjob->progs[i]);
1237
1238 if ((i + 1) < newjob->num_progs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001239 if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001240 nextout = pipefds[1];
1241 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001242 if (outpipe[1]!=-1) {
1243 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001244 } else {
1245 nextout = 1;
1246 }
Erik Andersen161220c2000-03-16 08:12:48 +00001247 }
1248
Eric Andersen501c88b2000-07-28 15:14:45 +00001249
Eric Andersene9f07fb2000-12-21 18:31:36 +00001250 /* Check if the command matches any non-forking builtins,
1251 * but only if this is a simple command.
1252 * Non-forking builtins within pipes have to fork anyway,
1253 * and are handled in pseudo_exec. "echo foo | read bar"
1254 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001255 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001256 if (newjob->num_progs == 1) {
Rob Landley7a8e4912006-07-01 17:19:02 +00001257 /* Check if the command sets an environment variable. */
1258 if (strchr(child->argv[0], '=') != NULL) {
1259 child->argv[1] = child->argv[0];
1260 return builtin_export(child);
1261 }
1262
Eric Andersene9f07fb2000-12-21 18:31:36 +00001263 for (x = bltins; x->cmd; x++) {
1264 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersene9f07fb2000-12-21 18:31:36 +00001265 int rcode;
Eric Andersenff9ad472004-02-10 01:07:45 +00001266 int squirrel[] = {-1, -1, -1};
Eric Andersene9f07fb2000-12-21 18:31:36 +00001267 setup_redirects(child, squirrel);
1268 rcode = x->function(child);
1269 restore_redirects(squirrel);
1270 return rcode;
1271 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001272 }
1273 }
1274
Eric Andersene3efc922004-04-12 17:59:24 +00001275#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersenff9ad472004-02-10 01:07:45 +00001276 if (!(child->pid = fork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001277#else
Eric Andersenff9ad472004-02-10 01:07:45 +00001278 if (!(child->pid = vfork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001279#endif
1280 {
Eric Andersen2d848a42001-06-25 17:11:54 +00001281 /* Set the handling for job control signals back to the default. */
1282 signal(SIGINT, SIG_DFL);
1283 signal(SIGQUIT, SIG_DFL);
1284 signal(SIGTSTP, SIG_DFL);
1285 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001286 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001287 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001288
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001289 /* Close all open filehandles. */
Rob Landley7a260f02006-06-19 03:20:03 +00001290 while(close_me_list) close((long)llist_pop(&close_me_list));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001291
Eric Andersen86349772000-12-18 20:25:50 +00001292 if (outpipe[1]!=-1) {
1293 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001294 }
1295 if (nextin != 0) {
1296 dup2(nextin, 0);
1297 close(nextin);
1298 }
1299
1300 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001301 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001302 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001303 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001304 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001305 }
1306
Eric Andersen86349772000-12-18 20:25:50 +00001307 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001308 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001309
Eric Andersen86349772000-12-18 20:25:50 +00001310 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001311 }
Eric Andersen86349772000-12-18 20:25:50 +00001312 if (outpipe[1]!=-1) {
1313 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001314 }
Erik Andersen161220c2000-03-16 08:12:48 +00001315
1316 /* put our child in the process group whose leader is the
1317 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001318 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001319 if (nextin != 0)
1320 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001321 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001322 close(nextout);
1323
Eric Andersenff9ad472004-02-10 01:07:45 +00001324 /* If there isn't another process, nextin is garbage
Erik Andersen161220c2000-03-16 08:12:48 +00001325 but it doesn't matter */
1326 nextin = pipefds[0];
1327 }
1328
Eric Andersen86349772000-12-18 20:25:50 +00001329 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001330
Eric Andersen86349772000-12-18 20:25:50 +00001331 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001332
Erik Andersen161220c2000-03-16 08:12:48 +00001333 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001334}
1335
Erik Andersend75af992000-03-16 08:09:09 +00001336static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001337{
Erik Andersen161220c2000-03-16 08:12:48 +00001338 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001339 char *next_command = NULL;
1340 struct job newjob;
Erik Andersen161220c2000-03-16 08:12:48 +00001341 int i;
Eric Andersena68ea1c2006-01-30 22:48:39 +00001342 int inbg = 0;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001343 int status;
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001344#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersend20d3752004-03-12 22:08:42 +00001345 pid_t parent_pgrp;
1346 /* save current owner of TTY so we can restore it on exit */
1347 parent_pgrp = tcgetpgrp(shell_terminal);
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001348#endif
Eric Andersen86349772000-12-18 20:25:50 +00001349 newjob.job_list = &job_list;
1350 newjob.job_context = DEFAULT_CONTEXT;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001351
Rob Landleyf9463792006-06-13 18:50:20 +00001352 command = xzalloc(BUFSIZ);
Erik Andersend75af992000-03-16 08:09:09 +00001353
Erik Andersen161220c2000-03-16 08:12:48 +00001354 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001355 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001356 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001357
Erik Andersend75af992000-03-16 08:09:09 +00001358 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001359 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001360
Eric Andersen86349772000-12-18 20:25:50 +00001361 if (!next_command) {
1362 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001363 break;
Eric Andersen86349772000-12-18 20:25:50 +00001364 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001365 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001366
Matt Kraai1f0c4362001-12-20 23:13:26 +00001367 if (! expand_arguments(next_command)) {
Eric Andersenca604592001-03-08 17:17:13 +00001368 free(command);
Rob Landleyf9463792006-06-13 18:50:20 +00001369 command = xzalloc(BUFSIZ);
Eric Andersenca604592001-03-08 17:17:13 +00001370 next_command = NULL;
1371 continue;
1372 }
1373
Eric Andersen86349772000-12-18 20:25:50 +00001374 if (!parse_command(&next_command, &newjob, &inbg) &&
1375 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001376 int pipefds[2] = {-1,-1};
Eric Andersenff9ad472004-02-10 01:07:45 +00001377 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001378 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001379 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001380 }
1381 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001382 free(command);
Rob Landleyf9463792006-06-13 18:50:20 +00001383 command = (char *) xzalloc(BUFSIZ);
Eric Andersen86349772000-12-18 20:25:50 +00001384 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001385 }
1386 } else {
1387 /* a job is running in the foreground; wait for it */
1388 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001389 while (!job_list.fg->progs[i].pid ||
1390 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001391
Eric Andersen7373e482002-07-31 04:04:47 +00001392 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) {
1393 if (errno != ECHILD) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001394 bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
Eric Andersen7373e482002-07-31 04:04:47 +00001395 }
1396 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001397
Erik Andersend75af992000-03-16 08:09:09 +00001398 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001399 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001400 job_list.fg->running_progs--;
1401 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001402
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001403 last_return_code=WEXITSTATUS(status);
1404
Eric Andersen86349772000-12-18 20:25:50 +00001405 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001406 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001407 remove_job(&job_list, job_list.fg);
1408 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001409 }
Eric Andersen37032b42004-02-10 01:28:36 +00001410 }
1411#ifdef CONFIG_LASH_JOB_CONTROL
1412 else {
Erik Andersen161220c2000-03-16 08:12:48 +00001413 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001414 job_list.fg->stopped_progs++;
1415 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001416
Eric Andersen86349772000-12-18 20:25:50 +00001417 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1418 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1419 "Stopped", job_list.fg->text);
1420 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001421 }
Erik Andersend75af992000-03-16 08:09:09 +00001422 }
1423
Eric Andersen86349772000-12-18 20:25:50 +00001424 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001425 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001426 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001427 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Eric Andersenff9ad472004-02-10 01:07:45 +00001428 bb_perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001429 }
Eric Andersen37032b42004-02-10 01:28:36 +00001430#endif
Erik Andersend75af992000-03-16 08:09:09 +00001431 }
1432 }
Erik Andersen161220c2000-03-16 08:12:48 +00001433 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001434
Eric Andersen37032b42004-02-10 01:28:36 +00001435#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen1c314ad2000-06-28 16:56:25 +00001436 /* return controlling TTY back to parent process group before exiting */
Eric Andersen7373e482002-07-31 04:04:47 +00001437 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001438 bb_perror_msg("tcsetpgrp");
Eric Andersen37032b42004-02-10 01:28:36 +00001439#endif
Eric Andersenb54833c2000-07-03 23:56:26 +00001440
1441 /* return exit status if called with "-c" */
1442 if (input == NULL && WIFEXITED(status))
1443 return WEXITSTATUS(status);
Eric Andersenff9ad472004-02-10 01:07:45 +00001444
Erik Andersen161220c2000-03-16 08:12:48 +00001445 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001446}
1447
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001448#ifdef CONFIG_FEATURE_CLEAN_UP
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +00001449static void free_memory(void)
Eric Andersenfad9c112000-07-25 18:06:52 +00001450{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001451 if (cwd && cwd!=bb_msg_unknown) {
Eric Andersen5d60a462001-08-22 05:32:24 +00001452 free((char*)cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001453 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001454 if (local_pending_command)
1455 free(local_pending_command);
1456
Eric Andersen86349772000-12-18 20:25:50 +00001457 if (job_list.fg && !job_list.fg->running_progs) {
1458 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001459 }
1460}
Rob Landleyacf448d2006-07-09 17:03:07 +00001461#else
1462void free_memory(void);
Eric Andersenfad9c112000-07-25 18:06:52 +00001463#endif
1464
Eric Andersen37032b42004-02-10 01:28:36 +00001465#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen2d848a42001-06-25 17:11:54 +00001466/* Make sure we have a controlling tty. If we get started under a job
1467 * aware app (like bash for example), make sure we are now in charge so
1468 * we don't fight over who gets the foreground */
Eric Andersenece8fc22002-12-11 04:26:28 +00001469static void setup_job_control(void)
Eric Andersen2d848a42001-06-25 17:11:54 +00001470{
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001471 int status;
Eric Andersen37032b42004-02-10 01:28:36 +00001472 pid_t shell_pgrp;
Eric Andersenff9ad472004-02-10 01:07:45 +00001473
Eric Andersen2d848a42001-06-25 17:11:54 +00001474 /* Loop until we are in the foreground. */
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001475 while ((status = tcgetpgrp (shell_terminal)) >= 0) {
1476 if (status == (shell_pgrp = getpgrp ())) {
1477 break;
1478 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001479 kill (- shell_pgrp, SIGTTIN);
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001480 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001481
1482 /* Ignore interactive and job-control signals. */
1483 signal(SIGINT, SIG_IGN);
1484 signal(SIGQUIT, SIG_IGN);
1485 signal(SIGTSTP, SIG_IGN);
1486 signal(SIGTTIN, SIG_IGN);
1487 signal(SIGTTOU, SIG_IGN);
1488 signal(SIGCHLD, SIG_IGN);
1489
1490 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001491 setsid();
Eric Andersen2d848a42001-06-25 17:11:54 +00001492 shell_pgrp = getpid ();
Rob Landleyacf448d2006-07-09 17:03:07 +00001493 setpgid(shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001494
1495 /* Grab control of the terminal. */
1496 tcsetpgrp(shell_terminal, shell_pgrp);
1497}
Eric Andersen37032b42004-02-10 01:28:36 +00001498#else
1499static inline void setup_job_control(void)
1500{
1501}
1502#endif
Eric Andersen6efc48c2000-07-18 08:16:39 +00001503
Matt Kraai2d91deb2001-08-01 17:21:35 +00001504int lash_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001505{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001506 unsigned opt;
Erik Andersen161220c2000-03-16 08:12:48 +00001507 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001508 argc = argc_l;
1509 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001510
Eric Andersen702ec592001-03-06 22:17:29 +00001511 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001512 last_jobid = 0;
Rob Landleyf9463792006-06-13 18:50:20 +00001513 close_me_list = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001514 job_list.head = NULL;
1515 job_list.fg = NULL;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001516 last_return_code=1;
Eric Andersen501c88b2000-07-28 15:14:45 +00001517
Eric Andersenb558e762000-11-30 22:43:16 +00001518 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001519 FILE *prof_input;
1520 prof_input = fopen("/etc/profile", "r");
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001521 if (prof_input) {
Rob Landley7a260f02006-06-19 03:20:03 +00001522 llist_add_to(&close_me_list, (void *)(long)fileno(prof_input));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001523 /* Now run the file */
1524 busy_loop(prof_input);
Denis Vlasenkoddec5af2006-10-26 23:25:17 +00001525 fclose_if_not_stdin(prof_input);
Rob Landleyf9463792006-06-13 18:50:20 +00001526 llist_pop(&close_me_list);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001527 }
Eric Andersenb558e762000-11-30 22:43:16 +00001528 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001529
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001530 opt = getopt32(argc_l, argv_l, "+ic:", &local_pending_command);
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001531#define LASH_OPT_i (1<<0)
1532#define LASH_OPT_c (1<<2)
1533 if (opt & LASH_OPT_c) {
1534 input = NULL;
1535 optind++;
1536 argv += optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001537 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001538 /* A shell is interactive if the `-i' flag was given, or if all of
1539 * the following conditions are met:
1540 * no -c command
1541 * no arguments remaining or the -s flag given
1542 * standard input is a terminal
1543 * standard output is a terminal
1544 * Refer to Posix.2, the description of the `sh' utility. */
Eric Andersen86349772000-12-18 20:25:50 +00001545 if (argv[optind]==NULL && input==stdin &&
Rob Landleyacf448d2006-07-09 17:03:07 +00001546 isatty(STDIN_FILENO) && isatty(STDOUT_FILENO))
1547 {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001548 opt |= LASH_OPT_i;
Eric Andersen86349772000-12-18 20:25:50 +00001549 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001550 setup_job_control();
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001551 if (opt & LASH_OPT_i) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001552 /* Looks like they want an interactive shell */
Rob Landleyacf448d2006-07-09 17:03:07 +00001553 if (!ENABLE_FEATURE_SH_EXTRA_QUIET) {
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +00001554 printf("\n\n%s Built-in shell (lash)\n"
1555 "Enter 'help' for a list of built-in commands.\n\n",
1556 BB_BANNER);
Rob Landleyacf448d2006-07-09 17:03:07 +00001557 }
1558 } else if (!local_pending_command && argv[optind]) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001559 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Rob Landleyd921b2e2006-08-03 15:41:12 +00001560 input = xfopen(argv[optind], "r");
Rob Landleyf9463792006-06-13 18:50:20 +00001561 /* be lazy, never mark this closed */
Rob Landley7a260f02006-06-19 03:20:03 +00001562 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Eric Andersen501c88b2000-07-28 15:14:45 +00001563 }
1564
Eric Andersen6efc48c2000-07-18 08:16:39 +00001565 /* initialize the cwd -- this is never freed...*/
Eric Andersene5dfced2001-04-09 22:48:12 +00001566 cwd = xgetcwd(0);
Eric Andersen5f265b72001-05-11 16:58:46 +00001567 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001568 cwd = bb_msg_unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001569
Rob Landleyacf448d2006-07-09 17:03:07 +00001570 if (ENABLE_FEATURE_CLEAN_UP) atexit(free_memory);
Eric Andersenfad9c112000-07-25 18:06:52 +00001571
Rob Landleyacf448d2006-07-09 17:03:07 +00001572 if (ENABLE_FEATURE_COMMAND_EDITING) cmdedit_set_initial_prompt();
1573 else PS1 = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +00001574
Erik Andersen161220c2000-03-16 08:12:48 +00001575 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001576}