blob: 56a3a23dd9bb8089e19e10f4f00a7dac2b0836c2 [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
Erik Andersen3522eb12000-03-12 23:49:18 +000024#include <stdio.h>
25#include <stdlib.h>
26#include <ctype.h>
27#include <errno.h>
28#include <fcntl.h>
Erik Andersen3522eb12000-03-12 23:49:18 +000029#include <signal.h>
30#include <string.h>
31#include <sys/ioctl.h>
32#include <sys/wait.h>
33#include <unistd.h>
Eric Andersen501c88b2000-07-28 15:14:45 +000034#include <getopt.h>
Eric Andersen2d848a42001-06-25 17:11:54 +000035#include <termios.h>
Mark Whitley4b541a82001-04-25 17:10:30 +000036#include "busybox.h"
37#include "cmdedit.h"
Eric Andersene5dfced2001-04-09 22:48:12 +000038
Eric Andersenbdfd0d72001-10-24 05:00:29 +000039#ifdef CONFIG_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +000040#include <locale.h>
Eric Andersene5dfced2001-04-09 22:48:12 +000041#endif
Eric Andersen13d1fa12001-03-08 23:59:45 +000042
Eric Andersen13d1fa12001-03-08 23:59:45 +000043#include <glob.h>
44#define expand_t glob_t
Erik Andersen3522eb12000-03-12 23:49:18 +000045
Eric Andersenff9ad472004-02-10 01:07:45 +000046/* Always enable for the moment... */
Eric Andersen95b26252004-02-10 01:30:21 +000047#define CONFIG_LASH_PIPE_N_REDIRECTS
48#define CONFIG_LASH_JOB_CONTROL
Eric Andersen13d1fa12001-03-08 23:59:45 +000049
Mark Whitley59ab0252001-01-23 22:30:04 +000050static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000051#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
52
Erik Andersend75af992000-03-16 08:09:09 +000053
Eric Andersenff9ad472004-02-10 01:07:45 +000054#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000055enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000056 REDIRECT_APPEND
57};
Eric Andersenff9ad472004-02-10 01:07:45 +000058#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000059
Eric Andersen86349772000-12-18 20:25:50 +000060static const unsigned int DEFAULT_CONTEXT=0x1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +000061static const unsigned int IF_TRUE_CONTEXT=0x2;
62static const unsigned int IF_FALSE_CONTEXT=0x4;
63static const unsigned int THEN_EXP_CONTEXT=0x8;
64static const unsigned int ELSE_EXP_CONTEXT=0x10;
Eric Andersenfad9c112000-07-25 18:06:52 +000065
Eric Andersenff9ad472004-02-10 01:07:45 +000066#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000067struct redir_struct {
68 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000069 int fd; /* file descriptor being redirected */
70 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000071};
Eric Andersenff9ad472004-02-10 01:07:45 +000072#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000073
Eric Andersen86349772000-12-18 20:25:50 +000074struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000075 pid_t pid; /* 0 if exited */
76 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000077 int num_redirects; /* elements in redirection array */
Eric Andersen86349772000-12-18 20:25:50 +000078 int is_stopped; /* is the program currently running? */
79 struct job *family; /* pointer back to the child's parent job */
Eric Andersenff9ad472004-02-10 01:07:45 +000080#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
81 struct redir_struct *redirects; /* I/O redirects */
82#endif
83};
84
85struct jobset {
86 struct job *head; /* head of list of running jobs */
87 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000088};
89
90struct job {
Eric Andersen86349772000-12-18 20:25:50 +000091 int jobid; /* job number */
92 int num_progs; /* total number of programs in job */
93 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000094 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +000095 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +000096 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +000097 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +000098 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +000099 int stopped_progs; /* number of programs alive, but stopped */
100 unsigned int job_context; /* bitmask defining current context */
101 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +0000102};
103
Eric Andersen86349772000-12-18 20:25:50 +0000104struct built_in_command {
Erik Andersen161220c2000-03-16 08:12:48 +0000105 char *cmd; /* name */
106 char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +0000107 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +0000108};
109
Eric Andersen8ea28be2001-01-05 20:58:22 +0000110struct close_me {
111 int fd;
112 struct close_me *next;
113};
114
Eric Andersen34e19412000-07-10 18:47:24 +0000115/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000116static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000117static int builtin_exec(struct child_prog *cmd);
118static int builtin_exit(struct child_prog *cmd);
119static int builtin_fg_bg(struct child_prog *cmd);
120static int builtin_help(struct child_prog *cmd);
121static int builtin_jobs(struct child_prog *dummy);
122static int builtin_pwd(struct child_prog *dummy);
123static int builtin_export(struct child_prog *cmd);
124static int builtin_source(struct child_prog *cmd);
125static int builtin_unset(struct child_prog *cmd);
126static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000127
Eric Andersen34e19412000-07-10 18:47:24 +0000128
129/* function prototypes for shell stuff */
Eric Andersen8ea28be2001-01-05 20:58:22 +0000130static void mark_open(int fd);
131static void mark_closed(int fd);
Eric Andersen36278b92001-03-06 20:47:31 +0000132static void close_all(void);
Eric Andersen86349772000-12-18 20:25:50 +0000133static void checkjobs(struct jobset *job_list);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000134static void remove_job(struct jobset *j_list, struct job *job);
Eric Andersen86349772000-12-18 20:25:50 +0000135static int get_command(FILE * source, char *command);
136static int parse_command(char **command_ptr, struct job *job, int *inbg);
137static int run_command(struct job *newjob, int inbg, int outpipe[2]);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000138static int pseudo_exec(struct child_prog *cmd) ATTRIBUTE_NORETURN;
Erik Andersen3522eb12000-03-12 23:49:18 +0000139static int busy_loop(FILE * input);
140
Erik Andersend75af992000-03-16 08:09:09 +0000141
Mark Whitley37653aa2000-07-12 23:36:17 +0000142/* Table of built-in functions (these are non-forking builtins, meaning they
143 * can change global variables in the parent shell process but they will not
144 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Eric Andersen86349772000-12-18 20:25:50 +0000145static struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000146 {"bg", "Resume a job in the background", builtin_fg_bg},
147 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000148 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000149 {"exit", "Exit from shell()", builtin_exit},
150 {"fg", "Bring job into the foreground", builtin_fg_bg},
151 {"jobs", "Lists the active jobs", builtin_jobs},
152 {"export", "Set environment variable", builtin_export},
153 {"unset", "Unset environment variable", builtin_unset},
154 {"read", "Input environment variable", builtin_read},
Matt Kraaidd450a02000-09-13 03:43:36 +0000155 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000156 /* to do: add ulimit */
Eric Andersenfad9c112000-07-25 18:06:52 +0000157 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000158};
159
Mark Whitley37653aa2000-07-12 23:36:17 +0000160/* Table of forking built-in functions (things that fork cannot change global
161 * variables in the parent process, such as the current working directory) */
Eric Andersen86349772000-12-18 20:25:50 +0000162static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000163 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000164 {"help", "List shell built-in commands", builtin_help},
165 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000166};
167
Eric Andersen0bcc8132001-01-05 19:37:32 +0000168
Eric Andersen7467c8d2001-07-12 20:26:32 +0000169static int shell_context; /* Type prompt trigger (PS1 or PS2) */
Eric Andersen0bcc8132001-01-05 19:37:32 +0000170
171
172/* Globals that are static to this file */
Eric Andersencfa88ec2001-05-11 18:08:16 +0000173static const char *cwd;
Eric Andersen744b0642001-01-05 21:23:44 +0000174static char *local_pending_command = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000175static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000176static int argc;
177static char **argv;
Eric Andersen702ec592001-03-06 22:17:29 +0000178static struct close_me *close_me_head;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000179static int last_return_code;
180static int last_bg_pid;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000181static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000182static int shell_terminal;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000183static char *PS1;
Eric Andersene5dfced2001-04-09 22:48:12 +0000184static char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000185
186
Eric Andersenb558e762000-11-30 22:43:16 +0000187#ifdef DEBUG_SHELL
188static inline void debug_printf(const char *format, ...)
189{
190 va_list args;
191 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000192 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000193 va_end(args);
194}
195#else
196static inline void debug_printf(const char *format, ...) { }
197#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000198
Eric Andersen86349772000-12-18 20:25:50 +0000199/*
200 Most builtins need access to the struct child_prog that has
201 their arguments, previously coded as cmd->progs[0]. That coding
202 can exhibit a bug, if the builtin is not the first command in
203 a pipeline: "echo foo | exec sort" will attempt to exec foo.
204
205builtin previous use notes
206------ ----------------- ---------
207cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000208exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
209exit cmd->progs[0]
210fg_bg cmd->progs[0], job_list->head, job_list->fg
211help 0
212jobs job_list->head
213pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000214export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000215source cmd->progs[0]
216unset cmd->progs[0]
217read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000218
219I added "struct job *family;" to struct child_prog,
220and switched API to builtin_foo(struct child_prog *child);
221So cmd->text becomes child->family->text
222 cmd->job_context becomes child->family->job_context
223 cmd->progs[0] becomes *child
224 job_list becomes child->family->job_list
225 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000226
Erik Andersend75af992000-03-16 08:09:09 +0000227/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000228static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000229{
Erik Andersen161220c2000-03-16 08:12:48 +0000230 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000231
Eric Andersen86349772000-12-18 20:25:50 +0000232 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000233 newdir = getenv("HOME");
234 else
Eric Andersen86349772000-12-18 20:25:50 +0000235 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000236 if (chdir(newdir)) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000237 printf("cd: %s: %m\n", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000238 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000239 }
Eric Andersencfa88ec2001-05-11 18:08:16 +0000240 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000241 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000242 cwd = bb_msg_unknown;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000243 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000244}
245
Eric Andersend2f56772000-09-21 02:48:07 +0000246/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000247static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000248{
Eric Andersen86349772000-12-18 20:25:50 +0000249 if (child->argv[1] == NULL)
250 return EXIT_SUCCESS; /* Really? */
251 child->argv++;
Eric Andersen07f2f392001-03-06 20:28:22 +0000252 close_all();
Eric Andersen86349772000-12-18 20:25:50 +0000253 pseudo_exec(child);
254 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000255}
256
Erik Andersen3522eb12000-03-12 23:49:18 +0000257/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000258static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000259{
Eric Andersen86349772000-12-18 20:25:50 +0000260 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000261 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000262
Eric Andersen86349772000-12-18 20:25:50 +0000263 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000264}
265
266/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000267static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000268{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000269 int i, jobnum;
Erik Andersen6273f652000-03-17 01:12:41 +0000270 struct job *job=NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000271
Eric Andersen8a646dd2001-06-21 16:38:11 +0000272 /* If they gave us no args, assume they want the last backgrounded task */
273 if (!child->argv[1]) {
274 for (job = child->family->job_list->head; job; job = job->next) {
275 if (job->jobid == last_jobid) {
276 break;
277 }
Erik Andersen161220c2000-03-16 08:12:48 +0000278 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000279 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000280 bb_error_msg("%s: no current job", child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000281 return EXIT_FAILURE;
282 }
283 } else {
284 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000285 bb_error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000286 return EXIT_FAILURE;
287 }
288 for (job = child->family->job_list->head; job; job = job->next) {
289 if (job->jobid == jobnum) {
290 break;
291 }
292 }
293 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000294 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000295 return EXIT_FAILURE;
296 }
Erik Andersend75af992000-03-16 08:09:09 +0000297 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000298
Eric Andersen86349772000-12-18 20:25:50 +0000299 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000300 /* Put the job into the foreground. */
301 tcsetpgrp(shell_terminal, job->pgrp);
302
Eric Andersen86349772000-12-18 20:25:50 +0000303 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000304 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000305
Erik Andersen161220c2000-03-16 08:12:48 +0000306 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000307 for (i = 0; i < job->num_progs; i++)
308 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000309
Eric Andersen86349772000-12-18 20:25:50 +0000310 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000311
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000312 if ( (i=kill(- job->pgrp, SIGCONT)) < 0) {
313 if (i == ESRCH) {
314 remove_job(&job_list, job);
Eric Andersen07abfe22001-06-27 17:29:11 +0000315 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000316 bb_perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000317 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000318 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000319
Matt Kraai3e856ce2000-12-01 02:55:13 +0000320 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000321}
322
323/* built-in 'help' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000324static int builtin_help(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000325{
Eric Andersen86349772000-12-18 20:25:50 +0000326 struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000327
Eric Andersen86349772000-12-18 20:25:50 +0000328 printf("\nBuilt-in commands:\n");
329 printf("-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000330 for (x = bltins; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000331 if (x->descr==NULL)
332 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000333 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000334 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000335 for (x = bltins_forking; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000336 if (x->descr==NULL)
337 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000338 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000339 }
Eric Andersen86349772000-12-18 20:25:50 +0000340 printf("\n\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000341 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000342}
343
344/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000345static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000346{
Erik Andersen161220c2000-03-16 08:12:48 +0000347 struct job *job;
Eric Andersen86349772000-12-18 20:25:50 +0000348 char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000349
Eric Andersen86349772000-12-18 20:25:50 +0000350 for (job = child->family->job_list->head; job; job = job->next) {
351 if (job->running_progs == job->stopped_progs)
352 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000353 else
Eric Andersen86349772000-12-18 20:25:50 +0000354 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000355
Eric Andersen86349772000-12-18 20:25:50 +0000356 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000357 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000358 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000359}
360
361
362/* built-in 'pwd' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000363static int builtin_pwd(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000364{
Eric Andersencfa88ec2001-05-11 18:08:16 +0000365 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000366 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000367 cwd = bb_msg_unknown;
Matt Kraai59df6f72001-05-16 14:21:09 +0000368 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000369 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000370}
371
Erik Andersen6273f652000-03-17 01:12:41 +0000372/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000373static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000374{
Erik Andersen161220c2000-03-16 08:12:48 +0000375 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000376 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000377
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000378 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000379 char **e;
380 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000381 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000382 }
Matt Kraai2129f972001-04-04 17:50:04 +0000383 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000384 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000385 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000386 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000387 fprintf(stderr, "export: %m\n");
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000388#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000389 if (strncmp(v, "PS1=", 4)==0)
390 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000391#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000392
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000393#ifdef CONFIG_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +0000394 if(strncmp(v, "LC_ALL=", 7)==0)
395 setlocale(LC_ALL, getenv("LC_ALL"));
Mark Whitleya82a0032001-03-27 17:07:15 +0000396 if(strncmp(v, "LC_CTYPE=", 9)==0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000397 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000398#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000399
Erik Andersen161220c2000-03-16 08:12:48 +0000400 return (res);
Erik Andersen3522eb12000-03-12 23:49:18 +0000401}
402
Eric Andersenb54833c2000-07-03 23:56:26 +0000403/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000404static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000405{
406 int res = 0, len, newlen;
407 char *s;
408 char string[MAX_READ];
409
Eric Andersen86349772000-12-18 20:25:50 +0000410 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000411 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen0d2d1eb2002-06-06 13:33:01 +0000412 safe_strncpy(string, child->argv[1], MAX_READ-1);
Eric Andersenb54833c2000-07-03 23:56:26 +0000413 len = strlen(string);
414 string[len++] = '=';
415 string[len] = '\0';
416 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
417 newlen = strlen(string);
418 if(newlen > len)
419 string[--newlen] = '\0'; /* chomp trailing newline */
420 /*
421 ** string should now contain "VAR=<value>"
422 ** copy it (putenv() won't do that, so we must make sure
423 ** the string resides in a static buffer!)
424 */
425 res = -1;
426 if((s = strdup(string)))
427 res = putenv(s);
428 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000429 fprintf(stderr, "read: %m\n");
Eric Andersenb54833c2000-07-03 23:56:26 +0000430 }
431 else
432 fgets(string, sizeof(string), stdin);
433
434 return (res);
435}
436
Erik Andersen3522eb12000-03-12 23:49:18 +0000437/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000438static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000439{
Erik Andersen161220c2000-03-16 08:12:48 +0000440 FILE *input;
441 int status;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000442 int fd;
Erik Andersen3522eb12000-03-12 23:49:18 +0000443
Eric Andersen86349772000-12-18 20:25:50 +0000444 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000445 return EXIT_FAILURE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000446
Eric Andersen86349772000-12-18 20:25:50 +0000447 input = fopen(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000448 if (!input) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000449 printf( "Couldn't open file '%s'\n", child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000450 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000451 }
Erik Andersend75af992000-03-16 08:09:09 +0000452
Eric Andersen8ea28be2001-01-05 20:58:22 +0000453 fd=fileno(input);
454 mark_open(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000455 /* Now run the file */
456 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000457 fclose(input);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000458 mark_closed(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000459 return (status);
Erik Andersen3522eb12000-03-12 23:49:18 +0000460}
461
462/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000463static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000464{
Eric Andersen86349772000-12-18 20:25:50 +0000465 if (child->argv[1] == NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000466 printf( "unset: parameter required.\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000467 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000468 }
Eric Andersen86349772000-12-18 20:25:50 +0000469 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000470 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000471}
472
Eric Andersen8ea28be2001-01-05 20:58:22 +0000473static void mark_open(int fd)
474{
475 struct close_me *new = xmalloc(sizeof(struct close_me));
476 new->fd = fd;
477 new->next = close_me_head;
478 close_me_head = new;
479}
480
481static void mark_closed(int fd)
482{
483 struct close_me *tmp;
484 if (close_me_head == NULL || close_me_head->fd != fd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000485 bb_error_msg_and_die("corrupt close_me");
Eric Andersen8ea28be2001-01-05 20:58:22 +0000486 tmp = close_me_head;
487 close_me_head = close_me_head->next;
488 free(tmp);
489}
490
491static void close_all()
492{
Eric Andersen702ec592001-03-06 22:17:29 +0000493 struct close_me *c, *tmp;
494 for (c=close_me_head; c; c=tmp) {
495 close(c->fd);
496 tmp=c->next;
497 free(c);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000498 }
499 close_me_head = NULL;
500}
501
502
Eric Andersen37032b42004-02-10 01:28:36 +0000503#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen3522eb12000-03-12 23:49:18 +0000504/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000505static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000506{
Erik Andersen161220c2000-03-16 08:12:48 +0000507 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000508 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000509
Eric Andersen86349772000-12-18 20:25:50 +0000510 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000511 free(cmd->progs[i].argv);
Eric Andersenff9ad472004-02-10 01:07:45 +0000512#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000513 if (cmd->progs[i].redirects)
514 free(cmd->progs[i].redirects);
Eric Andersenff9ad472004-02-10 01:07:45 +0000515#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000516 }
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000517 free(cmd->progs);
518 free(cmd->text);
519 free(cmd->cmdbuf);
Mark Whitley44a99142001-03-14 17:26:37 +0000520 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000521 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000522 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000523}
524
Eric Andersen1ca20a72001-03-21 07:34:27 +0000525/* remove a job from a jobset */
526static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000527{
Eric Andersen86349772000-12-18 20:25:50 +0000528 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000529
Eric Andersen86349772000-12-18 20:25:50 +0000530 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000531 if (job == j_list->head) {
532 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000533 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000534 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000535 while (prevjob->next != job)
536 prevjob = prevjob->next;
537 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000538 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000539
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000540 if (j_list->head)
541 last_jobid = j_list->head->jobid;
542 else
543 last_jobid = 0;
544
Erik Andersen161220c2000-03-16 08:12:48 +0000545 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000546}
547
Eric Andersenff9ad472004-02-10 01:07:45 +0000548/* Checks to see if any background processes have exited -- if they
Erik Andersen3522eb12000-03-12 23:49:18 +0000549 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000550static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000551{
Erik Andersen161220c2000-03-16 08:12:48 +0000552 struct job *job;
553 pid_t childpid;
554 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000555 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000556
Erik Andersen161220c2000-03-16 08:12:48 +0000557 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000558 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000559 prognum = 0;
560 while (prognum < job->num_progs &&
561 job->progs[prognum].pid != childpid) prognum++;
562 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000563 break;
564 }
565
Eric Andersena1d187a2000-07-17 19:14:41 +0000566 /* This happens on backticked commands */
567 if(job==NULL)
568 return;
569
Erik Andersen161220c2000-03-16 08:12:48 +0000570 if (WIFEXITED(status) || WIFSIGNALED(status)) {
571 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000572 job->running_progs--;
573 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000574
Eric Andersen86349772000-12-18 20:25:50 +0000575 if (!job->running_progs) {
576 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000577 last_jobid=0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000578 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000579 }
580 } else {
581 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000582 job->stopped_progs++;
583 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000584
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000585#if 0
586 /* Printing this stuff is a pain, since it tends to
587 * overwrite the prompt an inconveinient moments. So
588 * don't do that. */
Eric Andersen86349772000-12-18 20:25:50 +0000589 if (job->stopped_progs == job->num_progs) {
590 printf(JOB_STATUS_FORMAT, job->jobid, "Stopped",
Erik Andersen161220c2000-03-16 08:12:48 +0000591 job->text);
592 }
Eric Andersenff9ad472004-02-10 01:07:45 +0000593#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000594 }
Erik Andersend75af992000-03-16 08:09:09 +0000595 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000596
Erik Andersen161220c2000-03-16 08:12:48 +0000597 if (childpid == -1 && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000598 bb_perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000599}
Eric Andersen37032b42004-02-10 01:28:36 +0000600#else
601static void checkjobs(struct jobset *j_list)
602{
603}
604static void free_job(struct job *cmd)
605{
606}
607static void remove_job(struct jobset *j_list, struct job *job)
608{
609}
610#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000611
Eric Andersenff9ad472004-02-10 01:07:45 +0000612#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersene9f07fb2000-12-21 18:31:36 +0000613/* squirrel != NULL means we squirrel away copies of stdin, stdout,
614 * and stderr if they are redirected. */
615static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000616{
617 int i;
618 int openfd;
619 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000620 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000621
Eric Andersen86349772000-12-18 20:25:50 +0000622 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000623 switch (redir->type) {
624 case REDIRECT_INPUT:
625 mode = O_RDONLY;
626 break;
627 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000628 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000629 break;
630 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000631 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000632 break;
633 }
634
635 openfd = open(redir->filename, mode, 0666);
636 if (openfd < 0) {
637 /* this could get lost if stderr has been redirected, but
638 bash and ash both lose it as well (though zsh doesn't!) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000639 bb_perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000640 return 1;
641 }
642
643 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000644 if (squirrel && redir->fd < 3) {
645 squirrel[redir->fd] = dup(redir->fd);
Eric Andersenacd647c2004-08-16 08:38:34 +0000646 fcntl (squirrel[redir->fd], F_SETFD, FD_CLOEXEC);
Eric Andersene9f07fb2000-12-21 18:31:36 +0000647 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000648 dup2(openfd, redir->fd);
649 close(openfd);
650 }
651 }
652
653 return 0;
654}
655
Eric Andersene9f07fb2000-12-21 18:31:36 +0000656static void restore_redirects(int squirrel[])
657{
658 int i, fd;
659 for (i=0; i<3; i++) {
660 fd = squirrel[i];
661 if (fd != -1) {
662 /* No error checking. I sure wouldn't know what
663 * to do with an error if I found one! */
664 dup2(fd, i);
665 close(fd);
666 }
667 }
668}
Eric Andersenff9ad472004-02-10 01:07:45 +0000669#else
670static inline int setup_redirects(struct child_prog *prog, int squirrel[])
671{
672 return 0;
673}
674static inline void restore_redirects(int squirrel[])
675{
676}
677#endif
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000678
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000679static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000680{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000681#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000682 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000683#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000684 PS1 = getenv("PS1");
Eric Andersene5dfced2001-04-09 22:48:12 +0000685 if(PS1==0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000686 PS1 = "\\w \\$ ";
Eric Andersenff9ad472004-02-10 01:07:45 +0000687#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000688}
689
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000690static inline void setup_prompt_string(char **prompt_str)
691{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000692#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000693 /* Set up the prompt */
694 if (shell_context == 0) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000695 free(PS1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000696 PS1=xmalloc(strlen(cwd)+4);
697 sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
698 *prompt_str = PS1;
699 } else {
700 *prompt_str = PS2;
701 }
702#else
703 *prompt_str = (shell_context==0)? PS1 : PS2;
Eric Andersenff9ad472004-02-10 01:07:45 +0000704#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000705}
Eric Andersen22332fd2001-01-30 23:40:39 +0000706
Eric Andersen09acc062001-01-04 11:10:38 +0000707static int get_command(FILE * source, char *command)
708{
709 char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000710
Eric Andersen1c314ad2000-06-28 16:56:25 +0000711 if (source == NULL) {
712 if (local_pending_command) {
713 /* a command specified (-c option): return it & mark it done */
714 strcpy(command, local_pending_command);
715 free(local_pending_command);
716 local_pending_command = NULL;
717 return 0;
718 }
719 return 1;
720 }
721
Erik Andersen161220c2000-03-16 08:12:48 +0000722 if (source == stdin) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000723 setup_prompt_string(&prompt_str);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000724
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000725#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000726 /*
727 ** enable command line editing only while a command line
728 ** is actually being read; otherwise, we'll end up bequeathing
729 ** atexit() handlers and other unwanted stuff to our
730 ** child processes (rob@sysgo.de)
731 */
Eric Andersen86349772000-12-18 20:25:50 +0000732 cmdedit_read_input(prompt_str, command);
Erik Andersen161220c2000-03-16 08:12:48 +0000733 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000734#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000735 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000736#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000737 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000738
Erik Andersen161220c2000-03-16 08:12:48 +0000739 if (!fgets(command, BUFSIZ - 2, source)) {
740 if (source == stdin)
741 printf("\n");
742 return 1;
743 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000744
Erik Andersen161220c2000-03-16 08:12:48 +0000745 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000746}
747
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000748static char* itoa(register int i)
749{
750 static char a[7]; /* Max 7 ints */
751 register char *b = a + sizeof(a) - 1;
752 int sign = (i < 0);
753
754 if (sign)
755 i = -i;
756 *b = 0;
757 do
758 {
759 *--b = '0' + (i % 10);
760 i /= 10;
761 }
762 while (i);
763 if (sign)
764 *--b = '-';
765 return b;
766}
767
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +0000768static char * strsep_space( char *string, int * ix)
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000769{
770 char *token, *begin;
771
772 begin = string;
773
774 /* Short circuit the trivial case */
775 if ( !string || ! string[*ix])
776 return NULL;
777
778 /* Find the end of the token. */
779 while( string && string[*ix] && !isspace(string[*ix]) ) {
780 (*ix)++;
781 }
782
Eric Andersenff9ad472004-02-10 01:07:45 +0000783 /* Find the end of any whitespace trailing behind
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000784 * the token and let that be part of the token */
785 while( string && string[*ix] && isspace(string[*ix]) ) {
786 (*ix)++;
787 }
788
789 if (! string && *ix==0) {
790 /* Nothing useful was found */
791 return NULL;
792 }
793
794 token = xmalloc(*ix+1);
795 token[*ix] = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000796 strncpy(token, string, *ix);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000797
798 return token;
799}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000800
Eric Andersenca604592001-03-08 17:17:13 +0000801static int expand_arguments(char *command)
802{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000803 int total_length=0, length, i, retval, ix = 0;
804 expand_t expand_result;
805 char *tmpcmd, *cmd, *cmd_copy;
806 char *src, *dst, *var;
807 const char *out_of_space = "out of space during expansion";
808 int flags = GLOB_NOCHECK
809#ifdef GLOB_BRACE
810 | GLOB_BRACE
Eric Andersenff9ad472004-02-10 01:07:45 +0000811#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000812#ifdef GLOB_TILDE
813 | GLOB_TILDE
Eric Andersenff9ad472004-02-10 01:07:45 +0000814#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000815 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000816
Eric Andersenca604592001-03-08 17:17:13 +0000817 /* get rid of the terminating \n */
818 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000819
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000820 /* Fix up escape sequences to be the Real Thing(tm) */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000821 while( command && command[ix]) {
822 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000823 const char *tmp = command+ix+1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000824 command[ix] = bb_process_escape_sequence( &tmp );
Eric Andersen1ca20a72001-03-21 07:34:27 +0000825 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000826 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000827 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000828 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000829 /* Use glob and then fixup environment variables and such */
830
831 /* It turns out that glob is very stupid. We have to feed it one word at a
832 * time since it can't cope with a full string. Here we convert command
833 * (char*) into cmd (char**, one word per string) */
834
835 /* We need a clean copy, so strsep can mess up the copy while
836 * we write stuff into the original (in a minute) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000837 cmd = cmd_copy = bb_xstrdup(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000838 *command = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000839 for (ix = 0, tmpcmd = cmd;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000840 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
841 if (*tmpcmd == '\0')
842 break;
843 /* we need to trim() the result for glob! */
844 trim(tmpcmd);
845 retval = glob(tmpcmd, flags, NULL, &expand_result);
846 free(tmpcmd); /* Free mem allocated by strsep_space */
847 if (retval == GLOB_NOSPACE) {
848 /* Mem may have been allocated... */
849 globfree (&expand_result);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000850 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000851 return FALSE;
852 } else if (retval != 0) {
853 /* Some other error. GLOB_NOMATCH shouldn't
Eric Andersenff9ad472004-02-10 01:07:45 +0000854 * happen because of the GLOB_NOCHECK flag in
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000855 * the glob call. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000856 bb_error_msg("syntax error");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000857 return FALSE;
858 } else {
859 /* Convert from char** (one word per string) to a simple char*,
860 * but don't overflow command which is BUFSIZ in length */
861 for (i=0; i < expand_result.gl_pathc; i++) {
862 length=strlen(expand_result.gl_pathv[i]);
863 if (total_length+length+1 >= 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 strcat(command+total_length, " ");
868 total_length+=1;
869 strcat(command+total_length, expand_result.gl_pathv[i]);
870 total_length+=length;
871 }
872 globfree (&expand_result);
873 }
874 }
875 free(cmd_copy);
876 trim(command);
877
Eric Andersenff9ad472004-02-10 01:07:45 +0000878 /* Now do the shell variable substitutions which
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000879 * wordexp can't do for us, namely $? and $! */
880 src = command;
881 while((dst = strchr(src,'$')) != NULL){
882 var = NULL;
883 switch(*(dst+1)) {
884 case '?':
885 var = itoa(last_return_code);
886 break;
887 case '!':
888 if (last_bg_pid==-1)
889 *(var)='\0';
890 else
891 var = itoa(last_bg_pid);
892 break;
893 /* Everything else like $$, $#, $[0-9], etc. should all be
894 * expanded by wordexp(), so we can in theory skip that stuff
895 * here, but just to be on the safe side (i.e., since uClibc
896 * wordexp doesn't do this stuff yet), lets leave it in for
897 * now. */
898 case '$':
899 var = itoa(getpid());
900 break;
901 case '#':
902 var = itoa(argc-1);
903 break;
904 case '0':case '1':case '2':case '3':case '4':
905 case '5':case '6':case '7':case '8':case '9':
906 {
Eric Andersen7ab93d92002-11-08 09:40:02 +0000907 int ixx=*(dst+1)-48+1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000908 if (ixx >= argc) {
909 var='\0';
910 } else {
911 var = argv[ixx];
912 }
913 }
914 break;
915
916 }
917 if (var) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000918 /* a single character construction was found, and
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000919 * already handled in the case statement */
920 src=dst+2;
921 } else {
922 /* Looks like an environment variable */
923 char delim_hold;
924 int num_skip_chars=0;
925 int dstlen = strlen(dst);
926 /* Is this a ${foo} type variable? */
927 if (dstlen >=2 && *(dst+1) == '{') {
928 src=strchr(dst+1, '}');
929 num_skip_chars=1;
930 } else {
931 src=dst+1;
932 while(isalnum(*src) || *src=='_') src++;
933 }
934 if (src == NULL) {
935 src = dst+dstlen;
936 }
937 delim_hold=*src;
938 *src='\0'; /* temporary */
939 var = getenv(dst + 1 + num_skip_chars);
940 *src=delim_hold;
941 src += num_skip_chars;
942 }
943 if (var == NULL) {
944 /* Seems we got an un-expandable variable. So delete it. */
945 var = "";
946 }
947 {
948 int subst_len = strlen(var);
949 int trail_len = strlen(src);
950 if (dst+subst_len+trail_len >= command+BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000951 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000952 return FALSE;
953 }
954 /* Move stuff to the end of the string to accommodate
955 * filling the created gap with the new stuff */
956 memmove(dst+subst_len, src, trail_len+1);
957 /* Now copy in the new stuff */
958 memcpy(dst, var, subst_len);
959 src = dst+subst_len;
960 }
961 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000962
Eric Andersenca604592001-03-08 17:17:13 +0000963 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000964}
965
Eric Andersen86349772000-12-18 20:25:50 +0000966/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
967 line). If a valid command is found, command_ptr is set to point to
Eric Andersenff9ad472004-02-10 01:07:45 +0000968 the beginning of the next command (if the original command had more
969 then one job associated with it) or NULL if no more commands are
Erik Andersen3522eb12000-03-12 23:49:18 +0000970 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000971static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000972{
Erik Andersen161220c2000-03-16 08:12:48 +0000973 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000974 char *return_command = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +0000975 char *src, *buf;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000976 int argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000977 int done = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000978 int argv_alloced;
Eric Andersenff9ad472004-02-10 01:07:45 +0000979 int saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000980 char quote = '\0';
981 int count;
Eric Andersen86349772000-12-18 20:25:50 +0000982 struct child_prog *prog;
Eric Andersenff9ad472004-02-10 01:07:45 +0000983#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
984 int i;
985 char *chptr;
986#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000987
Erik Andersen161220c2000-03-16 08:12:48 +0000988 /* skip leading white space */
Eric Andersen86349772000-12-18 20:25:50 +0000989 while (**command_ptr && isspace(**command_ptr))
990 (*command_ptr)++;
Erik Andersen3522eb12000-03-12 23:49:18 +0000991
Erik Andersen161220c2000-03-16 08:12:48 +0000992 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000993 if (!**command_ptr || (**command_ptr == '#')) {
994 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000995 return 0;
996 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000997
Eric Andersen86349772000-12-18 20:25:50 +0000998 *inbg = 0;
999 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +00001000 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +00001001
Eric Andersenff9ad472004-02-10 01:07:45 +00001002 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +00001003 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +00001004 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +00001005
Eric Andersenff9ad472004-02-10 01:07:45 +00001006 Getting clean memory relieves us of the task of NULL
1007 terminating things and makes the rest of this look a bit
Erik Andersen161220c2000-03-16 08:12:48 +00001008 cleaner (though it is, admittedly, a tad less efficient) */
Eric Andersen86349772000-12-18 20:25:50 +00001009 job->cmdbuf = command = xcalloc(2*strlen(*command_ptr) + 1, sizeof(char));
Erik Andersen161220c2000-03-16 08:12:48 +00001010 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001011
Erik Andersen161220c2000-03-16 08:12:48 +00001012 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +00001013 prog->num_redirects = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001014 prog->is_stopped = 0;
1015 prog->family = job;
Eric Andersenff9ad472004-02-10 01:07:45 +00001016#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
1017 prog->redirects = NULL;
1018#endif
Erik Andersen3522eb12000-03-12 23:49:18 +00001019
Eric Andersen86349772000-12-18 20:25:50 +00001020 argv_alloced = 5;
1021 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
1022 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +00001023
Erik Andersen161220c2000-03-16 08:12:48 +00001024 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +00001025 src = *command_ptr;
Erik Andersen161220c2000-03-16 08:12:48 +00001026 while (*src && !done) {
1027 if (quote == *src) {
1028 quote = '\0';
1029 } else if (quote) {
1030 if (*src == '\\') {
1031 src++;
1032 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001033 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001034 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001035 return 1;
1036 }
1037
1038 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +00001039 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +00001040 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +00001041 *buf++ = '\\';
1042 }
Erik Andersen161220c2000-03-16 08:12:48 +00001043 } else if (*src == '*' || *src == '?' || *src == '[' ||
1044 *src == ']') *buf++ = '\\';
1045 *buf++ = *src;
1046 } else if (isspace(*src)) {
Matt Kraaibe66ad32001-04-12 15:42:17 +00001047 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001048 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001049 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +00001050 if ((argc_l + 1) == argv_alloced) {
1051 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +00001052 prog->argv = xrealloc(prog->argv,
1053 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +00001054 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001055 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001056 prog->argv[argc_l] = buf;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001057 saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001058 }
1059 } else
1060 switch (*src) {
1061 case '"':
1062 case '\'':
1063 quote = *src;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001064 saw_quote = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001065 break;
1066
1067 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001068 if (*(src-1)== '$')
1069 *buf++ = *src;
1070 else
1071 done = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001072 break;
1073
Eric Andersenff9ad472004-02-10 01:07:45 +00001074#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +00001075 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +00001076 case '<':
Eric Andersen86349772000-12-18 20:25:50 +00001077 i = prog->num_redirects++;
1078 prog->redirects = xrealloc(prog->redirects,
1079 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +00001080 (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +00001081
Eric Andersen86349772000-12-18 20:25:50 +00001082 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001083 if (buf != prog->argv[argc_l]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001084 /* the stuff before this character may be the file number
Erik Andersen161220c2000-03-16 08:12:48 +00001085 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +00001086 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001087 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +00001088
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001089 if (*chptr && *prog->argv[argc_l]) {
1090 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001091 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001092 }
1093 }
1094
Eric Andersen86349772000-12-18 20:25:50 +00001095 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001096 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001097 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001098 else
Eric Andersen86349772000-12-18 20:25:50 +00001099 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001100 }
1101
1102 if (*src++ == '>') {
1103 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001104 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001105 REDIRECT_APPEND, src++;
1106 else
Eric Andersen86349772000-12-18 20:25:50 +00001107 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001108 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001109 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001110 }
1111
1112 /* This isn't POSIX sh compliant. Oh well. */
1113 chptr = src;
1114 while (isspace(*chptr))
1115 chptr++;
1116
1117 if (!*chptr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001118 bb_error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001119 free_job(job);
1120 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001121 return 1;
1122 }
1123
Eric Andersen86349772000-12-18 20:25:50 +00001124 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001125 while (*chptr && !isspace(*chptr))
1126 *buf++ = *chptr++;
1127
1128 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001129 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001130 break;
1131
1132 case '|': /* pipe */
1133 /* finish this command */
Matt Kraaibe66ad32001-04-12 15:42:17 +00001134 if (*prog->argv[argc_l] || saw_quote)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001135 argc_l++;
1136 if (!argc_l) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001137 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001138 free_job(job);
1139 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001140 return 1;
1141 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001142 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001143
1144 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001145 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001146 job->progs = xrealloc(job->progs,
Eric Andersen86349772000-12-18 20:25:50 +00001147 sizeof(*job->progs) * job->num_progs);
1148 prog = job->progs + (job->num_progs - 1);
1149 prog->num_redirects = 0;
1150 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001151 prog->is_stopped = 0;
1152 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001153 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001154
Eric Andersen86349772000-12-18 20:25:50 +00001155 argv_alloced = 5;
1156 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001157 prog->argv[0] = ++buf;
1158
1159 src++;
1160 while (*src && isspace(*src))
1161 src++;
1162
1163 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001164 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001165 free_job(job);
1166 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001167 return 1;
1168 }
1169 src--; /* we'll ++ it at the end of the loop */
1170
1171 break;
Eric Andersenff9ad472004-02-10 01:07:45 +00001172#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001173
Eric Andersen37032b42004-02-10 01:28:36 +00001174#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen161220c2000-03-16 08:12:48 +00001175 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001176 *inbg = 1;
Eric Andersen37032b42004-02-10 01:28:36 +00001177#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001178 case ';': /* multiple commands */
1179 done = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001180 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001181 break;
1182
Matt Kraai131241f2000-09-14 00:43:20 +00001183 case '\\':
1184 src++;
1185 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001186 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001187 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001188 return 1;
1189 }
1190 if (*src == '*' || *src == '[' || *src == ']'
1191 || *src == '?') *buf++ = '\\';
1192 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001193 default:
1194 *buf++ = *src;
1195 }
1196
Erik Andersend75af992000-03-16 08:09:09 +00001197 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001198 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001199
Matt Kraaibe66ad32001-04-12 15:42:17 +00001200 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001201 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001202 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001203 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001204 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001205 return 0;
1206 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001207 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001208
Eric Andersen86349772000-12-18 20:25:50 +00001209 if (!return_command) {
1210 job->text = xmalloc(strlen(*command_ptr) + 1);
1211 strcpy(job->text, *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001212 } else {
1213 /* This leaves any trailing spaces, which is a bit sloppy */
Eric Andersen86349772000-12-18 20:25:50 +00001214 count = return_command - *command_ptr;
Eric Andersenec10b9d2000-07-14 01:13:11 +00001215 job->text = xmalloc(count + 1);
Eric Andersen86349772000-12-18 20:25:50 +00001216 strncpy(job->text, *command_ptr, count);
Erik Andersen161220c2000-03-16 08:12:48 +00001217 job->text[count] = '\0';
1218 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001219
Eric Andersen86349772000-12-18 20:25:50 +00001220 *command_ptr = return_command;
Eric Andersenff9ad472004-02-10 01:07:45 +00001221
Erik Andersend75af992000-03-16 08:09:09 +00001222 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001223}
1224
Eric Andersen86349772000-12-18 20:25:50 +00001225/* Run the child_prog, no matter what kind of command it uses.
1226 */
1227static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001228{
Eric Andersen86349772000-12-18 20:25:50 +00001229 struct built_in_command *x;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001230#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
Eric Andersenaf4ac772001-02-01 22:43:49 +00001231 char *name;
Erik Andersenbcd61772000-05-13 06:33:19 +00001232#endif
Erik Andersen3522eb12000-03-12 23:49:18 +00001233
Eric Andersene9f07fb2000-12-21 18:31:36 +00001234 /* Check if the command matches any of the non-forking builtins.
1235 * Depending on context, this might be redundant. But it's
1236 * easier to waste a few CPU cycles than it is to figure out
1237 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001238 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001239 for (x = bltins; x->cmd; x++) {
1240 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001241 _exit(x->function(child));
Eric Andersene9f07fb2000-12-21 18:31:36 +00001242 }
1243 }
1244
1245 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001246 for (x = bltins_forking; x->cmd; x++) {
1247 if (strcmp(child->argv[0], x->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001248 bb_applet_name=x->cmd;
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001249 _exit (x->function(child));
Eric Andersen86349772000-12-18 20:25:50 +00001250 }
1251 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001252#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
Eric Andersen86349772000-12-18 20:25:50 +00001253 /* Check if the command matches any busybox internal
1254 * commands ("applets") here. Following discussions from
Eric Andersen2423b122001-12-08 01:56:15 +00001255 * November 2000 on busybox@busybox.net, don't use
Manuel Novoa III cad53642003-03-19 09:13:01 +00001256 * bb_get_last_path_component(). This way explicit (with
Eric Andersen86349772000-12-18 20:25:50 +00001257 * slashes) filenames will never be interpreted as an
1258 * applet, just like with builtins. This way the user can
1259 * override an applet with an explicit filename reference.
1260 * The only downside to this change is that an explicit
1261 * /bin/foo invocation will fork and exec /bin/foo, even if
1262 * /bin/foo is a symlink to busybox.
1263 */
Matt Kraaif2cc2762001-02-01 19:21:20 +00001264 name = child->argv[0];
Eric Andersen86349772000-12-18 20:25:50 +00001265
Eric Andersen67991cf2001-02-14 21:23:06 +00001266 {
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001267 char** argv_l=child->argv;
1268 int argc_l;
1269#ifdef _NEWLIB_VERSION
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001270 /* newlib uses __getopt_initialized for getopt() in
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001271 * addition to optind, see newlib/libc/sys/linux/getopt.c
1272 */
1273 extern int __getopt_initialized = 0;
1274#endif
1275 for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
1276 optind = 1;
1277 run_applet_by_name(name, argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001278 }
1279#endif
1280
1281 execvp(child->argv[0], child->argv);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001282
Eric Andersenff9ad472004-02-10 01:07:45 +00001283 /* Do not use bb_perror_msg_and_die() here, since we must not
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001284 * call exit() but should call _exit() instead */
Eric Andersenad082982002-09-30 20:20:20 +00001285 fprintf(stderr, "%s: %m\n", child->argv[0]);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001286 _exit(EXIT_FAILURE);
Eric Andersen86349772000-12-18 20:25:50 +00001287}
1288
1289static void insert_job(struct job *newjob, int inbg)
1290{
1291 struct job *thejob;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001292 struct jobset *j_list=newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001293
1294 /* find the ID for thejob to use */
1295 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001296 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001297 if (thejob->jobid >= newjob->jobid)
1298 newjob->jobid = thejob->jobid + 1;
1299
1300 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001301 if (!j_list->head) {
1302 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001303 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001304 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001305 thejob->next = xmalloc(sizeof(*thejob));
1306 thejob = thejob->next;
1307 }
1308
1309 *thejob = *newjob; /* physically copy the struct job */
1310 thejob->next = NULL;
1311 thejob->running_progs = thejob->num_progs;
1312 thejob->stopped_progs = 0;
1313
Eric Andersen37032b42004-02-10 01:28:36 +00001314#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen86349772000-12-18 20:25:50 +00001315 if (inbg) {
Eric Andersenff9ad472004-02-10 01:07:45 +00001316 /* we don't wait for background thejobs to return -- append it
Eric Andersen86349772000-12-18 20:25:50 +00001317 to the list of backgrounded thejobs and leave it alone */
1318 printf("[%d] %d\n", thejob->jobid,
1319 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001320 last_jobid = newjob->jobid;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001321 last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001322 } else {
1323 newjob->job_list->fg = thejob;
1324
1325 /* move the new process group into the foreground */
1326 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001327 if (tcsetpgrp(shell_terminal, newjob->pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001328 bb_perror_msg("tcsetpgrp");
Eric Andersen86349772000-12-18 20:25:50 +00001329 }
Eric Andersen37032b42004-02-10 01:28:36 +00001330#endif
Eric Andersen86349772000-12-18 20:25:50 +00001331}
1332
1333static int run_command(struct job *newjob, int inbg, int outpipe[2])
1334{
1335 /* struct job *thejob; */
1336 int i;
1337 int nextin, nextout;
1338 int pipefds[2]; /* pipefd[0] is for reading */
1339 struct built_in_command *x;
1340 struct child_prog *child;
1341
Eric Andersen6efc48c2000-07-18 08:16:39 +00001342 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001343 for (i = 0; i < newjob->num_progs; i++) {
1344 child = & (newjob->progs[i]);
1345
1346 if ((i + 1) < newjob->num_progs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001347 if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001348 nextout = pipefds[1];
1349 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001350 if (outpipe[1]!=-1) {
1351 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001352 } else {
1353 nextout = 1;
1354 }
Erik Andersen161220c2000-03-16 08:12:48 +00001355 }
1356
Eric Andersen501c88b2000-07-28 15:14:45 +00001357
Eric Andersene9f07fb2000-12-21 18:31:36 +00001358 /* Check if the command matches any non-forking builtins,
1359 * but only if this is a simple command.
1360 * Non-forking builtins within pipes have to fork anyway,
1361 * and are handled in pseudo_exec. "echo foo | read bar"
1362 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001363 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001364 if (newjob->num_progs == 1) {
1365 for (x = bltins; x->cmd; x++) {
1366 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersene9f07fb2000-12-21 18:31:36 +00001367 int rcode;
Eric Andersenff9ad472004-02-10 01:07:45 +00001368 int squirrel[] = {-1, -1, -1};
Eric Andersene9f07fb2000-12-21 18:31:36 +00001369 setup_redirects(child, squirrel);
1370 rcode = x->function(child);
1371 restore_redirects(squirrel);
1372 return rcode;
1373 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001374 }
1375 }
1376
Eric Andersene3efc922004-04-12 17:59:24 +00001377#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersenff9ad472004-02-10 01:07:45 +00001378 if (!(child->pid = fork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001379#else
Eric Andersenff9ad472004-02-10 01:07:45 +00001380 if (!(child->pid = vfork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001381#endif
1382 {
Eric Andersen2d848a42001-06-25 17:11:54 +00001383 /* Set the handling for job control signals back to the default. */
1384 signal(SIGINT, SIG_DFL);
1385 signal(SIGQUIT, SIG_DFL);
1386 signal(SIGTSTP, SIG_DFL);
1387 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001388 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001389 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001390
Eric Andersen8ea28be2001-01-05 20:58:22 +00001391 close_all();
1392
Eric Andersen86349772000-12-18 20:25:50 +00001393 if (outpipe[1]!=-1) {
1394 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001395 }
1396 if (nextin != 0) {
1397 dup2(nextin, 0);
1398 close(nextin);
1399 }
1400
1401 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001402 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001403 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001404 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001405 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001406 }
1407
Eric Andersen86349772000-12-18 20:25:50 +00001408 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001409 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001410
Eric Andersen86349772000-12-18 20:25:50 +00001411 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001412 }
Eric Andersen86349772000-12-18 20:25:50 +00001413 if (outpipe[1]!=-1) {
1414 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001415 }
Erik Andersen161220c2000-03-16 08:12:48 +00001416
1417 /* put our child in the process group whose leader is the
1418 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001419 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001420 if (nextin != 0)
1421 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001422 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001423 close(nextout);
1424
Eric Andersenff9ad472004-02-10 01:07:45 +00001425 /* If there isn't another process, nextin is garbage
Erik Andersen161220c2000-03-16 08:12:48 +00001426 but it doesn't matter */
1427 nextin = pipefds[0];
1428 }
1429
Eric Andersen86349772000-12-18 20:25:50 +00001430 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001431
Eric Andersen86349772000-12-18 20:25:50 +00001432 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001433
Erik Andersen161220c2000-03-16 08:12:48 +00001434 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001435}
1436
Erik Andersend75af992000-03-16 08:09:09 +00001437static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001438{
Erik Andersen161220c2000-03-16 08:12:48 +00001439 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001440 char *next_command = NULL;
1441 struct job newjob;
Erik Andersen161220c2000-03-16 08:12:48 +00001442 int i;
Eric Andersen86349772000-12-18 20:25:50 +00001443 int inbg;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001444 int status;
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001445#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersend20d3752004-03-12 22:08:42 +00001446 pid_t parent_pgrp;
1447 /* save current owner of TTY so we can restore it on exit */
1448 parent_pgrp = tcgetpgrp(shell_terminal);
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001449#endif
Eric Andersen86349772000-12-18 20:25:50 +00001450 newjob.job_list = &job_list;
1451 newjob.job_context = DEFAULT_CONTEXT;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001452
Matt Kraaib8907522000-09-13 02:08:21 +00001453 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Erik Andersend75af992000-03-16 08:09:09 +00001454
Erik Andersen161220c2000-03-16 08:12:48 +00001455 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001456 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001457 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001458
Erik Andersend75af992000-03-16 08:09:09 +00001459 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001460 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001461
Eric Andersen86349772000-12-18 20:25:50 +00001462 if (!next_command) {
1463 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001464 break;
Eric Andersen86349772000-12-18 20:25:50 +00001465 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001466 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001467
Matt Kraai1f0c4362001-12-20 23:13:26 +00001468 if (! expand_arguments(next_command)) {
Eric Andersenca604592001-03-08 17:17:13 +00001469 free(command);
1470 command = (char *) xcalloc(BUFSIZ, sizeof(char));
1471 next_command = NULL;
1472 continue;
1473 }
1474
Eric Andersen86349772000-12-18 20:25:50 +00001475 if (!parse_command(&next_command, &newjob, &inbg) &&
1476 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001477 int pipefds[2] = {-1,-1};
Eric Andersenff9ad472004-02-10 01:07:45 +00001478 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001479 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001480 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001481 }
1482 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001483 free(command);
Matt Kraaib8907522000-09-13 02:08:21 +00001484 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Eric Andersen86349772000-12-18 20:25:50 +00001485 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001486 }
1487 } else {
1488 /* a job is running in the foreground; wait for it */
1489 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001490 while (!job_list.fg->progs[i].pid ||
1491 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001492
Eric Andersen7373e482002-07-31 04:04:47 +00001493 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) {
1494 if (errno != ECHILD) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001495 bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
Eric Andersen7373e482002-07-31 04:04:47 +00001496 }
1497 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001498
Erik Andersend75af992000-03-16 08:09:09 +00001499 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001500 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001501 job_list.fg->running_progs--;
1502 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001503
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001504 last_return_code=WEXITSTATUS(status);
1505
Eric Andersen86349772000-12-18 20:25:50 +00001506 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001507 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001508 remove_job(&job_list, job_list.fg);
1509 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001510 }
Eric Andersen37032b42004-02-10 01:28:36 +00001511 }
1512#ifdef CONFIG_LASH_JOB_CONTROL
1513 else {
Erik Andersen161220c2000-03-16 08:12:48 +00001514 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001515 job_list.fg->stopped_progs++;
1516 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001517
Eric Andersen86349772000-12-18 20:25:50 +00001518 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1519 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1520 "Stopped", job_list.fg->text);
1521 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001522 }
Erik Andersend75af992000-03-16 08:09:09 +00001523 }
1524
Eric Andersen86349772000-12-18 20:25:50 +00001525 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001526 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001527 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001528 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Eric Andersenff9ad472004-02-10 01:07:45 +00001529 bb_perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001530 }
Eric Andersen37032b42004-02-10 01:28:36 +00001531#endif
Erik Andersend75af992000-03-16 08:09:09 +00001532 }
1533 }
Erik Andersen161220c2000-03-16 08:12:48 +00001534 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001535
Eric Andersen37032b42004-02-10 01:28:36 +00001536#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen1c314ad2000-06-28 16:56:25 +00001537 /* return controlling TTY back to parent process group before exiting */
Eric Andersen7373e482002-07-31 04:04:47 +00001538 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001539 bb_perror_msg("tcsetpgrp");
Eric Andersen37032b42004-02-10 01:28:36 +00001540#endif
Eric Andersenb54833c2000-07-03 23:56:26 +00001541
1542 /* return exit status if called with "-c" */
1543 if (input == NULL && WIFEXITED(status))
1544 return WEXITSTATUS(status);
Eric Andersenff9ad472004-02-10 01:07:45 +00001545
Erik Andersen161220c2000-03-16 08:12:48 +00001546 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001547}
1548
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001549#ifdef CONFIG_FEATURE_CLEAN_UP
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +00001550static void free_memory(void)
Eric Andersenfad9c112000-07-25 18:06:52 +00001551{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001552 if (cwd && cwd!=bb_msg_unknown) {
Eric Andersen5d60a462001-08-22 05:32:24 +00001553 free((char*)cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001554 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001555 if (local_pending_command)
1556 free(local_pending_command);
1557
Eric Andersen86349772000-12-18 20:25:50 +00001558 if (job_list.fg && !job_list.fg->running_progs) {
1559 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001560 }
1561}
1562#endif
1563
Eric Andersen37032b42004-02-10 01:28:36 +00001564#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen2d848a42001-06-25 17:11:54 +00001565/* Make sure we have a controlling tty. If we get started under a job
1566 * aware app (like bash for example), make sure we are now in charge so
1567 * we don't fight over who gets the foreground */
Eric Andersenece8fc22002-12-11 04:26:28 +00001568static void setup_job_control(void)
Eric Andersen2d848a42001-06-25 17:11:54 +00001569{
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001570 int status;
Eric Andersen37032b42004-02-10 01:28:36 +00001571 pid_t shell_pgrp;
Eric Andersenff9ad472004-02-10 01:07:45 +00001572
Eric Andersen2d848a42001-06-25 17:11:54 +00001573 /* Loop until we are in the foreground. */
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001574 while ((status = tcgetpgrp (shell_terminal)) >= 0) {
1575 if (status == (shell_pgrp = getpgrp ())) {
1576 break;
1577 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001578 kill (- shell_pgrp, SIGTTIN);
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001579 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001580
1581 /* Ignore interactive and job-control signals. */
1582 signal(SIGINT, SIG_IGN);
1583 signal(SIGQUIT, SIG_IGN);
1584 signal(SIGTSTP, SIG_IGN);
1585 signal(SIGTTIN, SIG_IGN);
1586 signal(SIGTTOU, SIG_IGN);
1587 signal(SIGCHLD, SIG_IGN);
1588
1589 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001590 setsid();
Eric Andersen2d848a42001-06-25 17:11:54 +00001591 shell_pgrp = getpid ();
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001592 setpgid (shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001593
1594 /* Grab control of the terminal. */
1595 tcsetpgrp(shell_terminal, shell_pgrp);
1596}
Eric Andersen37032b42004-02-10 01:28:36 +00001597#else
1598static inline void setup_job_control(void)
1599{
1600}
1601#endif
Eric Andersen6efc48c2000-07-18 08:16:39 +00001602
Matt Kraai2d91deb2001-08-01 17:21:35 +00001603int lash_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001604{
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001605 int opt, interactive=FALSE;
Erik Andersen161220c2000-03-16 08:12:48 +00001606 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001607 argc = argc_l;
1608 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001609
Eric Andersen702ec592001-03-06 22:17:29 +00001610 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001611 last_jobid = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001612 local_pending_command = NULL;
Eric Andersen702ec592001-03-06 22:17:29 +00001613 close_me_head = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001614 job_list.head = NULL;
1615 job_list.fg = NULL;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001616 last_return_code=1;
Eric Andersen501c88b2000-07-28 15:14:45 +00001617
Eric Andersenb558e762000-11-30 22:43:16 +00001618 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001619 FILE *prof_input;
1620 prof_input = fopen("/etc/profile", "r");
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001621 if (prof_input) {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001622 int tmp_fd = fileno(prof_input);
Eric Andersenff9ad472004-02-10 01:07:45 +00001623 mark_open(tmp_fd);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001624 /* Now run the file */
1625 busy_loop(prof_input);
1626 fclose(prof_input);
1627 mark_closed(tmp_fd);
1628 }
Eric Andersenb558e762000-11-30 22:43:16 +00001629 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001630
Eric Andersenf21aa842000-12-08 20:50:30 +00001631 while ((opt = getopt(argc_l, argv_l, "cxi")) > 0) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001632 switch (opt) {
1633 case 'c':
1634 input = NULL;
Matt Kraai6085c722000-09-06 01:46:18 +00001635 if (local_pending_command != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001636 bb_error_msg_and_die("multiple -c arguments");
1637 local_pending_command = bb_xstrdup(argv[optind]);
Matt Kraai6085c722000-09-06 01:46:18 +00001638 optind++;
1639 argv = argv+optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001640 break;
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001641 case 'i':
1642 interactive = TRUE;
1643 break;
Eric Andersen501c88b2000-07-28 15:14:45 +00001644 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001645 bb_show_usage();
Eric Andersen501c88b2000-07-28 15:14:45 +00001646 }
1647 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001648 /* A shell is interactive if the `-i' flag was given, or if all of
1649 * the following conditions are met:
1650 * no -c command
1651 * no arguments remaining or the -s flag given
1652 * standard input is a terminal
1653 * standard output is a terminal
1654 * Refer to Posix.2, the description of the `sh' utility. */
Eric Andersen86349772000-12-18 20:25:50 +00001655 if (argv[optind]==NULL && input==stdin &&
Eric Andersen70060d22004-03-27 10:02:48 +00001656 isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
Eric Andersen86349772000-12-18 20:25:50 +00001657 interactive=TRUE;
1658 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001659 setup_job_control();
Eric Andersen86349772000-12-18 20:25:50 +00001660 if (interactive==TRUE) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001661 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Eric Andersen501c88b2000-07-28 15:14:45 +00001662 /* Looks like they want an interactive shell */
Eric Andersen37032b42004-02-10 01:28:36 +00001663#ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
Matt Kraai4ef40c02001-04-12 20:44:21 +00001664 printf( "\n\n" BB_BANNER " Built-in shell (lash)\n");
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001665 printf( "Enter 'help' for a list of built-in commands.\n\n");
Eric Andersend63dee42001-10-19 00:22:23 +00001666#endif
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001667 } else if (local_pending_command==NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001668 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001669 input = bb_xfopen(argv[optind], "r");
Eric Andersen8ea28be2001-01-05 20:58:22 +00001670 mark_open(fileno(input)); /* be lazy, never mark this closed */
Eric Andersen501c88b2000-07-28 15:14:45 +00001671 }
1672
Eric Andersen6efc48c2000-07-18 08:16:39 +00001673 /* initialize the cwd -- this is never freed...*/
Eric Andersene5dfced2001-04-09 22:48:12 +00001674 cwd = xgetcwd(0);
Eric Andersen5f265b72001-05-11 16:58:46 +00001675 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001676 cwd = bb_msg_unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001677
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001678#ifdef CONFIG_FEATURE_CLEAN_UP
Eric Andersenfad9c112000-07-25 18:06:52 +00001679 atexit(free_memory);
1680#endif
1681
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001682#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001683 cmdedit_set_initial_prompt();
1684#else
1685 PS1 = NULL;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001686#endif
Eric Andersenff9ad472004-02-10 01:07:45 +00001687
Erik Andersen161220c2000-03-16 08:12:48 +00001688 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001689}