blob: 689c720f60ff61d84af1a2fdd247b907c069fc6d [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>
Mark Whitley4b541a82001-04-25 17:10:30 +000047#include "busybox.h"
48#include "cmdedit.h"
Eric Andersene5dfced2001-04-09 22:48:12 +000049
50#ifdef BB_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +000051#include <locale.h>
Eric Andersene5dfced2001-04-09 22:48:12 +000052#endif
Eric Andersen13d1fa12001-03-08 23:59:45 +000053
Eric Andersen13d1fa12001-03-08 23:59:45 +000054#include <glob.h>
55#define expand_t glob_t
Erik Andersen3522eb12000-03-12 23:49:18 +000056
Eric Andersen13d1fa12001-03-08 23:59:45 +000057
Mark Whitley59ab0252001-01-23 22:30:04 +000058static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000059#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
60
Erik Andersend75af992000-03-16 08:09:09 +000061
Eric Andersen86349772000-12-18 20:25:50 +000062enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000063 REDIRECT_APPEND
64};
Erik Andersen3522eb12000-03-12 23:49:18 +000065
Eric Andersen86349772000-12-18 20:25:50 +000066static const unsigned int DEFAULT_CONTEXT=0x1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +000067static const unsigned int IF_TRUE_CONTEXT=0x2;
68static const unsigned int IF_FALSE_CONTEXT=0x4;
69static const unsigned int THEN_EXP_CONTEXT=0x8;
70static const unsigned int ELSE_EXP_CONTEXT=0x10;
Eric Andersenfad9c112000-07-25 18:06:52 +000071
Eric Andersen86349772000-12-18 20:25:50 +000072
73struct jobset {
Erik Andersen161220c2000-03-16 08:12:48 +000074 struct job *head; /* head of list of running jobs */
75 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000076};
77
Eric Andersen86349772000-12-18 20:25:50 +000078struct redir_struct {
79 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000080 int fd; /* file descriptor being redirected */
81 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000082};
83
Eric Andersen86349772000-12-18 20:25:50 +000084struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000085 pid_t pid; /* 0 if exited */
86 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000087 int num_redirects; /* elements in redirection array */
88 struct redir_struct *redirects; /* I/O redirects */
Eric Andersen86349772000-12-18 20:25:50 +000089 int is_stopped; /* is the program currently running? */
90 struct job *family; /* pointer back to the child's parent job */
Erik Andersen3522eb12000-03-12 23:49:18 +000091};
92
93struct job {
Eric Andersen86349772000-12-18 20:25:50 +000094 int jobid; /* job number */
95 int num_progs; /* total number of programs in job */
96 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000097 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +000098 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +000099 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +0000100 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +0000101 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +0000102 int stopped_progs; /* number of programs alive, but stopped */
103 unsigned int job_context; /* bitmask defining current context */
104 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +0000105};
106
Eric Andersen86349772000-12-18 20:25:50 +0000107struct built_in_command {
Erik Andersen161220c2000-03-16 08:12:48 +0000108 char *cmd; /* name */
109 char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +0000110 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +0000111};
112
Eric Andersen8ea28be2001-01-05 20:58:22 +0000113struct close_me {
114 int fd;
115 struct close_me *next;
116};
117
Eric Andersen34e19412000-07-10 18:47:24 +0000118/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000119static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000120static int builtin_exec(struct child_prog *cmd);
121static int builtin_exit(struct child_prog *cmd);
122static int builtin_fg_bg(struct child_prog *cmd);
123static int builtin_help(struct child_prog *cmd);
124static int builtin_jobs(struct child_prog *dummy);
125static int builtin_pwd(struct child_prog *dummy);
126static int builtin_export(struct child_prog *cmd);
127static int builtin_source(struct child_prog *cmd);
128static int builtin_unset(struct child_prog *cmd);
129static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000130
Eric Andersen34e19412000-07-10 18:47:24 +0000131
132/* function prototypes for shell stuff */
Eric Andersen8ea28be2001-01-05 20:58:22 +0000133static void mark_open(int fd);
134static void mark_closed(int fd);
Eric Andersen36278b92001-03-06 20:47:31 +0000135static void close_all(void);
Eric Andersen86349772000-12-18 20:25:50 +0000136static void checkjobs(struct jobset *job_list);
137static int get_command(FILE * source, char *command);
138static int parse_command(char **command_ptr, struct job *job, int *inbg);
139static int run_command(struct job *newjob, int inbg, int outpipe[2]);
140static int pseudo_exec(struct child_prog *cmd) __attribute__ ((noreturn));
Erik Andersen3522eb12000-03-12 23:49:18 +0000141static int busy_loop(FILE * input);
142
Erik Andersend75af992000-03-16 08:09:09 +0000143
Mark Whitley37653aa2000-07-12 23:36:17 +0000144/* Table of built-in functions (these are non-forking builtins, meaning they
145 * can change global variables in the parent shell process but they will not
146 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Eric Andersen86349772000-12-18 20:25:50 +0000147static struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000148 {"bg", "Resume a job in the background", builtin_fg_bg},
149 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000150 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000151 {"exit", "Exit from shell()", builtin_exit},
152 {"fg", "Bring job into the foreground", builtin_fg_bg},
153 {"jobs", "Lists the active jobs", builtin_jobs},
154 {"export", "Set environment variable", builtin_export},
155 {"unset", "Unset environment variable", builtin_unset},
156 {"read", "Input environment variable", builtin_read},
Matt Kraaidd450a02000-09-13 03:43:36 +0000157 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000158 /* to do: add ulimit */
Eric Andersenfad9c112000-07-25 18:06:52 +0000159 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000160};
161
Mark Whitley37653aa2000-07-12 23:36:17 +0000162/* Table of forking built-in functions (things that fork cannot change global
163 * variables in the parent process, such as the current working directory) */
Eric Andersen86349772000-12-18 20:25:50 +0000164static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000165 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000166 {"help", "List shell built-in commands", builtin_help},
167 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000168};
169
Eric Andersen0bcc8132001-01-05 19:37:32 +0000170
171/* Variables we export */
172unsigned int shell_context; /* Used in cmdedit.c to reset the
173 context when someone hits ^C */
174
175
176/* Globals that are static to this file */
Eric Andersencfa88ec2001-05-11 18:08:16 +0000177static const char *cwd;
Eric Andersen744b0642001-01-05 21:23:44 +0000178static char *local_pending_command = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000179static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000180static int argc;
181static char **argv;
Eric Andersen702ec592001-03-06 22:17:29 +0000182static struct close_me *close_me_head;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000183static unsigned int last_jobid;
Eric Andersen86349772000-12-18 20:25:50 +0000184
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000185static char *PS1;
Eric Andersene5dfced2001-04-09 22:48:12 +0000186static char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000187
188
Eric Andersenb558e762000-11-30 22:43:16 +0000189#ifdef DEBUG_SHELL
190static inline void debug_printf(const char *format, ...)
191{
192 va_list args;
193 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000194 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000195 va_end(args);
196}
197#else
198static inline void debug_printf(const char *format, ...) { }
199#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000200
Eric Andersen86349772000-12-18 20:25:50 +0000201/*
202 Most builtins need access to the struct child_prog that has
203 their arguments, previously coded as cmd->progs[0]. That coding
204 can exhibit a bug, if the builtin is not the first command in
205 a pipeline: "echo foo | exec sort" will attempt to exec foo.
206
207builtin previous use notes
208------ ----------------- ---------
209cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000210exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
211exit cmd->progs[0]
212fg_bg cmd->progs[0], job_list->head, job_list->fg
213help 0
214jobs job_list->head
215pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000216export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000217source cmd->progs[0]
218unset cmd->progs[0]
219read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000220
221I added "struct job *family;" to struct child_prog,
222and switched API to builtin_foo(struct child_prog *child);
223So cmd->text becomes child->family->text
224 cmd->job_context becomes child->family->job_context
225 cmd->progs[0] becomes *child
226 job_list becomes child->family->job_list
227 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000228
Erik Andersend75af992000-03-16 08:09:09 +0000229/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000230static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000231{
Erik Andersen161220c2000-03-16 08:12:48 +0000232 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000233
Eric Andersen86349772000-12-18 20:25:50 +0000234 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000235 newdir = getenv("HOME");
236 else
Eric Andersen86349772000-12-18 20:25:50 +0000237 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000238 if (chdir(newdir)) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000239 printf("cd: %s: %m\n", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000240 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000241 }
Eric Andersencfa88ec2001-05-11 18:08:16 +0000242 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000243 if (!cwd)
244 cwd = unknown;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000245 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000246}
247
Eric Andersend2f56772000-09-21 02:48:07 +0000248/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000249static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000250{
Eric Andersen86349772000-12-18 20:25:50 +0000251 if (child->argv[1] == NULL)
252 return EXIT_SUCCESS; /* Really? */
253 child->argv++;
Eric Andersen07f2f392001-03-06 20:28:22 +0000254 close_all();
Eric Andersen86349772000-12-18 20:25:50 +0000255 pseudo_exec(child);
256 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000257}
258
Erik Andersen3522eb12000-03-12 23:49:18 +0000259/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000260static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000261{
Eric Andersen86349772000-12-18 20:25:50 +0000262 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000263 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000264
Eric Andersen86349772000-12-18 20:25:50 +0000265 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000266}
267
268/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000269static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000270{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000271 int i, jobnum;
Erik Andersen6273f652000-03-17 01:12:41 +0000272 struct job *job=NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000273
Eric Andersen8a646dd2001-06-21 16:38:11 +0000274 /* If they gave us no args, assume they want the last backgrounded task */
275 if (!child->argv[1]) {
276 for (job = child->family->job_list->head; job; job = job->next) {
277 if (job->jobid == last_jobid) {
278 break;
279 }
Erik Andersen161220c2000-03-16 08:12:48 +0000280 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000281 if (!job) {
282 error_msg("%s: no current job", child->argv[0]);
283 return EXIT_FAILURE;
284 }
285 } else {
286 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
287 error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]);
288 return EXIT_FAILURE;
289 }
290 for (job = child->family->job_list->head; job; job = job->next) {
291 if (job->jobid == jobnum) {
292 break;
293 }
294 }
295 if (!job) {
296 error_msg("%s: %d: no such job", child->argv[0], jobnum);
297 return EXIT_FAILURE;
298 }
Erik Andersend75af992000-03-16 08:09:09 +0000299 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000300
Eric Andersen86349772000-12-18 20:25:50 +0000301 if (*child->argv[0] == 'f') {
Erik Andersen161220c2000-03-16 08:12:48 +0000302 /* Make this job the foreground job */
Eric Andersen1c314ad2000-06-28 16:56:25 +0000303 /* suppress messages when run from /linuxrc mag@sysgo.de */
304 if (tcsetpgrp(0, job->pgrp) && errno != ENOTTY)
Matt Kraaia9819b22000-12-22 01:48:07 +0000305 perror_msg("tcsetpgrp");
Eric Andersen86349772000-12-18 20:25:50 +0000306 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000307 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000308
Erik Andersen161220c2000-03-16 08:12:48 +0000309 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000310 for (i = 0; i < job->num_progs; i++)
311 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000312
Erik Andersen161220c2000-03-16 08:12:48 +0000313 kill(-job->pgrp, SIGCONT);
Erik Andersen3522eb12000-03-12 23:49:18 +0000314
Eric Andersen86349772000-12-18 20:25:50 +0000315 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000316
Matt Kraai3e856ce2000-12-01 02:55:13 +0000317 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000318}
319
320/* built-in 'help' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000321static int builtin_help(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000322{
Eric Andersen86349772000-12-18 20:25:50 +0000323 struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000324
Eric Andersen86349772000-12-18 20:25:50 +0000325 printf("\nBuilt-in commands:\n");
326 printf("-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000327 for (x = bltins; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000328 if (x->descr==NULL)
329 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000330 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000331 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000332 for (x = bltins_forking; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000333 if (x->descr==NULL)
334 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000335 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000336 }
Eric Andersen86349772000-12-18 20:25:50 +0000337 printf("\n\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000338 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000339}
340
341/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000342static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000343{
Erik Andersen161220c2000-03-16 08:12:48 +0000344 struct job *job;
Eric Andersen86349772000-12-18 20:25:50 +0000345 char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000346
Eric Andersen86349772000-12-18 20:25:50 +0000347 for (job = child->family->job_list->head; job; job = job->next) {
348 if (job->running_progs == job->stopped_progs)
349 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000350 else
Eric Andersen86349772000-12-18 20:25:50 +0000351 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000352
Eric Andersen86349772000-12-18 20:25:50 +0000353 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000354 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000355 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000356}
357
358
359/* built-in 'pwd' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000360static int builtin_pwd(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000361{
Eric Andersencfa88ec2001-05-11 18:08:16 +0000362 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000363 if (!cwd)
364 cwd = unknown;
Matt Kraai59df6f72001-05-16 14:21:09 +0000365 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000366 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000367}
368
Erik Andersen6273f652000-03-17 01:12:41 +0000369/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000370static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000371{
Erik Andersen161220c2000-03-16 08:12:48 +0000372 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000373 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000374
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000375 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000376 char **e;
377 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000378 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000379 }
Matt Kraai2129f972001-04-04 17:50:04 +0000380 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000381 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000382 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000383 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000384 fprintf(stderr, "export: %m\n");
Eric Andersen004015e2001-05-21 20:30:51 +0000385#ifdef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000386 if (strncmp(v, "PS1=", 4)==0)
387 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000388#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000389
390#ifdef BB_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +0000391 if(strncmp(v, "LC_ALL=", 7)==0)
392 setlocale(LC_ALL, getenv("LC_ALL"));
Mark Whitleya82a0032001-03-27 17:07:15 +0000393 if(strncmp(v, "LC_CTYPE=", 9)==0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000394 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000395#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000396
Erik Andersen161220c2000-03-16 08:12:48 +0000397 return (res);
Erik Andersen3522eb12000-03-12 23:49:18 +0000398}
399
Eric Andersenb54833c2000-07-03 23:56:26 +0000400/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000401static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000402{
403 int res = 0, len, newlen;
404 char *s;
405 char string[MAX_READ];
406
Eric Andersen86349772000-12-18 20:25:50 +0000407 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000408 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen86349772000-12-18 20:25:50 +0000409 strcpy(string, child->argv[1]);
Eric Andersenb54833c2000-07-03 23:56:26 +0000410 len = strlen(string);
411 string[len++] = '=';
412 string[len] = '\0';
413 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
414 newlen = strlen(string);
415 if(newlen > len)
416 string[--newlen] = '\0'; /* chomp trailing newline */
417 /*
418 ** string should now contain "VAR=<value>"
419 ** copy it (putenv() won't do that, so we must make sure
420 ** the string resides in a static buffer!)
421 */
422 res = -1;
423 if((s = strdup(string)))
424 res = putenv(s);
425 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000426 fprintf(stderr, "read: %m\n");
Eric Andersenb54833c2000-07-03 23:56:26 +0000427 }
428 else
429 fgets(string, sizeof(string), stdin);
430
431 return (res);
432}
433
Erik Andersen3522eb12000-03-12 23:49:18 +0000434/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000435static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000436{
Erik Andersen161220c2000-03-16 08:12:48 +0000437 FILE *input;
438 int status;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000439 int fd;
Erik Andersen3522eb12000-03-12 23:49:18 +0000440
Eric Andersen86349772000-12-18 20:25:50 +0000441 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000442 return EXIT_FAILURE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000443
Eric Andersen86349772000-12-18 20:25:50 +0000444 input = fopen(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000445 if (!input) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000446 printf( "Couldn't open file '%s'\n", child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000447 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000448 }
Erik Andersend75af992000-03-16 08:09:09 +0000449
Eric Andersen8ea28be2001-01-05 20:58:22 +0000450 fd=fileno(input);
451 mark_open(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000452 /* Now run the file */
453 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000454 fclose(input);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000455 mark_closed(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000456 return (status);
Erik Andersen3522eb12000-03-12 23:49:18 +0000457}
458
459/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000460static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000461{
Eric Andersen86349772000-12-18 20:25:50 +0000462 if (child->argv[1] == NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000463 printf( "unset: parameter required.\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000464 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000465 }
Eric Andersen86349772000-12-18 20:25:50 +0000466 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000467 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000468}
469
Eric Andersen8ea28be2001-01-05 20:58:22 +0000470static void mark_open(int fd)
471{
472 struct close_me *new = xmalloc(sizeof(struct close_me));
473 new->fd = fd;
474 new->next = close_me_head;
475 close_me_head = new;
476}
477
478static void mark_closed(int fd)
479{
480 struct close_me *tmp;
481 if (close_me_head == NULL || close_me_head->fd != fd)
482 error_msg_and_die("corrupt close_me");
483 tmp = close_me_head;
484 close_me_head = close_me_head->next;
485 free(tmp);
486}
487
488static void close_all()
489{
Eric Andersen702ec592001-03-06 22:17:29 +0000490 struct close_me *c, *tmp;
491 for (c=close_me_head; c; c=tmp) {
492 close(c->fd);
493 tmp=c->next;
494 free(c);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000495 }
496 close_me_head = NULL;
497}
498
499
Erik Andersen3522eb12000-03-12 23:49:18 +0000500/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000501static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000502{
Erik Andersen161220c2000-03-16 08:12:48 +0000503 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000504 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000505
Eric Andersen86349772000-12-18 20:25:50 +0000506 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000507 free(cmd->progs[i].argv);
Eric Andersen86349772000-12-18 20:25:50 +0000508 if (cmd->progs[i].redirects)
509 free(cmd->progs[i].redirects);
Erik Andersen161220c2000-03-16 08:12:48 +0000510 }
Mark Whitley44a99142001-03-14 17:26:37 +0000511 if (cmd->progs)
512 free(cmd->progs);
Erik Andersen161220c2000-03-16 08:12:48 +0000513 if (cmd->text)
514 free(cmd->text);
Mark Whitley44a99142001-03-14 17:26:37 +0000515 if (cmd->cmdbuf)
516 free(cmd->cmdbuf);
517 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000518 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000519 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000520}
521
Eric Andersen1ca20a72001-03-21 07:34:27 +0000522/* remove a job from a jobset */
523static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000524{
Eric Andersen86349772000-12-18 20:25:50 +0000525 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000526
Eric Andersen86349772000-12-18 20:25:50 +0000527 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000528 if (job == j_list->head) {
529 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000530 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000531 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000532 while (prevjob->next != job)
533 prevjob = prevjob->next;
534 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000535 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000536
Erik Andersen161220c2000-03-16 08:12:48 +0000537 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000538}
539
540/* Checks to see if any background processes have exited -- if they
541 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000542static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000543{
Erik Andersen161220c2000-03-16 08:12:48 +0000544 struct job *job;
545 pid_t childpid;
546 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000547 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000548
Erik Andersen161220c2000-03-16 08:12:48 +0000549 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000550 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000551 prognum = 0;
552 while (prognum < job->num_progs &&
553 job->progs[prognum].pid != childpid) prognum++;
554 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000555 break;
556 }
557
Eric Andersena1d187a2000-07-17 19:14:41 +0000558 /* This happens on backticked commands */
559 if(job==NULL)
560 return;
561
Erik Andersen161220c2000-03-16 08:12:48 +0000562 if (WIFEXITED(status) || WIFSIGNALED(status)) {
563 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000564 job->running_progs--;
565 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000566
Eric Andersen86349772000-12-18 20:25:50 +0000567 if (!job->running_progs) {
568 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000569 last_jobid=0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000570 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000571 }
572 } else {
573 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000574 job->stopped_progs++;
575 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000576
Eric Andersen86349772000-12-18 20:25:50 +0000577 if (job->stopped_progs == job->num_progs) {
578 printf(JOB_STATUS_FORMAT, job->jobid, "Stopped",
Erik Andersen161220c2000-03-16 08:12:48 +0000579 job->text);
580 }
581 }
Erik Andersend75af992000-03-16 08:09:09 +0000582 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000583
Erik Andersen161220c2000-03-16 08:12:48 +0000584 if (childpid == -1 && errno != ECHILD)
Matt Kraaia9819b22000-12-22 01:48:07 +0000585 perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000586}
587
Eric Andersene9f07fb2000-12-21 18:31:36 +0000588/* squirrel != NULL means we squirrel away copies of stdin, stdout,
589 * and stderr if they are redirected. */
590static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000591{
592 int i;
593 int openfd;
594 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000595 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000596
Eric Andersen86349772000-12-18 20:25:50 +0000597 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000598 switch (redir->type) {
599 case REDIRECT_INPUT:
600 mode = O_RDONLY;
601 break;
602 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000603 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000604 break;
605 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000606 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000607 break;
608 }
609
610 openfd = open(redir->filename, mode, 0666);
611 if (openfd < 0) {
612 /* this could get lost if stderr has been redirected, but
613 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000614 perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000615 return 1;
616 }
617
618 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000619 if (squirrel && redir->fd < 3) {
620 squirrel[redir->fd] = dup(redir->fd);
621 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000622 dup2(openfd, redir->fd);
623 close(openfd);
624 }
625 }
626
627 return 0;
628}
629
Eric Andersene9f07fb2000-12-21 18:31:36 +0000630static void restore_redirects(int squirrel[])
631{
632 int i, fd;
633 for (i=0; i<3; i++) {
634 fd = squirrel[i];
635 if (fd != -1) {
636 /* No error checking. I sure wouldn't know what
637 * to do with an error if I found one! */
638 dup2(fd, i);
639 close(fd);
640 }
641 }
642}
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000643
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000644static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000645{
Eric Andersen004015e2001-05-21 20:30:51 +0000646#ifndef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000647 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000648#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000649 PS1 = getenv("PS1");
Eric Andersene5dfced2001-04-09 22:48:12 +0000650 if(PS1==0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000651 PS1 = "\\w \\$ ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000652#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000653}
654
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000655static inline void setup_prompt_string(char **prompt_str)
656{
Eric Andersen004015e2001-05-21 20:30:51 +0000657#ifndef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000658 /* Set up the prompt */
659 if (shell_context == 0) {
660 if (PS1)
661 free(PS1);
662 PS1=xmalloc(strlen(cwd)+4);
663 sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
664 *prompt_str = PS1;
665 } else {
666 *prompt_str = PS2;
667 }
668#else
669 *prompt_str = (shell_context==0)? PS1 : PS2;
670#endif
671}
Eric Andersen22332fd2001-01-30 23:40:39 +0000672
Eric Andersen09acc062001-01-04 11:10:38 +0000673static int get_command(FILE * source, char *command)
674{
675 char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000676
Eric Andersen1c314ad2000-06-28 16:56:25 +0000677 if (source == NULL) {
678 if (local_pending_command) {
679 /* a command specified (-c option): return it & mark it done */
680 strcpy(command, local_pending_command);
681 free(local_pending_command);
682 local_pending_command = NULL;
683 return 0;
684 }
685 return 1;
686 }
687
Erik Andersen161220c2000-03-16 08:12:48 +0000688 if (source == stdin) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000689 setup_prompt_string(&prompt_str);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000690
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000691#ifdef BB_FEATURE_COMMAND_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000692 /*
693 ** enable command line editing only while a command line
694 ** is actually being read; otherwise, we'll end up bequeathing
695 ** atexit() handlers and other unwanted stuff to our
696 ** child processes (rob@sysgo.de)
697 */
Eric Andersen86349772000-12-18 20:25:50 +0000698 cmdedit_read_input(prompt_str, command);
Eric Andersen501c88b2000-07-28 15:14:45 +0000699 cmdedit_terminate();
Erik Andersen161220c2000-03-16 08:12:48 +0000700 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000701#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000702 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000703#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000704 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000705
Erik Andersen161220c2000-03-16 08:12:48 +0000706 if (!fgets(command, BUFSIZ - 2, source)) {
707 if (source == stdin)
708 printf("\n");
709 return 1;
710 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000711
Erik Andersen161220c2000-03-16 08:12:48 +0000712 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000713}
714
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000715
Eric Andersenca604592001-03-08 17:17:13 +0000716static int expand_arguments(char *command)
717{
Eric Andersen1ca20a72001-03-21 07:34:27 +0000718 int ix = 0;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000719
Eric Andersenca604592001-03-08 17:17:13 +0000720 /* get rid of the terminating \n */
721 chomp(command);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000722
723 /* Fix up escape sequences to be the Real Thing(tm) */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000724 while( command && command[ix]) {
725 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000726 const char *tmp = command+ix+1;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000727 command[ix] = process_escape_sequence( &tmp );
728 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000729 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000730 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000731 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000732
Eric Andersenca604592001-03-08 17:17:13 +0000733 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000734}
735
Eric Andersen86349772000-12-18 20:25:50 +0000736/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
737 line). If a valid command is found, command_ptr is set to point to
Erik Andersen3522eb12000-03-12 23:49:18 +0000738 the beginning of the next command (if the original command had more
739 then one job associated with it) or NULL if no more commands are
740 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000741static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000742{
Erik Andersen161220c2000-03-16 08:12:48 +0000743 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000744 char *return_command = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +0000745 char *src, *buf, *chptr;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000746 int argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000747 int done = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000748 int argv_alloced;
Matt Kraaibe66ad32001-04-12 15:42:17 +0000749 int i, saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000750 char quote = '\0';
751 int count;
Eric Andersen86349772000-12-18 20:25:50 +0000752 struct child_prog *prog;
Erik Andersen3522eb12000-03-12 23:49:18 +0000753
Erik Andersen161220c2000-03-16 08:12:48 +0000754 /* skip leading white space */
Eric Andersen86349772000-12-18 20:25:50 +0000755 while (**command_ptr && isspace(**command_ptr))
756 (*command_ptr)++;
Erik Andersen3522eb12000-03-12 23:49:18 +0000757
Erik Andersen161220c2000-03-16 08:12:48 +0000758 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000759 if (!**command_ptr || (**command_ptr == '#')) {
760 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000761 return 0;
762 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000763
Eric Andersen86349772000-12-18 20:25:50 +0000764 *inbg = 0;
765 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000766 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000767
Erik Andersen161220c2000-03-16 08:12:48 +0000768 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000769 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000770 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000771
Erik Andersen161220c2000-03-16 08:12:48 +0000772 Getting clean memory relieves us of the task of NULL
773 terminating things and makes the rest of this look a bit
774 cleaner (though it is, admittedly, a tad less efficient) */
Eric Andersen86349772000-12-18 20:25:50 +0000775 job->cmdbuf = command = xcalloc(2*strlen(*command_ptr) + 1, sizeof(char));
Erik Andersen161220c2000-03-16 08:12:48 +0000776 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000777
Erik Andersen161220c2000-03-16 08:12:48 +0000778 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000779 prog->num_redirects = 0;
780 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000781 prog->is_stopped = 0;
782 prog->family = job;
Erik Andersen3522eb12000-03-12 23:49:18 +0000783
Eric Andersen86349772000-12-18 20:25:50 +0000784 argv_alloced = 5;
785 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
786 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +0000787
Erik Andersen161220c2000-03-16 08:12:48 +0000788 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +0000789 src = *command_ptr;
Erik Andersen161220c2000-03-16 08:12:48 +0000790 while (*src && !done) {
791 if (quote == *src) {
792 quote = '\0';
793 } else if (quote) {
794 if (*src == '\\') {
795 src++;
796 if (!*src) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000797 error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +0000798 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +0000799 return 1;
800 }
801
802 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +0000803 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +0000804 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +0000805 *buf++ = '\\';
806 }
Erik Andersen161220c2000-03-16 08:12:48 +0000807 } else if (*src == '*' || *src == '?' || *src == '[' ||
808 *src == ']') *buf++ = '\\';
809 *buf++ = *src;
810 } else if (isspace(*src)) {
Matt Kraaibe66ad32001-04-12 15:42:17 +0000811 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000812 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +0000813 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +0000814 if ((argc_l + 1) == argv_alloced) {
815 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +0000816 prog->argv = xrealloc(prog->argv,
817 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +0000818 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +0000819 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000820 prog->argv[argc_l] = buf;
Matt Kraaibe66ad32001-04-12 15:42:17 +0000821 saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000822 }
823 } else
824 switch (*src) {
825 case '"':
826 case '\'':
827 quote = *src;
Matt Kraaibe66ad32001-04-12 15:42:17 +0000828 saw_quote = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000829 break;
830
831 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000832 if (*(src-1)== '$')
833 *buf++ = *src;
834 else
835 done = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000836 break;
837
Eric Andersen86349772000-12-18 20:25:50 +0000838 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +0000839 case '<':
Eric Andersen86349772000-12-18 20:25:50 +0000840 i = prog->num_redirects++;
841 prog->redirects = xrealloc(prog->redirects,
842 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +0000843 (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +0000844
Eric Andersen86349772000-12-18 20:25:50 +0000845 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000846 if (buf != prog->argv[argc_l]) {
Erik Andersen161220c2000-03-16 08:12:48 +0000847 /* the stuff before this character may be the file number
848 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +0000849 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000850 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +0000851
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000852 if (*chptr && *prog->argv[argc_l]) {
853 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000854 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +0000855 }
856 }
857
Eric Andersen86349772000-12-18 20:25:50 +0000858 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +0000859 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +0000860 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000861 else
Eric Andersen86349772000-12-18 20:25:50 +0000862 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000863 }
864
865 if (*src++ == '>') {
866 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +0000867 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +0000868 REDIRECT_APPEND, src++;
869 else
Eric Andersen86349772000-12-18 20:25:50 +0000870 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +0000871 } else {
Eric Andersen86349772000-12-18 20:25:50 +0000872 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +0000873 }
874
875 /* This isn't POSIX sh compliant. Oh well. */
876 chptr = src;
877 while (isspace(*chptr))
878 chptr++;
879
880 if (!*chptr) {
Mark Whitley44a99142001-03-14 17:26:37 +0000881 error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +0000882 free_job(job);
883 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000884 return 1;
885 }
886
Eric Andersen86349772000-12-18 20:25:50 +0000887 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +0000888 while (*chptr && !isspace(*chptr))
889 *buf++ = *chptr++;
890
891 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000892 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +0000893 break;
894
895 case '|': /* pipe */
896 /* finish this command */
Matt Kraaibe66ad32001-04-12 15:42:17 +0000897 if (*prog->argv[argc_l] || saw_quote)
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000898 argc_l++;
899 if (!argc_l) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000900 error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +0000901 free_job(job);
902 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000903 return 1;
904 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000905 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +0000906
907 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +0000908 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +0000909 job->progs = xrealloc(job->progs,
Eric Andersen86349772000-12-18 20:25:50 +0000910 sizeof(*job->progs) * job->num_progs);
911 prog = job->progs + (job->num_progs - 1);
912 prog->num_redirects = 0;
913 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000914 prog->is_stopped = 0;
915 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000916 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000917
Eric Andersen86349772000-12-18 20:25:50 +0000918 argv_alloced = 5;
919 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +0000920 prog->argv[0] = ++buf;
921
922 src++;
923 while (*src && isspace(*src))
924 src++;
925
926 if (!*src) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000927 error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +0000928 free_job(job);
929 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000930 return 1;
931 }
932 src--; /* we'll ++ it at the end of the loop */
933
934 break;
935
936 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +0000937 *inbg = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000938 case ';': /* multiple commands */
939 done = 1;
Eric Andersen86349772000-12-18 20:25:50 +0000940 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000941 break;
942
Matt Kraai131241f2000-09-14 00:43:20 +0000943 case '\\':
944 src++;
945 if (!*src) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000946 error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +0000947 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +0000948 return 1;
949 }
950 if (*src == '*' || *src == '[' || *src == ']'
951 || *src == '?') *buf++ = '\\';
952 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +0000953 default:
954 *buf++ = *src;
955 }
956
Erik Andersend75af992000-03-16 08:09:09 +0000957 src++;
Erik Andersen161220c2000-03-16 08:12:48 +0000958 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000959
Matt Kraaibe66ad32001-04-12 15:42:17 +0000960 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000961 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +0000962 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000963 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +0000964 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +0000965 return 0;
966 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000967 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000968
Eric Andersen86349772000-12-18 20:25:50 +0000969 if (!return_command) {
970 job->text = xmalloc(strlen(*command_ptr) + 1);
971 strcpy(job->text, *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +0000972 } else {
973 /* This leaves any trailing spaces, which is a bit sloppy */
Eric Andersen86349772000-12-18 20:25:50 +0000974 count = return_command - *command_ptr;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000975 job->text = xmalloc(count + 1);
Eric Andersen86349772000-12-18 20:25:50 +0000976 strncpy(job->text, *command_ptr, count);
Erik Andersen161220c2000-03-16 08:12:48 +0000977 job->text[count] = '\0';
978 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000979
Eric Andersen86349772000-12-18 20:25:50 +0000980 *command_ptr = return_command;
Eric Andersen46f0beb2000-11-14 21:59:22 +0000981
Erik Andersend75af992000-03-16 08:09:09 +0000982 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000983}
984
Eric Andersen86349772000-12-18 20:25:50 +0000985/* Run the child_prog, no matter what kind of command it uses.
986 */
987static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000988{
Eric Andersen86349772000-12-18 20:25:50 +0000989 struct built_in_command *x;
Eric Andersenb54833c2000-07-03 23:56:26 +0000990#ifdef BB_FEATURE_SH_STANDALONE_SHELL
Eric Andersenaf4ac772001-02-01 22:43:49 +0000991 char *name;
Erik Andersenbcd61772000-05-13 06:33:19 +0000992#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000993
Eric Andersene9f07fb2000-12-21 18:31:36 +0000994 /* Check if the command matches any of the non-forking builtins.
995 * Depending on context, this might be redundant. But it's
996 * easier to waste a few CPU cycles than it is to figure out
997 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +0000998 */
Eric Andersene9f07fb2000-12-21 18:31:36 +0000999 for (x = bltins; x->cmd; x++) {
1000 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1001 exit(x->function(child));
1002 }
1003 }
1004
1005 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001006 for (x = bltins_forking; x->cmd; x++) {
1007 if (strcmp(child->argv[0], x->cmd) == 0) {
1008 applet_name=x->cmd;
1009 exit (x->function(child));
1010 }
1011 }
1012#ifdef BB_FEATURE_SH_STANDALONE_SHELL
1013 /* Check if the command matches any busybox internal
1014 * commands ("applets") here. Following discussions from
1015 * November 2000 on busybox@opensource.lineo.com, don't use
1016 * get_last_path_component(). This way explicit (with
1017 * slashes) filenames will never be interpreted as an
1018 * applet, just like with builtins. This way the user can
1019 * override an applet with an explicit filename reference.
1020 * The only downside to this change is that an explicit
1021 * /bin/foo invocation will fork and exec /bin/foo, even if
1022 * /bin/foo is a symlink to busybox.
1023 */
Matt Kraaif2cc2762001-02-01 19:21:20 +00001024 name = child->argv[0];
Eric Andersen86349772000-12-18 20:25:50 +00001025
1026#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
1027 /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then
1028 * if you run /bin/cat, it will use BusyBox cat even if
1029 * /bin/cat exists on the filesystem and is _not_ busybox.
1030 * Some systems want this, others do not. Choose wisely. :-)
1031 */
Matt Kraaif2cc2762001-02-01 19:21:20 +00001032 name = get_last_path_component(name);
Eric Andersen86349772000-12-18 20:25:50 +00001033#endif
1034
Eric Andersen67991cf2001-02-14 21:23:06 +00001035 {
Eric Andersen82ab8da2001-03-23 17:06:01 +00001036 char** argv_l=child->argv;
Eric Andersen67991cf2001-02-14 21:23:06 +00001037 int argc_l;
Eric Andersen82ab8da2001-03-23 17:06:01 +00001038 for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
Eric Andersen67991cf2001-02-14 21:23:06 +00001039 optind = 1;
1040 run_applet_by_name(name, argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001041 }
1042#endif
1043
1044 execvp(child->argv[0], child->argv);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001045 perror_msg_and_die("%s", child->argv[0]);
Eric Andersen86349772000-12-18 20:25:50 +00001046}
1047
1048static void insert_job(struct job *newjob, int inbg)
1049{
1050 struct job *thejob;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001051 struct jobset *j_list=newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001052
1053 /* find the ID for thejob to use */
1054 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001055 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001056 if (thejob->jobid >= newjob->jobid)
1057 newjob->jobid = thejob->jobid + 1;
1058
1059 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001060 if (!j_list->head) {
1061 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001062 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001063 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001064 thejob->next = xmalloc(sizeof(*thejob));
1065 thejob = thejob->next;
1066 }
1067
1068 *thejob = *newjob; /* physically copy the struct job */
1069 thejob->next = NULL;
1070 thejob->running_progs = thejob->num_progs;
1071 thejob->stopped_progs = 0;
1072
1073 if (inbg) {
1074 /* we don't wait for background thejobs to return -- append it
1075 to the list of backgrounded thejobs and leave it alone */
1076 printf("[%d] %d\n", thejob->jobid,
1077 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001078 last_jobid = newjob->jobid;
Eric Andersen86349772000-12-18 20:25:50 +00001079 } else {
1080 newjob->job_list->fg = thejob;
1081
1082 /* move the new process group into the foreground */
1083 /* suppress messages when run from /linuxrc mag@sysgo.de */
1084 if (tcsetpgrp(0, newjob->pgrp) && errno != ENOTTY)
Matt Kraaia9819b22000-12-22 01:48:07 +00001085 perror_msg("tcsetpgrp");
Eric Andersen86349772000-12-18 20:25:50 +00001086 }
1087}
1088
1089static int run_command(struct job *newjob, int inbg, int outpipe[2])
1090{
1091 /* struct job *thejob; */
1092 int i;
1093 int nextin, nextout;
1094 int pipefds[2]; /* pipefd[0] is for reading */
1095 struct built_in_command *x;
1096 struct child_prog *child;
1097
Eric Andersen6efc48c2000-07-18 08:16:39 +00001098 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001099 for (i = 0; i < newjob->num_progs; i++) {
1100 child = & (newjob->progs[i]);
1101
1102 if ((i + 1) < newjob->num_progs) {
1103 if (pipe(pipefds)<0) perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001104 nextout = pipefds[1];
1105 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001106 if (outpipe[1]!=-1) {
1107 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001108 } else {
1109 nextout = 1;
1110 }
Erik Andersen161220c2000-03-16 08:12:48 +00001111 }
1112
Eric Andersen501c88b2000-07-28 15:14:45 +00001113
Eric Andersene9f07fb2000-12-21 18:31:36 +00001114 /* Check if the command matches any non-forking builtins,
1115 * but only if this is a simple command.
1116 * Non-forking builtins within pipes have to fork anyway,
1117 * and are handled in pseudo_exec. "echo foo | read bar"
1118 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001119 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001120 if (newjob->num_progs == 1) {
1121 for (x = bltins; x->cmd; x++) {
1122 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1123 int squirrel[] = {-1, -1, -1};
1124 int rcode;
1125 setup_redirects(child, squirrel);
1126 rcode = x->function(child);
1127 restore_redirects(squirrel);
1128 return rcode;
1129 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001130 }
1131 }
1132
Eric Andersen86349772000-12-18 20:25:50 +00001133 if (!(child->pid = fork())) {
Erik Andersen161220c2000-03-16 08:12:48 +00001134 signal(SIGTTOU, SIG_DFL);
1135
Eric Andersen8ea28be2001-01-05 20:58:22 +00001136 close_all();
1137
Eric Andersen86349772000-12-18 20:25:50 +00001138 if (outpipe[1]!=-1) {
1139 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001140 }
1141 if (nextin != 0) {
1142 dup2(nextin, 0);
1143 close(nextin);
1144 }
1145
1146 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001147 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001148 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001149 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001150 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001151 }
1152
Eric Andersen86349772000-12-18 20:25:50 +00001153 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001154 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001155
Eric Andersen86349772000-12-18 20:25:50 +00001156 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001157 }
Eric Andersen86349772000-12-18 20:25:50 +00001158 if (outpipe[1]!=-1) {
1159 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001160 }
Erik Andersen161220c2000-03-16 08:12:48 +00001161
1162 /* put our child in the process group whose leader is the
1163 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001164 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001165 if (nextin != 0)
1166 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001167 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001168 close(nextout);
1169
1170 /* If there isn't another process, nextin is garbage
1171 but it doesn't matter */
1172 nextin = pipefds[0];
1173 }
1174
Eric Andersen86349772000-12-18 20:25:50 +00001175 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001176
Eric Andersen86349772000-12-18 20:25:50 +00001177 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001178
Erik Andersen161220c2000-03-16 08:12:48 +00001179 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001180}
1181
Erik Andersend75af992000-03-16 08:09:09 +00001182static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001183{
Erik Andersen161220c2000-03-16 08:12:48 +00001184 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001185 char *next_command = NULL;
1186 struct job newjob;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001187 pid_t parent_pgrp;
Erik Andersen161220c2000-03-16 08:12:48 +00001188 int i;
Eric Andersen86349772000-12-18 20:25:50 +00001189 int inbg;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001190 int status;
Eric Andersen86349772000-12-18 20:25:50 +00001191 newjob.job_list = &job_list;
1192 newjob.job_context = DEFAULT_CONTEXT;
Erik Andersen3522eb12000-03-12 23:49:18 +00001193
Eric Andersen1c314ad2000-06-28 16:56:25 +00001194 /* save current owner of TTY so we can restore it on exit */
1195 parent_pgrp = tcgetpgrp(0);
1196
Matt Kraaib8907522000-09-13 02:08:21 +00001197 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Erik Andersend75af992000-03-16 08:09:09 +00001198
Erik Andersen161220c2000-03-16 08:12:48 +00001199 /* don't pay any attention to this signal; it just confuses
1200 things and isn't really meant for shells anyway */
1201 signal(SIGTTOU, SIG_IGN);
Erik Andersend75af992000-03-16 08:09:09 +00001202
Erik Andersen161220c2000-03-16 08:12:48 +00001203 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001204 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001205 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001206
Erik Andersend75af992000-03-16 08:09:09 +00001207 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001208 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001209
Eric Andersen86349772000-12-18 20:25:50 +00001210 if (!next_command) {
1211 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001212 break;
Eric Andersen86349772000-12-18 20:25:50 +00001213 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001214 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001215
Eric Andersenca604592001-03-08 17:17:13 +00001216 if (expand_arguments(next_command) == FALSE) {
1217 free(command);
1218 command = (char *) xcalloc(BUFSIZ, sizeof(char));
1219 next_command = NULL;
1220 continue;
1221 }
1222
Eric Andersen86349772000-12-18 20:25:50 +00001223 if (!parse_command(&next_command, &newjob, &inbg) &&
1224 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001225 int pipefds[2] = {-1,-1};
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001226 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
1227 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001228 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001229 }
1230 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001231 free(command);
Matt Kraaib8907522000-09-13 02:08:21 +00001232 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Eric Andersen86349772000-12-18 20:25:50 +00001233 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001234 }
1235 } else {
1236 /* a job is running in the foreground; wait for it */
1237 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001238 while (!job_list.fg->progs[i].pid ||
1239 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001240
Eric Andersen8ea28be2001-01-05 20:58:22 +00001241 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0)
1242 perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
Erik Andersen3522eb12000-03-12 23:49:18 +00001243
Erik Andersend75af992000-03-16 08:09:09 +00001244 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001245 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001246 job_list.fg->running_progs--;
1247 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001248
Eric Andersen86349772000-12-18 20:25:50 +00001249 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001250 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001251 remove_job(&job_list, job_list.fg);
1252 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001253 }
Erik Andersend75af992000-03-16 08:09:09 +00001254 } else {
Erik Andersen161220c2000-03-16 08:12:48 +00001255 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001256 job_list.fg->stopped_progs++;
1257 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001258
Eric Andersen86349772000-12-18 20:25:50 +00001259 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1260 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1261 "Stopped", job_list.fg->text);
1262 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001263 }
Erik Andersend75af992000-03-16 08:09:09 +00001264 }
1265
Eric Andersen86349772000-12-18 20:25:50 +00001266 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001267 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001268 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersened424db2001-04-23 15:28:28 +00001269 if (tcsetpgrp(0, getpgrp()) && errno != ENOTTY)
Matt Kraaia9819b22000-12-22 01:48:07 +00001270 perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001271 }
1272 }
1273 }
Erik Andersen161220c2000-03-16 08:12:48 +00001274 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001275
Eric Andersen1c314ad2000-06-28 16:56:25 +00001276 /* return controlling TTY back to parent process group before exiting */
1277 if (tcsetpgrp(0, parent_pgrp))
Matt Kraaia9819b22000-12-22 01:48:07 +00001278 perror_msg("tcsetpgrp");
Eric Andersenb54833c2000-07-03 23:56:26 +00001279
1280 /* return exit status if called with "-c" */
1281 if (input == NULL && WIFEXITED(status))
1282 return WEXITSTATUS(status);
1283
Erik Andersen161220c2000-03-16 08:12:48 +00001284 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001285}
1286
1287
Eric Andersenfad9c112000-07-25 18:06:52 +00001288#ifdef BB_FEATURE_CLEAN_UP
1289void free_memory(void)
1290{
Eric Andersene5dfced2001-04-09 22:48:12 +00001291 if (cwd) {
Eric Andersenfad9c112000-07-25 18:06:52 +00001292 free(cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001293 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001294 if (local_pending_command)
1295 free(local_pending_command);
1296
Eric Andersen86349772000-12-18 20:25:50 +00001297 if (job_list.fg && !job_list.fg->running_progs) {
1298 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001299 }
1300}
1301#endif
1302
Eric Andersen6efc48c2000-07-18 08:16:39 +00001303
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001304int shell_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001305{
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001306 int opt, interactive=FALSE;
Erik Andersen161220c2000-03-16 08:12:48 +00001307 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001308 argc = argc_l;
1309 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001310
Eric Andersen702ec592001-03-06 22:17:29 +00001311 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001312 last_jobid = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001313 shell_context = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001314 local_pending_command = NULL;
Eric Andersen702ec592001-03-06 22:17:29 +00001315 close_me_head = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001316 job_list.head = NULL;
1317 job_list.fg = NULL;
Eric Andersen501c88b2000-07-28 15:14:45 +00001318
Eric Andersenb558e762000-11-30 22:43:16 +00001319 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001320 FILE *prof_input;
1321 prof_input = fopen("/etc/profile", "r");
1322 if (!prof_input) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001323 printf( "Couldn't open file '/etc/profile'\n");
Eric Andersen8ea28be2001-01-05 20:58:22 +00001324 } else {
1325 int tmp_fd = fileno(prof_input);
1326 mark_open(tmp_fd);
1327 /* Now run the file */
1328 busy_loop(prof_input);
1329 fclose(prof_input);
1330 mark_closed(tmp_fd);
1331 }
Eric Andersenb558e762000-11-30 22:43:16 +00001332 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001333
Eric Andersenf21aa842000-12-08 20:50:30 +00001334 while ((opt = getopt(argc_l, argv_l, "cxi")) > 0) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001335 switch (opt) {
1336 case 'c':
1337 input = NULL;
Matt Kraai6085c722000-09-06 01:46:18 +00001338 if (local_pending_command != 0)
Matt Kraaidd19c692001-01-31 19:00:21 +00001339 error_msg_and_die("multiple -c arguments");
Matt Kraai6085c722000-09-06 01:46:18 +00001340 local_pending_command = xstrdup(argv[optind]);
1341 optind++;
1342 argv = argv+optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001343 break;
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001344 case 'i':
1345 interactive = TRUE;
1346 break;
Eric Andersen501c88b2000-07-28 15:14:45 +00001347 default:
Eric Andersen67991cf2001-02-14 21:23:06 +00001348 show_usage();
Eric Andersen501c88b2000-07-28 15:14:45 +00001349 }
1350 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001351 /* A shell is interactive if the `-i' flag was given, or if all of
1352 * the following conditions are met:
1353 * no -c command
1354 * no arguments remaining or the -s flag given
1355 * standard input is a terminal
1356 * standard output is a terminal
1357 * Refer to Posix.2, the description of the `sh' utility. */
Eric Andersen86349772000-12-18 20:25:50 +00001358 if (argv[optind]==NULL && input==stdin &&
1359 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
1360 interactive=TRUE;
1361 }
1362 if (interactive==TRUE) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001363 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Eric Andersen501c88b2000-07-28 15:14:45 +00001364 /* Looks like they want an interactive shell */
Matt Kraai4ef40c02001-04-12 20:44:21 +00001365 printf( "\n\n" BB_BANNER " Built-in shell (lash)\n");
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001366 printf( "Enter 'help' for a list of built-in commands.\n\n");
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001367 } else if (local_pending_command==NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001368 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Matt Kraaibbaef662000-09-27 02:43:35 +00001369 input = xfopen(argv[optind], "r");
Eric Andersen8ea28be2001-01-05 20:58:22 +00001370 mark_open(fileno(input)); /* be lazy, never mark this closed */
Eric Andersen501c88b2000-07-28 15:14:45 +00001371 }
1372
Eric Andersen6efc48c2000-07-18 08:16:39 +00001373 /* initialize the cwd -- this is never freed...*/
Eric Andersene5dfced2001-04-09 22:48:12 +00001374 cwd = xgetcwd(0);
Eric Andersen5f265b72001-05-11 16:58:46 +00001375 if (!cwd)
1376 cwd = unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001377
Eric Andersenfad9c112000-07-25 18:06:52 +00001378#ifdef BB_FEATURE_CLEAN_UP
1379 atexit(free_memory);
1380#endif
1381
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001382#ifdef BB_FEATURE_COMMAND_EDITING
1383 cmdedit_set_initial_prompt();
1384#else
1385 PS1 = NULL;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001386#endif
1387
Erik Andersen161220c2000-03-16 08:12:48 +00001388 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001389}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001390