blob: 8e8d45ae9718cd5a969a3867b170f07658ac2b40 [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
Rob Landleybc68cd12006-03-10 19:22:06 +000060enum {
61 DEFAULT_CONTEXT = 0x1,
62 IF_TRUE_CONTEXT = 0x2,
63 IF_FALSE_CONTEXT = 0x4,
64 THEN_EXP_CONTEXT = 0x8,
65 ELSE_EXP_CONTEXT = 0x10
66};
Eric Andersenfad9c112000-07-25 18:06:52 +000067
Eric Andersenff9ad472004-02-10 01:07:45 +000068#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000069struct redir_struct {
70 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000071 int fd; /* file descriptor being redirected */
72 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000073};
Eric Andersenff9ad472004-02-10 01:07:45 +000074#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000075
Eric Andersen86349772000-12-18 20:25:50 +000076struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000077 pid_t pid; /* 0 if exited */
78 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000079 int num_redirects; /* elements in redirection array */
Eric Andersen86349772000-12-18 20:25:50 +000080 int is_stopped; /* is the program currently running? */
81 struct job *family; /* pointer back to the child's parent job */
Eric Andersenff9ad472004-02-10 01:07:45 +000082#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
83 struct redir_struct *redirects; /* I/O redirects */
84#endif
85};
86
87struct jobset {
88 struct job *head; /* head of list of running jobs */
89 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000090};
91
92struct job {
Eric Andersen86349772000-12-18 20:25:50 +000093 int jobid; /* job number */
94 int num_progs; /* total number of programs in job */
95 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000096 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +000097 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +000098 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +000099 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +0000100 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +0000101 int stopped_progs; /* number of programs alive, but stopped */
102 unsigned int job_context; /* bitmask defining current context */
103 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +0000104};
105
Eric Andersen86349772000-12-18 20:25:50 +0000106struct built_in_command {
Erik Andersen161220c2000-03-16 08:12:48 +0000107 char *cmd; /* name */
108 char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +0000109 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +0000110};
111
Eric Andersen8ea28be2001-01-05 20:58:22 +0000112struct close_me {
113 int fd;
114 struct close_me *next;
115};
116
Eric Andersen34e19412000-07-10 18:47:24 +0000117/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000118static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000119static int builtin_exec(struct child_prog *cmd);
120static int builtin_exit(struct child_prog *cmd);
121static int builtin_fg_bg(struct child_prog *cmd);
122static int builtin_help(struct child_prog *cmd);
123static int builtin_jobs(struct child_prog *dummy);
124static int builtin_pwd(struct child_prog *dummy);
125static int builtin_export(struct child_prog *cmd);
126static int builtin_source(struct child_prog *cmd);
127static int builtin_unset(struct child_prog *cmd);
128static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000129
Eric Andersen34e19412000-07-10 18:47:24 +0000130
131/* function prototypes for shell stuff */
Eric Andersen8ea28be2001-01-05 20:58:22 +0000132static void mark_open(int fd);
133static void mark_closed(int fd);
Eric Andersen36278b92001-03-06 20:47:31 +0000134static void close_all(void);
Eric Andersen86349772000-12-18 20:25:50 +0000135static void checkjobs(struct jobset *job_list);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000136static void remove_job(struct jobset *j_list, struct job *job);
Eric Andersen86349772000-12-18 20:25:50 +0000137static int get_command(FILE * source, char *command);
138static int parse_command(char **command_ptr, struct job *job, int *inbg);
139static int run_command(struct job *newjob, int inbg, int outpipe[2]);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000140static int pseudo_exec(struct child_prog *cmd) ATTRIBUTE_NORETURN;
Erik Andersen3522eb12000-03-12 23:49:18 +0000141static int busy_loop(FILE * input);
142
Erik Andersend75af992000-03-16 08:09:09 +0000143
Mark Whitley37653aa2000-07-12 23:36:17 +0000144/* Table of built-in functions (these are non-forking builtins, meaning they
145 * can change global variables in the parent shell process but they will not
146 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Eric Andersen86349772000-12-18 20:25:50 +0000147static struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000148 {"bg", "Resume a job in the background", builtin_fg_bg},
149 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000150 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000151 {"exit", "Exit from shell()", builtin_exit},
152 {"fg", "Bring job into the foreground", builtin_fg_bg},
153 {"jobs", "Lists the active jobs", builtin_jobs},
154 {"export", "Set environment variable", builtin_export},
155 {"unset", "Unset environment variable", builtin_unset},
156 {"read", "Input environment variable", builtin_read},
Matt Kraaidd450a02000-09-13 03:43:36 +0000157 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000158 /* to do: add ulimit */
Eric Andersenfad9c112000-07-25 18:06:52 +0000159 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000160};
161
Mark Whitley37653aa2000-07-12 23:36:17 +0000162/* Table of forking built-in functions (things that fork cannot change global
163 * variables in the parent process, such as the current working directory) */
Eric Andersen86349772000-12-18 20:25:50 +0000164static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000165 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000166 {"help", "List shell built-in commands", builtin_help},
167 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000168};
169
Eric Andersen0bcc8132001-01-05 19:37:32 +0000170
Eric Andersen7467c8d2001-07-12 20:26:32 +0000171static int shell_context; /* Type prompt trigger (PS1 or PS2) */
Eric Andersen0bcc8132001-01-05 19:37:32 +0000172
173
174/* Globals that are static to this file */
Eric Andersencfa88ec2001-05-11 18:08:16 +0000175static const char *cwd;
Eric Andersen744b0642001-01-05 21:23:44 +0000176static char *local_pending_command = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000177static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000178static int argc;
179static char **argv;
Eric Andersen702ec592001-03-06 22:17:29 +0000180static struct close_me *close_me_head;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000181static int last_return_code;
182static int last_bg_pid;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000183static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000184static int shell_terminal;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000185static char *PS1;
Eric Andersene5dfced2001-04-09 22:48:12 +0000186static char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000187
188
Eric Andersenb558e762000-11-30 22:43:16 +0000189#ifdef DEBUG_SHELL
190static inline void debug_printf(const char *format, ...)
191{
192 va_list args;
193 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000194 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000195 va_end(args);
196}
197#else
198static inline void debug_printf(const char *format, ...) { }
199#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000200
Eric Andersen86349772000-12-18 20:25:50 +0000201/*
202 Most builtins need access to the struct child_prog that has
203 their arguments, previously coded as cmd->progs[0]. That coding
204 can exhibit a bug, if the builtin is not the first command in
205 a pipeline: "echo foo | exec sort" will attempt to exec foo.
206
207builtin previous use notes
208------ ----------------- ---------
209cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000210exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
211exit cmd->progs[0]
212fg_bg cmd->progs[0], job_list->head, job_list->fg
213help 0
214jobs job_list->head
215pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000216export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000217source cmd->progs[0]
218unset cmd->progs[0]
219read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000220
221I added "struct job *family;" to struct child_prog,
222and switched API to builtin_foo(struct child_prog *child);
223So cmd->text becomes child->family->text
224 cmd->job_context becomes child->family->job_context
225 cmd->progs[0] becomes *child
226 job_list becomes child->family->job_list
227 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000228
Erik Andersend75af992000-03-16 08:09:09 +0000229/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000230static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000231{
Erik Andersen161220c2000-03-16 08:12:48 +0000232 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000233
Eric Andersen86349772000-12-18 20:25:50 +0000234 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000235 newdir = getenv("HOME");
236 else
Eric Andersen86349772000-12-18 20:25:50 +0000237 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000238 if (chdir(newdir)) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000239 printf("cd: %s: %m\n", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000240 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000241 }
Eric Andersencfa88ec2001-05-11 18:08:16 +0000242 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000243 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000244 cwd = bb_msg_unknown;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000245 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000246}
247
Eric Andersend2f56772000-09-21 02:48:07 +0000248/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000249static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000250{
Eric Andersen86349772000-12-18 20:25:50 +0000251 if (child->argv[1] == NULL)
252 return EXIT_SUCCESS; /* Really? */
253 child->argv++;
Eric Andersen07f2f392001-03-06 20:28:22 +0000254 close_all();
Eric Andersen86349772000-12-18 20:25:50 +0000255 pseudo_exec(child);
256 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000257}
258
Erik Andersen3522eb12000-03-12 23:49:18 +0000259/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000260static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000261{
Eric Andersen86349772000-12-18 20:25:50 +0000262 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000263 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000264
Eric Andersen86349772000-12-18 20:25:50 +0000265 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000266}
267
268/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000269static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000270{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000271 int i, jobnum;
Erik Andersen6273f652000-03-17 01:12:41 +0000272 struct job *job=NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000273
Eric Andersen8a646dd2001-06-21 16:38:11 +0000274 /* If they gave us no args, assume they want the last backgrounded task */
275 if (!child->argv[1]) {
276 for (job = child->family->job_list->head; job; job = job->next) {
277 if (job->jobid == last_jobid) {
278 break;
279 }
Erik Andersen161220c2000-03-16 08:12:48 +0000280 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000281 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000282 bb_error_msg("%s: no current job", child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000283 return EXIT_FAILURE;
284 }
285 } else {
286 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000287 bb_error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000288 return EXIT_FAILURE;
289 }
290 for (job = child->family->job_list->head; job; job = job->next) {
291 if (job->jobid == jobnum) {
292 break;
293 }
294 }
295 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000296 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000297 return EXIT_FAILURE;
298 }
Erik Andersend75af992000-03-16 08:09:09 +0000299 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000300
Eric Andersen86349772000-12-18 20:25:50 +0000301 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000302 /* Put the job into the foreground. */
303 tcsetpgrp(shell_terminal, job->pgrp);
304
Eric Andersen86349772000-12-18 20:25:50 +0000305 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000306 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000307
Erik Andersen161220c2000-03-16 08:12:48 +0000308 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000309 for (i = 0; i < job->num_progs; i++)
310 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000311
Eric Andersen86349772000-12-18 20:25:50 +0000312 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000313
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000314 if ( (i=kill(- job->pgrp, SIGCONT)) < 0) {
315 if (i == ESRCH) {
316 remove_job(&job_list, job);
Eric Andersen07abfe22001-06-27 17:29:11 +0000317 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000318 bb_perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000319 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000320 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000321
Matt Kraai3e856ce2000-12-01 02:55:13 +0000322 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000323}
324
325/* built-in 'help' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000326static int builtin_help(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000327{
Eric Andersen86349772000-12-18 20:25:50 +0000328 struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000329
Eric Andersen86349772000-12-18 20:25:50 +0000330 printf("\nBuilt-in commands:\n");
331 printf("-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000332 for (x = bltins; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000333 if (x->descr==NULL)
334 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000335 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000336 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000337 for (x = bltins_forking; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000338 if (x->descr==NULL)
339 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000340 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000341 }
Eric Andersen86349772000-12-18 20:25:50 +0000342 printf("\n\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000343 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000344}
345
346/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000347static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000348{
Erik Andersen161220c2000-03-16 08:12:48 +0000349 struct job *job;
Eric Andersen86349772000-12-18 20:25:50 +0000350 char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000351
Eric Andersen86349772000-12-18 20:25:50 +0000352 for (job = child->family->job_list->head; job; job = job->next) {
353 if (job->running_progs == job->stopped_progs)
354 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000355 else
Eric Andersen86349772000-12-18 20:25:50 +0000356 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000357
Eric Andersen86349772000-12-18 20:25:50 +0000358 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000359 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000360 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000361}
362
363
364/* built-in 'pwd' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000365static int builtin_pwd(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000366{
Eric Andersencfa88ec2001-05-11 18:08:16 +0000367 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000368 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000369 cwd = bb_msg_unknown;
Matt Kraai59df6f72001-05-16 14:21:09 +0000370 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000371 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000372}
373
Erik Andersen6273f652000-03-17 01:12:41 +0000374/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000375static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000376{
Erik Andersen161220c2000-03-16 08:12:48 +0000377 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000378 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000379
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000380 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000381 char **e;
382 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000383 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000384 }
Matt Kraai2129f972001-04-04 17:50:04 +0000385 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000386 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000387 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000388 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000389 fprintf(stderr, "export: %m\n");
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000390#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000391 if (strncmp(v, "PS1=", 4)==0)
392 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000393#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000394
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000395#ifdef CONFIG_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +0000396 if(strncmp(v, "LC_ALL=", 7)==0)
397 setlocale(LC_ALL, getenv("LC_ALL"));
Mark Whitleya82a0032001-03-27 17:07:15 +0000398 if(strncmp(v, "LC_CTYPE=", 9)==0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000399 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000400#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000401
Erik Andersen161220c2000-03-16 08:12:48 +0000402 return (res);
Erik Andersen3522eb12000-03-12 23:49:18 +0000403}
404
Eric Andersenb54833c2000-07-03 23:56:26 +0000405/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000406static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000407{
408 int res = 0, len, newlen;
409 char *s;
410 char string[MAX_READ];
411
Eric Andersen86349772000-12-18 20:25:50 +0000412 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000413 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen0d2d1eb2002-06-06 13:33:01 +0000414 safe_strncpy(string, child->argv[1], MAX_READ-1);
Eric Andersenb54833c2000-07-03 23:56:26 +0000415 len = strlen(string);
416 string[len++] = '=';
417 string[len] = '\0';
418 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
419 newlen = strlen(string);
420 if(newlen > len)
421 string[--newlen] = '\0'; /* chomp trailing newline */
422 /*
423 ** string should now contain "VAR=<value>"
424 ** copy it (putenv() won't do that, so we must make sure
425 ** the string resides in a static buffer!)
426 */
427 res = -1;
428 if((s = strdup(string)))
429 res = putenv(s);
430 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000431 fprintf(stderr, "read: %m\n");
Eric Andersenb54833c2000-07-03 23:56:26 +0000432 }
433 else
434 fgets(string, sizeof(string), stdin);
435
436 return (res);
437}
438
Erik Andersen3522eb12000-03-12 23:49:18 +0000439/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000440static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000441{
Erik Andersen161220c2000-03-16 08:12:48 +0000442 FILE *input;
443 int status;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000444 int fd;
Erik Andersen3522eb12000-03-12 23:49:18 +0000445
Eric Andersen86349772000-12-18 20:25:50 +0000446 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000447 return EXIT_FAILURE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000448
Eric Andersen86349772000-12-18 20:25:50 +0000449 input = fopen(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000450 if (!input) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000451 printf( "Couldn't open file '%s'\n", child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000452 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000453 }
Erik Andersend75af992000-03-16 08:09:09 +0000454
Eric Andersen8ea28be2001-01-05 20:58:22 +0000455 fd=fileno(input);
456 mark_open(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000457 /* Now run the file */
458 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000459 fclose(input);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000460 mark_closed(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000461 return (status);
Erik Andersen3522eb12000-03-12 23:49:18 +0000462}
463
464/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000465static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000466{
Eric Andersen86349772000-12-18 20:25:50 +0000467 if (child->argv[1] == NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000468 printf( "unset: parameter required.\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000469 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000470 }
Eric Andersen86349772000-12-18 20:25:50 +0000471 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000472 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000473}
474
Eric Andersen8ea28be2001-01-05 20:58:22 +0000475static void mark_open(int fd)
476{
477 struct close_me *new = xmalloc(sizeof(struct close_me));
478 new->fd = fd;
479 new->next = close_me_head;
480 close_me_head = new;
481}
482
483static void mark_closed(int fd)
484{
485 struct close_me *tmp;
486 if (close_me_head == NULL || close_me_head->fd != fd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000487 bb_error_msg_and_die("corrupt close_me");
Eric Andersen8ea28be2001-01-05 20:58:22 +0000488 tmp = close_me_head;
489 close_me_head = close_me_head->next;
490 free(tmp);
491}
492
493static void close_all()
494{
Eric Andersen702ec592001-03-06 22:17:29 +0000495 struct close_me *c, *tmp;
496 for (c=close_me_head; c; c=tmp) {
497 close(c->fd);
498 tmp=c->next;
499 free(c);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000500 }
501 close_me_head = NULL;
502}
503
504
Eric Andersen37032b42004-02-10 01:28:36 +0000505#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen3522eb12000-03-12 23:49:18 +0000506/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000507static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000508{
Erik Andersen161220c2000-03-16 08:12:48 +0000509 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000510 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000511
Eric Andersen86349772000-12-18 20:25:50 +0000512 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000513 free(cmd->progs[i].argv);
Eric Andersenff9ad472004-02-10 01:07:45 +0000514#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000515 if (cmd->progs[i].redirects)
516 free(cmd->progs[i].redirects);
Eric Andersenff9ad472004-02-10 01:07:45 +0000517#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000518 }
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000519 free(cmd->progs);
520 free(cmd->text);
521 free(cmd->cmdbuf);
Mark Whitley44a99142001-03-14 17:26:37 +0000522 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000523 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000524 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000525}
526
Eric Andersen1ca20a72001-03-21 07:34:27 +0000527/* remove a job from a jobset */
528static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000529{
Eric Andersen86349772000-12-18 20:25:50 +0000530 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000531
Eric Andersen86349772000-12-18 20:25:50 +0000532 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000533 if (job == j_list->head) {
534 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000535 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000536 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000537 while (prevjob->next != job)
538 prevjob = prevjob->next;
539 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000540 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000541
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000542 if (j_list->head)
543 last_jobid = j_list->head->jobid;
544 else
545 last_jobid = 0;
546
Erik Andersen161220c2000-03-16 08:12:48 +0000547 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000548}
549
Eric Andersenff9ad472004-02-10 01:07:45 +0000550/* Checks to see if any background processes have exited -- if they
Erik Andersen3522eb12000-03-12 23:49:18 +0000551 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000552static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000553{
Erik Andersen161220c2000-03-16 08:12:48 +0000554 struct job *job;
555 pid_t childpid;
556 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000557 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000558
Erik Andersen161220c2000-03-16 08:12:48 +0000559 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000560 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000561 prognum = 0;
562 while (prognum < job->num_progs &&
563 job->progs[prognum].pid != childpid) prognum++;
564 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000565 break;
566 }
567
Eric Andersena1d187a2000-07-17 19:14:41 +0000568 /* This happens on backticked commands */
569 if(job==NULL)
570 return;
571
Erik Andersen161220c2000-03-16 08:12:48 +0000572 if (WIFEXITED(status) || WIFSIGNALED(status)) {
573 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000574 job->running_progs--;
575 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000576
Eric Andersen86349772000-12-18 20:25:50 +0000577 if (!job->running_progs) {
578 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000579 last_jobid=0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000580 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000581 }
582 } else {
583 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000584 job->stopped_progs++;
585 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000586
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000587#if 0
588 /* Printing this stuff is a pain, since it tends to
589 * overwrite the prompt an inconveinient moments. So
590 * don't do that. */
Eric Andersen86349772000-12-18 20:25:50 +0000591 if (job->stopped_progs == job->num_progs) {
592 printf(JOB_STATUS_FORMAT, job->jobid, "Stopped",
Erik Andersen161220c2000-03-16 08:12:48 +0000593 job->text);
594 }
Eric Andersenff9ad472004-02-10 01:07:45 +0000595#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000596 }
Erik Andersend75af992000-03-16 08:09:09 +0000597 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000598
Erik Andersen161220c2000-03-16 08:12:48 +0000599 if (childpid == -1 && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000600 bb_perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000601}
Eric Andersen37032b42004-02-10 01:28:36 +0000602#else
603static void checkjobs(struct jobset *j_list)
604{
605}
606static void free_job(struct job *cmd)
607{
608}
609static void remove_job(struct jobset *j_list, struct job *job)
610{
611}
612#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000613
Eric Andersenff9ad472004-02-10 01:07:45 +0000614#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersene9f07fb2000-12-21 18:31:36 +0000615/* squirrel != NULL means we squirrel away copies of stdin, stdout,
616 * and stderr if they are redirected. */
617static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000618{
619 int i;
620 int openfd;
621 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000622 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000623
Eric Andersen86349772000-12-18 20:25:50 +0000624 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000625 switch (redir->type) {
626 case REDIRECT_INPUT:
627 mode = O_RDONLY;
628 break;
629 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000630 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000631 break;
632 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000633 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000634 break;
635 }
636
637 openfd = open(redir->filename, mode, 0666);
638 if (openfd < 0) {
639 /* this could get lost if stderr has been redirected, but
640 bash and ash both lose it as well (though zsh doesn't!) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000641 bb_perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000642 return 1;
643 }
644
645 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000646 if (squirrel && redir->fd < 3) {
647 squirrel[redir->fd] = dup(redir->fd);
Eric Andersenacd647c2004-08-16 08:38:34 +0000648 fcntl (squirrel[redir->fd], F_SETFD, FD_CLOEXEC);
Eric Andersene9f07fb2000-12-21 18:31:36 +0000649 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000650 dup2(openfd, redir->fd);
651 close(openfd);
652 }
653 }
654
655 return 0;
656}
657
Eric Andersene9f07fb2000-12-21 18:31:36 +0000658static void restore_redirects(int squirrel[])
659{
660 int i, fd;
661 for (i=0; i<3; i++) {
662 fd = squirrel[i];
663 if (fd != -1) {
664 /* No error checking. I sure wouldn't know what
665 * to do with an error if I found one! */
666 dup2(fd, i);
667 close(fd);
668 }
669 }
670}
Eric Andersenff9ad472004-02-10 01:07:45 +0000671#else
672static inline int setup_redirects(struct child_prog *prog, int squirrel[])
673{
674 return 0;
675}
676static inline void restore_redirects(int squirrel[])
677{
678}
679#endif
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000680
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000681static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000682{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000683#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000684 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000685#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000686 PS1 = getenv("PS1");
Eric Andersene5dfced2001-04-09 22:48:12 +0000687 if(PS1==0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000688 PS1 = "\\w \\$ ";
Eric Andersenff9ad472004-02-10 01:07:45 +0000689#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000690}
691
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000692static inline void setup_prompt_string(char **prompt_str)
693{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000694#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000695 /* Set up the prompt */
696 if (shell_context == 0) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000697 free(PS1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000698 PS1=xmalloc(strlen(cwd)+4);
699 sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
700 *prompt_str = PS1;
701 } else {
702 *prompt_str = PS2;
703 }
704#else
705 *prompt_str = (shell_context==0)? PS1 : PS2;
Eric Andersenff9ad472004-02-10 01:07:45 +0000706#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000707}
Eric Andersen22332fd2001-01-30 23:40:39 +0000708
Eric Andersen09acc062001-01-04 11:10:38 +0000709static int get_command(FILE * source, char *command)
710{
711 char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000712
Eric Andersen1c314ad2000-06-28 16:56:25 +0000713 if (source == NULL) {
714 if (local_pending_command) {
715 /* a command specified (-c option): return it & mark it done */
716 strcpy(command, local_pending_command);
717 free(local_pending_command);
718 local_pending_command = NULL;
719 return 0;
720 }
721 return 1;
722 }
723
Erik Andersen161220c2000-03-16 08:12:48 +0000724 if (source == stdin) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000725 setup_prompt_string(&prompt_str);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000726
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000727#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000728 /*
729 ** enable command line editing only while a command line
730 ** is actually being read; otherwise, we'll end up bequeathing
731 ** atexit() handlers and other unwanted stuff to our
732 ** child processes (rob@sysgo.de)
733 */
Eric Andersen86349772000-12-18 20:25:50 +0000734 cmdedit_read_input(prompt_str, command);
Erik Andersen161220c2000-03-16 08:12:48 +0000735 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000736#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000737 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000738#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000739 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000740
Erik Andersen161220c2000-03-16 08:12:48 +0000741 if (!fgets(command, BUFSIZ - 2, source)) {
742 if (source == stdin)
743 printf("\n");
744 return 1;
745 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000746
Erik Andersen161220c2000-03-16 08:12:48 +0000747 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000748}
749
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000750static char* itoa(register int i)
751{
752 static char a[7]; /* Max 7 ints */
753 register char *b = a + sizeof(a) - 1;
754 int sign = (i < 0);
755
756 if (sign)
757 i = -i;
758 *b = 0;
759 do
760 {
761 *--b = '0' + (i % 10);
762 i /= 10;
763 }
764 while (i);
765 if (sign)
766 *--b = '-';
767 return b;
768}
769
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +0000770static char * strsep_space( char *string, int * ix)
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000771{
772 char *token, *begin;
773
774 begin = string;
775
776 /* Short circuit the trivial case */
777 if ( !string || ! string[*ix])
778 return NULL;
779
780 /* Find the end of the token. */
781 while( string && string[*ix] && !isspace(string[*ix]) ) {
782 (*ix)++;
783 }
784
Eric Andersenff9ad472004-02-10 01:07:45 +0000785 /* Find the end of any whitespace trailing behind
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000786 * the token and let that be part of the token */
787 while( string && string[*ix] && isspace(string[*ix]) ) {
788 (*ix)++;
789 }
790
791 if (! string && *ix==0) {
792 /* Nothing useful was found */
793 return NULL;
794 }
795
796 token = xmalloc(*ix+1);
797 token[*ix] = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000798 strncpy(token, string, *ix);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000799
800 return token;
801}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000802
Eric Andersenca604592001-03-08 17:17:13 +0000803static int expand_arguments(char *command)
804{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000805 int total_length=0, length, i, retval, ix = 0;
806 expand_t expand_result;
807 char *tmpcmd, *cmd, *cmd_copy;
808 char *src, *dst, *var;
809 const char *out_of_space = "out of space during expansion";
810 int flags = GLOB_NOCHECK
811#ifdef GLOB_BRACE
812 | GLOB_BRACE
Eric Andersenff9ad472004-02-10 01:07:45 +0000813#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000814#ifdef GLOB_TILDE
815 | GLOB_TILDE
Eric Andersenff9ad472004-02-10 01:07:45 +0000816#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000817 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000818
Eric Andersenca604592001-03-08 17:17:13 +0000819 /* get rid of the terminating \n */
820 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000821
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000822 /* Fix up escape sequences to be the Real Thing(tm) */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000823 while( command && command[ix]) {
824 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000825 const char *tmp = command+ix+1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000826 command[ix] = bb_process_escape_sequence( &tmp );
Eric Andersen1ca20a72001-03-21 07:34:27 +0000827 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000828 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000829 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000830 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000831 /* Use glob and then fixup environment variables and such */
832
833 /* It turns out that glob is very stupid. We have to feed it one word at a
834 * time since it can't cope with a full string. Here we convert command
835 * (char*) into cmd (char**, one word per string) */
836
837 /* We need a clean copy, so strsep can mess up the copy while
838 * we write stuff into the original (in a minute) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000839 cmd = cmd_copy = bb_xstrdup(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000840 *command = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000841 for (ix = 0, tmpcmd = cmd;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000842 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
843 if (*tmpcmd == '\0')
844 break;
845 /* we need to trim() the result for glob! */
846 trim(tmpcmd);
847 retval = glob(tmpcmd, flags, NULL, &expand_result);
848 free(tmpcmd); /* Free mem allocated by strsep_space */
849 if (retval == GLOB_NOSPACE) {
850 /* Mem may have been allocated... */
851 globfree (&expand_result);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000852 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000853 return FALSE;
854 } else if (retval != 0) {
855 /* Some other error. GLOB_NOMATCH shouldn't
Eric Andersenff9ad472004-02-10 01:07:45 +0000856 * happen because of the GLOB_NOCHECK flag in
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000857 * the glob call. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000858 bb_error_msg("syntax error");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000859 return FALSE;
860 } else {
861 /* Convert from char** (one word per string) to a simple char*,
862 * but don't overflow command which is BUFSIZ in length */
863 for (i=0; i < expand_result.gl_pathc; i++) {
864 length=strlen(expand_result.gl_pathv[i]);
865 if (total_length+length+1 >= BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000866 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000867 return FALSE;
868 }
869 strcat(command+total_length, " ");
870 total_length+=1;
871 strcat(command+total_length, expand_result.gl_pathv[i]);
872 total_length+=length;
873 }
874 globfree (&expand_result);
875 }
876 }
877 free(cmd_copy);
878 trim(command);
879
Eric Andersenff9ad472004-02-10 01:07:45 +0000880 /* Now do the shell variable substitutions which
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000881 * wordexp can't do for us, namely $? and $! */
882 src = command;
883 while((dst = strchr(src,'$')) != NULL){
884 var = NULL;
885 switch(*(dst+1)) {
886 case '?':
887 var = itoa(last_return_code);
888 break;
889 case '!':
890 if (last_bg_pid==-1)
891 *(var)='\0';
892 else
893 var = itoa(last_bg_pid);
894 break;
895 /* Everything else like $$, $#, $[0-9], etc. should all be
896 * expanded by wordexp(), so we can in theory skip that stuff
897 * here, but just to be on the safe side (i.e., since uClibc
898 * wordexp doesn't do this stuff yet), lets leave it in for
899 * now. */
900 case '$':
901 var = itoa(getpid());
902 break;
903 case '#':
904 var = itoa(argc-1);
905 break;
906 case '0':case '1':case '2':case '3':case '4':
907 case '5':case '6':case '7':case '8':case '9':
908 {
Eric Andersen7ab93d92002-11-08 09:40:02 +0000909 int ixx=*(dst+1)-48+1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000910 if (ixx >= argc) {
911 var='\0';
912 } else {
913 var = argv[ixx];
914 }
915 }
916 break;
917
918 }
919 if (var) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000920 /* a single character construction was found, and
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000921 * already handled in the case statement */
922 src=dst+2;
923 } else {
924 /* Looks like an environment variable */
925 char delim_hold;
926 int num_skip_chars=0;
927 int dstlen = strlen(dst);
928 /* Is this a ${foo} type variable? */
929 if (dstlen >=2 && *(dst+1) == '{') {
930 src=strchr(dst+1, '}');
931 num_skip_chars=1;
932 } else {
933 src=dst+1;
934 while(isalnum(*src) || *src=='_') src++;
935 }
936 if (src == NULL) {
937 src = dst+dstlen;
938 }
939 delim_hold=*src;
940 *src='\0'; /* temporary */
941 var = getenv(dst + 1 + num_skip_chars);
942 *src=delim_hold;
943 src += num_skip_chars;
944 }
945 if (var == NULL) {
946 /* Seems we got an un-expandable variable. So delete it. */
947 var = "";
948 }
949 {
950 int subst_len = strlen(var);
951 int trail_len = strlen(src);
952 if (dst+subst_len+trail_len >= command+BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000953 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000954 return FALSE;
955 }
956 /* Move stuff to the end of the string to accommodate
957 * filling the created gap with the new stuff */
958 memmove(dst+subst_len, src, trail_len+1);
959 /* Now copy in the new stuff */
960 memcpy(dst, var, subst_len);
961 src = dst+subst_len;
962 }
963 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000964
Eric Andersenca604592001-03-08 17:17:13 +0000965 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000966}
967
Eric Andersen86349772000-12-18 20:25:50 +0000968/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
969 line). If a valid command is found, command_ptr is set to point to
Eric Andersenff9ad472004-02-10 01:07:45 +0000970 the beginning of the next command (if the original command had more
971 then one job associated with it) or NULL if no more commands are
Erik Andersen3522eb12000-03-12 23:49:18 +0000972 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000973static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000974{
Erik Andersen161220c2000-03-16 08:12:48 +0000975 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000976 char *return_command = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +0000977 char *src, *buf;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000978 int argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000979 int done = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000980 int argv_alloced;
Eric Andersenff9ad472004-02-10 01:07:45 +0000981 int saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000982 char quote = '\0';
983 int count;
Eric Andersen86349772000-12-18 20:25:50 +0000984 struct child_prog *prog;
Eric Andersenff9ad472004-02-10 01:07:45 +0000985#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
986 int i;
987 char *chptr;
988#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000989
Erik Andersen161220c2000-03-16 08:12:48 +0000990 /* skip leading white space */
Eric Andersen86349772000-12-18 20:25:50 +0000991 while (**command_ptr && isspace(**command_ptr))
992 (*command_ptr)++;
Erik Andersen3522eb12000-03-12 23:49:18 +0000993
Erik Andersen161220c2000-03-16 08:12:48 +0000994 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000995 if (!**command_ptr || (**command_ptr == '#')) {
996 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000997 return 0;
998 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000999
Eric Andersen86349772000-12-18 20:25:50 +00001000 *inbg = 0;
1001 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +00001002 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +00001003
Eric Andersenff9ad472004-02-10 01:07:45 +00001004 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +00001005 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +00001006 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +00001007
Eric Andersenff9ad472004-02-10 01:07:45 +00001008 Getting clean memory relieves us of the task of NULL
1009 terminating things and makes the rest of this look a bit
Erik Andersen161220c2000-03-16 08:12:48 +00001010 cleaner (though it is, admittedly, a tad less efficient) */
Eric Andersen86349772000-12-18 20:25:50 +00001011 job->cmdbuf = command = xcalloc(2*strlen(*command_ptr) + 1, sizeof(char));
Erik Andersen161220c2000-03-16 08:12:48 +00001012 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001013
Erik Andersen161220c2000-03-16 08:12:48 +00001014 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +00001015 prog->num_redirects = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001016 prog->is_stopped = 0;
1017 prog->family = job;
Eric Andersenff9ad472004-02-10 01:07:45 +00001018#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
1019 prog->redirects = NULL;
1020#endif
Erik Andersen3522eb12000-03-12 23:49:18 +00001021
Eric Andersen86349772000-12-18 20:25:50 +00001022 argv_alloced = 5;
1023 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
1024 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +00001025
Erik Andersen161220c2000-03-16 08:12:48 +00001026 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +00001027 src = *command_ptr;
Erik Andersen161220c2000-03-16 08:12:48 +00001028 while (*src && !done) {
1029 if (quote == *src) {
1030 quote = '\0';
1031 } else if (quote) {
1032 if (*src == '\\') {
1033 src++;
1034 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001035 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001036 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001037 return 1;
1038 }
1039
1040 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +00001041 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +00001042 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +00001043 *buf++ = '\\';
1044 }
Erik Andersen161220c2000-03-16 08:12:48 +00001045 } else if (*src == '*' || *src == '?' || *src == '[' ||
1046 *src == ']') *buf++ = '\\';
1047 *buf++ = *src;
1048 } else if (isspace(*src)) {
Matt Kraaibe66ad32001-04-12 15:42:17 +00001049 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001050 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001051 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +00001052 if ((argc_l + 1) == argv_alloced) {
1053 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +00001054 prog->argv = xrealloc(prog->argv,
1055 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +00001056 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001057 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001058 prog->argv[argc_l] = buf;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001059 saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001060 }
1061 } else
1062 switch (*src) {
1063 case '"':
1064 case '\'':
1065 quote = *src;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001066 saw_quote = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001067 break;
1068
1069 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001070 if (*(src-1)== '$')
1071 *buf++ = *src;
1072 else
1073 done = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001074 break;
1075
Eric Andersenff9ad472004-02-10 01:07:45 +00001076#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +00001077 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +00001078 case '<':
Eric Andersen86349772000-12-18 20:25:50 +00001079 i = prog->num_redirects++;
1080 prog->redirects = xrealloc(prog->redirects,
1081 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +00001082 (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +00001083
Eric Andersen86349772000-12-18 20:25:50 +00001084 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001085 if (buf != prog->argv[argc_l]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001086 /* the stuff before this character may be the file number
Erik Andersen161220c2000-03-16 08:12:48 +00001087 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +00001088 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001089 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +00001090
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001091 if (*chptr && *prog->argv[argc_l]) {
1092 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001093 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001094 }
1095 }
1096
Eric Andersen86349772000-12-18 20:25:50 +00001097 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001098 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001099 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001100 else
Eric Andersen86349772000-12-18 20:25:50 +00001101 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001102 }
1103
1104 if (*src++ == '>') {
1105 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001106 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001107 REDIRECT_APPEND, src++;
1108 else
Eric Andersen86349772000-12-18 20:25:50 +00001109 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001110 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001111 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001112 }
1113
1114 /* This isn't POSIX sh compliant. Oh well. */
1115 chptr = src;
1116 while (isspace(*chptr))
1117 chptr++;
1118
1119 if (!*chptr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001120 bb_error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001121 free_job(job);
1122 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001123 return 1;
1124 }
1125
Eric Andersen86349772000-12-18 20:25:50 +00001126 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001127 while (*chptr && !isspace(*chptr))
1128 *buf++ = *chptr++;
1129
1130 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001131 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001132 break;
1133
1134 case '|': /* pipe */
1135 /* finish this command */
Matt Kraaibe66ad32001-04-12 15:42:17 +00001136 if (*prog->argv[argc_l] || saw_quote)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001137 argc_l++;
1138 if (!argc_l) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001139 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001140 free_job(job);
1141 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001142 return 1;
1143 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001144 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001145
1146 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001147 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001148 job->progs = xrealloc(job->progs,
Eric Andersen86349772000-12-18 20:25:50 +00001149 sizeof(*job->progs) * job->num_progs);
1150 prog = job->progs + (job->num_progs - 1);
1151 prog->num_redirects = 0;
1152 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001153 prog->is_stopped = 0;
1154 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001155 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001156
Eric Andersen86349772000-12-18 20:25:50 +00001157 argv_alloced = 5;
1158 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001159 prog->argv[0] = ++buf;
1160
1161 src++;
1162 while (*src && isspace(*src))
1163 src++;
1164
1165 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001166 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001167 free_job(job);
1168 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001169 return 1;
1170 }
1171 src--; /* we'll ++ it at the end of the loop */
1172
1173 break;
Eric Andersenff9ad472004-02-10 01:07:45 +00001174#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001175
Eric Andersen37032b42004-02-10 01:28:36 +00001176#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen161220c2000-03-16 08:12:48 +00001177 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001178 *inbg = 1;
Eric Andersen37032b42004-02-10 01:28:36 +00001179#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001180 case ';': /* multiple commands */
1181 done = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001182 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001183 break;
1184
Matt Kraai131241f2000-09-14 00:43:20 +00001185 case '\\':
1186 src++;
1187 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001188 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001189 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001190 return 1;
1191 }
1192 if (*src == '*' || *src == '[' || *src == ']'
1193 || *src == '?') *buf++ = '\\';
1194 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001195 default:
1196 *buf++ = *src;
1197 }
1198
Erik Andersend75af992000-03-16 08:09:09 +00001199 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001200 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001201
Matt Kraaibe66ad32001-04-12 15:42:17 +00001202 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001203 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001204 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001205 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001206 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001207 return 0;
1208 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001209 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001210
Eric Andersen86349772000-12-18 20:25:50 +00001211 if (!return_command) {
1212 job->text = xmalloc(strlen(*command_ptr) + 1);
1213 strcpy(job->text, *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001214 } else {
1215 /* This leaves any trailing spaces, which is a bit sloppy */
Eric Andersen86349772000-12-18 20:25:50 +00001216 count = return_command - *command_ptr;
Eric Andersenec10b9d2000-07-14 01:13:11 +00001217 job->text = xmalloc(count + 1);
Eric Andersen86349772000-12-18 20:25:50 +00001218 strncpy(job->text, *command_ptr, count);
Erik Andersen161220c2000-03-16 08:12:48 +00001219 job->text[count] = '\0';
1220 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001221
Eric Andersen86349772000-12-18 20:25:50 +00001222 *command_ptr = return_command;
Eric Andersenff9ad472004-02-10 01:07:45 +00001223
Erik Andersend75af992000-03-16 08:09:09 +00001224 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001225}
1226
Eric Andersen86349772000-12-18 20:25:50 +00001227/* Run the child_prog, no matter what kind of command it uses.
1228 */
1229static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001230{
Eric Andersen86349772000-12-18 20:25:50 +00001231 struct built_in_command *x;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001232#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
Eric Andersenaf4ac772001-02-01 22:43:49 +00001233 char *name;
Erik Andersenbcd61772000-05-13 06:33:19 +00001234#endif
Erik Andersen3522eb12000-03-12 23:49:18 +00001235
Eric Andersene9f07fb2000-12-21 18:31:36 +00001236 /* Check if the command matches any of the non-forking builtins.
1237 * Depending on context, this might be redundant. But it's
1238 * easier to waste a few CPU cycles than it is to figure out
1239 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001240 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001241 for (x = bltins; x->cmd; x++) {
1242 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001243 _exit(x->function(child));
Eric Andersene9f07fb2000-12-21 18:31:36 +00001244 }
1245 }
1246
1247 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001248 for (x = bltins_forking; x->cmd; x++) {
1249 if (strcmp(child->argv[0], x->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001250 bb_applet_name=x->cmd;
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001251 _exit (x->function(child));
Eric Andersen86349772000-12-18 20:25:50 +00001252 }
1253 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001254#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
Eric Andersen86349772000-12-18 20:25:50 +00001255 /* Check if the command matches any busybox internal
1256 * commands ("applets") here. Following discussions from
Eric Andersen2423b122001-12-08 01:56:15 +00001257 * November 2000 on busybox@busybox.net, don't use
Manuel Novoa III cad53642003-03-19 09:13:01 +00001258 * bb_get_last_path_component(). This way explicit (with
Eric Andersen86349772000-12-18 20:25:50 +00001259 * slashes) filenames will never be interpreted as an
1260 * applet, just like with builtins. This way the user can
1261 * override an applet with an explicit filename reference.
1262 * The only downside to this change is that an explicit
1263 * /bin/foo invocation will fork and exec /bin/foo, even if
1264 * /bin/foo is a symlink to busybox.
1265 */
Matt Kraaif2cc2762001-02-01 19:21:20 +00001266 name = child->argv[0];
Eric Andersen86349772000-12-18 20:25:50 +00001267
Eric Andersen67991cf2001-02-14 21:23:06 +00001268 {
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001269 char** argv_l=child->argv;
1270 int argc_l;
1271#ifdef _NEWLIB_VERSION
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001272 /* newlib uses __getopt_initialized for getopt() in
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001273 * addition to optind, see newlib/libc/sys/linux/getopt.c
1274 */
1275 extern int __getopt_initialized = 0;
1276#endif
1277 for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
1278 optind = 1;
1279 run_applet_by_name(name, argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001280 }
1281#endif
1282
1283 execvp(child->argv[0], child->argv);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001284
Eric Andersenff9ad472004-02-10 01:07:45 +00001285 /* Do not use bb_perror_msg_and_die() here, since we must not
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001286 * call exit() but should call _exit() instead */
Eric Andersenad082982002-09-30 20:20:20 +00001287 fprintf(stderr, "%s: %m\n", child->argv[0]);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001288 _exit(EXIT_FAILURE);
Eric Andersen86349772000-12-18 20:25:50 +00001289}
1290
1291static void insert_job(struct job *newjob, int inbg)
1292{
1293 struct job *thejob;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001294 struct jobset *j_list=newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001295
1296 /* find the ID for thejob to use */
1297 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001298 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001299 if (thejob->jobid >= newjob->jobid)
1300 newjob->jobid = thejob->jobid + 1;
1301
1302 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001303 if (!j_list->head) {
1304 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001305 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001306 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001307 thejob->next = xmalloc(sizeof(*thejob));
1308 thejob = thejob->next;
1309 }
1310
1311 *thejob = *newjob; /* physically copy the struct job */
1312 thejob->next = NULL;
1313 thejob->running_progs = thejob->num_progs;
1314 thejob->stopped_progs = 0;
1315
Eric Andersen37032b42004-02-10 01:28:36 +00001316#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen86349772000-12-18 20:25:50 +00001317 if (inbg) {
Eric Andersenff9ad472004-02-10 01:07:45 +00001318 /* we don't wait for background thejobs to return -- append it
Eric Andersen86349772000-12-18 20:25:50 +00001319 to the list of backgrounded thejobs and leave it alone */
1320 printf("[%d] %d\n", thejob->jobid,
1321 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001322 last_jobid = newjob->jobid;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001323 last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001324 } else {
1325 newjob->job_list->fg = thejob;
1326
1327 /* move the new process group into the foreground */
1328 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001329 if (tcsetpgrp(shell_terminal, newjob->pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001330 bb_perror_msg("tcsetpgrp");
Eric Andersen86349772000-12-18 20:25:50 +00001331 }
Eric Andersen37032b42004-02-10 01:28:36 +00001332#endif
Eric Andersen86349772000-12-18 20:25:50 +00001333}
1334
1335static int run_command(struct job *newjob, int inbg, int outpipe[2])
1336{
1337 /* struct job *thejob; */
1338 int i;
1339 int nextin, nextout;
1340 int pipefds[2]; /* pipefd[0] is for reading */
1341 struct built_in_command *x;
1342 struct child_prog *child;
1343
Eric Andersen6efc48c2000-07-18 08:16:39 +00001344 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001345 for (i = 0; i < newjob->num_progs; i++) {
1346 child = & (newjob->progs[i]);
1347
1348 if ((i + 1) < newjob->num_progs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001349 if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001350 nextout = pipefds[1];
1351 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001352 if (outpipe[1]!=-1) {
1353 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001354 } else {
1355 nextout = 1;
1356 }
Erik Andersen161220c2000-03-16 08:12:48 +00001357 }
1358
Eric Andersen501c88b2000-07-28 15:14:45 +00001359
Eric Andersene9f07fb2000-12-21 18:31:36 +00001360 /* Check if the command matches any non-forking builtins,
1361 * but only if this is a simple command.
1362 * Non-forking builtins within pipes have to fork anyway,
1363 * and are handled in pseudo_exec. "echo foo | read bar"
1364 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001365 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001366 if (newjob->num_progs == 1) {
1367 for (x = bltins; x->cmd; x++) {
1368 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersene9f07fb2000-12-21 18:31:36 +00001369 int rcode;
Eric Andersenff9ad472004-02-10 01:07:45 +00001370 int squirrel[] = {-1, -1, -1};
Eric Andersene9f07fb2000-12-21 18:31:36 +00001371 setup_redirects(child, squirrel);
1372 rcode = x->function(child);
1373 restore_redirects(squirrel);
1374 return rcode;
1375 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001376 }
1377 }
1378
Eric Andersene3efc922004-04-12 17:59:24 +00001379#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersenff9ad472004-02-10 01:07:45 +00001380 if (!(child->pid = fork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001381#else
Eric Andersenff9ad472004-02-10 01:07:45 +00001382 if (!(child->pid = vfork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001383#endif
1384 {
Eric Andersen2d848a42001-06-25 17:11:54 +00001385 /* Set the handling for job control signals back to the default. */
1386 signal(SIGINT, SIG_DFL);
1387 signal(SIGQUIT, SIG_DFL);
1388 signal(SIGTSTP, SIG_DFL);
1389 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001390 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001391 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001392
Eric Andersen8ea28be2001-01-05 20:58:22 +00001393 close_all();
1394
Eric Andersen86349772000-12-18 20:25:50 +00001395 if (outpipe[1]!=-1) {
1396 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001397 }
1398 if (nextin != 0) {
1399 dup2(nextin, 0);
1400 close(nextin);
1401 }
1402
1403 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001404 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001405 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001406 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001407 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001408 }
1409
Eric Andersen86349772000-12-18 20:25:50 +00001410 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001411 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001412
Eric Andersen86349772000-12-18 20:25:50 +00001413 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001414 }
Eric Andersen86349772000-12-18 20:25:50 +00001415 if (outpipe[1]!=-1) {
1416 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001417 }
Erik Andersen161220c2000-03-16 08:12:48 +00001418
1419 /* put our child in the process group whose leader is the
1420 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001421 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001422 if (nextin != 0)
1423 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001424 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001425 close(nextout);
1426
Eric Andersenff9ad472004-02-10 01:07:45 +00001427 /* If there isn't another process, nextin is garbage
Erik Andersen161220c2000-03-16 08:12:48 +00001428 but it doesn't matter */
1429 nextin = pipefds[0];
1430 }
1431
Eric Andersen86349772000-12-18 20:25:50 +00001432 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001433
Eric Andersen86349772000-12-18 20:25:50 +00001434 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001435
Erik Andersen161220c2000-03-16 08:12:48 +00001436 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001437}
1438
Erik Andersend75af992000-03-16 08:09:09 +00001439static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001440{
Erik Andersen161220c2000-03-16 08:12:48 +00001441 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001442 char *next_command = NULL;
1443 struct job newjob;
Erik Andersen161220c2000-03-16 08:12:48 +00001444 int i;
Eric Andersena68ea1c2006-01-30 22:48:39 +00001445 int inbg = 0;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001446 int status;
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001447#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersend20d3752004-03-12 22:08:42 +00001448 pid_t parent_pgrp;
1449 /* save current owner of TTY so we can restore it on exit */
1450 parent_pgrp = tcgetpgrp(shell_terminal);
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001451#endif
Eric Andersen86349772000-12-18 20:25:50 +00001452 newjob.job_list = &job_list;
1453 newjob.job_context = DEFAULT_CONTEXT;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001454
Matt Kraaib8907522000-09-13 02:08:21 +00001455 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Erik Andersend75af992000-03-16 08:09:09 +00001456
Erik Andersen161220c2000-03-16 08:12:48 +00001457 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001458 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001459 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001460
Erik Andersend75af992000-03-16 08:09:09 +00001461 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001462 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001463
Eric Andersen86349772000-12-18 20:25:50 +00001464 if (!next_command) {
1465 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001466 break;
Eric Andersen86349772000-12-18 20:25:50 +00001467 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001468 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001469
Matt Kraai1f0c4362001-12-20 23:13:26 +00001470 if (! expand_arguments(next_command)) {
Eric Andersenca604592001-03-08 17:17:13 +00001471 free(command);
1472 command = (char *) xcalloc(BUFSIZ, sizeof(char));
1473 next_command = NULL;
1474 continue;
1475 }
1476
Eric Andersen86349772000-12-18 20:25:50 +00001477 if (!parse_command(&next_command, &newjob, &inbg) &&
1478 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001479 int pipefds[2] = {-1,-1};
Eric Andersenff9ad472004-02-10 01:07:45 +00001480 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001481 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001482 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001483 }
1484 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001485 free(command);
Matt Kraaib8907522000-09-13 02:08:21 +00001486 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Eric Andersen86349772000-12-18 20:25:50 +00001487 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001488 }
1489 } else {
1490 /* a job is running in the foreground; wait for it */
1491 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001492 while (!job_list.fg->progs[i].pid ||
1493 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001494
Eric Andersen7373e482002-07-31 04:04:47 +00001495 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) {
1496 if (errno != ECHILD) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001497 bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
Eric Andersen7373e482002-07-31 04:04:47 +00001498 }
1499 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001500
Erik Andersend75af992000-03-16 08:09:09 +00001501 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001502 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001503 job_list.fg->running_progs--;
1504 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001505
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001506 last_return_code=WEXITSTATUS(status);
1507
Eric Andersen86349772000-12-18 20:25:50 +00001508 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001509 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001510 remove_job(&job_list, job_list.fg);
1511 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001512 }
Eric Andersen37032b42004-02-10 01:28:36 +00001513 }
1514#ifdef CONFIG_LASH_JOB_CONTROL
1515 else {
Erik Andersen161220c2000-03-16 08:12:48 +00001516 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001517 job_list.fg->stopped_progs++;
1518 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001519
Eric Andersen86349772000-12-18 20:25:50 +00001520 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1521 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1522 "Stopped", job_list.fg->text);
1523 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001524 }
Erik Andersend75af992000-03-16 08:09:09 +00001525 }
1526
Eric Andersen86349772000-12-18 20:25:50 +00001527 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001528 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001529 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001530 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Eric Andersenff9ad472004-02-10 01:07:45 +00001531 bb_perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001532 }
Eric Andersen37032b42004-02-10 01:28:36 +00001533#endif
Erik Andersend75af992000-03-16 08:09:09 +00001534 }
1535 }
Erik Andersen161220c2000-03-16 08:12:48 +00001536 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001537
Eric Andersen37032b42004-02-10 01:28:36 +00001538#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen1c314ad2000-06-28 16:56:25 +00001539 /* return controlling TTY back to parent process group before exiting */
Eric Andersen7373e482002-07-31 04:04:47 +00001540 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001541 bb_perror_msg("tcsetpgrp");
Eric Andersen37032b42004-02-10 01:28:36 +00001542#endif
Eric Andersenb54833c2000-07-03 23:56:26 +00001543
1544 /* return exit status if called with "-c" */
1545 if (input == NULL && WIFEXITED(status))
1546 return WEXITSTATUS(status);
Eric Andersenff9ad472004-02-10 01:07:45 +00001547
Erik Andersen161220c2000-03-16 08:12:48 +00001548 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001549}
1550
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001551#ifdef CONFIG_FEATURE_CLEAN_UP
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +00001552static void free_memory(void)
Eric Andersenfad9c112000-07-25 18:06:52 +00001553{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001554 if (cwd && cwd!=bb_msg_unknown) {
Eric Andersen5d60a462001-08-22 05:32:24 +00001555 free((char*)cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001556 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001557 if (local_pending_command)
1558 free(local_pending_command);
1559
Eric Andersen86349772000-12-18 20:25:50 +00001560 if (job_list.fg && !job_list.fg->running_progs) {
1561 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001562 }
1563}
1564#endif
1565
Eric Andersen37032b42004-02-10 01:28:36 +00001566#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen2d848a42001-06-25 17:11:54 +00001567/* Make sure we have a controlling tty. If we get started under a job
1568 * aware app (like bash for example), make sure we are now in charge so
1569 * we don't fight over who gets the foreground */
Eric Andersenece8fc22002-12-11 04:26:28 +00001570static void setup_job_control(void)
Eric Andersen2d848a42001-06-25 17:11:54 +00001571{
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001572 int status;
Eric Andersen37032b42004-02-10 01:28:36 +00001573 pid_t shell_pgrp;
Eric Andersenff9ad472004-02-10 01:07:45 +00001574
Eric Andersen2d848a42001-06-25 17:11:54 +00001575 /* Loop until we are in the foreground. */
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001576 while ((status = tcgetpgrp (shell_terminal)) >= 0) {
1577 if (status == (shell_pgrp = getpgrp ())) {
1578 break;
1579 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001580 kill (- shell_pgrp, SIGTTIN);
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001581 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001582
1583 /* Ignore interactive and job-control signals. */
1584 signal(SIGINT, SIG_IGN);
1585 signal(SIGQUIT, SIG_IGN);
1586 signal(SIGTSTP, SIG_IGN);
1587 signal(SIGTTIN, SIG_IGN);
1588 signal(SIGTTOU, SIG_IGN);
1589 signal(SIGCHLD, SIG_IGN);
1590
1591 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001592 setsid();
Eric Andersen2d848a42001-06-25 17:11:54 +00001593 shell_pgrp = getpid ();
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001594 setpgid (shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001595
1596 /* Grab control of the terminal. */
1597 tcsetpgrp(shell_terminal, shell_pgrp);
1598}
Eric Andersen37032b42004-02-10 01:28:36 +00001599#else
1600static inline void setup_job_control(void)
1601{
1602}
1603#endif
Eric Andersen6efc48c2000-07-18 08:16:39 +00001604
Matt Kraai2d91deb2001-08-01 17:21:35 +00001605int lash_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001606{
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001607 int opt, interactive=FALSE;
Erik Andersen161220c2000-03-16 08:12:48 +00001608 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001609 argc = argc_l;
1610 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001611
Eric Andersen702ec592001-03-06 22:17:29 +00001612 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001613 last_jobid = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001614 local_pending_command = NULL;
Eric Andersen702ec592001-03-06 22:17:29 +00001615 close_me_head = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001616 job_list.head = NULL;
1617 job_list.fg = NULL;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001618 last_return_code=1;
Eric Andersen501c88b2000-07-28 15:14:45 +00001619
Eric Andersenb558e762000-11-30 22:43:16 +00001620 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001621 FILE *prof_input;
1622 prof_input = fopen("/etc/profile", "r");
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001623 if (prof_input) {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001624 int tmp_fd = fileno(prof_input);
Eric Andersenff9ad472004-02-10 01:07:45 +00001625 mark_open(tmp_fd);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001626 /* Now run the file */
1627 busy_loop(prof_input);
1628 fclose(prof_input);
1629 mark_closed(tmp_fd);
1630 }
Eric Andersenb558e762000-11-30 22:43:16 +00001631 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001632
Eric Andersenf21aa842000-12-08 20:50:30 +00001633 while ((opt = getopt(argc_l, argv_l, "cxi")) > 0) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001634 switch (opt) {
1635 case 'c':
1636 input = NULL;
Matt Kraai6085c722000-09-06 01:46:18 +00001637 if (local_pending_command != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001638 bb_error_msg_and_die("multiple -c arguments");
1639 local_pending_command = bb_xstrdup(argv[optind]);
Matt Kraai6085c722000-09-06 01:46:18 +00001640 optind++;
1641 argv = argv+optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001642 break;
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001643 case 'i':
1644 interactive = TRUE;
1645 break;
Eric Andersen501c88b2000-07-28 15:14:45 +00001646 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001647 bb_show_usage();
Eric Andersen501c88b2000-07-28 15:14:45 +00001648 }
1649 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001650 /* A shell is interactive if the `-i' flag was given, or if all of
1651 * the following conditions are met:
1652 * no -c command
1653 * no arguments remaining or the -s flag given
1654 * standard input is a terminal
1655 * standard output is a terminal
1656 * Refer to Posix.2, the description of the `sh' utility. */
Eric Andersen86349772000-12-18 20:25:50 +00001657 if (argv[optind]==NULL && input==stdin &&
Eric Andersen70060d22004-03-27 10:02:48 +00001658 isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
Eric Andersen86349772000-12-18 20:25:50 +00001659 interactive=TRUE;
1660 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001661 setup_job_control();
Eric Andersen86349772000-12-18 20:25:50 +00001662 if (interactive==TRUE) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001663 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Eric Andersen501c88b2000-07-28 15:14:45 +00001664 /* Looks like they want an interactive shell */
Eric Andersen37032b42004-02-10 01:28:36 +00001665#ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +00001666 printf( "\n\n%s Built-in shell (lash)\n", BB_BANNER);
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001667 printf( "Enter 'help' for a list of built-in commands.\n\n");
Eric Andersend63dee42001-10-19 00:22:23 +00001668#endif
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001669 } else if (local_pending_command==NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001670 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001671 input = bb_xfopen(argv[optind], "r");
Eric Andersen8ea28be2001-01-05 20:58:22 +00001672 mark_open(fileno(input)); /* be lazy, never mark this closed */
Eric Andersen501c88b2000-07-28 15:14:45 +00001673 }
1674
Eric Andersen6efc48c2000-07-18 08:16:39 +00001675 /* initialize the cwd -- this is never freed...*/
Eric Andersene5dfced2001-04-09 22:48:12 +00001676 cwd = xgetcwd(0);
Eric Andersen5f265b72001-05-11 16:58:46 +00001677 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001678 cwd = bb_msg_unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001679
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001680#ifdef CONFIG_FEATURE_CLEAN_UP
Eric Andersenfad9c112000-07-25 18:06:52 +00001681 atexit(free_memory);
1682#endif
1683
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001684#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001685 cmdedit_set_initial_prompt();
1686#else
1687 PS1 = NULL;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001688#endif
Eric Andersenff9ad472004-02-10 01:07:45 +00001689
Erik Andersen161220c2000-03-16 08:12:48 +00001690 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001691}