blob: 1fcd63bdf5f731d6af2c55257a049a07913daa59 [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 Andersencb81e642003-07-14 21:21:08 +00005 * Copyright (C) 1999-2003 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 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
Eric Andersenbdfd0d72001-10-24 05:00:29 +000027/* This shell's parsing engine is officially at a dead-end. Future
28 * work shell work should be done using hush, msh, or ash. This is
29 * still a very useful, small shell -- it just don't need any more
30 * features beyond what it already has...
Eric Andersen8ea28be2001-01-05 20:58:22 +000031 */
Eric Andersen8a646dd2001-06-21 16:38:11 +000032
Eric Andersen1e7cea92000-12-06 23:47:38 +000033//For debugging/development on the shell only...
Eric Andersen501c88b2000-07-28 15:14:45 +000034//#define DEBUG_SHELL
Eric Andersena1d187a2000-07-17 19:14:41 +000035
36
Erik Andersen3522eb12000-03-12 23:49:18 +000037#include <stdio.h>
38#include <stdlib.h>
39#include <ctype.h>
40#include <errno.h>
41#include <fcntl.h>
Erik Andersen3522eb12000-03-12 23:49:18 +000042#include <signal.h>
43#include <string.h>
44#include <sys/ioctl.h>
45#include <sys/wait.h>
46#include <unistd.h>
Eric Andersen501c88b2000-07-28 15:14:45 +000047#include <getopt.h>
Eric Andersen2d848a42001-06-25 17:11:54 +000048#include <termios.h>
Mark Whitley4b541a82001-04-25 17:10:30 +000049#include "busybox.h"
50#include "cmdedit.h"
Eric Andersene5dfced2001-04-09 22:48:12 +000051
Eric Andersenbdfd0d72001-10-24 05:00:29 +000052#ifdef CONFIG_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +000053#include <locale.h>
Eric Andersene5dfced2001-04-09 22:48:12 +000054#endif
Eric Andersen13d1fa12001-03-08 23:59:45 +000055
Eric Andersen13d1fa12001-03-08 23:59:45 +000056#include <glob.h>
57#define expand_t glob_t
Erik Andersen3522eb12000-03-12 23:49:18 +000058
Eric Andersen13d1fa12001-03-08 23:59:45 +000059
Mark Whitley59ab0252001-01-23 22:30:04 +000060static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000061#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
62
Erik Andersend75af992000-03-16 08:09:09 +000063
Eric Andersen86349772000-12-18 20:25:50 +000064enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000065 REDIRECT_APPEND
66};
Erik Andersen3522eb12000-03-12 23:49:18 +000067
Eric Andersen86349772000-12-18 20:25:50 +000068static const unsigned int DEFAULT_CONTEXT=0x1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +000069static const unsigned int IF_TRUE_CONTEXT=0x2;
70static const unsigned int IF_FALSE_CONTEXT=0x4;
71static const unsigned int THEN_EXP_CONTEXT=0x8;
72static const unsigned int ELSE_EXP_CONTEXT=0x10;
Eric Andersenfad9c112000-07-25 18:06:52 +000073
Eric Andersen86349772000-12-18 20:25:50 +000074
75struct jobset {
Erik Andersen161220c2000-03-16 08:12:48 +000076 struct job *head; /* head of list of running jobs */
77 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000078};
79
Eric Andersen86349772000-12-18 20:25:50 +000080struct redir_struct {
81 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000082 int fd; /* file descriptor being redirected */
83 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000084};
85
Eric Andersen86349772000-12-18 20:25:50 +000086struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000087 pid_t pid; /* 0 if exited */
88 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000089 int num_redirects; /* elements in redirection array */
90 struct redir_struct *redirects; /* I/O redirects */
Eric Andersen86349772000-12-18 20:25:50 +000091 int is_stopped; /* is the program currently running? */
92 struct job *family; /* pointer back to the child's parent job */
Erik Andersen3522eb12000-03-12 23:49:18 +000093};
94
95struct job {
Eric Andersen86349772000-12-18 20:25:50 +000096 int jobid; /* job number */
97 int num_progs; /* total number of programs in job */
98 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000099 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +0000100 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +0000101 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +0000102 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +0000103 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +0000104 int stopped_progs; /* number of programs alive, but stopped */
105 unsigned int job_context; /* bitmask defining current context */
106 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +0000107};
108
Eric Andersen86349772000-12-18 20:25:50 +0000109struct built_in_command {
Erik Andersen161220c2000-03-16 08:12:48 +0000110 char *cmd; /* name */
111 char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +0000112 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +0000113};
114
Eric Andersen8ea28be2001-01-05 20:58:22 +0000115struct close_me {
116 int fd;
117 struct close_me *next;
118};
119
Eric Andersen34e19412000-07-10 18:47:24 +0000120/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000121static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000122static int builtin_exec(struct child_prog *cmd);
123static int builtin_exit(struct child_prog *cmd);
124static int builtin_fg_bg(struct child_prog *cmd);
125static int builtin_help(struct child_prog *cmd);
126static int builtin_jobs(struct child_prog *dummy);
127static int builtin_pwd(struct child_prog *dummy);
128static int builtin_export(struct child_prog *cmd);
129static int builtin_source(struct child_prog *cmd);
130static int builtin_unset(struct child_prog *cmd);
131static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000132
Eric Andersen34e19412000-07-10 18:47:24 +0000133
134/* function prototypes for shell stuff */
Eric Andersen8ea28be2001-01-05 20:58:22 +0000135static void mark_open(int fd);
136static void mark_closed(int fd);
Eric Andersen36278b92001-03-06 20:47:31 +0000137static void close_all(void);
Eric Andersen86349772000-12-18 20:25:50 +0000138static void checkjobs(struct jobset *job_list);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000139static void remove_job(struct jobset *j_list, struct job *job);
Eric Andersen86349772000-12-18 20:25:50 +0000140static int get_command(FILE * source, char *command);
141static int parse_command(char **command_ptr, struct job *job, int *inbg);
142static int run_command(struct job *newjob, int inbg, int outpipe[2]);
143static int pseudo_exec(struct child_prog *cmd) __attribute__ ((noreturn));
Erik Andersen3522eb12000-03-12 23:49:18 +0000144static int busy_loop(FILE * input);
145
Erik Andersend75af992000-03-16 08:09:09 +0000146
Mark Whitley37653aa2000-07-12 23:36:17 +0000147/* Table of built-in functions (these are non-forking builtins, meaning they
148 * can change global variables in the parent shell process but they will not
149 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Eric Andersen86349772000-12-18 20:25:50 +0000150static struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000151 {"bg", "Resume a job in the background", builtin_fg_bg},
152 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000153 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000154 {"exit", "Exit from shell()", builtin_exit},
155 {"fg", "Bring job into the foreground", builtin_fg_bg},
156 {"jobs", "Lists the active jobs", builtin_jobs},
157 {"export", "Set environment variable", builtin_export},
158 {"unset", "Unset environment variable", builtin_unset},
159 {"read", "Input environment variable", builtin_read},
Matt Kraaidd450a02000-09-13 03:43:36 +0000160 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000161 /* to do: add ulimit */
Eric Andersenfad9c112000-07-25 18:06:52 +0000162 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000163};
164
Mark Whitley37653aa2000-07-12 23:36:17 +0000165/* Table of forking built-in functions (things that fork cannot change global
166 * variables in the parent process, such as the current working directory) */
Eric Andersen86349772000-12-18 20:25:50 +0000167static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000168 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000169 {"help", "List shell built-in commands", builtin_help},
170 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000171};
172
Eric Andersen0bcc8132001-01-05 19:37:32 +0000173
Eric Andersen7467c8d2001-07-12 20:26:32 +0000174static int shell_context; /* Type prompt trigger (PS1 or PS2) */
Eric Andersen0bcc8132001-01-05 19:37:32 +0000175
176
177/* Globals that are static to this file */
Eric Andersencfa88ec2001-05-11 18:08:16 +0000178static const char *cwd;
Eric Andersen744b0642001-01-05 21:23:44 +0000179static char *local_pending_command = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000180static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000181static int argc;
182static char **argv;
Eric Andersen702ec592001-03-06 22:17:29 +0000183static struct close_me *close_me_head;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000184static int last_return_code;
185static int last_bg_pid;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000186static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000187static int shell_terminal;
188static pid_t shell_pgrp;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000189static char *PS1;
Eric Andersene5dfced2001-04-09 22:48:12 +0000190static char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000191
192
Eric Andersenb558e762000-11-30 22:43:16 +0000193#ifdef DEBUG_SHELL
194static inline void debug_printf(const char *format, ...)
195{
196 va_list args;
197 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000198 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000199 va_end(args);
200}
201#else
202static inline void debug_printf(const char *format, ...) { }
203#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000204
Eric Andersen86349772000-12-18 20:25:50 +0000205/*
206 Most builtins need access to the struct child_prog that has
207 their arguments, previously coded as cmd->progs[0]. That coding
208 can exhibit a bug, if the builtin is not the first command in
209 a pipeline: "echo foo | exec sort" will attempt to exec foo.
210
211builtin previous use notes
212------ ----------------- ---------
213cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000214exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
215exit cmd->progs[0]
216fg_bg cmd->progs[0], job_list->head, job_list->fg
217help 0
218jobs job_list->head
219pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000220export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000221source cmd->progs[0]
222unset cmd->progs[0]
223read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000224
225I added "struct job *family;" to struct child_prog,
226and switched API to builtin_foo(struct child_prog *child);
227So cmd->text becomes child->family->text
228 cmd->job_context becomes child->family->job_context
229 cmd->progs[0] becomes *child
230 job_list becomes child->family->job_list
231 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000232
Erik Andersend75af992000-03-16 08:09:09 +0000233/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000234static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000235{
Erik Andersen161220c2000-03-16 08:12:48 +0000236 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000237
Eric Andersen86349772000-12-18 20:25:50 +0000238 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000239 newdir = getenv("HOME");
240 else
Eric Andersen86349772000-12-18 20:25:50 +0000241 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000242 if (chdir(newdir)) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000243 printf("cd: %s: %m\n", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000244 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000245 }
Eric Andersencfa88ec2001-05-11 18:08:16 +0000246 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000247 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000248 cwd = bb_msg_unknown;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000249 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000250}
251
Eric Andersend2f56772000-09-21 02:48:07 +0000252/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000253static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000254{
Eric Andersen86349772000-12-18 20:25:50 +0000255 if (child->argv[1] == NULL)
256 return EXIT_SUCCESS; /* Really? */
257 child->argv++;
Eric Andersen07f2f392001-03-06 20:28:22 +0000258 close_all();
Eric Andersen86349772000-12-18 20:25:50 +0000259 pseudo_exec(child);
260 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000261}
262
Erik Andersen3522eb12000-03-12 23:49:18 +0000263/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000264static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000265{
Eric Andersen86349772000-12-18 20:25:50 +0000266 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000267 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000268
Eric Andersen86349772000-12-18 20:25:50 +0000269 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000270}
271
272/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000273static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000274{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000275 int i, jobnum;
Erik Andersen6273f652000-03-17 01:12:41 +0000276 struct job *job=NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000277
Eric Andersen8a646dd2001-06-21 16:38:11 +0000278 /* If they gave us no args, assume they want the last backgrounded task */
279 if (!child->argv[1]) {
280 for (job = child->family->job_list->head; job; job = job->next) {
281 if (job->jobid == last_jobid) {
282 break;
283 }
Erik Andersen161220c2000-03-16 08:12:48 +0000284 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000285 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000286 bb_error_msg("%s: no current job", child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000287 return EXIT_FAILURE;
288 }
289 } else {
290 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000291 bb_error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000292 return EXIT_FAILURE;
293 }
294 for (job = child->family->job_list->head; job; job = job->next) {
295 if (job->jobid == jobnum) {
296 break;
297 }
298 }
299 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000300 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000301 return EXIT_FAILURE;
302 }
Erik Andersend75af992000-03-16 08:09:09 +0000303 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000304
Eric Andersen86349772000-12-18 20:25:50 +0000305 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000306 /* Put the job into the foreground. */
307 tcsetpgrp(shell_terminal, job->pgrp);
308
Eric Andersen86349772000-12-18 20:25:50 +0000309 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000310 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000311
Erik Andersen161220c2000-03-16 08:12:48 +0000312 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000313 for (i = 0; i < job->num_progs; i++)
314 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000315
Eric Andersen86349772000-12-18 20:25:50 +0000316 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000317
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000318 if ( (i=kill(- job->pgrp, SIGCONT)) < 0) {
319 if (i == ESRCH) {
320 remove_job(&job_list, job);
Eric Andersen07abfe22001-06-27 17:29:11 +0000321 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000322 bb_perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000323 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000324 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000325
Matt Kraai3e856ce2000-12-01 02:55:13 +0000326 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000327}
328
329/* built-in 'help' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000330static int builtin_help(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000331{
Eric Andersen86349772000-12-18 20:25:50 +0000332 struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000333
Eric Andersen86349772000-12-18 20:25:50 +0000334 printf("\nBuilt-in commands:\n");
335 printf("-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000336 for (x = bltins; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000337 if (x->descr==NULL)
338 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000339 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000340 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000341 for (x = bltins_forking; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000342 if (x->descr==NULL)
343 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000344 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000345 }
Eric Andersen86349772000-12-18 20:25:50 +0000346 printf("\n\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000347 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000348}
349
350/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000351static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000352{
Erik Andersen161220c2000-03-16 08:12:48 +0000353 struct job *job;
Eric Andersen86349772000-12-18 20:25:50 +0000354 char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000355
Eric Andersen86349772000-12-18 20:25:50 +0000356 for (job = child->family->job_list->head; job; job = job->next) {
357 if (job->running_progs == job->stopped_progs)
358 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000359 else
Eric Andersen86349772000-12-18 20:25:50 +0000360 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000361
Eric Andersen86349772000-12-18 20:25:50 +0000362 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000363 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000364 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000365}
366
367
368/* built-in 'pwd' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000369static int builtin_pwd(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000370{
Eric Andersencfa88ec2001-05-11 18:08:16 +0000371 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000372 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000373 cwd = bb_msg_unknown;
Matt Kraai59df6f72001-05-16 14:21:09 +0000374 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000375 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000376}
377
Erik Andersen6273f652000-03-17 01:12:41 +0000378/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000379static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000380{
Erik Andersen161220c2000-03-16 08:12:48 +0000381 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000382 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000383
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000384 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000385 char **e;
386 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000387 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000388 }
Matt Kraai2129f972001-04-04 17:50:04 +0000389 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000390 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000391 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000392 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000393 fprintf(stderr, "export: %m\n");
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000394#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000395 if (strncmp(v, "PS1=", 4)==0)
396 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000397#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000398
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000399#ifdef CONFIG_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +0000400 if(strncmp(v, "LC_ALL=", 7)==0)
401 setlocale(LC_ALL, getenv("LC_ALL"));
Mark Whitleya82a0032001-03-27 17:07:15 +0000402 if(strncmp(v, "LC_CTYPE=", 9)==0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000403 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000404#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000405
Erik Andersen161220c2000-03-16 08:12:48 +0000406 return (res);
Erik Andersen3522eb12000-03-12 23:49:18 +0000407}
408
Eric Andersenb54833c2000-07-03 23:56:26 +0000409/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000410static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000411{
412 int res = 0, len, newlen;
413 char *s;
414 char string[MAX_READ];
415
Eric Andersen86349772000-12-18 20:25:50 +0000416 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000417 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen0d2d1eb2002-06-06 13:33:01 +0000418 safe_strncpy(string, child->argv[1], MAX_READ-1);
Eric Andersenb54833c2000-07-03 23:56:26 +0000419 len = strlen(string);
420 string[len++] = '=';
421 string[len] = '\0';
422 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
423 newlen = strlen(string);
424 if(newlen > len)
425 string[--newlen] = '\0'; /* chomp trailing newline */
426 /*
427 ** string should now contain "VAR=<value>"
428 ** copy it (putenv() won't do that, so we must make sure
429 ** the string resides in a static buffer!)
430 */
431 res = -1;
432 if((s = strdup(string)))
433 res = putenv(s);
434 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000435 fprintf(stderr, "read: %m\n");
Eric Andersenb54833c2000-07-03 23:56:26 +0000436 }
437 else
438 fgets(string, sizeof(string), stdin);
439
440 return (res);
441}
442
Erik Andersen3522eb12000-03-12 23:49:18 +0000443/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000444static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000445{
Erik Andersen161220c2000-03-16 08:12:48 +0000446 FILE *input;
447 int status;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000448 int fd;
Erik Andersen3522eb12000-03-12 23:49:18 +0000449
Eric Andersen86349772000-12-18 20:25:50 +0000450 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000451 return EXIT_FAILURE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000452
Eric Andersen86349772000-12-18 20:25:50 +0000453 input = fopen(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000454 if (!input) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000455 printf( "Couldn't open file '%s'\n", child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000456 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000457 }
Erik Andersend75af992000-03-16 08:09:09 +0000458
Eric Andersen8ea28be2001-01-05 20:58:22 +0000459 fd=fileno(input);
460 mark_open(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000461 /* Now run the file */
462 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000463 fclose(input);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000464 mark_closed(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000465 return (status);
Erik Andersen3522eb12000-03-12 23:49:18 +0000466}
467
468/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000469static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000470{
Eric Andersen86349772000-12-18 20:25:50 +0000471 if (child->argv[1] == NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000472 printf( "unset: parameter required.\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000473 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000474 }
Eric Andersen86349772000-12-18 20:25:50 +0000475 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000476 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000477}
478
Eric Andersen8ea28be2001-01-05 20:58:22 +0000479static void mark_open(int fd)
480{
481 struct close_me *new = xmalloc(sizeof(struct close_me));
482 new->fd = fd;
483 new->next = close_me_head;
484 close_me_head = new;
485}
486
487static void mark_closed(int fd)
488{
489 struct close_me *tmp;
490 if (close_me_head == NULL || close_me_head->fd != fd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000491 bb_error_msg_and_die("corrupt close_me");
Eric Andersen8ea28be2001-01-05 20:58:22 +0000492 tmp = close_me_head;
493 close_me_head = close_me_head->next;
494 free(tmp);
495}
496
497static void close_all()
498{
Eric Andersen702ec592001-03-06 22:17:29 +0000499 struct close_me *c, *tmp;
500 for (c=close_me_head; c; c=tmp) {
501 close(c->fd);
502 tmp=c->next;
503 free(c);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000504 }
505 close_me_head = NULL;
506}
507
508
Erik Andersen3522eb12000-03-12 23:49:18 +0000509/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000510static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000511{
Erik Andersen161220c2000-03-16 08:12:48 +0000512 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000513 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000514
Eric Andersen86349772000-12-18 20:25:50 +0000515 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000516 free(cmd->progs[i].argv);
Eric Andersen86349772000-12-18 20:25:50 +0000517 if (cmd->progs[i].redirects)
518 free(cmd->progs[i].redirects);
Erik Andersen161220c2000-03-16 08:12:48 +0000519 }
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000520 free(cmd->progs);
521 free(cmd->text);
522 free(cmd->cmdbuf);
Mark Whitley44a99142001-03-14 17:26:37 +0000523 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000524 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000525 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000526}
527
Eric Andersen1ca20a72001-03-21 07:34:27 +0000528/* remove a job from a jobset */
529static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000530{
Eric Andersen86349772000-12-18 20:25:50 +0000531 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000532
Eric Andersen86349772000-12-18 20:25:50 +0000533 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000534 if (job == j_list->head) {
535 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000536 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000537 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000538 while (prevjob->next != job)
539 prevjob = prevjob->next;
540 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000541 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000542
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000543 if (j_list->head)
544 last_jobid = j_list->head->jobid;
545 else
546 last_jobid = 0;
547
Erik Andersen161220c2000-03-16 08:12:48 +0000548 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000549}
550
551/* Checks to see if any background processes have exited -- if they
552 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000553static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000554{
Erik Andersen161220c2000-03-16 08:12:48 +0000555 struct job *job;
556 pid_t childpid;
557 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000558 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000559
Erik Andersen161220c2000-03-16 08:12:48 +0000560 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000561 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000562 prognum = 0;
563 while (prognum < job->num_progs &&
564 job->progs[prognum].pid != childpid) prognum++;
565 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000566 break;
567 }
568
Eric Andersena1d187a2000-07-17 19:14:41 +0000569 /* This happens on backticked commands */
570 if(job==NULL)
571 return;
572
Erik Andersen161220c2000-03-16 08:12:48 +0000573 if (WIFEXITED(status) || WIFSIGNALED(status)) {
574 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000575 job->running_progs--;
576 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000577
Eric Andersen86349772000-12-18 20:25:50 +0000578 if (!job->running_progs) {
579 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000580 last_jobid=0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000581 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000582 }
583 } else {
584 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000585 job->stopped_progs++;
586 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000587
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000588#if 0
589 /* Printing this stuff is a pain, since it tends to
590 * overwrite the prompt an inconveinient moments. So
591 * don't do that. */
Eric Andersen86349772000-12-18 20:25:50 +0000592 if (job->stopped_progs == job->num_progs) {
593 printf(JOB_STATUS_FORMAT, job->jobid, "Stopped",
Erik Andersen161220c2000-03-16 08:12:48 +0000594 job->text);
595 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000596#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000597 }
Erik Andersend75af992000-03-16 08:09:09 +0000598 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000599
Erik Andersen161220c2000-03-16 08:12:48 +0000600 if (childpid == -1 && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000601 bb_perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000602}
603
Eric Andersene9f07fb2000-12-21 18:31:36 +0000604/* squirrel != NULL means we squirrel away copies of stdin, stdout,
605 * and stderr if they are redirected. */
606static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000607{
608 int i;
609 int openfd;
610 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000611 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000612
Eric Andersen86349772000-12-18 20:25:50 +0000613 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000614 switch (redir->type) {
615 case REDIRECT_INPUT:
616 mode = O_RDONLY;
617 break;
618 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000619 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000620 break;
621 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000622 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000623 break;
624 }
625
626 openfd = open(redir->filename, mode, 0666);
627 if (openfd < 0) {
628 /* this could get lost if stderr has been redirected, but
629 bash and ash both lose it as well (though zsh doesn't!) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000630 bb_perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000631 return 1;
632 }
633
634 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000635 if (squirrel && redir->fd < 3) {
636 squirrel[redir->fd] = dup(redir->fd);
637 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000638 dup2(openfd, redir->fd);
639 close(openfd);
640 }
641 }
642
643 return 0;
644}
645
Eric Andersene9f07fb2000-12-21 18:31:36 +0000646static void restore_redirects(int squirrel[])
647{
648 int i, fd;
649 for (i=0; i<3; i++) {
650 fd = squirrel[i];
651 if (fd != -1) {
652 /* No error checking. I sure wouldn't know what
653 * to do with an error if I found one! */
654 dup2(fd, i);
655 close(fd);
656 }
657 }
658}
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000659
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000660static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000661{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000662#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000663 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000664#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000665 PS1 = getenv("PS1");
Eric Andersene5dfced2001-04-09 22:48:12 +0000666 if(PS1==0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000667 PS1 = "\\w \\$ ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000668#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000669}
670
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000671static inline void setup_prompt_string(char **prompt_str)
672{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000673#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000674 /* Set up the prompt */
675 if (shell_context == 0) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000676 free(PS1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000677 PS1=xmalloc(strlen(cwd)+4);
678 sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
679 *prompt_str = PS1;
680 } else {
681 *prompt_str = PS2;
682 }
683#else
684 *prompt_str = (shell_context==0)? PS1 : PS2;
685#endif
686}
Eric Andersen22332fd2001-01-30 23:40:39 +0000687
Eric Andersen09acc062001-01-04 11:10:38 +0000688static int get_command(FILE * source, char *command)
689{
690 char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000691
Eric Andersen1c314ad2000-06-28 16:56:25 +0000692 if (source == NULL) {
693 if (local_pending_command) {
694 /* a command specified (-c option): return it & mark it done */
695 strcpy(command, local_pending_command);
696 free(local_pending_command);
697 local_pending_command = NULL;
698 return 0;
699 }
700 return 1;
701 }
702
Erik Andersen161220c2000-03-16 08:12:48 +0000703 if (source == stdin) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000704 setup_prompt_string(&prompt_str);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000705
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000706#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000707 /*
708 ** enable command line editing only while a command line
709 ** is actually being read; otherwise, we'll end up bequeathing
710 ** atexit() handlers and other unwanted stuff to our
711 ** child processes (rob@sysgo.de)
712 */
Eric Andersen86349772000-12-18 20:25:50 +0000713 cmdedit_read_input(prompt_str, command);
Erik Andersen161220c2000-03-16 08:12:48 +0000714 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000715#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000716 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000717#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000718 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000719
Erik Andersen161220c2000-03-16 08:12:48 +0000720 if (!fgets(command, BUFSIZ - 2, source)) {
721 if (source == stdin)
722 printf("\n");
723 return 1;
724 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000725
Erik Andersen161220c2000-03-16 08:12:48 +0000726 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000727}
728
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000729static char* itoa(register int i)
730{
731 static char a[7]; /* Max 7 ints */
732 register char *b = a + sizeof(a) - 1;
733 int sign = (i < 0);
734
735 if (sign)
736 i = -i;
737 *b = 0;
738 do
739 {
740 *--b = '0' + (i % 10);
741 i /= 10;
742 }
743 while (i);
744 if (sign)
745 *--b = '-';
746 return b;
747}
748
749char * strsep_space( char *string, int * ix)
750{
751 char *token, *begin;
752
753 begin = string;
754
755 /* Short circuit the trivial case */
756 if ( !string || ! string[*ix])
757 return NULL;
758
759 /* Find the end of the token. */
760 while( string && string[*ix] && !isspace(string[*ix]) ) {
761 (*ix)++;
762 }
763
764 /* Find the end of any whitespace trailing behind
765 * the token and let that be part of the token */
766 while( string && string[*ix] && isspace(string[*ix]) ) {
767 (*ix)++;
768 }
769
770 if (! string && *ix==0) {
771 /* Nothing useful was found */
772 return NULL;
773 }
774
775 token = xmalloc(*ix+1);
776 token[*ix] = '\0';
777 strncpy(token, string, *ix);
778
779 return token;
780}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000781
Eric Andersenca604592001-03-08 17:17:13 +0000782static int expand_arguments(char *command)
783{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000784 int total_length=0, length, i, retval, ix = 0;
785 expand_t expand_result;
786 char *tmpcmd, *cmd, *cmd_copy;
787 char *src, *dst, *var;
788 const char *out_of_space = "out of space during expansion";
789 int flags = GLOB_NOCHECK
790#ifdef GLOB_BRACE
791 | GLOB_BRACE
792#endif
793#ifdef GLOB_TILDE
794 | GLOB_TILDE
795#endif
796 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000797
Eric Andersenca604592001-03-08 17:17:13 +0000798 /* get rid of the terminating \n */
799 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000800
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000801 /* Fix up escape sequences to be the Real Thing(tm) */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000802 while( command && command[ix]) {
803 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000804 const char *tmp = command+ix+1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000805 command[ix] = bb_process_escape_sequence( &tmp );
Eric Andersen1ca20a72001-03-21 07:34:27 +0000806 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000807 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000808 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000809 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000810 /* Use glob and then fixup environment variables and such */
811
812 /* It turns out that glob is very stupid. We have to feed it one word at a
813 * time since it can't cope with a full string. Here we convert command
814 * (char*) into cmd (char**, one word per string) */
815
816 /* We need a clean copy, so strsep can mess up the copy while
817 * we write stuff into the original (in a minute) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000818 cmd = cmd_copy = bb_xstrdup(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000819 *command = '\0';
820 for (ix = 0, tmpcmd = cmd;
821 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
822 if (*tmpcmd == '\0')
823 break;
824 /* we need to trim() the result for glob! */
825 trim(tmpcmd);
826 retval = glob(tmpcmd, flags, NULL, &expand_result);
827 free(tmpcmd); /* Free mem allocated by strsep_space */
828 if (retval == GLOB_NOSPACE) {
829 /* Mem may have been allocated... */
830 globfree (&expand_result);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000831 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000832 return FALSE;
833 } else if (retval != 0) {
834 /* Some other error. GLOB_NOMATCH shouldn't
835 * happen because of the GLOB_NOCHECK flag in
836 * the glob call. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000837 bb_error_msg("syntax error");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000838 return FALSE;
839 } else {
840 /* Convert from char** (one word per string) to a simple char*,
841 * but don't overflow command which is BUFSIZ in length */
842 for (i=0; i < expand_result.gl_pathc; i++) {
843 length=strlen(expand_result.gl_pathv[i]);
844 if (total_length+length+1 >= BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000845 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000846 return FALSE;
847 }
848 strcat(command+total_length, " ");
849 total_length+=1;
850 strcat(command+total_length, expand_result.gl_pathv[i]);
851 total_length+=length;
852 }
853 globfree (&expand_result);
854 }
855 }
856 free(cmd_copy);
857 trim(command);
858
859 /* Now do the shell variable substitutions which
860 * wordexp can't do for us, namely $? and $! */
861 src = command;
862 while((dst = strchr(src,'$')) != NULL){
863 var = NULL;
864 switch(*(dst+1)) {
865 case '?':
866 var = itoa(last_return_code);
867 break;
868 case '!':
869 if (last_bg_pid==-1)
870 *(var)='\0';
871 else
872 var = itoa(last_bg_pid);
873 break;
874 /* Everything else like $$, $#, $[0-9], etc. should all be
875 * expanded by wordexp(), so we can in theory skip that stuff
876 * here, but just to be on the safe side (i.e., since uClibc
877 * wordexp doesn't do this stuff yet), lets leave it in for
878 * now. */
879 case '$':
880 var = itoa(getpid());
881 break;
882 case '#':
883 var = itoa(argc-1);
884 break;
885 case '0':case '1':case '2':case '3':case '4':
886 case '5':case '6':case '7':case '8':case '9':
887 {
Eric Andersen7ab93d92002-11-08 09:40:02 +0000888 int ixx=*(dst+1)-48+1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000889 if (ixx >= argc) {
890 var='\0';
891 } else {
892 var = argv[ixx];
893 }
894 }
895 break;
896
897 }
898 if (var) {
899 /* a single character construction was found, and
900 * already handled in the case statement */
901 src=dst+2;
902 } else {
903 /* Looks like an environment variable */
904 char delim_hold;
905 int num_skip_chars=0;
906 int dstlen = strlen(dst);
907 /* Is this a ${foo} type variable? */
908 if (dstlen >=2 && *(dst+1) == '{') {
909 src=strchr(dst+1, '}');
910 num_skip_chars=1;
911 } else {
912 src=dst+1;
913 while(isalnum(*src) || *src=='_') src++;
914 }
915 if (src == NULL) {
916 src = dst+dstlen;
917 }
918 delim_hold=*src;
919 *src='\0'; /* temporary */
920 var = getenv(dst + 1 + num_skip_chars);
921 *src=delim_hold;
922 src += num_skip_chars;
923 }
924 if (var == NULL) {
925 /* Seems we got an un-expandable variable. So delete it. */
926 var = "";
927 }
928 {
929 int subst_len = strlen(var);
930 int trail_len = strlen(src);
931 if (dst+subst_len+trail_len >= command+BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000932 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000933 return FALSE;
934 }
935 /* Move stuff to the end of the string to accommodate
936 * filling the created gap with the new stuff */
937 memmove(dst+subst_len, src, trail_len+1);
938 /* Now copy in the new stuff */
939 memcpy(dst, var, subst_len);
940 src = dst+subst_len;
941 }
942 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000943
Eric Andersenca604592001-03-08 17:17:13 +0000944 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000945}
946
Eric Andersen86349772000-12-18 20:25:50 +0000947/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
948 line). If a valid command is found, command_ptr is set to point to
Erik Andersen3522eb12000-03-12 23:49:18 +0000949 the beginning of the next command (if the original command had more
950 then one job associated with it) or NULL if no more commands are
951 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000952static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000953{
Erik Andersen161220c2000-03-16 08:12:48 +0000954 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000955 char *return_command = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +0000956 char *src, *buf, *chptr;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000957 int argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000958 int done = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000959 int argv_alloced;
Matt Kraaibe66ad32001-04-12 15:42:17 +0000960 int i, saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000961 char quote = '\0';
962 int count;
Eric Andersen86349772000-12-18 20:25:50 +0000963 struct child_prog *prog;
Erik Andersen3522eb12000-03-12 23:49:18 +0000964
Erik Andersen161220c2000-03-16 08:12:48 +0000965 /* skip leading white space */
Eric Andersen86349772000-12-18 20:25:50 +0000966 while (**command_ptr && isspace(**command_ptr))
967 (*command_ptr)++;
Erik Andersen3522eb12000-03-12 23:49:18 +0000968
Erik Andersen161220c2000-03-16 08:12:48 +0000969 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000970 if (!**command_ptr || (**command_ptr == '#')) {
971 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000972 return 0;
973 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000974
Eric Andersen86349772000-12-18 20:25:50 +0000975 *inbg = 0;
976 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000977 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000978
Erik Andersen161220c2000-03-16 08:12:48 +0000979 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000980 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000981 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000982
Erik Andersen161220c2000-03-16 08:12:48 +0000983 Getting clean memory relieves us of the task of NULL
984 terminating things and makes the rest of this look a bit
985 cleaner (though it is, admittedly, a tad less efficient) */
Eric Andersen86349772000-12-18 20:25:50 +0000986 job->cmdbuf = command = xcalloc(2*strlen(*command_ptr) + 1, sizeof(char));
Erik Andersen161220c2000-03-16 08:12:48 +0000987 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000988
Erik Andersen161220c2000-03-16 08:12:48 +0000989 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000990 prog->num_redirects = 0;
991 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000992 prog->is_stopped = 0;
993 prog->family = job;
Erik Andersen3522eb12000-03-12 23:49:18 +0000994
Eric Andersen86349772000-12-18 20:25:50 +0000995 argv_alloced = 5;
996 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
997 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +0000998
Erik Andersen161220c2000-03-16 08:12:48 +0000999 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +00001000 src = *command_ptr;
Erik Andersen161220c2000-03-16 08:12:48 +00001001 while (*src && !done) {
1002 if (quote == *src) {
1003 quote = '\0';
1004 } else if (quote) {
1005 if (*src == '\\') {
1006 src++;
1007 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001008 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001009 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001010 return 1;
1011 }
1012
1013 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +00001014 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +00001015 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +00001016 *buf++ = '\\';
1017 }
Erik Andersen161220c2000-03-16 08:12:48 +00001018 } else if (*src == '*' || *src == '?' || *src == '[' ||
1019 *src == ']') *buf++ = '\\';
1020 *buf++ = *src;
1021 } else if (isspace(*src)) {
Matt Kraaibe66ad32001-04-12 15:42:17 +00001022 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001023 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001024 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +00001025 if ((argc_l + 1) == argv_alloced) {
1026 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +00001027 prog->argv = xrealloc(prog->argv,
1028 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +00001029 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001030 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001031 prog->argv[argc_l] = buf;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001032 saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001033 }
1034 } else
1035 switch (*src) {
1036 case '"':
1037 case '\'':
1038 quote = *src;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001039 saw_quote = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001040 break;
1041
1042 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001043 if (*(src-1)== '$')
1044 *buf++ = *src;
1045 else
1046 done = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001047 break;
1048
Eric Andersen86349772000-12-18 20:25:50 +00001049 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +00001050 case '<':
Eric Andersen86349772000-12-18 20:25:50 +00001051 i = prog->num_redirects++;
1052 prog->redirects = xrealloc(prog->redirects,
1053 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +00001054 (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +00001055
Eric Andersen86349772000-12-18 20:25:50 +00001056 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001057 if (buf != prog->argv[argc_l]) {
Erik Andersen161220c2000-03-16 08:12:48 +00001058 /* the stuff before this character may be the file number
1059 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +00001060 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001061 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +00001062
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001063 if (*chptr && *prog->argv[argc_l]) {
1064 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001065 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001066 }
1067 }
1068
Eric Andersen86349772000-12-18 20:25:50 +00001069 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001070 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001071 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001072 else
Eric Andersen86349772000-12-18 20:25:50 +00001073 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001074 }
1075
1076 if (*src++ == '>') {
1077 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001078 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001079 REDIRECT_APPEND, src++;
1080 else
Eric Andersen86349772000-12-18 20:25:50 +00001081 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001082 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001083 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001084 }
1085
1086 /* This isn't POSIX sh compliant. Oh well. */
1087 chptr = src;
1088 while (isspace(*chptr))
1089 chptr++;
1090
1091 if (!*chptr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001092 bb_error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001093 free_job(job);
1094 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001095 return 1;
1096 }
1097
Eric Andersen86349772000-12-18 20:25:50 +00001098 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001099 while (*chptr && !isspace(*chptr))
1100 *buf++ = *chptr++;
1101
1102 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001103 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001104 break;
1105
1106 case '|': /* pipe */
1107 /* finish this command */
Matt Kraaibe66ad32001-04-12 15:42:17 +00001108 if (*prog->argv[argc_l] || saw_quote)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001109 argc_l++;
1110 if (!argc_l) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001111 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001112 free_job(job);
1113 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001114 return 1;
1115 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001116 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001117
1118 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001119 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001120 job->progs = xrealloc(job->progs,
Eric Andersen86349772000-12-18 20:25:50 +00001121 sizeof(*job->progs) * job->num_progs);
1122 prog = job->progs + (job->num_progs - 1);
1123 prog->num_redirects = 0;
1124 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001125 prog->is_stopped = 0;
1126 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001127 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001128
Eric Andersen86349772000-12-18 20:25:50 +00001129 argv_alloced = 5;
1130 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001131 prog->argv[0] = ++buf;
1132
1133 src++;
1134 while (*src && isspace(*src))
1135 src++;
1136
1137 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001138 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001139 free_job(job);
1140 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001141 return 1;
1142 }
1143 src--; /* we'll ++ it at the end of the loop */
1144
1145 break;
1146
1147 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001148 *inbg = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001149 case ';': /* multiple commands */
1150 done = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001151 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001152 break;
1153
Matt Kraai131241f2000-09-14 00:43:20 +00001154 case '\\':
1155 src++;
1156 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001157 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001158 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001159 return 1;
1160 }
1161 if (*src == '*' || *src == '[' || *src == ']'
1162 || *src == '?') *buf++ = '\\';
1163 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001164 default:
1165 *buf++ = *src;
1166 }
1167
Erik Andersend75af992000-03-16 08:09:09 +00001168 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001169 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001170
Matt Kraaibe66ad32001-04-12 15:42:17 +00001171 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001172 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001173 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001174 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001175 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001176 return 0;
1177 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001178 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001179
Eric Andersen86349772000-12-18 20:25:50 +00001180 if (!return_command) {
1181 job->text = xmalloc(strlen(*command_ptr) + 1);
1182 strcpy(job->text, *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001183 } else {
1184 /* This leaves any trailing spaces, which is a bit sloppy */
Eric Andersen86349772000-12-18 20:25:50 +00001185 count = return_command - *command_ptr;
Eric Andersenec10b9d2000-07-14 01:13:11 +00001186 job->text = xmalloc(count + 1);
Eric Andersen86349772000-12-18 20:25:50 +00001187 strncpy(job->text, *command_ptr, count);
Erik Andersen161220c2000-03-16 08:12:48 +00001188 job->text[count] = '\0';
1189 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001190
Eric Andersen86349772000-12-18 20:25:50 +00001191 *command_ptr = return_command;
Eric Andersen46f0beb2000-11-14 21:59:22 +00001192
Erik Andersend75af992000-03-16 08:09:09 +00001193 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001194}
1195
Eric Andersen86349772000-12-18 20:25:50 +00001196/* Run the child_prog, no matter what kind of command it uses.
1197 */
1198static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001199{
Eric Andersen86349772000-12-18 20:25:50 +00001200 struct built_in_command *x;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001201#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
Eric Andersenaf4ac772001-02-01 22:43:49 +00001202 char *name;
Erik Andersenbcd61772000-05-13 06:33:19 +00001203#endif
Erik Andersen3522eb12000-03-12 23:49:18 +00001204
Eric Andersene9f07fb2000-12-21 18:31:36 +00001205 /* Check if the command matches any of the non-forking builtins.
1206 * Depending on context, this might be redundant. But it's
1207 * easier to waste a few CPU cycles than it is to figure out
1208 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001209 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001210 for (x = bltins; x->cmd; x++) {
1211 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001212 _exit(x->function(child));
Eric Andersene9f07fb2000-12-21 18:31:36 +00001213 }
1214 }
1215
1216 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001217 for (x = bltins_forking; x->cmd; x++) {
1218 if (strcmp(child->argv[0], x->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001219 bb_applet_name=x->cmd;
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001220 _exit (x->function(child));
Eric Andersen86349772000-12-18 20:25:50 +00001221 }
1222 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001223#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
Eric Andersen86349772000-12-18 20:25:50 +00001224 /* Check if the command matches any busybox internal
1225 * commands ("applets") here. Following discussions from
Eric Andersen2423b122001-12-08 01:56:15 +00001226 * November 2000 on busybox@busybox.net, don't use
Manuel Novoa III cad53642003-03-19 09:13:01 +00001227 * bb_get_last_path_component(). This way explicit (with
Eric Andersen86349772000-12-18 20:25:50 +00001228 * slashes) filenames will never be interpreted as an
1229 * applet, just like with builtins. This way the user can
1230 * override an applet with an explicit filename reference.
1231 * The only downside to this change is that an explicit
1232 * /bin/foo invocation will fork and exec /bin/foo, even if
1233 * /bin/foo is a symlink to busybox.
1234 */
Matt Kraaif2cc2762001-02-01 19:21:20 +00001235 name = child->argv[0];
Eric Andersen86349772000-12-18 20:25:50 +00001236
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001237#ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN
1238 /* If you enable CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN, then
Eric Andersen86349772000-12-18 20:25:50 +00001239 * if you run /bin/cat, it will use BusyBox cat even if
1240 * /bin/cat exists on the filesystem and is _not_ busybox.
1241 * Some systems want this, others do not. Choose wisely. :-)
1242 */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001243 name = bb_get_last_path_component(name);
Eric Andersen86349772000-12-18 20:25:50 +00001244#endif
1245
Eric Andersen67991cf2001-02-14 21:23:06 +00001246 {
Eric Andersen82ab8da2001-03-23 17:06:01 +00001247 char** argv_l=child->argv;
Eric Andersen67991cf2001-02-14 21:23:06 +00001248 int argc_l;
Eric Andersen82ab8da2001-03-23 17:06:01 +00001249 for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
Eric Andersen67991cf2001-02-14 21:23:06 +00001250 optind = 1;
1251 run_applet_by_name(name, argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001252 }
1253#endif
1254
1255 execvp(child->argv[0], child->argv);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001256
Manuel Novoa III cad53642003-03-19 09:13:01 +00001257 /* Do not use bb_perror_msg_and_die() here, since we must not
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001258 * call exit() but should call _exit() instead */
Eric Andersenad082982002-09-30 20:20:20 +00001259 fprintf(stderr, "%s: %m\n", child->argv[0]);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001260 _exit(EXIT_FAILURE);
Eric Andersen86349772000-12-18 20:25:50 +00001261}
1262
1263static void insert_job(struct job *newjob, int inbg)
1264{
1265 struct job *thejob;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001266 struct jobset *j_list=newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001267
1268 /* find the ID for thejob to use */
1269 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001270 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001271 if (thejob->jobid >= newjob->jobid)
1272 newjob->jobid = thejob->jobid + 1;
1273
1274 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001275 if (!j_list->head) {
1276 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001277 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001278 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001279 thejob->next = xmalloc(sizeof(*thejob));
1280 thejob = thejob->next;
1281 }
1282
1283 *thejob = *newjob; /* physically copy the struct job */
1284 thejob->next = NULL;
1285 thejob->running_progs = thejob->num_progs;
1286 thejob->stopped_progs = 0;
1287
1288 if (inbg) {
1289 /* we don't wait for background thejobs to return -- append it
1290 to the list of backgrounded thejobs and leave it alone */
1291 printf("[%d] %d\n", thejob->jobid,
1292 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001293 last_jobid = newjob->jobid;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001294 last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001295 } else {
1296 newjob->job_list->fg = thejob;
1297
1298 /* move the new process group into the foreground */
1299 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001300 if (tcsetpgrp(shell_terminal, newjob->pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001301 bb_perror_msg("tcsetpgrp");
Eric Andersen86349772000-12-18 20:25:50 +00001302 }
1303}
1304
1305static int run_command(struct job *newjob, int inbg, int outpipe[2])
1306{
1307 /* struct job *thejob; */
1308 int i;
1309 int nextin, nextout;
1310 int pipefds[2]; /* pipefd[0] is for reading */
1311 struct built_in_command *x;
1312 struct child_prog *child;
1313
Eric Andersen6efc48c2000-07-18 08:16:39 +00001314 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001315 for (i = 0; i < newjob->num_progs; i++) {
1316 child = & (newjob->progs[i]);
1317
1318 if ((i + 1) < newjob->num_progs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001319 if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001320 nextout = pipefds[1];
1321 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001322 if (outpipe[1]!=-1) {
1323 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001324 } else {
1325 nextout = 1;
1326 }
Erik Andersen161220c2000-03-16 08:12:48 +00001327 }
1328
Eric Andersen501c88b2000-07-28 15:14:45 +00001329
Eric Andersene9f07fb2000-12-21 18:31:36 +00001330 /* Check if the command matches any non-forking builtins,
1331 * but only if this is a simple command.
1332 * Non-forking builtins within pipes have to fork anyway,
1333 * and are handled in pseudo_exec. "echo foo | read bar"
1334 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001335 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001336 if (newjob->num_progs == 1) {
1337 for (x = bltins; x->cmd; x++) {
1338 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1339 int squirrel[] = {-1, -1, -1};
1340 int rcode;
1341 setup_redirects(child, squirrel);
1342 rcode = x->function(child);
1343 restore_redirects(squirrel);
1344 return rcode;
1345 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001346 }
1347 }
1348
Eric Andersen72f9a422001-10-28 05:12:20 +00001349#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
1350 if (!(child->pid = fork()))
1351#else
1352 if (!(child->pid = vfork()))
1353#endif
1354 {
Eric Andersen2d848a42001-06-25 17:11:54 +00001355 /* Set the handling for job control signals back to the default. */
1356 signal(SIGINT, SIG_DFL);
1357 signal(SIGQUIT, SIG_DFL);
1358 signal(SIGTSTP, SIG_DFL);
1359 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001360 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001361 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001362
Eric Andersen8ea28be2001-01-05 20:58:22 +00001363 close_all();
1364
Eric Andersen86349772000-12-18 20:25:50 +00001365 if (outpipe[1]!=-1) {
1366 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001367 }
1368 if (nextin != 0) {
1369 dup2(nextin, 0);
1370 close(nextin);
1371 }
1372
1373 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001374 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001375 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001376 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001377 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001378 }
1379
Eric Andersen86349772000-12-18 20:25:50 +00001380 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001381 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001382
Eric Andersen86349772000-12-18 20:25:50 +00001383 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001384 }
Eric Andersen86349772000-12-18 20:25:50 +00001385 if (outpipe[1]!=-1) {
1386 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001387 }
Erik Andersen161220c2000-03-16 08:12:48 +00001388
1389 /* put our child in the process group whose leader is the
1390 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001391 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001392 if (nextin != 0)
1393 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001394 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001395 close(nextout);
1396
1397 /* If there isn't another process, nextin is garbage
1398 but it doesn't matter */
1399 nextin = pipefds[0];
1400 }
1401
Eric Andersen86349772000-12-18 20:25:50 +00001402 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001403
Eric Andersen86349772000-12-18 20:25:50 +00001404 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001405
Erik Andersen161220c2000-03-16 08:12:48 +00001406 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001407}
1408
Erik Andersend75af992000-03-16 08:09:09 +00001409static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001410{
Erik Andersen161220c2000-03-16 08:12:48 +00001411 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001412 char *next_command = NULL;
1413 struct job newjob;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001414 pid_t parent_pgrp;
Erik Andersen161220c2000-03-16 08:12:48 +00001415 int i;
Eric Andersen86349772000-12-18 20:25:50 +00001416 int inbg;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001417 int status;
Eric Andersen86349772000-12-18 20:25:50 +00001418 newjob.job_list = &job_list;
1419 newjob.job_context = DEFAULT_CONTEXT;
Erik Andersen3522eb12000-03-12 23:49:18 +00001420
Eric Andersen1c314ad2000-06-28 16:56:25 +00001421 /* save current owner of TTY so we can restore it on exit */
Eric Andersen2d848a42001-06-25 17:11:54 +00001422 parent_pgrp = tcgetpgrp(shell_terminal);
Eric Andersen1c314ad2000-06-28 16:56:25 +00001423
Matt Kraaib8907522000-09-13 02:08:21 +00001424 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Erik Andersend75af992000-03-16 08:09:09 +00001425
Erik Andersen161220c2000-03-16 08:12:48 +00001426 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001427 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001428 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001429
Erik Andersend75af992000-03-16 08:09:09 +00001430 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001431 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001432
Eric Andersen86349772000-12-18 20:25:50 +00001433 if (!next_command) {
1434 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001435 break;
Eric Andersen86349772000-12-18 20:25:50 +00001436 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001437 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001438
Matt Kraai1f0c4362001-12-20 23:13:26 +00001439 if (! expand_arguments(next_command)) {
Eric Andersenca604592001-03-08 17:17:13 +00001440 free(command);
1441 command = (char *) xcalloc(BUFSIZ, sizeof(char));
1442 next_command = NULL;
1443 continue;
1444 }
1445
Eric Andersen86349772000-12-18 20:25:50 +00001446 if (!parse_command(&next_command, &newjob, &inbg) &&
1447 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001448 int pipefds[2] = {-1,-1};
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001449 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
1450 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001451 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001452 }
1453 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001454 free(command);
Matt Kraaib8907522000-09-13 02:08:21 +00001455 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Eric Andersen86349772000-12-18 20:25:50 +00001456 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001457 }
1458 } else {
1459 /* a job is running in the foreground; wait for it */
1460 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001461 while (!job_list.fg->progs[i].pid ||
1462 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001463
Eric Andersen7373e482002-07-31 04:04:47 +00001464 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) {
1465 if (errno != ECHILD) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001466 bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
Eric Andersen7373e482002-07-31 04:04:47 +00001467 }
1468 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001469
Erik Andersend75af992000-03-16 08:09:09 +00001470 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001471 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001472 job_list.fg->running_progs--;
1473 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001474
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001475 last_return_code=WEXITSTATUS(status);
1476
Eric Andersen86349772000-12-18 20:25:50 +00001477 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001478 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001479 remove_job(&job_list, job_list.fg);
1480 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001481 }
Erik Andersend75af992000-03-16 08:09:09 +00001482 } else {
Erik Andersen161220c2000-03-16 08:12:48 +00001483 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001484 job_list.fg->stopped_progs++;
1485 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001486
Eric Andersen86349772000-12-18 20:25:50 +00001487 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1488 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1489 "Stopped", job_list.fg->text);
1490 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001491 }
Erik Andersend75af992000-03-16 08:09:09 +00001492 }
1493
Eric Andersen86349772000-12-18 20:25:50 +00001494 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001495 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001496 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001497 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001498 bb_perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001499 }
1500 }
1501 }
Erik Andersen161220c2000-03-16 08:12:48 +00001502 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001503
Eric Andersen1c314ad2000-06-28 16:56:25 +00001504 /* return controlling TTY back to parent process group before exiting */
Eric Andersen7373e482002-07-31 04:04:47 +00001505 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001506 bb_perror_msg("tcsetpgrp");
Eric Andersenb54833c2000-07-03 23:56:26 +00001507
1508 /* return exit status if called with "-c" */
1509 if (input == NULL && WIFEXITED(status))
1510 return WEXITSTATUS(status);
1511
Erik Andersen161220c2000-03-16 08:12:48 +00001512 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001513}
1514
1515
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001516#ifdef CONFIG_FEATURE_CLEAN_UP
Eric Andersenfad9c112000-07-25 18:06:52 +00001517void free_memory(void)
1518{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001519 if (cwd && cwd!=bb_msg_unknown) {
Eric Andersen5d60a462001-08-22 05:32:24 +00001520 free((char*)cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001521 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001522 if (local_pending_command)
1523 free(local_pending_command);
1524
Eric Andersen86349772000-12-18 20:25:50 +00001525 if (job_list.fg && !job_list.fg->running_progs) {
1526 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001527 }
1528}
1529#endif
1530
Eric Andersen2d848a42001-06-25 17:11:54 +00001531/* Make sure we have a controlling tty. If we get started under a job
1532 * aware app (like bash for example), make sure we are now in charge so
1533 * we don't fight over who gets the foreground */
Eric Andersenece8fc22002-12-11 04:26:28 +00001534static void setup_job_control(void)
Eric Andersen2d848a42001-06-25 17:11:54 +00001535{
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001536 int status;
1537
Eric Andersen2d848a42001-06-25 17:11:54 +00001538 /* Loop until we are in the foreground. */
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001539 while ((status = tcgetpgrp (shell_terminal)) >= 0) {
1540 if (status == (shell_pgrp = getpgrp ())) {
1541 break;
1542 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001543 kill (- shell_pgrp, SIGTTIN);
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001544 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001545
1546 /* Ignore interactive and job-control signals. */
1547 signal(SIGINT, SIG_IGN);
1548 signal(SIGQUIT, SIG_IGN);
1549 signal(SIGTSTP, SIG_IGN);
1550 signal(SIGTTIN, SIG_IGN);
1551 signal(SIGTTOU, SIG_IGN);
1552 signal(SIGCHLD, SIG_IGN);
1553
1554 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001555 setsid();
Eric Andersen2d848a42001-06-25 17:11:54 +00001556 shell_pgrp = getpid ();
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001557 setpgid (shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001558
1559 /* Grab control of the terminal. */
1560 tcsetpgrp(shell_terminal, shell_pgrp);
1561}
Eric Andersen6efc48c2000-07-18 08:16:39 +00001562
Matt Kraai2d91deb2001-08-01 17:21:35 +00001563int lash_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001564{
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001565 int opt, interactive=FALSE;
Erik Andersen161220c2000-03-16 08:12:48 +00001566 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001567 argc = argc_l;
1568 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001569
Eric Andersen702ec592001-03-06 22:17:29 +00001570 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001571 last_jobid = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001572 local_pending_command = NULL;
Eric Andersen702ec592001-03-06 22:17:29 +00001573 close_me_head = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001574 job_list.head = NULL;
1575 job_list.fg = NULL;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001576 last_return_code=1;
Eric Andersen501c88b2000-07-28 15:14:45 +00001577
Eric Andersenb558e762000-11-30 22:43:16 +00001578 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001579 FILE *prof_input;
1580 prof_input = fopen("/etc/profile", "r");
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001581 if (prof_input) {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001582 int tmp_fd = fileno(prof_input);
1583 mark_open(tmp_fd);
1584 /* Now run the file */
1585 busy_loop(prof_input);
1586 fclose(prof_input);
1587 mark_closed(tmp_fd);
1588 }
Eric Andersenb558e762000-11-30 22:43:16 +00001589 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001590
Eric Andersenf21aa842000-12-08 20:50:30 +00001591 while ((opt = getopt(argc_l, argv_l, "cxi")) > 0) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001592 switch (opt) {
1593 case 'c':
1594 input = NULL;
Matt Kraai6085c722000-09-06 01:46:18 +00001595 if (local_pending_command != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001596 bb_error_msg_and_die("multiple -c arguments");
1597 local_pending_command = bb_xstrdup(argv[optind]);
Matt Kraai6085c722000-09-06 01:46:18 +00001598 optind++;
1599 argv = argv+optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001600 break;
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001601 case 'i':
1602 interactive = TRUE;
1603 break;
Eric Andersen501c88b2000-07-28 15:14:45 +00001604 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001605 bb_show_usage();
Eric Andersen501c88b2000-07-28 15:14:45 +00001606 }
1607 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001608 /* A shell is interactive if the `-i' flag was given, or if all of
1609 * the following conditions are met:
1610 * no -c command
1611 * no arguments remaining or the -s flag given
1612 * standard input is a terminal
1613 * standard output is a terminal
1614 * Refer to Posix.2, the description of the `sh' utility. */
Eric Andersen86349772000-12-18 20:25:50 +00001615 if (argv[optind]==NULL && input==stdin &&
1616 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
1617 interactive=TRUE;
1618 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001619 setup_job_control();
Eric Andersen86349772000-12-18 20:25:50 +00001620 if (interactive==TRUE) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001621 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Eric Andersen501c88b2000-07-28 15:14:45 +00001622 /* Looks like they want an interactive shell */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001623#ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
Matt Kraai4ef40c02001-04-12 20:44:21 +00001624 printf( "\n\n" BB_BANNER " Built-in shell (lash)\n");
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001625 printf( "Enter 'help' for a list of built-in commands.\n\n");
Eric Andersend63dee42001-10-19 00:22:23 +00001626#endif
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001627 } else if (local_pending_command==NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001628 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001629 input = bb_xfopen(argv[optind], "r");
Eric Andersen8ea28be2001-01-05 20:58:22 +00001630 mark_open(fileno(input)); /* be lazy, never mark this closed */
Eric Andersen501c88b2000-07-28 15:14:45 +00001631 }
1632
Eric Andersen6efc48c2000-07-18 08:16:39 +00001633 /* initialize the cwd -- this is never freed...*/
Eric Andersene5dfced2001-04-09 22:48:12 +00001634 cwd = xgetcwd(0);
Eric Andersen5f265b72001-05-11 16:58:46 +00001635 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001636 cwd = bb_msg_unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001637
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001638#ifdef CONFIG_FEATURE_CLEAN_UP
Eric Andersenfad9c112000-07-25 18:06:52 +00001639 atexit(free_memory);
1640#endif
1641
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001642#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001643 cmdedit_set_initial_prompt();
1644#else
1645 PS1 = NULL;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001646#endif
1647
Erik Andersen161220c2000-03-16 08:12:48 +00001648 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001649}