blob: 68f67353967a265491bd2ca7ddba845c7fb0057e [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 Andersen8ec10a92001-01-27 09:33:39 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Erik Andersen3522eb12000-03-12 23:49:18 +00006 * 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 Andersen8a646dd2001-06-21 16:38:11 +000028/* This shell's parsing engine is officially at a dead-end.
29 * Future work shell work should be done using hush.c
Eric Andersen8ea28be2001-01-05 20:58:22 +000030 */
Eric Andersen8a646dd2001-06-21 16:38:11 +000031
Eric Andersen1e7cea92000-12-06 23:47:38 +000032//For debugging/development on the shell only...
Eric Andersen501c88b2000-07-28 15:14:45 +000033//#define DEBUG_SHELL
Eric Andersena1d187a2000-07-17 19:14:41 +000034
35
Erik Andersen3522eb12000-03-12 23:49:18 +000036#include <stdio.h>
37#include <stdlib.h>
38#include <ctype.h>
39#include <errno.h>
40#include <fcntl.h>
Erik Andersen3522eb12000-03-12 23:49:18 +000041#include <signal.h>
42#include <string.h>
43#include <sys/ioctl.h>
44#include <sys/wait.h>
45#include <unistd.h>
Eric Andersen501c88b2000-07-28 15:14:45 +000046#include <getopt.h>
Eric Andersen2d848a42001-06-25 17:11:54 +000047#include <termios.h>
Mark Whitley4b541a82001-04-25 17:10:30 +000048#include "busybox.h"
49#include "cmdedit.h"
Eric Andersene5dfced2001-04-09 22:48:12 +000050
51#ifdef BB_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +000052#include <locale.h>
Eric Andersene5dfced2001-04-09 22:48:12 +000053#endif
Eric Andersen13d1fa12001-03-08 23:59:45 +000054
Eric Andersen13d1fa12001-03-08 23:59:45 +000055#include <glob.h>
56#define expand_t glob_t
Erik Andersen3522eb12000-03-12 23:49:18 +000057
Eric Andersen13d1fa12001-03-08 23:59:45 +000058
Mark Whitley59ab0252001-01-23 22:30:04 +000059static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000060#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
61
Erik Andersend75af992000-03-16 08:09:09 +000062
Eric Andersen86349772000-12-18 20:25:50 +000063enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000064 REDIRECT_APPEND
65};
Erik Andersen3522eb12000-03-12 23:49:18 +000066
Eric Andersen86349772000-12-18 20:25:50 +000067static const unsigned int DEFAULT_CONTEXT=0x1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +000068static const unsigned int IF_TRUE_CONTEXT=0x2;
69static const unsigned int IF_FALSE_CONTEXT=0x4;
70static const unsigned int THEN_EXP_CONTEXT=0x8;
71static const unsigned int ELSE_EXP_CONTEXT=0x10;
Eric Andersenfad9c112000-07-25 18:06:52 +000072
Eric Andersen86349772000-12-18 20:25:50 +000073
74struct jobset {
Erik Andersen161220c2000-03-16 08:12:48 +000075 struct job *head; /* head of list of running jobs */
76 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000077};
78
Eric Andersen86349772000-12-18 20:25:50 +000079struct redir_struct {
80 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000081 int fd; /* file descriptor being redirected */
82 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000083};
84
Eric Andersen86349772000-12-18 20:25:50 +000085struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000086 pid_t pid; /* 0 if exited */
87 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000088 int num_redirects; /* elements in redirection array */
89 struct redir_struct *redirects; /* I/O redirects */
Eric Andersen86349772000-12-18 20:25:50 +000090 int is_stopped; /* is the program currently running? */
91 struct job *family; /* pointer back to the child's parent job */
Erik Andersen3522eb12000-03-12 23:49:18 +000092};
93
94struct job {
Eric Andersen86349772000-12-18 20:25:50 +000095 int jobid; /* job number */
96 int num_progs; /* total number of programs in job */
97 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000098 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +000099 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +0000100 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +0000101 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +0000102 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +0000103 int stopped_progs; /* number of programs alive, but stopped */
104 unsigned int job_context; /* bitmask defining current context */
105 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +0000106};
107
Eric Andersen86349772000-12-18 20:25:50 +0000108struct built_in_command {
Erik Andersen161220c2000-03-16 08:12:48 +0000109 char *cmd; /* name */
110 char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +0000111 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +0000112};
113
Eric Andersen8ea28be2001-01-05 20:58:22 +0000114struct close_me {
115 int fd;
116 struct close_me *next;
117};
118
Eric Andersen34e19412000-07-10 18:47:24 +0000119/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000120static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000121static int builtin_exec(struct child_prog *cmd);
122static int builtin_exit(struct child_prog *cmd);
123static int builtin_fg_bg(struct child_prog *cmd);
124static int builtin_help(struct child_prog *cmd);
125static int builtin_jobs(struct child_prog *dummy);
126static int builtin_pwd(struct child_prog *dummy);
127static int builtin_export(struct child_prog *cmd);
128static int builtin_source(struct child_prog *cmd);
129static int builtin_unset(struct child_prog *cmd);
130static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000131
Eric Andersen34e19412000-07-10 18:47:24 +0000132
133/* function prototypes for shell stuff */
Eric Andersen8ea28be2001-01-05 20:58:22 +0000134static void mark_open(int fd);
135static void mark_closed(int fd);
Eric Andersen36278b92001-03-06 20:47:31 +0000136static void close_all(void);
Eric Andersen86349772000-12-18 20:25:50 +0000137static void checkjobs(struct jobset *job_list);
138static int get_command(FILE * source, char *command);
139static int parse_command(char **command_ptr, struct job *job, int *inbg);
140static int run_command(struct job *newjob, int inbg, int outpipe[2]);
141static int pseudo_exec(struct child_prog *cmd) __attribute__ ((noreturn));
Erik Andersen3522eb12000-03-12 23:49:18 +0000142static int busy_loop(FILE * input);
143
Erik Andersend75af992000-03-16 08:09:09 +0000144
Mark Whitley37653aa2000-07-12 23:36:17 +0000145/* Table of built-in functions (these are non-forking builtins, meaning they
146 * can change global variables in the parent shell process but they will not
147 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Eric Andersen86349772000-12-18 20:25:50 +0000148static struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000149 {"bg", "Resume a job in the background", builtin_fg_bg},
150 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000151 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000152 {"exit", "Exit from shell()", builtin_exit},
153 {"fg", "Bring job into the foreground", builtin_fg_bg},
154 {"jobs", "Lists the active jobs", builtin_jobs},
155 {"export", "Set environment variable", builtin_export},
156 {"unset", "Unset environment variable", builtin_unset},
157 {"read", "Input environment variable", builtin_read},
Matt Kraaidd450a02000-09-13 03:43:36 +0000158 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000159 /* to do: add ulimit */
Eric Andersenfad9c112000-07-25 18:06:52 +0000160 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000161};
162
Mark Whitley37653aa2000-07-12 23:36:17 +0000163/* Table of forking built-in functions (things that fork cannot change global
164 * variables in the parent process, such as the current working directory) */
Eric Andersen86349772000-12-18 20:25:50 +0000165static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000166 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000167 {"help", "List shell built-in commands", builtin_help},
168 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000169};
170
Eric Andersen0bcc8132001-01-05 19:37:32 +0000171
172/* Variables we export */
173unsigned int shell_context; /* Used in cmdedit.c to reset the
174 context when someone hits ^C */
175
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 Andersen8a646dd2001-06-21 16:38:11 +0000184static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000185static int shell_terminal;
186static pid_t shell_pgrp;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000187static char *PS1;
Eric Andersene5dfced2001-04-09 22:48:12 +0000188static char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000189
190
Eric Andersenb558e762000-11-30 22:43:16 +0000191#ifdef DEBUG_SHELL
192static inline void debug_printf(const char *format, ...)
193{
194 va_list args;
195 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000196 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000197 va_end(args);
198}
199#else
200static inline void debug_printf(const char *format, ...) { }
201#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000202
Eric Andersen86349772000-12-18 20:25:50 +0000203/*
204 Most builtins need access to the struct child_prog that has
205 their arguments, previously coded as cmd->progs[0]. That coding
206 can exhibit a bug, if the builtin is not the first command in
207 a pipeline: "echo foo | exec sort" will attempt to exec foo.
208
209builtin previous use notes
210------ ----------------- ---------
211cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000212exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
213exit cmd->progs[0]
214fg_bg cmd->progs[0], job_list->head, job_list->fg
215help 0
216jobs job_list->head
217pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000218export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000219source cmd->progs[0]
220unset cmd->progs[0]
221read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000222
223I added "struct job *family;" to struct child_prog,
224and switched API to builtin_foo(struct child_prog *child);
225So cmd->text becomes child->family->text
226 cmd->job_context becomes child->family->job_context
227 cmd->progs[0] becomes *child
228 job_list becomes child->family->job_list
229 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000230
Erik Andersend75af992000-03-16 08:09:09 +0000231/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000232static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000233{
Erik Andersen161220c2000-03-16 08:12:48 +0000234 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000235
Eric Andersen86349772000-12-18 20:25:50 +0000236 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000237 newdir = getenv("HOME");
238 else
Eric Andersen86349772000-12-18 20:25:50 +0000239 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000240 if (chdir(newdir)) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000241 printf("cd: %s: %m\n", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000242 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000243 }
Eric Andersencfa88ec2001-05-11 18:08:16 +0000244 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000245 if (!cwd)
246 cwd = unknown;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000247 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000248}
249
Eric Andersend2f56772000-09-21 02:48:07 +0000250/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000251static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000252{
Eric Andersen86349772000-12-18 20:25:50 +0000253 if (child->argv[1] == NULL)
254 return EXIT_SUCCESS; /* Really? */
255 child->argv++;
Eric Andersen07f2f392001-03-06 20:28:22 +0000256 close_all();
Eric Andersen86349772000-12-18 20:25:50 +0000257 pseudo_exec(child);
258 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000259}
260
Erik Andersen3522eb12000-03-12 23:49:18 +0000261/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000262static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000263{
Eric Andersen86349772000-12-18 20:25:50 +0000264 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000265 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000266
Eric Andersen86349772000-12-18 20:25:50 +0000267 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000268}
269
270/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000271static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000272{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000273 int i, jobnum;
Erik Andersen6273f652000-03-17 01:12:41 +0000274 struct job *job=NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000275
Eric Andersen8a646dd2001-06-21 16:38:11 +0000276 /* If they gave us no args, assume they want the last backgrounded task */
277 if (!child->argv[1]) {
278 for (job = child->family->job_list->head; job; job = job->next) {
279 if (job->jobid == last_jobid) {
280 break;
281 }
Erik Andersen161220c2000-03-16 08:12:48 +0000282 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000283 if (!job) {
284 error_msg("%s: no current job", child->argv[0]);
285 return EXIT_FAILURE;
286 }
287 } else {
288 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
289 error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]);
290 return EXIT_FAILURE;
291 }
292 for (job = child->family->job_list->head; job; job = job->next) {
293 if (job->jobid == jobnum) {
294 break;
295 }
296 }
297 if (!job) {
298 error_msg("%s: %d: no such job", child->argv[0], jobnum);
299 return EXIT_FAILURE;
300 }
Erik Andersend75af992000-03-16 08:09:09 +0000301 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000302
Eric Andersen86349772000-12-18 20:25:50 +0000303 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000304 /* Put the job into the foreground. */
305 tcsetpgrp(shell_terminal, job->pgrp);
306
Eric Andersen86349772000-12-18 20:25:50 +0000307 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000308 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000309
Erik Andersen161220c2000-03-16 08:12:48 +0000310 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000311 for (i = 0; i < job->num_progs; i++)
312 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000313
Eric Andersen86349772000-12-18 20:25:50 +0000314 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000315
Eric Andersen2d848a42001-06-25 17:11:54 +0000316 if (kill(- job->pgrp, SIGCONT) < 0)
317 perror_msg("kill (SIGCONT)");
318
Matt Kraai3e856ce2000-12-01 02:55:13 +0000319 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000320}
321
322/* built-in 'help' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000323static int builtin_help(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000324{
Eric Andersen86349772000-12-18 20:25:50 +0000325 struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000326
Eric Andersen86349772000-12-18 20:25:50 +0000327 printf("\nBuilt-in commands:\n");
328 printf("-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000329 for (x = bltins; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000330 if (x->descr==NULL)
331 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000332 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000333 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000334 for (x = bltins_forking; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000335 if (x->descr==NULL)
336 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000337 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000338 }
Eric Andersen86349772000-12-18 20:25:50 +0000339 printf("\n\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000340 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000341}
342
343/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000344static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000345{
Erik Andersen161220c2000-03-16 08:12:48 +0000346 struct job *job;
Eric Andersen86349772000-12-18 20:25:50 +0000347 char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000348
Eric Andersen86349772000-12-18 20:25:50 +0000349 for (job = child->family->job_list->head; job; job = job->next) {
350 if (job->running_progs == job->stopped_progs)
351 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000352 else
Eric Andersen86349772000-12-18 20:25:50 +0000353 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000354
Eric Andersen86349772000-12-18 20:25:50 +0000355 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000356 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000357 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000358}
359
360
361/* built-in 'pwd' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000362static int builtin_pwd(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000363{
Eric Andersencfa88ec2001-05-11 18:08:16 +0000364 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000365 if (!cwd)
366 cwd = unknown;
Matt Kraai59df6f72001-05-16 14:21:09 +0000367 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000368 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000369}
370
Erik Andersen6273f652000-03-17 01:12:41 +0000371/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000372static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000373{
Erik Andersen161220c2000-03-16 08:12:48 +0000374 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000375 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000376
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000377 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000378 char **e;
379 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000380 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000381 }
Matt Kraai2129f972001-04-04 17:50:04 +0000382 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000383 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000384 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000385 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000386 fprintf(stderr, "export: %m\n");
Eric Andersen004015e2001-05-21 20:30:51 +0000387#ifdef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000388 if (strncmp(v, "PS1=", 4)==0)
389 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000390#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000391
392#ifdef BB_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +0000393 if(strncmp(v, "LC_ALL=", 7)==0)
394 setlocale(LC_ALL, getenv("LC_ALL"));
Mark Whitleya82a0032001-03-27 17:07:15 +0000395 if(strncmp(v, "LC_CTYPE=", 9)==0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000396 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000397#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000398
Erik Andersen161220c2000-03-16 08:12:48 +0000399 return (res);
Erik Andersen3522eb12000-03-12 23:49:18 +0000400}
401
Eric Andersenb54833c2000-07-03 23:56:26 +0000402/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000403static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000404{
405 int res = 0, len, newlen;
406 char *s;
407 char string[MAX_READ];
408
Eric Andersen86349772000-12-18 20:25:50 +0000409 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000410 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen86349772000-12-18 20:25:50 +0000411 strcpy(string, child->argv[1]);
Eric Andersenb54833c2000-07-03 23:56:26 +0000412 len = strlen(string);
413 string[len++] = '=';
414 string[len] = '\0';
415 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
416 newlen = strlen(string);
417 if(newlen > len)
418 string[--newlen] = '\0'; /* chomp trailing newline */
419 /*
420 ** string should now contain "VAR=<value>"
421 ** copy it (putenv() won't do that, so we must make sure
422 ** the string resides in a static buffer!)
423 */
424 res = -1;
425 if((s = strdup(string)))
426 res = putenv(s);
427 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000428 fprintf(stderr, "read: %m\n");
Eric Andersenb54833c2000-07-03 23:56:26 +0000429 }
430 else
431 fgets(string, sizeof(string), stdin);
432
433 return (res);
434}
435
Erik Andersen3522eb12000-03-12 23:49:18 +0000436/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000437static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000438{
Erik Andersen161220c2000-03-16 08:12:48 +0000439 FILE *input;
440 int status;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000441 int fd;
Erik Andersen3522eb12000-03-12 23:49:18 +0000442
Eric Andersen86349772000-12-18 20:25:50 +0000443 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000444 return EXIT_FAILURE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000445
Eric Andersen86349772000-12-18 20:25:50 +0000446 input = fopen(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000447 if (!input) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000448 printf( "Couldn't open file '%s'\n", child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000449 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000450 }
Erik Andersend75af992000-03-16 08:09:09 +0000451
Eric Andersen8ea28be2001-01-05 20:58:22 +0000452 fd=fileno(input);
453 mark_open(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000454 /* Now run the file */
455 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000456 fclose(input);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000457 mark_closed(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000458 return (status);
Erik Andersen3522eb12000-03-12 23:49:18 +0000459}
460
461/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000462static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000463{
Eric Andersen86349772000-12-18 20:25:50 +0000464 if (child->argv[1] == NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000465 printf( "unset: parameter required.\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000466 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000467 }
Eric Andersen86349772000-12-18 20:25:50 +0000468 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000469 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000470}
471
Eric Andersen8ea28be2001-01-05 20:58:22 +0000472static void mark_open(int fd)
473{
474 struct close_me *new = xmalloc(sizeof(struct close_me));
475 new->fd = fd;
476 new->next = close_me_head;
477 close_me_head = new;
478}
479
480static void mark_closed(int fd)
481{
482 struct close_me *tmp;
483 if (close_me_head == NULL || close_me_head->fd != fd)
484 error_msg_and_die("corrupt close_me");
485 tmp = close_me_head;
486 close_me_head = close_me_head->next;
487 free(tmp);
488}
489
490static void close_all()
491{
Eric Andersen702ec592001-03-06 22:17:29 +0000492 struct close_me *c, *tmp;
493 for (c=close_me_head; c; c=tmp) {
494 close(c->fd);
495 tmp=c->next;
496 free(c);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000497 }
498 close_me_head = NULL;
499}
500
501
Erik Andersen3522eb12000-03-12 23:49:18 +0000502/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000503static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000504{
Erik Andersen161220c2000-03-16 08:12:48 +0000505 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000506 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000507
Eric Andersen86349772000-12-18 20:25:50 +0000508 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000509 free(cmd->progs[i].argv);
Eric Andersen86349772000-12-18 20:25:50 +0000510 if (cmd->progs[i].redirects)
511 free(cmd->progs[i].redirects);
Erik Andersen161220c2000-03-16 08:12:48 +0000512 }
Mark Whitley44a99142001-03-14 17:26:37 +0000513 if (cmd->progs)
514 free(cmd->progs);
Erik Andersen161220c2000-03-16 08:12:48 +0000515 if (cmd->text)
516 free(cmd->text);
Mark Whitley44a99142001-03-14 17:26:37 +0000517 if (cmd->cmdbuf)
518 free(cmd->cmdbuf);
519 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000520 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000521 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000522}
523
Eric Andersen1ca20a72001-03-21 07:34:27 +0000524/* remove a job from a jobset */
525static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000526{
Eric Andersen86349772000-12-18 20:25:50 +0000527 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000528
Eric Andersen86349772000-12-18 20:25:50 +0000529 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000530 if (job == j_list->head) {
531 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000532 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000533 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000534 while (prevjob->next != job)
535 prevjob = prevjob->next;
536 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000537 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000538
Erik Andersen161220c2000-03-16 08:12:48 +0000539 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000540}
541
542/* Checks to see if any background processes have exited -- if they
543 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000544static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000545{
Erik Andersen161220c2000-03-16 08:12:48 +0000546 struct job *job;
547 pid_t childpid;
548 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000549 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000550
Erik Andersen161220c2000-03-16 08:12:48 +0000551 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000552 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000553 prognum = 0;
554 while (prognum < job->num_progs &&
555 job->progs[prognum].pid != childpid) prognum++;
556 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000557 break;
558 }
559
Eric Andersena1d187a2000-07-17 19:14:41 +0000560 /* This happens on backticked commands */
561 if(job==NULL)
562 return;
563
Erik Andersen161220c2000-03-16 08:12:48 +0000564 if (WIFEXITED(status) || WIFSIGNALED(status)) {
565 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000566 job->running_progs--;
567 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000568
Eric Andersen86349772000-12-18 20:25:50 +0000569 if (!job->running_progs) {
570 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000571 last_jobid=0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000572 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000573 }
574 } else {
575 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000576 job->stopped_progs++;
577 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000578
Eric Andersen86349772000-12-18 20:25:50 +0000579 if (job->stopped_progs == job->num_progs) {
580 printf(JOB_STATUS_FORMAT, job->jobid, "Stopped",
Erik Andersen161220c2000-03-16 08:12:48 +0000581 job->text);
582 }
583 }
Erik Andersend75af992000-03-16 08:09:09 +0000584 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000585
Erik Andersen161220c2000-03-16 08:12:48 +0000586 if (childpid == -1 && errno != ECHILD)
Matt Kraaia9819b22000-12-22 01:48:07 +0000587 perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000588}
589
Eric Andersene9f07fb2000-12-21 18:31:36 +0000590/* squirrel != NULL means we squirrel away copies of stdin, stdout,
591 * and stderr if they are redirected. */
592static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000593{
594 int i;
595 int openfd;
596 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000597 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000598
Eric Andersen86349772000-12-18 20:25:50 +0000599 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000600 switch (redir->type) {
601 case REDIRECT_INPUT:
602 mode = O_RDONLY;
603 break;
604 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000605 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000606 break;
607 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000608 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000609 break;
610 }
611
612 openfd = open(redir->filename, mode, 0666);
613 if (openfd < 0) {
614 /* this could get lost if stderr has been redirected, but
615 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000616 perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000617 return 1;
618 }
619
620 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000621 if (squirrel && redir->fd < 3) {
622 squirrel[redir->fd] = dup(redir->fd);
623 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000624 dup2(openfd, redir->fd);
625 close(openfd);
626 }
627 }
628
629 return 0;
630}
631
Eric Andersene9f07fb2000-12-21 18:31:36 +0000632static void restore_redirects(int squirrel[])
633{
634 int i, fd;
635 for (i=0; i<3; i++) {
636 fd = squirrel[i];
637 if (fd != -1) {
638 /* No error checking. I sure wouldn't know what
639 * to do with an error if I found one! */
640 dup2(fd, i);
641 close(fd);
642 }
643 }
644}
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000645
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000646static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000647{
Eric Andersen004015e2001-05-21 20:30:51 +0000648#ifndef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000649 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000650#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000651 PS1 = getenv("PS1");
Eric Andersene5dfced2001-04-09 22:48:12 +0000652 if(PS1==0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000653 PS1 = "\\w \\$ ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000654#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000655}
656
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000657static inline void setup_prompt_string(char **prompt_str)
658{
Eric Andersen004015e2001-05-21 20:30:51 +0000659#ifndef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000660 /* Set up the prompt */
661 if (shell_context == 0) {
662 if (PS1)
663 free(PS1);
664 PS1=xmalloc(strlen(cwd)+4);
665 sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
666 *prompt_str = PS1;
667 } else {
668 *prompt_str = PS2;
669 }
670#else
671 *prompt_str = (shell_context==0)? PS1 : PS2;
672#endif
673}
Eric Andersen22332fd2001-01-30 23:40:39 +0000674
Eric Andersen09acc062001-01-04 11:10:38 +0000675static int get_command(FILE * source, char *command)
676{
677 char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000678
Eric Andersen1c314ad2000-06-28 16:56:25 +0000679 if (source == NULL) {
680 if (local_pending_command) {
681 /* a command specified (-c option): return it & mark it done */
682 strcpy(command, local_pending_command);
683 free(local_pending_command);
684 local_pending_command = NULL;
685 return 0;
686 }
687 return 1;
688 }
689
Erik Andersen161220c2000-03-16 08:12:48 +0000690 if (source == stdin) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000691 setup_prompt_string(&prompt_str);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000692
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000693#ifdef BB_FEATURE_COMMAND_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000694 /*
695 ** enable command line editing only while a command line
696 ** is actually being read; otherwise, we'll end up bequeathing
697 ** atexit() handlers and other unwanted stuff to our
698 ** child processes (rob@sysgo.de)
699 */
Eric Andersen86349772000-12-18 20:25:50 +0000700 cmdedit_read_input(prompt_str, command);
Eric Andersen501c88b2000-07-28 15:14:45 +0000701 cmdedit_terminate();
Erik Andersen161220c2000-03-16 08:12:48 +0000702 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000703#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000704 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000705#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000706 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000707
Erik Andersen161220c2000-03-16 08:12:48 +0000708 if (!fgets(command, BUFSIZ - 2, source)) {
709 if (source == stdin)
710 printf("\n");
711 return 1;
712 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000713
Erik Andersen161220c2000-03-16 08:12:48 +0000714 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000715}
716
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000717
Eric Andersenca604592001-03-08 17:17:13 +0000718static int expand_arguments(char *command)
719{
Eric Andersen1ca20a72001-03-21 07:34:27 +0000720 int ix = 0;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000721
Eric Andersenca604592001-03-08 17:17:13 +0000722 /* get rid of the terminating \n */
723 chomp(command);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000724
725 /* Fix up escape sequences to be the Real Thing(tm) */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000726 while( command && command[ix]) {
727 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000728 const char *tmp = command+ix+1;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000729 command[ix] = process_escape_sequence( &tmp );
730 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000731 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000732 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000733 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000734
Eric Andersenca604592001-03-08 17:17:13 +0000735 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000736}
737
Eric Andersen86349772000-12-18 20:25:50 +0000738/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
739 line). If a valid command is found, command_ptr is set to point to
Erik Andersen3522eb12000-03-12 23:49:18 +0000740 the beginning of the next command (if the original command had more
741 then one job associated with it) or NULL if no more commands are
742 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000743static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000744{
Erik Andersen161220c2000-03-16 08:12:48 +0000745 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000746 char *return_command = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +0000747 char *src, *buf, *chptr;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000748 int argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000749 int done = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000750 int argv_alloced;
Matt Kraaibe66ad32001-04-12 15:42:17 +0000751 int i, saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000752 char quote = '\0';
753 int count;
Eric Andersen86349772000-12-18 20:25:50 +0000754 struct child_prog *prog;
Erik Andersen3522eb12000-03-12 23:49:18 +0000755
Erik Andersen161220c2000-03-16 08:12:48 +0000756 /* skip leading white space */
Eric Andersen86349772000-12-18 20:25:50 +0000757 while (**command_ptr && isspace(**command_ptr))
758 (*command_ptr)++;
Erik Andersen3522eb12000-03-12 23:49:18 +0000759
Erik Andersen161220c2000-03-16 08:12:48 +0000760 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000761 if (!**command_ptr || (**command_ptr == '#')) {
762 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000763 return 0;
764 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000765
Eric Andersen86349772000-12-18 20:25:50 +0000766 *inbg = 0;
767 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000768 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000769
Erik Andersen161220c2000-03-16 08:12:48 +0000770 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000771 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000772 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000773
Erik Andersen161220c2000-03-16 08:12:48 +0000774 Getting clean memory relieves us of the task of NULL
775 terminating things and makes the rest of this look a bit
776 cleaner (though it is, admittedly, a tad less efficient) */
Eric Andersen86349772000-12-18 20:25:50 +0000777 job->cmdbuf = command = xcalloc(2*strlen(*command_ptr) + 1, sizeof(char));
Erik Andersen161220c2000-03-16 08:12:48 +0000778 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000779
Erik Andersen161220c2000-03-16 08:12:48 +0000780 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000781 prog->num_redirects = 0;
782 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000783 prog->is_stopped = 0;
784 prog->family = job;
Erik Andersen3522eb12000-03-12 23:49:18 +0000785
Eric Andersen86349772000-12-18 20:25:50 +0000786 argv_alloced = 5;
787 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
788 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +0000789
Erik Andersen161220c2000-03-16 08:12:48 +0000790 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +0000791 src = *command_ptr;
Erik Andersen161220c2000-03-16 08:12:48 +0000792 while (*src && !done) {
793 if (quote == *src) {
794 quote = '\0';
795 } else if (quote) {
796 if (*src == '\\') {
797 src++;
798 if (!*src) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000799 error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +0000800 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +0000801 return 1;
802 }
803
804 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +0000805 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +0000806 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +0000807 *buf++ = '\\';
808 }
Erik Andersen161220c2000-03-16 08:12:48 +0000809 } else if (*src == '*' || *src == '?' || *src == '[' ||
810 *src == ']') *buf++ = '\\';
811 *buf++ = *src;
812 } else if (isspace(*src)) {
Matt Kraaibe66ad32001-04-12 15:42:17 +0000813 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000814 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +0000815 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +0000816 if ((argc_l + 1) == argv_alloced) {
817 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +0000818 prog->argv = xrealloc(prog->argv,
819 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +0000820 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +0000821 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000822 prog->argv[argc_l] = buf;
Matt Kraaibe66ad32001-04-12 15:42:17 +0000823 saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000824 }
825 } else
826 switch (*src) {
827 case '"':
828 case '\'':
829 quote = *src;
Matt Kraaibe66ad32001-04-12 15:42:17 +0000830 saw_quote = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000831 break;
832
833 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000834 if (*(src-1)== '$')
835 *buf++ = *src;
836 else
837 done = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000838 break;
839
Eric Andersen86349772000-12-18 20:25:50 +0000840 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +0000841 case '<':
Eric Andersen86349772000-12-18 20:25:50 +0000842 i = prog->num_redirects++;
843 prog->redirects = xrealloc(prog->redirects,
844 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +0000845 (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +0000846
Eric Andersen86349772000-12-18 20:25:50 +0000847 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000848 if (buf != prog->argv[argc_l]) {
Erik Andersen161220c2000-03-16 08:12:48 +0000849 /* the stuff before this character may be the file number
850 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +0000851 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000852 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +0000853
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000854 if (*chptr && *prog->argv[argc_l]) {
855 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000856 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +0000857 }
858 }
859
Eric Andersen86349772000-12-18 20:25:50 +0000860 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +0000861 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +0000862 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000863 else
Eric Andersen86349772000-12-18 20:25:50 +0000864 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000865 }
866
867 if (*src++ == '>') {
868 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +0000869 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +0000870 REDIRECT_APPEND, src++;
871 else
Eric Andersen86349772000-12-18 20:25:50 +0000872 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +0000873 } else {
Eric Andersen86349772000-12-18 20:25:50 +0000874 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +0000875 }
876
877 /* This isn't POSIX sh compliant. Oh well. */
878 chptr = src;
879 while (isspace(*chptr))
880 chptr++;
881
882 if (!*chptr) {
Mark Whitley44a99142001-03-14 17:26:37 +0000883 error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +0000884 free_job(job);
885 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000886 return 1;
887 }
888
Eric Andersen86349772000-12-18 20:25:50 +0000889 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +0000890 while (*chptr && !isspace(*chptr))
891 *buf++ = *chptr++;
892
893 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000894 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +0000895 break;
896
897 case '|': /* pipe */
898 /* finish this command */
Matt Kraaibe66ad32001-04-12 15:42:17 +0000899 if (*prog->argv[argc_l] || saw_quote)
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000900 argc_l++;
901 if (!argc_l) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000902 error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +0000903 free_job(job);
904 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000905 return 1;
906 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000907 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +0000908
909 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +0000910 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +0000911 job->progs = xrealloc(job->progs,
Eric Andersen86349772000-12-18 20:25:50 +0000912 sizeof(*job->progs) * job->num_progs);
913 prog = job->progs + (job->num_progs - 1);
914 prog->num_redirects = 0;
915 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000916 prog->is_stopped = 0;
917 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000918 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000919
Eric Andersen86349772000-12-18 20:25:50 +0000920 argv_alloced = 5;
921 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +0000922 prog->argv[0] = ++buf;
923
924 src++;
925 while (*src && isspace(*src))
926 src++;
927
928 if (!*src) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000929 error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +0000930 free_job(job);
931 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000932 return 1;
933 }
934 src--; /* we'll ++ it at the end of the loop */
935
936 break;
937
938 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +0000939 *inbg = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000940 case ';': /* multiple commands */
941 done = 1;
Eric Andersen86349772000-12-18 20:25:50 +0000942 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000943 break;
944
Matt Kraai131241f2000-09-14 00:43:20 +0000945 case '\\':
946 src++;
947 if (!*src) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000948 error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +0000949 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +0000950 return 1;
951 }
952 if (*src == '*' || *src == '[' || *src == ']'
953 || *src == '?') *buf++ = '\\';
954 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +0000955 default:
956 *buf++ = *src;
957 }
958
Erik Andersend75af992000-03-16 08:09:09 +0000959 src++;
Erik Andersen161220c2000-03-16 08:12:48 +0000960 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000961
Matt Kraaibe66ad32001-04-12 15:42:17 +0000962 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000963 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +0000964 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000965 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +0000966 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +0000967 return 0;
968 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000969 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000970
Eric Andersen86349772000-12-18 20:25:50 +0000971 if (!return_command) {
972 job->text = xmalloc(strlen(*command_ptr) + 1);
973 strcpy(job->text, *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +0000974 } else {
975 /* This leaves any trailing spaces, which is a bit sloppy */
Eric Andersen86349772000-12-18 20:25:50 +0000976 count = return_command - *command_ptr;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000977 job->text = xmalloc(count + 1);
Eric Andersen86349772000-12-18 20:25:50 +0000978 strncpy(job->text, *command_ptr, count);
Erik Andersen161220c2000-03-16 08:12:48 +0000979 job->text[count] = '\0';
980 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000981
Eric Andersen86349772000-12-18 20:25:50 +0000982 *command_ptr = return_command;
Eric Andersen46f0beb2000-11-14 21:59:22 +0000983
Erik Andersend75af992000-03-16 08:09:09 +0000984 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000985}
986
Eric Andersen86349772000-12-18 20:25:50 +0000987/* Run the child_prog, no matter what kind of command it uses.
988 */
989static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000990{
Eric Andersen86349772000-12-18 20:25:50 +0000991 struct built_in_command *x;
Eric Andersenb54833c2000-07-03 23:56:26 +0000992#ifdef BB_FEATURE_SH_STANDALONE_SHELL
Eric Andersenaf4ac772001-02-01 22:43:49 +0000993 char *name;
Erik Andersenbcd61772000-05-13 06:33:19 +0000994#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000995
Eric Andersene9f07fb2000-12-21 18:31:36 +0000996 /* Check if the command matches any of the non-forking builtins.
997 * Depending on context, this might be redundant. But it's
998 * easier to waste a few CPU cycles than it is to figure out
999 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001000 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001001 for (x = bltins; x->cmd; x++) {
1002 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1003 exit(x->function(child));
1004 }
1005 }
1006
1007 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001008 for (x = bltins_forking; x->cmd; x++) {
1009 if (strcmp(child->argv[0], x->cmd) == 0) {
1010 applet_name=x->cmd;
1011 exit (x->function(child));
1012 }
1013 }
1014#ifdef BB_FEATURE_SH_STANDALONE_SHELL
1015 /* Check if the command matches any busybox internal
1016 * commands ("applets") here. Following discussions from
1017 * November 2000 on busybox@opensource.lineo.com, don't use
1018 * get_last_path_component(). This way explicit (with
1019 * slashes) filenames will never be interpreted as an
1020 * applet, just like with builtins. This way the user can
1021 * override an applet with an explicit filename reference.
1022 * The only downside to this change is that an explicit
1023 * /bin/foo invocation will fork and exec /bin/foo, even if
1024 * /bin/foo is a symlink to busybox.
1025 */
Matt Kraaif2cc2762001-02-01 19:21:20 +00001026 name = child->argv[0];
Eric Andersen86349772000-12-18 20:25:50 +00001027
1028#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
1029 /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then
1030 * if you run /bin/cat, it will use BusyBox cat even if
1031 * /bin/cat exists on the filesystem and is _not_ busybox.
1032 * Some systems want this, others do not. Choose wisely. :-)
1033 */
Matt Kraaif2cc2762001-02-01 19:21:20 +00001034 name = get_last_path_component(name);
Eric Andersen86349772000-12-18 20:25:50 +00001035#endif
1036
Eric Andersen67991cf2001-02-14 21:23:06 +00001037 {
Eric Andersen82ab8da2001-03-23 17:06:01 +00001038 char** argv_l=child->argv;
Eric Andersen67991cf2001-02-14 21:23:06 +00001039 int argc_l;
Eric Andersen82ab8da2001-03-23 17:06:01 +00001040 for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
Eric Andersen67991cf2001-02-14 21:23:06 +00001041 optind = 1;
1042 run_applet_by_name(name, argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001043 }
1044#endif
1045
1046 execvp(child->argv[0], child->argv);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001047 perror_msg_and_die("%s", child->argv[0]);
Eric Andersen86349772000-12-18 20:25:50 +00001048}
1049
1050static void insert_job(struct job *newjob, int inbg)
1051{
1052 struct job *thejob;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001053 struct jobset *j_list=newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001054
1055 /* find the ID for thejob to use */
1056 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001057 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001058 if (thejob->jobid >= newjob->jobid)
1059 newjob->jobid = thejob->jobid + 1;
1060
1061 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001062 if (!j_list->head) {
1063 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001064 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001065 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001066 thejob->next = xmalloc(sizeof(*thejob));
1067 thejob = thejob->next;
1068 }
1069
1070 *thejob = *newjob; /* physically copy the struct job */
1071 thejob->next = NULL;
1072 thejob->running_progs = thejob->num_progs;
1073 thejob->stopped_progs = 0;
1074
1075 if (inbg) {
1076 /* we don't wait for background thejobs to return -- append it
1077 to the list of backgrounded thejobs and leave it alone */
1078 printf("[%d] %d\n", thejob->jobid,
1079 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001080 last_jobid = newjob->jobid;
Eric Andersen86349772000-12-18 20:25:50 +00001081 } else {
1082 newjob->job_list->fg = thejob;
1083
1084 /* move the new process group into the foreground */
1085 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001086 if (tcsetpgrp(shell_terminal, newjob->pgrp) && errno != ENOTTY)
Matt Kraaia9819b22000-12-22 01:48:07 +00001087 perror_msg("tcsetpgrp");
Eric Andersen86349772000-12-18 20:25:50 +00001088 }
1089}
1090
1091static int run_command(struct job *newjob, int inbg, int outpipe[2])
1092{
1093 /* struct job *thejob; */
1094 int i;
1095 int nextin, nextout;
1096 int pipefds[2]; /* pipefd[0] is for reading */
1097 struct built_in_command *x;
1098 struct child_prog *child;
1099
Eric Andersen6efc48c2000-07-18 08:16:39 +00001100 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001101 for (i = 0; i < newjob->num_progs; i++) {
1102 child = & (newjob->progs[i]);
1103
1104 if ((i + 1) < newjob->num_progs) {
1105 if (pipe(pipefds)<0) perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001106 nextout = pipefds[1];
1107 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001108 if (outpipe[1]!=-1) {
1109 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001110 } else {
1111 nextout = 1;
1112 }
Erik Andersen161220c2000-03-16 08:12:48 +00001113 }
1114
Eric Andersen501c88b2000-07-28 15:14:45 +00001115
Eric Andersene9f07fb2000-12-21 18:31:36 +00001116 /* Check if the command matches any non-forking builtins,
1117 * but only if this is a simple command.
1118 * Non-forking builtins within pipes have to fork anyway,
1119 * and are handled in pseudo_exec. "echo foo | read bar"
1120 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001121 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001122 if (newjob->num_progs == 1) {
1123 for (x = bltins; x->cmd; x++) {
1124 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1125 int squirrel[] = {-1, -1, -1};
1126 int rcode;
1127 setup_redirects(child, squirrel);
1128 rcode = x->function(child);
1129 restore_redirects(squirrel);
1130 return rcode;
1131 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001132 }
1133 }
1134
Eric Andersen86349772000-12-18 20:25:50 +00001135 if (!(child->pid = fork())) {
Eric Andersen2d848a42001-06-25 17:11:54 +00001136 /* Set the handling for job control signals back to the default. */
1137 signal(SIGINT, SIG_DFL);
1138 signal(SIGQUIT, SIG_DFL);
1139 signal(SIGTSTP, SIG_DFL);
1140 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001141 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001142 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001143
Eric Andersen8ea28be2001-01-05 20:58:22 +00001144 close_all();
1145
Eric Andersen86349772000-12-18 20:25:50 +00001146 if (outpipe[1]!=-1) {
1147 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001148 }
1149 if (nextin != 0) {
1150 dup2(nextin, 0);
1151 close(nextin);
1152 }
1153
1154 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001155 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001156 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001157 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001158 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001159 }
1160
Eric Andersen86349772000-12-18 20:25:50 +00001161 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001162 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001163
Eric Andersen86349772000-12-18 20:25:50 +00001164 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001165 }
Eric Andersen86349772000-12-18 20:25:50 +00001166 if (outpipe[1]!=-1) {
1167 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001168 }
Erik Andersen161220c2000-03-16 08:12:48 +00001169
1170 /* put our child in the process group whose leader is the
1171 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001172 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001173 if (nextin != 0)
1174 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001175 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001176 close(nextout);
1177
1178 /* If there isn't another process, nextin is garbage
1179 but it doesn't matter */
1180 nextin = pipefds[0];
1181 }
1182
Eric Andersen86349772000-12-18 20:25:50 +00001183 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001184
Eric Andersen86349772000-12-18 20:25:50 +00001185 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001186
Erik Andersen161220c2000-03-16 08:12:48 +00001187 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001188}
1189
Erik Andersend75af992000-03-16 08:09:09 +00001190static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001191{
Erik Andersen161220c2000-03-16 08:12:48 +00001192 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001193 char *next_command = NULL;
1194 struct job newjob;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001195 pid_t parent_pgrp;
Erik Andersen161220c2000-03-16 08:12:48 +00001196 int i;
Eric Andersen86349772000-12-18 20:25:50 +00001197 int inbg;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001198 int status;
Eric Andersen86349772000-12-18 20:25:50 +00001199 newjob.job_list = &job_list;
1200 newjob.job_context = DEFAULT_CONTEXT;
Erik Andersen3522eb12000-03-12 23:49:18 +00001201
Eric Andersen1c314ad2000-06-28 16:56:25 +00001202 /* save current owner of TTY so we can restore it on exit */
Eric Andersen2d848a42001-06-25 17:11:54 +00001203 parent_pgrp = tcgetpgrp(shell_terminal);
Eric Andersen1c314ad2000-06-28 16:56:25 +00001204
Matt Kraaib8907522000-09-13 02:08:21 +00001205 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Erik Andersend75af992000-03-16 08:09:09 +00001206
Erik Andersen161220c2000-03-16 08:12:48 +00001207 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001208 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001209 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001210
Erik Andersend75af992000-03-16 08:09:09 +00001211 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001212 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001213
Eric Andersen86349772000-12-18 20:25:50 +00001214 if (!next_command) {
1215 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001216 break;
Eric Andersen86349772000-12-18 20:25:50 +00001217 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001218 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001219
Eric Andersenca604592001-03-08 17:17:13 +00001220 if (expand_arguments(next_command) == FALSE) {
1221 free(command);
1222 command = (char *) xcalloc(BUFSIZ, sizeof(char));
1223 next_command = NULL;
1224 continue;
1225 }
1226
Eric Andersen86349772000-12-18 20:25:50 +00001227 if (!parse_command(&next_command, &newjob, &inbg) &&
1228 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001229 int pipefds[2] = {-1,-1};
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001230 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
1231 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001232 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001233 }
1234 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001235 free(command);
Matt Kraaib8907522000-09-13 02:08:21 +00001236 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Eric Andersen86349772000-12-18 20:25:50 +00001237 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001238 }
1239 } else {
1240 /* a job is running in the foreground; wait for it */
1241 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001242 while (!job_list.fg->progs[i].pid ||
1243 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001244
Eric Andersen8ea28be2001-01-05 20:58:22 +00001245 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0)
1246 perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
Erik Andersen3522eb12000-03-12 23:49:18 +00001247
Erik Andersend75af992000-03-16 08:09:09 +00001248 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001249 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001250 job_list.fg->running_progs--;
1251 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001252
Eric Andersen86349772000-12-18 20:25:50 +00001253 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001254 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001255 remove_job(&job_list, job_list.fg);
1256 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001257 }
Erik Andersend75af992000-03-16 08:09:09 +00001258 } else {
Erik Andersen161220c2000-03-16 08:12:48 +00001259 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001260 job_list.fg->stopped_progs++;
1261 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001262
Eric Andersen86349772000-12-18 20:25:50 +00001263 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1264 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1265 "Stopped", job_list.fg->text);
1266 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001267 }
Erik Andersend75af992000-03-16 08:09:09 +00001268 }
1269
Eric Andersen86349772000-12-18 20:25:50 +00001270 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001271 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001272 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001273 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Matt Kraaia9819b22000-12-22 01:48:07 +00001274 perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001275 }
1276 }
1277 }
Erik Andersen161220c2000-03-16 08:12:48 +00001278 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001279
Eric Andersen1c314ad2000-06-28 16:56:25 +00001280 /* return controlling TTY back to parent process group before exiting */
Eric Andersen2d848a42001-06-25 17:11:54 +00001281 if (tcsetpgrp(shell_terminal, parent_pgrp))
Matt Kraaia9819b22000-12-22 01:48:07 +00001282 perror_msg("tcsetpgrp");
Eric Andersenb54833c2000-07-03 23:56:26 +00001283
1284 /* return exit status if called with "-c" */
1285 if (input == NULL && WIFEXITED(status))
1286 return WEXITSTATUS(status);
1287
Erik Andersen161220c2000-03-16 08:12:48 +00001288 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001289}
1290
1291
Eric Andersenfad9c112000-07-25 18:06:52 +00001292#ifdef BB_FEATURE_CLEAN_UP
1293void free_memory(void)
1294{
Eric Andersene5dfced2001-04-09 22:48:12 +00001295 if (cwd) {
Eric Andersenfad9c112000-07-25 18:06:52 +00001296 free(cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001297 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001298 if (local_pending_command)
1299 free(local_pending_command);
1300
Eric Andersen86349772000-12-18 20:25:50 +00001301 if (job_list.fg && !job_list.fg->running_progs) {
1302 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001303 }
1304}
1305#endif
1306
Eric Andersen2d848a42001-06-25 17:11:54 +00001307/* Make sure we have a controlling tty. If we get started under a job
1308 * aware app (like bash for example), make sure we are now in charge so
1309 * we don't fight over who gets the foreground */
1310static void setup_job_control()
1311{
1312 /* Loop until we are in the foreground. */
1313 while (tcgetpgrp (shell_terminal) != (shell_pgrp = getpgrp ()))
1314 kill (- shell_pgrp, SIGTTIN);
1315
1316 /* Ignore interactive and job-control signals. */
1317 signal(SIGINT, SIG_IGN);
1318 signal(SIGQUIT, SIG_IGN);
1319 signal(SIGTSTP, SIG_IGN);
1320 signal(SIGTTIN, SIG_IGN);
1321 signal(SIGTTOU, SIG_IGN);
1322 signal(SIGCHLD, SIG_IGN);
1323
1324 /* Put ourselves in our own process group. */
1325 shell_pgrp = getpid ();
1326 if (setpgid (shell_pgrp, shell_pgrp) < 0) {
1327 perror_msg_and_die("Couldn't put the shell in its own process group");
1328 }
1329
1330 /* Grab control of the terminal. */
1331 tcsetpgrp(shell_terminal, shell_pgrp);
1332}
Eric Andersen6efc48c2000-07-18 08:16:39 +00001333
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001334int shell_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001335{
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001336 int opt, interactive=FALSE;
Erik Andersen161220c2000-03-16 08:12:48 +00001337 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001338 argc = argc_l;
1339 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001340
Eric Andersen702ec592001-03-06 22:17:29 +00001341 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001342 last_jobid = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001343 shell_context = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001344 local_pending_command = NULL;
Eric Andersen702ec592001-03-06 22:17:29 +00001345 close_me_head = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001346 job_list.head = NULL;
1347 job_list.fg = NULL;
Eric Andersen501c88b2000-07-28 15:14:45 +00001348
Eric Andersenb558e762000-11-30 22:43:16 +00001349 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001350 FILE *prof_input;
1351 prof_input = fopen("/etc/profile", "r");
1352 if (!prof_input) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001353 printf( "Couldn't open file '/etc/profile'\n");
Eric Andersen8ea28be2001-01-05 20:58:22 +00001354 } else {
1355 int tmp_fd = fileno(prof_input);
1356 mark_open(tmp_fd);
1357 /* Now run the file */
1358 busy_loop(prof_input);
1359 fclose(prof_input);
1360 mark_closed(tmp_fd);
1361 }
Eric Andersenb558e762000-11-30 22:43:16 +00001362 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001363
Eric Andersenf21aa842000-12-08 20:50:30 +00001364 while ((opt = getopt(argc_l, argv_l, "cxi")) > 0) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001365 switch (opt) {
1366 case 'c':
1367 input = NULL;
Matt Kraai6085c722000-09-06 01:46:18 +00001368 if (local_pending_command != 0)
Matt Kraaidd19c692001-01-31 19:00:21 +00001369 error_msg_and_die("multiple -c arguments");
Matt Kraai6085c722000-09-06 01:46:18 +00001370 local_pending_command = xstrdup(argv[optind]);
1371 optind++;
1372 argv = argv+optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001373 break;
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001374 case 'i':
1375 interactive = TRUE;
1376 break;
Eric Andersen501c88b2000-07-28 15:14:45 +00001377 default:
Eric Andersen67991cf2001-02-14 21:23:06 +00001378 show_usage();
Eric Andersen501c88b2000-07-28 15:14:45 +00001379 }
1380 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001381 /* A shell is interactive if the `-i' flag was given, or if all of
1382 * the following conditions are met:
1383 * no -c command
1384 * no arguments remaining or the -s flag given
1385 * standard input is a terminal
1386 * standard output is a terminal
1387 * Refer to Posix.2, the description of the `sh' utility. */
Eric Andersen86349772000-12-18 20:25:50 +00001388 if (argv[optind]==NULL && input==stdin &&
1389 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
1390 interactive=TRUE;
1391 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001392 setup_job_control();
Eric Andersen86349772000-12-18 20:25:50 +00001393 if (interactive==TRUE) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001394 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Eric Andersen501c88b2000-07-28 15:14:45 +00001395 /* Looks like they want an interactive shell */
Matt Kraai4ef40c02001-04-12 20:44:21 +00001396 printf( "\n\n" BB_BANNER " Built-in shell (lash)\n");
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001397 printf( "Enter 'help' for a list of built-in commands.\n\n");
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001398 } else if (local_pending_command==NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001399 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Matt Kraaibbaef662000-09-27 02:43:35 +00001400 input = xfopen(argv[optind], "r");
Eric Andersen8ea28be2001-01-05 20:58:22 +00001401 mark_open(fileno(input)); /* be lazy, never mark this closed */
Eric Andersen501c88b2000-07-28 15:14:45 +00001402 }
1403
Eric Andersen6efc48c2000-07-18 08:16:39 +00001404 /* initialize the cwd -- this is never freed...*/
Eric Andersene5dfced2001-04-09 22:48:12 +00001405 cwd = xgetcwd(0);
Eric Andersen5f265b72001-05-11 16:58:46 +00001406 if (!cwd)
1407 cwd = unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001408
Eric Andersenfad9c112000-07-25 18:06:52 +00001409#ifdef BB_FEATURE_CLEAN_UP
1410 atexit(free_memory);
1411#endif
1412
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001413#ifdef BB_FEATURE_COMMAND_EDITING
1414 cmdedit_set_initial_prompt();
1415#else
1416 PS1 = NULL;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001417#endif
1418
Erik Andersen161220c2000-03-16 08:12:48 +00001419 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001420}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001421