blob: 22a696785ac9a60d0d69d9e91c03e93143fbbce2 [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 *
5 * Copyright (C) 2000 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 *
8 * Based in part on ladsh.c by Michael K. Johnson and Erik W. Troan, which is
9 * under the following liberal license: "We have placed this source code in the
10 * public domain. Use it in any project, free or commercial."
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
Eric Andersen1e7cea92000-12-06 23:47:38 +000028//
Eric Andersene9f07fb2000-12-21 18:31:36 +000029//This works pretty well now, and is now on by default.
Eric Andersen06f64b22000-09-19 07:16:39 +000030#define BB_FEATURE_SH_ENVIRONMENT
Eric Andersen1e7cea92000-12-06 23:47:38 +000031//
32//Backtick support has some problems, use at your own risk!
33//#define BB_FEATURE_SH_BACKTICKS
34//
Eric Andersen86349772000-12-18 20:25:50 +000035//If, then, else, etc. support.. This should now behave basically
36//like any other Bourne shell...
Eric Andersene9f07fb2000-12-21 18:31:36 +000037#define BB_FEATURE_SH_IF_EXPRESSIONS
Eric Andersen1e7cea92000-12-06 23:47:38 +000038//
Eric Andersen86349772000-12-18 20:25:50 +000039/* This is currently a little broken... */
40//#define HANDLE_CONTINUATION_CHARS
41//
Eric Andersen1e7cea92000-12-06 23:47:38 +000042//For debugging/development on the shell only...
Eric Andersen501c88b2000-07-28 15:14:45 +000043//#define DEBUG_SHELL
Eric Andersena1d187a2000-07-17 19:14:41 +000044
45
Eric Andersen3570a342000-09-25 21:45:58 +000046#include "busybox.h"
Erik Andersen3522eb12000-03-12 23:49:18 +000047#include <stdio.h>
48#include <stdlib.h>
49#include <ctype.h>
50#include <errno.h>
51#include <fcntl.h>
52#include <glob.h>
53#include <signal.h>
54#include <string.h>
55#include <sys/ioctl.h>
56#include <sys/wait.h>
57#include <unistd.h>
Eric Andersen501c88b2000-07-28 15:14:45 +000058#include <getopt.h>
Erik Andersenf0657d32000-04-12 17:49:52 +000059#include "cmdedit.h"
Erik Andersen3522eb12000-03-12 23:49:18 +000060
Eric Andersen86349772000-12-18 20:25:50 +000061#define MAX_LINE 256 /* size of input buffer for cwd data */
Eric Andersenb54833c2000-07-03 23:56:26 +000062#define MAX_READ 128 /* size of input buffer for `read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000063#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
Eric Andersen8c725e62000-11-30 00:27:06 +000064extern size_t NUM_APPLETS;
Erik Andersen3522eb12000-03-12 23:49:18 +000065
Erik Andersend75af992000-03-16 08:09:09 +000066
Eric Andersen86349772000-12-18 20:25:50 +000067enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000068 REDIRECT_APPEND
69};
Erik Andersen3522eb12000-03-12 23:49:18 +000070
Eric Andersen86349772000-12-18 20:25:50 +000071static const unsigned int DEFAULT_CONTEXT=0x1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +000072static const unsigned int IF_TRUE_CONTEXT=0x2;
73static const unsigned int IF_FALSE_CONTEXT=0x4;
74static const unsigned int THEN_EXP_CONTEXT=0x8;
75static const unsigned int ELSE_EXP_CONTEXT=0x10;
Eric Andersen86349772000-12-18 20:25:50 +000076unsigned int shell_context = 0;
Eric Andersene92108a2000-07-26 00:53:56 +000077
Eric Andersenfad9c112000-07-25 18:06:52 +000078
Eric Andersen86349772000-12-18 20:25:50 +000079
80struct jobset {
Erik Andersen161220c2000-03-16 08:12:48 +000081 struct job *head; /* head of list of running jobs */
82 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000083};
84
Eric Andersen86349772000-12-18 20:25:50 +000085struct redir_struct {
86 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000087 int fd; /* file descriptor being redirected */
88 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000089};
90
Eric Andersen86349772000-12-18 20:25:50 +000091struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000092 pid_t pid; /* 0 if exited */
93 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000094 int num_redirects; /* elements in redirection array */
95 struct redir_struct *redirects; /* I/O redirects */
96 glob_t glob_result; /* result of parameter globbing */
97 int free_glob; /* should we globfree(&glob_result)? */
98 int is_stopped; /* is the program currently running? */
99 struct job *family; /* pointer back to the child's parent job */
Erik Andersen3522eb12000-03-12 23:49:18 +0000100};
101
102struct job {
Eric Andersen86349772000-12-18 20:25:50 +0000103 int jobid; /* job number */
104 int num_progs; /* total number of programs in job */
105 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +0000106 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +0000107 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +0000108 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +0000109 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +0000110 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +0000111 int stopped_progs; /* number of programs alive, but stopped */
112 unsigned int job_context; /* bitmask defining current context */
113 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +0000114};
115
Eric Andersen86349772000-12-18 20:25:50 +0000116struct built_in_command {
Erik Andersen161220c2000-03-16 08:12:48 +0000117 char *cmd; /* name */
118 char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +0000119 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +0000120};
121
Eric Andersen34e19412000-07-10 18:47:24 +0000122/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000123static int builtin_cd(struct child_prog *cmd);
124static int builtin_env(struct child_prog *dummy);
125static int builtin_exec(struct child_prog *cmd);
126static int builtin_exit(struct child_prog *cmd);
127static int builtin_fg_bg(struct child_prog *cmd);
128static int builtin_help(struct child_prog *cmd);
129static int builtin_jobs(struct child_prog *dummy);
130static int builtin_pwd(struct child_prog *dummy);
131static int builtin_export(struct child_prog *cmd);
132static int builtin_source(struct child_prog *cmd);
133static int builtin_unset(struct child_prog *cmd);
134static int builtin_read(struct child_prog *cmd);
Eric Andersenfad9c112000-07-25 18:06:52 +0000135#ifdef BB_FEATURE_SH_IF_EXPRESSIONS
Eric Andersen86349772000-12-18 20:25:50 +0000136static int builtin_if(struct child_prog *cmd);
137static int builtin_then(struct child_prog *cmd);
138static int builtin_else(struct child_prog *cmd);
139static int builtin_fi(struct child_prog *cmd);
Eric Andersen70da6a62000-12-20 22:59:16 +0000140/* function prototypes for shell stuff */
141static int run_command_predicate(char *cmd);
Eric Andersenfad9c112000-07-25 18:06:52 +0000142#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000143
Eric Andersen34e19412000-07-10 18:47:24 +0000144
145/* function prototypes for shell stuff */
Eric Andersen86349772000-12-18 20:25:50 +0000146static void checkjobs(struct jobset *job_list);
147static int get_command(FILE * source, char *command);
148static int parse_command(char **command_ptr, struct job *job, int *inbg);
149static int run_command(struct job *newjob, int inbg, int outpipe[2]);
150static int pseudo_exec(struct child_prog *cmd) __attribute__ ((noreturn));
Erik Andersen3522eb12000-03-12 23:49:18 +0000151static int busy_loop(FILE * input);
152
Erik Andersend75af992000-03-16 08:09:09 +0000153
Mark Whitley37653aa2000-07-12 23:36:17 +0000154/* Table of built-in functions (these are non-forking builtins, meaning they
155 * can change global variables in the parent shell process but they will not
156 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Eric Andersen86349772000-12-18 20:25:50 +0000157static struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000158 {"bg", "Resume a job in the background", builtin_fg_bg},
159 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000160 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000161 {"exit", "Exit from shell()", builtin_exit},
162 {"fg", "Bring job into the foreground", builtin_fg_bg},
163 {"jobs", "Lists the active jobs", builtin_jobs},
164 {"export", "Set environment variable", builtin_export},
165 {"unset", "Unset environment variable", builtin_unset},
166 {"read", "Input environment variable", builtin_read},
Matt Kraaidd450a02000-09-13 03:43:36 +0000167 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000168 /* to do: add ulimit */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000169#ifdef BB_FEATURE_SH_IF_EXPRESSIONS
170 {"if", NULL, builtin_if},
171 {"then", NULL, builtin_then},
172 {"else", NULL, builtin_else},
173 {"fi", NULL, builtin_fi},
174#endif
Eric Andersenfad9c112000-07-25 18:06:52 +0000175 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000176};
177
Mark Whitley37653aa2000-07-12 23:36:17 +0000178/* Table of forking built-in functions (things that fork cannot change global
179 * variables in the parent process, such as the current working directory) */
Eric Andersen86349772000-12-18 20:25:50 +0000180static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000181 {"env", "Print all environment variables", builtin_env},
182 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000183 {"help", "List shell built-in commands", builtin_help},
184 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000185};
186
Eric Andersenb558e762000-11-30 22:43:16 +0000187static char prompt[3];
Eric Andersen6efc48c2000-07-18 08:16:39 +0000188static char *cwd;
Eric Andersen1c314ad2000-06-28 16:56:25 +0000189static char *local_pending_command = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000190static char *prompt_str = NULL;
191static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000192static int argc;
193static char **argv;
194#ifdef BB_FEATURE_SH_ENVIRONMENT
Eric Andersen86349772000-12-18 20:25:50 +0000195static int last_bg_pid=-1;
196static int last_return_code=-1;
197static int show_x_trace=FALSE;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000198#endif
Eric Andersen86349772000-12-18 20:25:50 +0000199#ifdef BB_FEATURE_SH_IF_EXPRESSIONS
200static char syntax_err[]="syntax error near unexpected token";
201#endif
202
Eric Andersenb558e762000-11-30 22:43:16 +0000203#ifdef DEBUG_SHELL
204static inline void debug_printf(const char *format, ...)
205{
206 va_list args;
207 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000208 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000209 va_end(args);
210}
211#else
212static inline void debug_printf(const char *format, ...) { }
213#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000214
Erik Andersenf0657d32000-04-12 17:49:52 +0000215#ifdef BB_FEATURE_SH_COMMAND_EDITING
Eric Andersenb558e762000-11-30 22:43:16 +0000216static inline void win_changed(int junk)
Erik Andersenf0657d32000-04-12 17:49:52 +0000217{
Eric Andersenfad04fd2000-07-14 06:49:52 +0000218 struct winsize win = { 0, 0, 0, 0 };
Erik Andersenf0657d32000-04-12 17:49:52 +0000219 ioctl(0, TIOCGWINSZ, &win);
220 if (win.ws_col > 0) {
221 cmdedit_setwidth( win.ws_col - 1);
222 }
223}
Eric Andersenb558e762000-11-30 22:43:16 +0000224#else
225static inline void win_changed(int junk) {}
Erik Andersenf0657d32000-04-12 17:49:52 +0000226#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000227
Eric Andersen86349772000-12-18 20:25:50 +0000228/*
229 Most builtins need access to the struct child_prog that has
230 their arguments, previously coded as cmd->progs[0]. That coding
231 can exhibit a bug, if the builtin is not the first command in
232 a pipeline: "echo foo | exec sort" will attempt to exec foo.
233
234builtin previous use notes
235------ ----------------- ---------
236cd cmd->progs[0]
237env 0
238exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
239exit cmd->progs[0]
240fg_bg cmd->progs[0], job_list->head, job_list->fg
241help 0
242jobs job_list->head
243pwd 0
244export cmd->progs[0] passes cmd, job_list to builtin_env(), which ignores them
245source cmd->progs[0]
246unset cmd->progs[0]
247read cmd->progs[0]
248if cmd->job_context, cmd->text
249then cmd->job_context, cmd->text
250else cmd->job_context, cmd->text
251fi cmd->job_context
252
253The use of cmd->text by if/then/else/fi is hopelessly hacky.
254Would it work to increment cmd->progs[0]->argv and recurse,
255somewhat like builtin_exec does?
256
257I added "struct job *family;" to struct child_prog,
258and switched API to builtin_foo(struct child_prog *child);
259So cmd->text becomes child->family->text
260 cmd->job_context becomes child->family->job_context
261 cmd->progs[0] becomes *child
262 job_list becomes child->family->job_list
263 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000264
Erik Andersend75af992000-03-16 08:09:09 +0000265/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000266static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000267{
Erik Andersen161220c2000-03-16 08:12:48 +0000268 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000269
Eric Andersen86349772000-12-18 20:25:50 +0000270 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000271 newdir = getenv("HOME");
272 else
Eric Andersen86349772000-12-18 20:25:50 +0000273 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000274 if (chdir(newdir)) {
275 printf("cd: %s: %s\n", newdir, strerror(errno));
Matt Kraai3e856ce2000-12-01 02:55:13 +0000276 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000277 }
Eric Andersen6efc48c2000-07-18 08:16:39 +0000278 getcwd(cwd, sizeof(char)*MAX_LINE);
Erik Andersen161220c2000-03-16 08:12:48 +0000279
Matt Kraai3e856ce2000-12-01 02:55:13 +0000280 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000281}
282
283/* built-in 'env' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000284static int builtin_env(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000285{
Erik Andersen161220c2000-03-16 08:12:48 +0000286 char **e;
Erik Andersen3522eb12000-03-12 23:49:18 +0000287
Erik Andersen161220c2000-03-16 08:12:48 +0000288 for (e = environ; *e; e++) {
289 fprintf(stdout, "%s\n", *e);
290 }
291 return (0);
Erik Andersen3522eb12000-03-12 23:49:18 +0000292}
293
Eric Andersend2f56772000-09-21 02:48:07 +0000294/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000295static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000296{
Eric Andersen86349772000-12-18 20:25:50 +0000297 if (child->argv[1] == NULL)
298 return EXIT_SUCCESS; /* Really? */
299 child->argv++;
300 pseudo_exec(child);
301 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000302}
303
Erik Andersen3522eb12000-03-12 23:49:18 +0000304/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000305static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000306{
Eric Andersen86349772000-12-18 20:25:50 +0000307 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000308 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000309
Eric Andersen86349772000-12-18 20:25:50 +0000310 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000311}
312
313/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000314static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000315{
Erik Andersen161220c2000-03-16 08:12:48 +0000316 int i, jobNum;
Erik Andersen6273f652000-03-17 01:12:41 +0000317 struct job *job=NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000318
Eric Andersene9f07fb2000-12-21 18:31:36 +0000319 if (!child->argv[1] || child->argv[2]) {
320 error_msg("%s: exactly one argument is expected\n",
321 child->argv[0]);
322 return EXIT_FAILURE;
323 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000324
Eric Andersene9f07fb2000-12-21 18:31:36 +0000325 if (sscanf(child->argv[1], "%%%d", &jobNum) != 1) {
326 error_msg("%s: bad argument '%s'\n",
327 child->argv[0], child->argv[1]);
328 return EXIT_FAILURE;
329 }
330
331 for (job = child->family->job_list->head; job; job = job->next) {
332 if (job->jobid == jobNum) {
333 break;
Erik Andersen161220c2000-03-16 08:12:48 +0000334 }
Eric Andersene9f07fb2000-12-21 18:31:36 +0000335 }
Erik Andersen161220c2000-03-16 08:12:48 +0000336
337 if (!job) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000338 error_msg("%s: unknown job %d\n",
Eric Andersen86349772000-12-18 20:25:50 +0000339 child->argv[0], jobNum);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000340 return EXIT_FAILURE;
Erik Andersend75af992000-03-16 08:09:09 +0000341 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000342
Eric Andersen86349772000-12-18 20:25:50 +0000343 if (*child->argv[0] == 'f') {
Erik Andersen161220c2000-03-16 08:12:48 +0000344 /* Make this job the foreground job */
Eric Andersen1c314ad2000-06-28 16:56:25 +0000345 /* suppress messages when run from /linuxrc mag@sysgo.de */
346 if (tcsetpgrp(0, job->pgrp) && errno != ENOTTY)
347 perror("tcsetpgrp");
Eric Andersen86349772000-12-18 20:25:50 +0000348 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000349 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000350
Erik Andersen161220c2000-03-16 08:12:48 +0000351 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000352 for (i = 0; i < job->num_progs; i++)
353 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000354
Erik Andersen161220c2000-03-16 08:12:48 +0000355 kill(-job->pgrp, SIGCONT);
Erik Andersen3522eb12000-03-12 23:49:18 +0000356
Eric Andersen86349772000-12-18 20:25:50 +0000357 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000358
Matt Kraai3e856ce2000-12-01 02:55:13 +0000359 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000360}
361
362/* built-in 'help' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000363static int builtin_help(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000364{
Eric Andersen86349772000-12-18 20:25:50 +0000365 struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000366
Eric Andersen86349772000-12-18 20:25:50 +0000367 printf("\nBuilt-in commands:\n");
368 printf("-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000369 for (x = bltins; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000370 if (x->descr==NULL)
371 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000372 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000373 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000374 for (x = bltins_forking; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000375 if (x->descr==NULL)
376 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000377 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000378 }
Eric Andersen86349772000-12-18 20:25:50 +0000379 printf("\n\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000380 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000381}
382
383/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000384static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000385{
Erik Andersen161220c2000-03-16 08:12:48 +0000386 struct job *job;
Eric Andersen86349772000-12-18 20:25:50 +0000387 char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000388
Eric Andersen86349772000-12-18 20:25:50 +0000389 for (job = child->family->job_list->head; job; job = job->next) {
390 if (job->running_progs == job->stopped_progs)
391 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000392 else
Eric Andersen86349772000-12-18 20:25:50 +0000393 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000394
Eric Andersen86349772000-12-18 20:25:50 +0000395 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000396 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000397 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000398}
399
400
401/* built-in 'pwd' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000402static int builtin_pwd(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000403{
Eric Andersen86349772000-12-18 20:25:50 +0000404 getcwd(cwd, MAX_LINE);
Erik Andersen161220c2000-03-16 08:12:48 +0000405 fprintf(stdout, "%s\n", cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000406 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000407}
408
Erik Andersen6273f652000-03-17 01:12:41 +0000409/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000410static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000411{
Erik Andersen161220c2000-03-16 08:12:48 +0000412 int res;
Erik Andersen3522eb12000-03-12 23:49:18 +0000413
Eric Andersen86349772000-12-18 20:25:50 +0000414 if (child->argv[1] == NULL) {
415 return (builtin_env(child));
Erik Andersen161220c2000-03-16 08:12:48 +0000416 }
Eric Andersen86349772000-12-18 20:25:50 +0000417 res = putenv(child->argv[1]);
Erik Andersen161220c2000-03-16 08:12:48 +0000418 if (res)
Eric Andersen86349772000-12-18 20:25:50 +0000419 fprintf(stderr, "export: %s\n", strerror(errno));
Erik Andersen161220c2000-03-16 08:12:48 +0000420 return (res);
Erik Andersen3522eb12000-03-12 23:49:18 +0000421}
422
Eric Andersenb54833c2000-07-03 23:56:26 +0000423/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000424static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000425{
426 int res = 0, len, newlen;
427 char *s;
428 char string[MAX_READ];
429
Eric Andersen86349772000-12-18 20:25:50 +0000430 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000431 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen86349772000-12-18 20:25:50 +0000432 strcpy(string, child->argv[1]);
Eric Andersenb54833c2000-07-03 23:56:26 +0000433 len = strlen(string);
434 string[len++] = '=';
435 string[len] = '\0';
436 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
437 newlen = strlen(string);
438 if(newlen > len)
439 string[--newlen] = '\0'; /* chomp trailing newline */
440 /*
441 ** string should now contain "VAR=<value>"
442 ** copy it (putenv() won't do that, so we must make sure
443 ** the string resides in a static buffer!)
444 */
445 res = -1;
446 if((s = strdup(string)))
447 res = putenv(s);
448 if (res)
Eric Andersen86349772000-12-18 20:25:50 +0000449 fprintf(stderr, "read: %s\n", strerror(errno));
Eric Andersenb54833c2000-07-03 23:56:26 +0000450 }
451 else
452 fgets(string, sizeof(string), stdin);
453
454 return (res);
455}
456
Eric Andersenfad9c112000-07-25 18:06:52 +0000457#ifdef BB_FEATURE_SH_IF_EXPRESSIONS
458/* Built-in handler for 'if' commands */
Eric Andersen86349772000-12-18 20:25:50 +0000459static int builtin_if(struct child_prog *child)
Eric Andersenfad9c112000-07-25 18:06:52 +0000460{
Eric Andersen86349772000-12-18 20:25:50 +0000461 struct job *cmd = child->family;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000462 int status;
463 char* charptr1=cmd->text+3; /* skip over the leading 'if ' */
464
465 /* Now run the 'if' command */
Eric Andersen86349772000-12-18 20:25:50 +0000466 debug_printf( "job=%p entering builtin_if ('%s')-- context=%d\n", cmd, charptr1, cmd->job_context);
467 status = run_command_predicate(charptr1);
468 debug_printf( "if test returned ");
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000469 if (status == 0) {
Eric Andersen86349772000-12-18 20:25:50 +0000470 debug_printf( "TRUE\n");
471 cmd->job_context |= IF_TRUE_CONTEXT;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000472 } else {
Eric Andersen86349772000-12-18 20:25:50 +0000473 debug_printf( "FALSE\n");
474 cmd->job_context |= IF_FALSE_CONTEXT;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000475 }
Eric Andersen86349772000-12-18 20:25:50 +0000476 debug_printf("job=%p builtin_if set job context to %x\n", cmd, cmd->job_context);
477 shell_context++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000478
479 return status;
Eric Andersenfad9c112000-07-25 18:06:52 +0000480}
481
482/* Built-in handler for 'then' (part of the 'if' command) */
Eric Andersen86349772000-12-18 20:25:50 +0000483static int builtin_then(struct child_prog *child)
Eric Andersenfad9c112000-07-25 18:06:52 +0000484{
Eric Andersen86349772000-12-18 20:25:50 +0000485 struct job *cmd = child->family;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000486 char* charptr1=cmd->text+5; /* skip over the leading 'then ' */
487
Eric Andersen86349772000-12-18 20:25:50 +0000488 debug_printf( "job=%p entering builtin_then ('%s')-- context=%d\n", cmd, charptr1, cmd->job_context);
489 if (! (cmd->job_context & (IF_TRUE_CONTEXT|IF_FALSE_CONTEXT))) {
490 shell_context = 0; /* Reset the shell's context on an error */
491 error_msg("%s `then'\n", syntax_err);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000492 return EXIT_FAILURE;
Eric Andersenfad9c112000-07-25 18:06:52 +0000493 }
Eric Andersen86349772000-12-18 20:25:50 +0000494
495 cmd->job_context |= THEN_EXP_CONTEXT;
496 debug_printf("job=%p builtin_then set job context to %x\n", cmd, cmd->job_context);
497
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000498 /* If the if result was FALSE, skip the 'then' stuff */
Eric Andersen86349772000-12-18 20:25:50 +0000499 if (cmd->job_context & IF_FALSE_CONTEXT) {
Matt Kraai3e856ce2000-12-01 02:55:13 +0000500 return EXIT_SUCCESS;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000501 }
502
Eric Andersen86349772000-12-18 20:25:50 +0000503 /* Seems the if result was TRUE, so run the 'then' command */
504 debug_printf( "'then' now running '%s'\n", charptr1);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000505
Eric Andersen86349772000-12-18 20:25:50 +0000506 return(run_command_predicate(charptr1));
Eric Andersenfad9c112000-07-25 18:06:52 +0000507}
508
509/* Built-in handler for 'else' (part of the 'if' command) */
Eric Andersen86349772000-12-18 20:25:50 +0000510static int builtin_else(struct child_prog *child)
Eric Andersenfad9c112000-07-25 18:06:52 +0000511{
Eric Andersen86349772000-12-18 20:25:50 +0000512 struct job *cmd = child->family;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000513 char* charptr1=cmd->text+5; /* skip over the leading 'else ' */
514
Eric Andersen86349772000-12-18 20:25:50 +0000515 debug_printf( "job=%p entering builtin_else ('%s')-- context=%d\n", cmd, charptr1, cmd->job_context);
516
517 if (! (cmd->job_context & THEN_EXP_CONTEXT)) {
518 shell_context = 0; /* Reset the shell's context on an error */
519 error_msg("%s `else'\n", syntax_err);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000520 return EXIT_FAILURE;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000521 }
522 /* If the if result was TRUE, skip the 'else' stuff */
Eric Andersen86349772000-12-18 20:25:50 +0000523 if (cmd->job_context & IF_TRUE_CONTEXT) {
Matt Kraai3e856ce2000-12-01 02:55:13 +0000524 return EXIT_SUCCESS;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000525 }
526
Eric Andersen86349772000-12-18 20:25:50 +0000527 cmd->job_context |= ELSE_EXP_CONTEXT;
Eric Andersene9f07fb2000-12-21 18:31:36 +0000528 debug_printf("job=%p builtin_else set job context to %x\n", cmd, cmd->job_context);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000529
530 /* Now run the 'else' command */
Eric Andersen86349772000-12-18 20:25:50 +0000531 debug_printf( "'else' now running '%s'\n", charptr1);
532 return(run_command_predicate(charptr1));
Eric Andersenfad9c112000-07-25 18:06:52 +0000533}
534
535/* Built-in handler for 'fi' (part of the 'if' command) */
Eric Andersen86349772000-12-18 20:25:50 +0000536static int builtin_fi(struct child_prog *child)
Eric Andersenfad9c112000-07-25 18:06:52 +0000537{
Eric Andersen86349772000-12-18 20:25:50 +0000538 struct job *cmd = child->family;
539 debug_printf( "job=%p entering builtin_fi ('%s')-- context=%d\n", cmd, "", cmd->job_context);
540 if (! (cmd->job_context & (IF_TRUE_CONTEXT|IF_FALSE_CONTEXT))) {
541 shell_context = 0; /* Reset the shell's context on an error */
542 error_msg("%s `fi'\n", syntax_err);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000543 return EXIT_FAILURE;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000544 }
545 /* Clear out the if and then context bits */
Eric Andersen86349772000-12-18 20:25:50 +0000546 cmd->job_context &= ~(IF_TRUE_CONTEXT|IF_FALSE_CONTEXT|THEN_EXP_CONTEXT|ELSE_EXP_CONTEXT);
547 debug_printf("job=%p builtin_fi set job context to %x\n", cmd, cmd->job_context);
548 shell_context--;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000549 return EXIT_SUCCESS;
Eric Andersenfad9c112000-07-25 18:06:52 +0000550}
551#endif
552
Erik Andersen3522eb12000-03-12 23:49:18 +0000553/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000554static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000555{
Erik Andersen161220c2000-03-16 08:12:48 +0000556 FILE *input;
557 int status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000558
Eric Andersen86349772000-12-18 20:25:50 +0000559 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000560 return EXIT_FAILURE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000561
Eric Andersen86349772000-12-18 20:25:50 +0000562 input = fopen(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000563 if (!input) {
Eric Andersen86349772000-12-18 20:25:50 +0000564 fprintf(stdout, "Couldn't open file '%s'\n", child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000565 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000566 }
Erik Andersend75af992000-03-16 08:09:09 +0000567
Erik Andersen161220c2000-03-16 08:12:48 +0000568 /* Now run the file */
569 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000570 fclose(input);
Erik Andersen161220c2000-03-16 08:12:48 +0000571 return (status);
Erik Andersen3522eb12000-03-12 23:49:18 +0000572}
573
574/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000575static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000576{
Eric Andersen86349772000-12-18 20:25:50 +0000577 if (child->argv[1] == NULL) {
Erik Andersen161220c2000-03-16 08:12:48 +0000578 fprintf(stdout, "unset: parameter required.\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000579 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000580 }
Eric Andersen86349772000-12-18 20:25:50 +0000581 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000582 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000583}
584
Eric Andersen70da6a62000-12-20 22:59:16 +0000585#ifdef BB_FEATURE_SH_IF_EXPRESSIONS
Eric Andersen86349772000-12-18 20:25:50 +0000586/* currently used by if/then/else.
Eric Andersene9f07fb2000-12-21 18:31:36 +0000587 *
588 * Reparsing the command line for this purpose is gross,
589 * incorrect, and fundamentally unfixable; in particular,
590 * think about what happens with command substitution.
591 * We really need to pull out the run, wait, return status
592 * functionality out of busy_loop so we can child->argv++
593 * and use that, without going back through parse_command.
Eric Andersen86349772000-12-18 20:25:50 +0000594 */
595static int run_command_predicate(char *cmd)
596{
597 int n=strlen(cmd);
598 local_pending_command = xmalloc(n+1);
599 strncpy(local_pending_command, cmd, n);
600 local_pending_command[n]='\0';
601 return( busy_loop(NULL));
602}
Eric Andersen70da6a62000-12-20 22:59:16 +0000603#endif
Eric Andersen86349772000-12-18 20:25:50 +0000604
Erik Andersen3522eb12000-03-12 23:49:18 +0000605/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000606static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000607{
Erik Andersen161220c2000-03-16 08:12:48 +0000608 int i;
Erik Andersen3522eb12000-03-12 23:49:18 +0000609
Eric Andersen86349772000-12-18 20:25:50 +0000610 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000611 free(cmd->progs[i].argv);
Eric Andersen86349772000-12-18 20:25:50 +0000612 if (cmd->progs[i].redirects)
613 free(cmd->progs[i].redirects);
614 if (cmd->progs[i].free_glob)
615 globfree(&cmd->progs[i].glob_result);
Erik Andersen161220c2000-03-16 08:12:48 +0000616 }
617 free(cmd->progs);
618 if (cmd->text)
619 free(cmd->text);
Eric Andersen86349772000-12-18 20:25:50 +0000620 free(cmd->cmdbuf);
Eric Andersenec10b9d2000-07-14 01:13:11 +0000621 memset(cmd, 0, sizeof(struct job));
Erik Andersen3522eb12000-03-12 23:49:18 +0000622}
623
Eric Andersen86349772000-12-18 20:25:50 +0000624/* remove a job from the job_list */
625static void remove_job(struct jobset *job_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000626{
Eric Andersen86349772000-12-18 20:25:50 +0000627 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000628
Eric Andersen86349772000-12-18 20:25:50 +0000629 free_job(job);
630 if (job == job_list->head) {
631 job_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000632 } else {
Eric Andersen86349772000-12-18 20:25:50 +0000633 prevjob = job_list->head;
634 while (prevjob->next != job)
635 prevjob = prevjob->next;
636 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000637 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000638
Erik Andersen161220c2000-03-16 08:12:48 +0000639 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000640}
641
642/* Checks to see if any background processes have exited -- if they
643 have, figure out why and see if a job has completed */
Eric Andersen86349772000-12-18 20:25:50 +0000644static void checkjobs(struct jobset *job_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000645{
Erik Andersen161220c2000-03-16 08:12:48 +0000646 struct job *job;
647 pid_t childpid;
648 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000649 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000650
Erik Andersen161220c2000-03-16 08:12:48 +0000651 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen86349772000-12-18 20:25:50 +0000652 for (job = job_list->head; job; job = job->next) {
653 prognum = 0;
654 while (prognum < job->num_progs &&
655 job->progs[prognum].pid != childpid) prognum++;
656 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000657 break;
658 }
659
Eric Andersena1d187a2000-07-17 19:14:41 +0000660 /* This happens on backticked commands */
661 if(job==NULL)
662 return;
663
Erik Andersen161220c2000-03-16 08:12:48 +0000664 if (WIFEXITED(status) || WIFSIGNALED(status)) {
665 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000666 job->running_progs--;
667 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000668
Eric Andersen86349772000-12-18 20:25:50 +0000669 if (!job->running_progs) {
670 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
671 remove_job(job_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000672 }
673 } else {
674 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000675 job->stopped_progs++;
676 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000677
Eric Andersen86349772000-12-18 20:25:50 +0000678 if (job->stopped_progs == job->num_progs) {
679 printf(JOB_STATUS_FORMAT, job->jobid, "Stopped",
Erik Andersen161220c2000-03-16 08:12:48 +0000680 job->text);
681 }
682 }
Erik Andersend75af992000-03-16 08:09:09 +0000683 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000684
Erik Andersen161220c2000-03-16 08:12:48 +0000685 if (childpid == -1 && errno != ECHILD)
686 perror("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000687}
688
Eric Andersene9f07fb2000-12-21 18:31:36 +0000689/* squirrel != NULL means we squirrel away copies of stdin, stdout,
690 * and stderr if they are redirected. */
691static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000692{
693 int i;
694 int openfd;
695 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000696 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000697
Eric Andersen86349772000-12-18 20:25:50 +0000698 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000699 switch (redir->type) {
700 case REDIRECT_INPUT:
701 mode = O_RDONLY;
702 break;
703 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000704 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000705 break;
706 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000707 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000708 break;
709 }
710
711 openfd = open(redir->filename, mode, 0666);
712 if (openfd < 0) {
713 /* this could get lost if stderr has been redirected, but
714 bash and ash both lose it as well (though zsh doesn't!) */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000715 error_msg("error opening %s: %s\n", redir->filename,
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000716 strerror(errno));
717 return 1;
718 }
719
720 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000721 if (squirrel && redir->fd < 3) {
722 squirrel[redir->fd] = dup(redir->fd);
723 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000724 dup2(openfd, redir->fd);
725 close(openfd);
726 }
727 }
728
729 return 0;
730}
731
Eric Andersene9f07fb2000-12-21 18:31:36 +0000732static void restore_redirects(int squirrel[])
733{
734 int i, fd;
735 for (i=0; i<3; i++) {
736 fd = squirrel[i];
737 if (fd != -1) {
738 /* No error checking. I sure wouldn't know what
739 * to do with an error if I found one! */
740 dup2(fd, i);
741 close(fd);
742 }
743 }
744}
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000745
Eric Andersen86349772000-12-18 20:25:50 +0000746static int get_command(FILE * source, char *command)
Erik Andersen3522eb12000-03-12 23:49:18 +0000747{
Eric Andersenf361ac22000-12-12 23:45:36 +0000748 char user[9],buf[255],*s;
Eric Andersenb558e762000-11-30 22:43:16 +0000749
Eric Andersen1c314ad2000-06-28 16:56:25 +0000750 if (source == NULL) {
751 if (local_pending_command) {
752 /* a command specified (-c option): return it & mark it done */
753 strcpy(command, local_pending_command);
754 free(local_pending_command);
755 local_pending_command = NULL;
756 return 0;
757 }
758 return 1;
759 }
760
Eric Andersen86349772000-12-18 20:25:50 +0000761 if (shell_context == 0) {
762 /* get User Name and setup prompt */
763 strcpy(prompt,( geteuid() != 0 ) ? "$ ":"# ");
764 my_getpwuid(user, geteuid());
765
766 /* get HostName */
767 gethostname(buf, 255);
768 s = strchr(buf, '.');
769 if (s) {
770 *s = 0;
771 }
772 } else {
773 strcpy(prompt,"> ");
774 }
Eric Andersenb558e762000-11-30 22:43:16 +0000775
Erik Andersen161220c2000-03-16 08:12:48 +0000776 if (source == stdin) {
Erik Andersend75af992000-03-16 08:09:09 +0000777#ifdef BB_FEATURE_SH_COMMAND_EDITING
Erik Andersenc7c634b2000-03-19 05:28:55 +0000778 int len;
Eric Andersen501c88b2000-07-28 15:14:45 +0000779
780 /*
781 ** enable command line editing only while a command line
782 ** is actually being read; otherwise, we'll end up bequeathing
783 ** atexit() handlers and other unwanted stuff to our
784 ** child processes (rob@sysgo.de)
785 */
786 cmdedit_init();
787 signal(SIGWINCH, win_changed);
Eric Andersen86349772000-12-18 20:25:50 +0000788 debug_printf( "in get_command() -- job_context=%d\n", shell_context);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000789 fflush(stdout);
Eric Andersen86349772000-12-18 20:25:50 +0000790 if (shell_context == 0) {
791 len=fprintf(stdout, "[%s@%s %s]%s", user, buf,
792 get_last_path_component(cwd), prompt);
793 } else {
794 len=fprintf(stdout, "%s", prompt);
795 }
796 fflush(stdout);
797 prompt_str=(char*)xmalloc(sizeof(char)*(len+1));
798 if (shell_context == 0) {
799 sprintf(prompt_str, "[%s@%s %s]%s", user, buf,
800 get_last_path_component(cwd), prompt);
801 } else {
802 sprintf(prompt_str, "%s", prompt);
803 }
804 cmdedit_read_input(prompt_str, command);
805 free( prompt_str);
Eric Andersen501c88b2000-07-28 15:14:45 +0000806 cmdedit_terminate();
807 signal(SIGWINCH, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +0000808 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000809#else
Eric Andersenb558e762000-11-30 22:43:16 +0000810 i=strlen(cwd);
811 i--;
812 if (i>1){
813 while ((i>0) && (*(cwd+i)!='/') ) i--;
814 if (*(cwd+i)=='/') i++;
815 }
816
817 fprintf(stdout, "[%s@%s %s]%s",user, buf, (cwd+i), prompt);
Erik Andersenc7c634b2000-03-19 05:28:55 +0000818 fflush(stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000819#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000820 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000821
Erik Andersen161220c2000-03-16 08:12:48 +0000822 if (!fgets(command, BUFSIZ - 2, source)) {
823 if (source == stdin)
824 printf("\n");
825 return 1;
826 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000827
Erik Andersen161220c2000-03-16 08:12:48 +0000828 /* remove trailing newline */
829 command[strlen(command) - 1] = '\0';
Erik Andersen3522eb12000-03-12 23:49:18 +0000830
Erik Andersen161220c2000-03-16 08:12:48 +0000831 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000832}
833
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000834#ifdef BB_FEATURE_SH_ENVIRONMENT
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000835static char* itoa(register int i)
836{
Eric Andersenb558e762000-11-30 22:43:16 +0000837 static char a[7]; /* Max 7 ints */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000838 register char *b = a + sizeof(a) - 1;
839 int sign = (i < 0);
840
841 if (sign)
842 i = -i;
843 *b = 0;
844 do
845 {
846 *--b = '0' + (i % 10);
847 i /= 10;
848 }
849 while (i);
850 if (sign)
851 *--b = '-';
852 return b;
853}
854#endif
855
Eric Andersen86349772000-12-18 20:25:50 +0000856static void expand_argument(struct child_prog *prog, int *argcPtr,
857 int *argv_alloced_ptr)
Erik Andersen3522eb12000-03-12 23:49:18 +0000858{
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000859 int argc_l = *argcPtr;
Eric Andersen86349772000-12-18 20:25:50 +0000860 int argv_alloced = *argv_alloced_ptr;
Erik Andersen161220c2000-03-16 08:12:48 +0000861 int rc;
862 int flags;
863 int i;
Eric Andersenb54833c2000-07-03 23:56:26 +0000864 char *src, *dst, *var;
Erik Andersen3522eb12000-03-12 23:49:18 +0000865
Eric Andersen86349772000-12-18 20:25:50 +0000866 if (argc_l > 1) { /* cmd->glob_result is already initialized */
Erik Andersen161220c2000-03-16 08:12:48 +0000867 flags = GLOB_APPEND;
Eric Andersen86349772000-12-18 20:25:50 +0000868 i = prog->glob_result.gl_pathc;
Erik Andersen161220c2000-03-16 08:12:48 +0000869 } else {
Eric Andersen86349772000-12-18 20:25:50 +0000870 prog->free_glob = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000871 flags = 0;
872 i = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000873 }
Eric Andersenb54833c2000-07-03 23:56:26 +0000874 /* do shell variable substitution */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000875 if(*prog->argv[argc_l - 1] == '$') {
876 if ((var = getenv(prog->argv[argc_l - 1] + 1))) {
877 prog->argv[argc_l - 1] = var;
878 }
879#ifdef BB_FEATURE_SH_ENVIRONMENT
880 else {
881 switch(*(prog->argv[argc_l - 1] + 1)) {
882 case '?':
Eric Andersen86349772000-12-18 20:25:50 +0000883 prog->argv[argc_l - 1] = itoa(last_return_code);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000884 break;
885 case '$':
886 prog->argv[argc_l - 1] = itoa(getpid());
887 break;
888 case '#':
889 prog->argv[argc_l - 1] = itoa(argc-1);
890 break;
891 case '!':
Eric Andersen86349772000-12-18 20:25:50 +0000892 if (last_bg_pid==-1)
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000893 *(prog->argv[argc_l - 1])='\0';
894 else
Eric Andersen86349772000-12-18 20:25:50 +0000895 prog->argv[argc_l - 1] = itoa(last_bg_pid);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000896 break;
897 case '0':case '1':case '2':case '3':case '4':
898 case '5':case '6':case '7':case '8':case '9':
899 {
900 int index=*(prog->argv[argc_l - 1] + 1)-48;
901 if (index >= argc) {
902 *(prog->argv[argc_l - 1])='\0';
903 } else {
904 prog->argv[argc_l - 1] = argv[index];
905 }
906 }
907 break;
908 }
909 }
910#endif
911 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000912
Eric Andersen46f0beb2000-11-14 21:59:22 +0000913 if (strpbrk(prog->argv[argc_l - 1],"*[]?")!= NULL){
Eric Andersen86349772000-12-18 20:25:50 +0000914 rc = glob(prog->argv[argc_l - 1], flags, NULL, &prog->glob_result);
Eric Andersen46f0beb2000-11-14 21:59:22 +0000915 if (rc == GLOB_NOSPACE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000916 error_msg("out of space during glob operation\n");
Eric Andersen46f0beb2000-11-14 21:59:22 +0000917 return;
918 } else if (rc == GLOB_NOMATCH ||
Eric Andersen86349772000-12-18 20:25:50 +0000919 (!rc && (prog->glob_result.gl_pathc - i) == 1 &&
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000920 strcmp(prog->argv[argc_l - 1],
Eric Andersen86349772000-12-18 20:25:50 +0000921 prog->glob_result.gl_pathv[i]) == 0)) {
Eric Andersen46f0beb2000-11-14 21:59:22 +0000922 /* we need to remove whatever \ quoting is still present */
923 src = dst = prog->argv[argc_l - 1];
924 while (*src) {
Eric Andersen8c2d3f42000-11-30 00:03:57 +0000925 if (*src == '\\') {
926 src++;
927 *dst++ = process_escape_sequence(&src);
928 } else {
Eric Andersen46f0beb2000-11-14 21:59:22 +0000929 *dst++ = *src;
Eric Andersen8c2d3f42000-11-30 00:03:57 +0000930 src++;
931 }
Eric Andersen46f0beb2000-11-14 21:59:22 +0000932 }
933 *dst = '\0';
934 } else if (!rc) {
Eric Andersen86349772000-12-18 20:25:50 +0000935 argv_alloced += (prog->glob_result.gl_pathc - i);
936 prog->argv = xrealloc(prog->argv, argv_alloced * sizeof(*prog->argv));
937 memcpy(prog->argv + (argc_l - 1), prog->glob_result.gl_pathv + i,
938 sizeof(*(prog->argv)) * (prog->glob_result.gl_pathc - i));
939 argc_l += (prog->glob_result.gl_pathc - i - 1);
Erik Andersen161220c2000-03-16 08:12:48 +0000940 }
Eric Andersen46f0beb2000-11-14 21:59:22 +0000941 }else{
942 src = dst = prog->argv[argc_l - 1];
943 while (*src) {
Eric Andersen8c2d3f42000-11-30 00:03:57 +0000944 if (*src == '\\') {
945 src++;
946 *dst++ = process_escape_sequence(&src);
947 } else {
Eric Andersen46f0beb2000-11-14 21:59:22 +0000948 *dst++ = *src;
Eric Andersen8c2d3f42000-11-30 00:03:57 +0000949 src++;
950 }
Eric Andersen46f0beb2000-11-14 21:59:22 +0000951 }
952 *dst = '\0';
Eric Andersen8c2d3f42000-11-30 00:03:57 +0000953
Eric Andersen86349772000-12-18 20:25:50 +0000954 prog->glob_result.gl_pathc=0;
Eric Andersen46f0beb2000-11-14 21:59:22 +0000955 if (flags==0)
Eric Andersen86349772000-12-18 20:25:50 +0000956 prog->glob_result.gl_pathv=NULL;
Erik Andersen161220c2000-03-16 08:12:48 +0000957 }
Eric Andersen86349772000-12-18 20:25:50 +0000958 *argv_alloced_ptr = argv_alloced;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000959 *argcPtr = argc_l;
Erik Andersen3522eb12000-03-12 23:49:18 +0000960}
961
Eric Andersen86349772000-12-18 20:25:50 +0000962/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
963 line). If a valid command is found, command_ptr is set to point to
Erik Andersen3522eb12000-03-12 23:49:18 +0000964 the beginning of the next command (if the original command had more
965 then one job associated with it) or NULL if no more commands are
966 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000967static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000968{
Erik Andersen161220c2000-03-16 08:12:48 +0000969 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000970 char *return_command = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +0000971 char *src, *buf, *chptr;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000972 int argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000973 int done = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000974 int argv_alloced;
Erik Andersen161220c2000-03-16 08:12:48 +0000975 int i;
976 char quote = '\0';
977 int count;
Eric Andersen86349772000-12-18 20:25:50 +0000978 struct child_prog *prog;
Erik Andersen3522eb12000-03-12 23:49:18 +0000979
Erik Andersen161220c2000-03-16 08:12:48 +0000980 /* skip leading white space */
Eric Andersen86349772000-12-18 20:25:50 +0000981 while (**command_ptr && isspace(**command_ptr))
982 (*command_ptr)++;
Erik Andersen3522eb12000-03-12 23:49:18 +0000983
Erik Andersen161220c2000-03-16 08:12:48 +0000984 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000985 if (!**command_ptr || (**command_ptr == '#')) {
986 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000987 return 0;
988 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000989
Eric Andersen86349772000-12-18 20:25:50 +0000990 *inbg = 0;
991 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000992 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000993
Erik Andersen161220c2000-03-16 08:12:48 +0000994 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000995 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000996 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000997
Erik Andersen161220c2000-03-16 08:12:48 +0000998 Getting clean memory relieves us of the task of NULL
999 terminating things and makes the rest of this look a bit
1000 cleaner (though it is, admittedly, a tad less efficient) */
Eric Andersen86349772000-12-18 20:25:50 +00001001 job->cmdbuf = command = xcalloc(2*strlen(*command_ptr) + 1, sizeof(char));
Erik Andersen161220c2000-03-16 08:12:48 +00001002 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001003
Erik Andersen161220c2000-03-16 08:12:48 +00001004 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +00001005 prog->num_redirects = 0;
1006 prog->redirects = NULL;
1007 prog->free_glob = 0;
1008 prog->is_stopped = 0;
1009 prog->family = job;
Erik Andersen3522eb12000-03-12 23:49:18 +00001010
Eric Andersen86349772000-12-18 20:25:50 +00001011 argv_alloced = 5;
1012 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
1013 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +00001014
Erik Andersen161220c2000-03-16 08:12:48 +00001015 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +00001016 src = *command_ptr;
Erik Andersen161220c2000-03-16 08:12:48 +00001017 while (*src && !done) {
1018 if (quote == *src) {
1019 quote = '\0';
1020 } else if (quote) {
1021 if (*src == '\\') {
1022 src++;
1023 if (!*src) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001024 error_msg("character expected after \\\n");
Eric Andersen86349772000-12-18 20:25:50 +00001025 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001026 return 1;
1027 }
1028
1029 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +00001030 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +00001031 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +00001032 *buf++ = '\\';
1033 }
Erik Andersen161220c2000-03-16 08:12:48 +00001034 } else if (*src == '*' || *src == '?' || *src == '[' ||
1035 *src == ']') *buf++ = '\\';
1036 *buf++ = *src;
1037 } else if (isspace(*src)) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001038 if (*prog->argv[argc_l]) {
1039 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001040 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +00001041 if ((argc_l + 1) == argv_alloced) {
1042 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +00001043 prog->argv = xrealloc(prog->argv,
1044 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +00001045 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001046 }
Eric Andersen86349772000-12-18 20:25:50 +00001047 expand_argument(prog, &argc_l, &argv_alloced);
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001048 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001049 }
1050 } else
1051 switch (*src) {
1052 case '"':
1053 case '\'':
1054 quote = *src;
1055 break;
1056
1057 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001058 if (*(src-1)== '$')
1059 *buf++ = *src;
1060 else
1061 done = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001062 break;
1063
Eric Andersen86349772000-12-18 20:25:50 +00001064 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +00001065 case '<':
Eric Andersen86349772000-12-18 20:25:50 +00001066 i = prog->num_redirects++;
1067 prog->redirects = xrealloc(prog->redirects,
1068 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +00001069 (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +00001070
Eric Andersen86349772000-12-18 20:25:50 +00001071 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001072 if (buf != prog->argv[argc_l]) {
Erik Andersen161220c2000-03-16 08:12:48 +00001073 /* the stuff before this character may be the file number
1074 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +00001075 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001076 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +00001077
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001078 if (*chptr && *prog->argv[argc_l]) {
1079 buf++, argc_l++;
Eric Andersen86349772000-12-18 20:25:50 +00001080 expand_argument(prog, &argc_l, &argv_alloced);
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001081 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001082 }
1083 }
1084
Eric Andersen86349772000-12-18 20:25:50 +00001085 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001086 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001087 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001088 else
Eric Andersen86349772000-12-18 20:25:50 +00001089 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001090 }
1091
1092 if (*src++ == '>') {
1093 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001094 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001095 REDIRECT_APPEND, src++;
1096 else
Eric Andersen86349772000-12-18 20:25:50 +00001097 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001098 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001099 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001100 }
1101
1102 /* This isn't POSIX sh compliant. Oh well. */
1103 chptr = src;
1104 while (isspace(*chptr))
1105 chptr++;
1106
1107 if (!*chptr) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001108 error_msg("file name expected after %c\n", *src);
Eric Andersen86349772000-12-18 20:25:50 +00001109 free_job(job);
1110 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001111 return 1;
1112 }
1113
Eric Andersen86349772000-12-18 20:25:50 +00001114 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001115 while (*chptr && !isspace(*chptr))
1116 *buf++ = *chptr++;
1117
1118 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001119 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001120 break;
1121
1122 case '|': /* pipe */
1123 /* finish this command */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001124 if (*prog->argv[argc_l])
1125 argc_l++;
1126 if (!argc_l) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001127 error_msg("empty command in pipe\n");
Eric Andersen86349772000-12-18 20:25:50 +00001128 free_job(job);
1129 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001130 return 1;
1131 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001132 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001133
1134 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001135 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001136 job->progs = xrealloc(job->progs,
Eric Andersen86349772000-12-18 20:25:50 +00001137 sizeof(*job->progs) * job->num_progs);
1138 prog = job->progs + (job->num_progs - 1);
1139 prog->num_redirects = 0;
1140 prog->redirects = NULL;
1141 prog->free_glob = 0;
1142 prog->is_stopped = 0;
1143 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001144 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001145
Eric Andersen86349772000-12-18 20:25:50 +00001146 argv_alloced = 5;
1147 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001148 prog->argv[0] = ++buf;
1149
1150 src++;
1151 while (*src && isspace(*src))
1152 src++;
1153
1154 if (!*src) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001155 error_msg("empty command in pipe\n");
Eric Andersen86349772000-12-18 20:25:50 +00001156 free_job(job);
1157 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001158 return 1;
1159 }
1160 src--; /* we'll ++ it at the end of the loop */
1161
1162 break;
1163
1164 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001165 *inbg = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001166 case ';': /* multiple commands */
1167 done = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001168 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001169 break;
1170
Eric Andersena1d187a2000-07-17 19:14:41 +00001171#ifdef BB_FEATURE_SH_BACKTICKS
Eric Andersenec10b9d2000-07-14 01:13:11 +00001172 case '`':
1173 /* Exec a backtick-ed command */
1174 {
Eric Andersena1d187a2000-07-17 19:14:41 +00001175 char* charptr1=NULL, *charptr2;
Eric Andersenec10b9d2000-07-14 01:13:11 +00001176 char* ptr=NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001177 struct job *newjob;
1178 struct jobset njob_list = { NULL, NULL };
Eric Andersena1d187a2000-07-17 19:14:41 +00001179 int pipefd[2];
1180 int size;
Eric Andersenec10b9d2000-07-14 01:13:11 +00001181
1182 ptr=strchr(++src, '`');
1183 if (ptr==NULL) {
1184 fprintf(stderr, "Unmatched '`' in command\n");
Eric Andersen86349772000-12-18 20:25:50 +00001185 free_job(job);
Eric Andersenec10b9d2000-07-14 01:13:11 +00001186 return 1;
1187 }
1188
Eric Andersena1d187a2000-07-17 19:14:41 +00001189 /* Make some space to hold just the backticked command */
Eric Andersen6efc48c2000-07-18 08:16:39 +00001190 charptr1 = charptr2 = xmalloc(1+ptr-src);
Matt Kraai0b2da462000-09-19 06:46:44 +00001191 memcpy(charptr1, src, ptr-src);
1192 charptr1[ptr-src] = '\0';
Eric Andersen86349772000-12-18 20:25:50 +00001193 newjob = xmalloc(sizeof(struct job));
1194 newjob->job_list = &njob_list;
Eric Andersena1d187a2000-07-17 19:14:41 +00001195 /* Now parse and run the backticked command */
Eric Andersen86349772000-12-18 20:25:50 +00001196 if (!parse_command(&charptr1, newjob, inbg)
1197 && newjob->num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001198 pipe(pipefd);
Eric Andersen86349772000-12-18 20:25:50 +00001199 run_command(newjob, 0, pipefd);
Eric Andersenec10b9d2000-07-14 01:13:11 +00001200 }
Eric Andersen86349772000-12-18 20:25:50 +00001201 checkjobs(job->job_list);
1202 free_job(newjob); /* doesn't actually free newjob,
1203 looks like a memory leak */
Eric Andersen6efc48c2000-07-18 08:16:39 +00001204 free(charptr2);
1205
1206 /* Make a copy of any stuff left over in the command
1207 * line after the second backtick */
1208 charptr2 = xmalloc(strlen(ptr)+1);
1209 memcpy(charptr2, ptr+1, strlen(ptr));
1210
Eric Andersenec10b9d2000-07-14 01:13:11 +00001211
Eric Andersena1d187a2000-07-17 19:14:41 +00001212 /* Copy the output from the backtick-ed command into the
1213 * command line, making extra room as needed */
1214 --src;
1215 charptr1 = xmalloc(BUFSIZ);
Mark Whitleyf57c9442000-12-07 19:56:48 +00001216 while ( (size=full_read(pipefd[0], charptr1, BUFSIZ-1)) >0) {
Eric Andersen86349772000-12-18 20:25:50 +00001217 int newsize=src - *command_ptr + size + 1 + strlen(charptr2);
1218 if (newsize > BUFSIZ) {
1219 *command_ptr=xrealloc(*command_ptr, newsize);
Eric Andersena1d187a2000-07-17 19:14:41 +00001220 }
1221 memcpy(src, charptr1, size);
1222 src+=size;
1223 }
1224 free(charptr1);
1225 close(pipefd[0]);
1226 if (*(src-1)=='\n')
1227 --src;
1228
Eric Andersen86349772000-12-18 20:25:50 +00001229 /* Now paste into the *command_ptr all the stuff
Eric Andersena1d187a2000-07-17 19:14:41 +00001230 * leftover after the second backtick */
Matt Kraaicbbe4d62000-09-14 00:26:50 +00001231 memcpy(src, charptr2, strlen(charptr2)+1);
Eric Andersena1d187a2000-07-17 19:14:41 +00001232 free(charptr2);
1233
Eric Andersen86349772000-12-18 20:25:50 +00001234 /* Now recursively call parse_command to deal with the new
Eric Andersena1d187a2000-07-17 19:14:41 +00001235 * and improved version of the command line with the backtick
1236 * results expanded in place... */
Eric Andersen86349772000-12-18 20:25:50 +00001237 {
1238 struct jobset *jl=job->job_list;
1239 free_job(job);
1240 job->job_list = jl;
1241 }
1242 return(parse_command(command_ptr, job, inbg));
Eric Andersenec10b9d2000-07-14 01:13:11 +00001243 }
1244 break;
Eric Andersena1d187a2000-07-17 19:14:41 +00001245#endif // BB_FEATURE_SH_BACKTICKS
Matt Kraai131241f2000-09-14 00:43:20 +00001246
1247 case '\\':
1248 src++;
1249 if (!*src) {
Eric Andersen86349772000-12-18 20:25:50 +00001250/* This is currently a little broken... */
1251#ifdef HANDLE_CONTINUATION_CHARS
1252 /* They fed us a continuation char, so continue reading stuff
1253 * on the next line, then tack that onto the end of the current
1254 * command */
1255 char *command;
1256 int newsize;
1257 printf("erik: found a continue char at EOL...\n");
1258 command = (char *) xcalloc(BUFSIZ, sizeof(char));
1259 if (get_command(input, command)) {
1260 error_msg("character expected after \\\n");
1261 free(command);
1262 free_job(job);
1263 return 1;
1264 }
1265 newsize = strlen(*command_ptr) + strlen(command) + 2;
1266 if (newsize > BUFSIZ) {
1267 printf("erik: doing realloc\n");
1268 *command_ptr=xrealloc(*command_ptr, newsize);
1269 }
1270 printf("erik: A: *command_ptr='%s'\n", *command_ptr);
1271 memcpy(--src, command, strlen(command));
1272 printf("erik: B: *command_ptr='%s'\n", *command_ptr);
1273 free(command);
1274 break;
1275#else
Mark Whitleyf57c9442000-12-07 19:56:48 +00001276 error_msg("character expected after \\\n");
Eric Andersen86349772000-12-18 20:25:50 +00001277 free(command);
1278 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001279 return 1;
Eric Andersen86349772000-12-18 20:25:50 +00001280#endif
Matt Kraai131241f2000-09-14 00:43:20 +00001281 }
1282 if (*src == '*' || *src == '[' || *src == ']'
1283 || *src == '?') *buf++ = '\\';
1284 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001285 default:
1286 *buf++ = *src;
1287 }
1288
Erik Andersend75af992000-03-16 08:09:09 +00001289 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001290 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001291
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001292 if (*prog->argv[argc_l]) {
1293 argc_l++;
Eric Andersen86349772000-12-18 20:25:50 +00001294 expand_argument(prog, &argc_l, &argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001295 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001296 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001297 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001298 return 0;
1299 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001300 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001301
Eric Andersen86349772000-12-18 20:25:50 +00001302 if (!return_command) {
1303 job->text = xmalloc(strlen(*command_ptr) + 1);
1304 strcpy(job->text, *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001305 } else {
1306 /* This leaves any trailing spaces, which is a bit sloppy */
Eric Andersen86349772000-12-18 20:25:50 +00001307 count = return_command - *command_ptr;
Eric Andersenec10b9d2000-07-14 01:13:11 +00001308 job->text = xmalloc(count + 1);
Eric Andersen86349772000-12-18 20:25:50 +00001309 strncpy(job->text, *command_ptr, count);
Erik Andersen161220c2000-03-16 08:12:48 +00001310 job->text[count] = '\0';
1311 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001312
Eric Andersen86349772000-12-18 20:25:50 +00001313 *command_ptr = return_command;
Eric Andersen46f0beb2000-11-14 21:59:22 +00001314
Erik Andersend75af992000-03-16 08:09:09 +00001315 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001316}
1317
Eric Andersen86349772000-12-18 20:25:50 +00001318/* Run the child_prog, no matter what kind of command it uses.
1319 */
1320static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001321{
Eric Andersen86349772000-12-18 20:25:50 +00001322 struct built_in_command *x;
Eric Andersenb54833c2000-07-03 23:56:26 +00001323#ifdef BB_FEATURE_SH_STANDALONE_SHELL
Eric Andersena683ee82000-11-17 18:51:45 +00001324 struct BB_applet search_applet, *applet;
Erik Andersenbcd61772000-05-13 06:33:19 +00001325#endif
Erik Andersen3522eb12000-03-12 23:49:18 +00001326
Eric Andersene9f07fb2000-12-21 18:31:36 +00001327 /* Check if the command matches any of the non-forking builtins.
1328 * Depending on context, this might be redundant. But it's
1329 * easier to waste a few CPU cycles than it is to figure out
1330 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001331 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001332 for (x = bltins; x->cmd; x++) {
1333 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1334 exit(x->function(child));
1335 }
1336 }
1337
1338 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001339 for (x = bltins_forking; x->cmd; x++) {
1340 if (strcmp(child->argv[0], x->cmd) == 0) {
1341 applet_name=x->cmd;
1342 exit (x->function(child));
1343 }
1344 }
1345#ifdef BB_FEATURE_SH_STANDALONE_SHELL
1346 /* Check if the command matches any busybox internal
1347 * commands ("applets") here. Following discussions from
1348 * November 2000 on busybox@opensource.lineo.com, don't use
1349 * get_last_path_component(). This way explicit (with
1350 * slashes) filenames will never be interpreted as an
1351 * applet, just like with builtins. This way the user can
1352 * override an applet with an explicit filename reference.
1353 * The only downside to this change is that an explicit
1354 * /bin/foo invocation will fork and exec /bin/foo, even if
1355 * /bin/foo is a symlink to busybox.
1356 */
1357 search_applet.name = child->argv[0];
1358
1359#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
1360 /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then
1361 * if you run /bin/cat, it will use BusyBox cat even if
1362 * /bin/cat exists on the filesystem and is _not_ busybox.
1363 * Some systems want this, others do not. Choose wisely. :-)
1364 */
1365 search_applet.name = get_last_path_component(search_applet.name);
1366#endif
1367
1368 /* Do a binary search to find the applet entry given the name. */
1369 applet = bsearch(&search_applet, applets, NUM_APPLETS,
1370 sizeof(struct BB_applet), applet_name_compare);
1371 if (applet != NULL) {
1372 int argc_l;
1373 char** argv=child->argv;
1374 for(argc_l=0;*argv!=NULL; argv++, argc_l++);
1375 applet_name=applet->name;
1376 optind = 1;
1377 exit((*(applet->main)) (argc_l, child->argv));
1378 }
1379#endif
1380
1381 execvp(child->argv[0], child->argv);
1382 error_msg_and_die("%s: %s\n", child->argv[0],
1383 strerror(errno));
1384}
1385
1386static void insert_job(struct job *newjob, int inbg)
1387{
1388 struct job *thejob;
1389 struct jobset *job_list=newjob->job_list;
1390
1391 /* find the ID for thejob to use */
1392 newjob->jobid = 1;
1393 for (thejob = job_list->head; thejob; thejob = thejob->next)
1394 if (thejob->jobid >= newjob->jobid)
1395 newjob->jobid = thejob->jobid + 1;
1396
1397 /* add thejob to the list of running jobs */
1398 if (!job_list->head) {
1399 thejob = job_list->head = xmalloc(sizeof(*thejob));
1400 } else {
1401 for (thejob = job_list->head; thejob->next; thejob = thejob->next) /* nothing */;
1402 thejob->next = xmalloc(sizeof(*thejob));
1403 thejob = thejob->next;
1404 }
1405
1406 *thejob = *newjob; /* physically copy the struct job */
1407 thejob->next = NULL;
1408 thejob->running_progs = thejob->num_progs;
1409 thejob->stopped_progs = 0;
1410
1411 if (inbg) {
1412 /* we don't wait for background thejobs to return -- append it
1413 to the list of backgrounded thejobs and leave it alone */
1414 printf("[%d] %d\n", thejob->jobid,
1415 newjob->progs[newjob->num_progs - 1].pid);
1416#ifdef BB_FEATURE_SH_ENVIRONMENT
1417 last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
1418#endif
1419 } else {
1420 newjob->job_list->fg = thejob;
1421
1422 /* move the new process group into the foreground */
1423 /* suppress messages when run from /linuxrc mag@sysgo.de */
1424 if (tcsetpgrp(0, newjob->pgrp) && errno != ENOTTY)
1425 perror("tcsetpgrp");
1426 }
1427}
1428
1429static int run_command(struct job *newjob, int inbg, int outpipe[2])
1430{
1431 /* struct job *thejob; */
1432 int i;
1433 int nextin, nextout;
1434 int pipefds[2]; /* pipefd[0] is for reading */
1435 struct built_in_command *x;
1436 struct child_prog *child;
1437
Eric Andersen6efc48c2000-07-18 08:16:39 +00001438 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001439 for (i = 0; i < newjob->num_progs; i++) {
1440 child = & (newjob->progs[i]);
1441
1442 if ((i + 1) < newjob->num_progs) {
1443 if (pipe(pipefds)<0) perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001444 nextout = pipefds[1];
1445 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001446 if (outpipe[1]!=-1) {
1447 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001448 } else {
1449 nextout = 1;
1450 }
Erik Andersen161220c2000-03-16 08:12:48 +00001451 }
1452
Eric Andersen501c88b2000-07-28 15:14:45 +00001453#ifdef BB_FEATURE_SH_ENVIRONMENT
Eric Andersen86349772000-12-18 20:25:50 +00001454 if (show_x_trace==TRUE) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001455 int j;
Eric Andersen86349772000-12-18 20:25:50 +00001456 fputc('+', stderr);
1457 for (j = 0; child->argv[j]; j++) {
1458 fputc(' ', stderr);
1459 fputs(child->argv[j], stderr);
1460 }
1461 fputc('\n', stderr);
Eric Andersen501c88b2000-07-28 15:14:45 +00001462 }
1463#endif
1464
Eric Andersene9f07fb2000-12-21 18:31:36 +00001465 /* Check if the command matches any non-forking builtins,
1466 * but only if this is a simple command.
1467 * Non-forking builtins within pipes have to fork anyway,
1468 * and are handled in pseudo_exec. "echo foo | read bar"
1469 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001470 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001471 if (newjob->num_progs == 1) {
1472 for (x = bltins; x->cmd; x++) {
1473 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1474 int squirrel[] = {-1, -1, -1};
1475 int rcode;
1476 setup_redirects(child, squirrel);
1477 rcode = x->function(child);
1478 restore_redirects(squirrel);
1479 return rcode;
1480 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001481 }
1482 }
1483
Eric Andersen86349772000-12-18 20:25:50 +00001484 if (!(child->pid = fork())) {
Erik Andersen161220c2000-03-16 08:12:48 +00001485 signal(SIGTTOU, SIG_DFL);
1486
Eric Andersen86349772000-12-18 20:25:50 +00001487 if (outpipe[1]!=-1) {
1488 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001489 }
1490 if (nextin != 0) {
1491 dup2(nextin, 0);
1492 close(nextin);
1493 }
1494
1495 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001496 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001497 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001498 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001499 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001500 }
1501
Eric Andersen86349772000-12-18 20:25:50 +00001502 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001503 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001504
Eric Andersen86349772000-12-18 20:25:50 +00001505 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001506 }
Eric Andersen86349772000-12-18 20:25:50 +00001507 if (outpipe[1]!=-1) {
1508 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001509 }
Erik Andersen161220c2000-03-16 08:12:48 +00001510
1511 /* put our child in the process group whose leader is the
1512 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001513 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001514 if (nextin != 0)
1515 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001516 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001517 close(nextout);
1518
1519 /* If there isn't another process, nextin is garbage
1520 but it doesn't matter */
1521 nextin = pipefds[0];
1522 }
1523
Eric Andersen86349772000-12-18 20:25:50 +00001524 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001525
Eric Andersen86349772000-12-18 20:25:50 +00001526 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001527
Erik Andersen161220c2000-03-16 08:12:48 +00001528 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001529}
1530
Erik Andersend75af992000-03-16 08:09:09 +00001531static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001532{
Erik Andersen161220c2000-03-16 08:12:48 +00001533 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001534 char *next_command = NULL;
1535 struct job newjob;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001536 pid_t parent_pgrp;
Erik Andersen161220c2000-03-16 08:12:48 +00001537 int i;
Eric Andersen86349772000-12-18 20:25:50 +00001538 int inbg;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001539 int status;
Eric Andersen86349772000-12-18 20:25:50 +00001540 newjob.job_list = &job_list;
1541 newjob.job_context = DEFAULT_CONTEXT;
Erik Andersen3522eb12000-03-12 23:49:18 +00001542
Eric Andersen1c314ad2000-06-28 16:56:25 +00001543 /* save current owner of TTY so we can restore it on exit */
1544 parent_pgrp = tcgetpgrp(0);
1545
Matt Kraaib8907522000-09-13 02:08:21 +00001546 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Erik Andersend75af992000-03-16 08:09:09 +00001547
Erik Andersen161220c2000-03-16 08:12:48 +00001548 /* don't pay any attention to this signal; it just confuses
1549 things and isn't really meant for shells anyway */
1550 signal(SIGTTOU, SIG_IGN);
Erik Andersend75af992000-03-16 08:09:09 +00001551
Erik Andersen161220c2000-03-16 08:12:48 +00001552 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001553 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001554 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001555
Erik Andersend75af992000-03-16 08:09:09 +00001556 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001557 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001558
Eric Andersen86349772000-12-18 20:25:50 +00001559 if (!next_command) {
1560 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001561 break;
Eric Andersen86349772000-12-18 20:25:50 +00001562 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001563 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001564
Eric Andersen86349772000-12-18 20:25:50 +00001565 if (!parse_command(&next_command, &newjob, &inbg) &&
1566 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001567 int pipefds[2] = {-1,-1};
Eric Andersen86349772000-12-18 20:25:50 +00001568 debug_printf( "job=%p being fed to run_command by busy_loop()'\n", &newjob);
1569 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001570 }
1571 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001572 free(command);
Matt Kraaib8907522000-09-13 02:08:21 +00001573 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Eric Andersen86349772000-12-18 20:25:50 +00001574 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001575 }
1576 } else {
1577 /* a job is running in the foreground; wait for it */
1578 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001579 while (!job_list.fg->progs[i].pid ||
1580 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001581
Eric Andersen86349772000-12-18 20:25:50 +00001582 waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED);
Erik Andersen3522eb12000-03-12 23:49:18 +00001583
Erik Andersend75af992000-03-16 08:09:09 +00001584 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001585 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001586 job_list.fg->running_progs--;
1587 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001588
Eric Andersen501c88b2000-07-28 15:14:45 +00001589#ifdef BB_FEATURE_SH_ENVIRONMENT
Eric Andersen86349772000-12-18 20:25:50 +00001590 last_return_code=WEXITSTATUS(status);
Eric Andersen501c88b2000-07-28 15:14:45 +00001591#endif
Eric Andersen86349772000-12-18 20:25:50 +00001592 debug_printf("'%s' exited -- return code %d\n",
1593 job_list.fg->text, last_return_code);
1594 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001595 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001596 remove_job(&job_list, job_list.fg);
1597 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001598 }
Erik Andersend75af992000-03-16 08:09:09 +00001599 } else {
Erik Andersen161220c2000-03-16 08:12:48 +00001600 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001601 job_list.fg->stopped_progs++;
1602 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001603
Eric Andersen86349772000-12-18 20:25:50 +00001604 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1605 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1606 "Stopped", job_list.fg->text);
1607 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001608 }
Erik Andersend75af992000-03-16 08:09:09 +00001609 }
1610
Eric Andersen86349772000-12-18 20:25:50 +00001611 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001612 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001613 /* suppress messages when run from /linuxrc mag@sysgo.de */
1614 if (tcsetpgrp(0, getpid()) && errno != ENOTTY)
1615 perror("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001616 }
1617 }
1618 }
Erik Andersen161220c2000-03-16 08:12:48 +00001619 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001620
Eric Andersen1c314ad2000-06-28 16:56:25 +00001621 /* return controlling TTY back to parent process group before exiting */
1622 if (tcsetpgrp(0, parent_pgrp))
Eric Andersenb54833c2000-07-03 23:56:26 +00001623 perror("tcsetpgrp");
1624
1625 /* return exit status if called with "-c" */
1626 if (input == NULL && WIFEXITED(status))
1627 return WEXITSTATUS(status);
1628
Erik Andersen161220c2000-03-16 08:12:48 +00001629 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001630}
1631
1632
Eric Andersenfad9c112000-07-25 18:06:52 +00001633#ifdef BB_FEATURE_CLEAN_UP
1634void free_memory(void)
1635{
Eric Andersen86349772000-12-18 20:25:50 +00001636 if (prompt_str)
1637 free(prompt_str);
Eric Andersenfad9c112000-07-25 18:06:52 +00001638 if (cwd)
1639 free(cwd);
1640 if (local_pending_command)
1641 free(local_pending_command);
1642
Eric Andersen86349772000-12-18 20:25:50 +00001643 if (job_list.fg && !job_list.fg->running_progs) {
1644 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001645 }
1646}
1647#endif
1648
Eric Andersen6efc48c2000-07-18 08:16:39 +00001649
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001650int shell_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001651{
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001652 int opt, interactive=FALSE;
Erik Andersen161220c2000-03-16 08:12:48 +00001653 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001654 argc = argc_l;
1655 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001656
Eric Andersen501c88b2000-07-28 15:14:45 +00001657
Eric Andersenb558e762000-11-30 22:43:16 +00001658 if (argv[0] && argv[0][0] == '-') {
1659 FILE *input;
1660 input = fopen("/etc/profile", "r");
1661 if (!input) {
1662 fprintf(stdout, "Couldn't open file '/etc/profile'\n");
1663 } else {
1664 /* Now run the file */
1665 busy_loop(input);
1666 fclose(input);
1667 }
1668 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001669
Eric Andersenf21aa842000-12-08 20:50:30 +00001670 while ((opt = getopt(argc_l, argv_l, "cxi")) > 0) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001671 switch (opt) {
1672 case 'c':
1673 input = NULL;
Matt Kraai6085c722000-09-06 01:46:18 +00001674 if (local_pending_command != 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001675 error_msg_and_die("multiple -c arguments\n");
Matt Kraai6085c722000-09-06 01:46:18 +00001676 local_pending_command = xstrdup(argv[optind]);
1677 optind++;
1678 argv = argv+optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001679 break;
Eric Andersen1428c4f2000-07-28 15:19:30 +00001680#ifdef BB_FEATURE_SH_ENVIRONMENT
Eric Andersen501c88b2000-07-28 15:14:45 +00001681 case 'x':
Eric Andersen86349772000-12-18 20:25:50 +00001682 show_x_trace = TRUE;
Eric Andersen501c88b2000-07-28 15:14:45 +00001683 break;
Eric Andersen1428c4f2000-07-28 15:19:30 +00001684#endif
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001685 case 'i':
1686 interactive = TRUE;
1687 break;
Eric Andersen501c88b2000-07-28 15:14:45 +00001688 default:
1689 usage(shell_usage);
1690 }
1691 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001692 /* A shell is interactive if the `-i' flag was given, or if all of
1693 * the following conditions are met:
1694 * no -c command
1695 * no arguments remaining or the -s flag given
1696 * standard input is a terminal
1697 * standard output is a terminal
1698 * Refer to Posix.2, the description of the `sh' utility. */
Eric Andersen86349772000-12-18 20:25:50 +00001699 if (argv[optind]==NULL && input==stdin &&
1700 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
1701 interactive=TRUE;
1702 }
1703 if (interactive==TRUE) {
Eric Andersen851ce892000-08-21 22:34:23 +00001704 //fprintf(stdout, "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Eric Andersen501c88b2000-07-28 15:14:45 +00001705 /* Looks like they want an interactive shell */
Eric Andersen86349772000-12-18 20:25:50 +00001706 fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell (lash)\n", BB_VER, BB_BT);
Eric Andersen501c88b2000-07-28 15:14:45 +00001707 fprintf(stdout, "Enter 'help' for a list of built-in commands.\n\n");
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001708 } else if (local_pending_command==NULL) {
Eric Andersen851ce892000-08-21 22:34:23 +00001709 //fprintf(stdout, "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Matt Kraaibbaef662000-09-27 02:43:35 +00001710 input = xfopen(argv[optind], "r");
Eric Andersen501c88b2000-07-28 15:14:45 +00001711 }
1712
Eric Andersen6efc48c2000-07-18 08:16:39 +00001713 /* initialize the cwd -- this is never freed...*/
1714 cwd=(char*)xmalloc(sizeof(char)*MAX_LINE+1);
1715 getcwd(cwd, sizeof(char)*MAX_LINE);
Erik Andersen3522eb12000-03-12 23:49:18 +00001716
Eric Andersenfad9c112000-07-25 18:06:52 +00001717#ifdef BB_FEATURE_CLEAN_UP
1718 atexit(free_memory);
1719#endif
1720
Erik Andersenf0657d32000-04-12 17:49:52 +00001721 win_changed(0);
Erik Andersen161220c2000-03-16 08:12:48 +00001722 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001723}