blob: c4655acd8850350da8565ac49c173da7d9214f78 [file] [log] [blame]
Erik Andersen3522eb12000-03-12 23:49:18 +00001/* vi: set sw=4 ts=4: */
2/*
Erik Andersen6acaa402000-03-26 14:03:20 +00003 * lash -- the BusyBox Lame-Ass SHell
Erik Andersen3522eb12000-03-12 23:49:18 +00004 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Erik Andersen3522eb12000-03-12 23:49:18 +00006 *
7 * Based in part on ladsh.c by Michael K. Johnson and Erik W. Troan, which is
8 * under the following liberal license: "We have placed this source code in the
9 * public domain. Use it in any project, free or commercial."
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
Eric Andersenbdfd0d72001-10-24 05:00:29 +000027/* This shell's parsing engine is officially at a dead-end. Future
28 * work shell work should be done using hush, msh, or ash. This is
29 * still a very useful, small shell -- it just don't need any more
30 * features beyond what it already has...
Eric Andersen8ea28be2001-01-05 20:58:22 +000031 */
Eric Andersen8a646dd2001-06-21 16:38:11 +000032
Eric Andersen1e7cea92000-12-06 23:47:38 +000033//For debugging/development on the shell only...
Eric Andersen501c88b2000-07-28 15:14:45 +000034//#define DEBUG_SHELL
Eric Andersena1d187a2000-07-17 19:14:41 +000035
36
Erik Andersen3522eb12000-03-12 23:49:18 +000037#include <stdio.h>
38#include <stdlib.h>
39#include <ctype.h>
40#include <errno.h>
41#include <fcntl.h>
Erik Andersen3522eb12000-03-12 23:49:18 +000042#include <signal.h>
43#include <string.h>
44#include <sys/ioctl.h>
45#include <sys/wait.h>
46#include <unistd.h>
Eric Andersen501c88b2000-07-28 15:14:45 +000047#include <getopt.h>
Eric Andersen2d848a42001-06-25 17:11:54 +000048#include <termios.h>
Mark Whitley4b541a82001-04-25 17:10:30 +000049#include "busybox.h"
50#include "cmdedit.h"
Eric Andersene5dfced2001-04-09 22:48:12 +000051
Eric Andersenbdfd0d72001-10-24 05:00:29 +000052#ifdef CONFIG_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +000053#include <locale.h>
Eric Andersene5dfced2001-04-09 22:48:12 +000054#endif
Eric Andersen13d1fa12001-03-08 23:59:45 +000055
Eric Andersen13d1fa12001-03-08 23:59:45 +000056#include <glob.h>
57#define expand_t glob_t
Erik Andersen3522eb12000-03-12 23:49:18 +000058
Eric Andersenff9ad472004-02-10 01:07:45 +000059/* Always enable for the moment... */
Eric Andersen95b26252004-02-10 01:30:21 +000060#define CONFIG_LASH_PIPE_N_REDIRECTS
61#define CONFIG_LASH_JOB_CONTROL
Eric Andersen13d1fa12001-03-08 23:59:45 +000062
Mark Whitley59ab0252001-01-23 22:30:04 +000063static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000064#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
65
Erik Andersend75af992000-03-16 08:09:09 +000066
Eric Andersenff9ad472004-02-10 01:07:45 +000067#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000068enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000069 REDIRECT_APPEND
70};
Eric Andersenff9ad472004-02-10 01:07:45 +000071#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000072
Eric Andersen86349772000-12-18 20:25:50 +000073static const unsigned int DEFAULT_CONTEXT=0x1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +000074static const unsigned int IF_TRUE_CONTEXT=0x2;
75static const unsigned int IF_FALSE_CONTEXT=0x4;
76static const unsigned int THEN_EXP_CONTEXT=0x8;
77static const unsigned int ELSE_EXP_CONTEXT=0x10;
Eric Andersenfad9c112000-07-25 18:06:52 +000078
Eric Andersenff9ad472004-02-10 01:07:45 +000079#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000080struct redir_struct {
81 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000082 int fd; /* file descriptor being redirected */
83 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000084};
Eric Andersenff9ad472004-02-10 01:07:45 +000085#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000086
Eric Andersen86349772000-12-18 20:25:50 +000087struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000088 pid_t pid; /* 0 if exited */
89 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000090 int num_redirects; /* elements in redirection array */
Eric Andersen86349772000-12-18 20:25:50 +000091 int is_stopped; /* is the program currently running? */
92 struct job *family; /* pointer back to the child's parent job */
Eric Andersenff9ad472004-02-10 01:07:45 +000093#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
94 struct redir_struct *redirects; /* I/O redirects */
95#endif
96};
97
98struct jobset {
99 struct job *head; /* head of list of running jobs */
100 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +0000101};
102
103struct job {
Eric Andersen86349772000-12-18 20:25:50 +0000104 int jobid; /* job number */
105 int num_progs; /* total number of programs in job */
106 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +0000107 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +0000108 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +0000109 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +0000110 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +0000111 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +0000112 int stopped_progs; /* number of programs alive, but stopped */
113 unsigned int job_context; /* bitmask defining current context */
114 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +0000115};
116
Eric Andersen86349772000-12-18 20:25:50 +0000117struct built_in_command {
Erik Andersen161220c2000-03-16 08:12:48 +0000118 char *cmd; /* name */
119 char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +0000120 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +0000121};
122
Eric Andersen8ea28be2001-01-05 20:58:22 +0000123struct close_me {
124 int fd;
125 struct close_me *next;
126};
127
Eric Andersen34e19412000-07-10 18:47:24 +0000128/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000129static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000130static int builtin_exec(struct child_prog *cmd);
131static int builtin_exit(struct child_prog *cmd);
132static int builtin_fg_bg(struct child_prog *cmd);
133static int builtin_help(struct child_prog *cmd);
134static int builtin_jobs(struct child_prog *dummy);
135static int builtin_pwd(struct child_prog *dummy);
136static int builtin_export(struct child_prog *cmd);
137static int builtin_source(struct child_prog *cmd);
138static int builtin_unset(struct child_prog *cmd);
139static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000140
Eric Andersen34e19412000-07-10 18:47:24 +0000141
142/* function prototypes for shell stuff */
Eric Andersen8ea28be2001-01-05 20:58:22 +0000143static void mark_open(int fd);
144static void mark_closed(int fd);
Eric Andersen36278b92001-03-06 20:47:31 +0000145static void close_all(void);
Eric Andersen86349772000-12-18 20:25:50 +0000146static void checkjobs(struct jobset *job_list);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000147static void remove_job(struct jobset *j_list, struct job *job);
Eric Andersen86349772000-12-18 20:25:50 +0000148static int get_command(FILE * source, char *command);
149static int parse_command(char **command_ptr, struct job *job, int *inbg);
150static int run_command(struct job *newjob, int inbg, int outpipe[2]);
151static int pseudo_exec(struct child_prog *cmd) __attribute__ ((noreturn));
Erik Andersen3522eb12000-03-12 23:49:18 +0000152static int busy_loop(FILE * input);
153
Erik Andersend75af992000-03-16 08:09:09 +0000154
Mark Whitley37653aa2000-07-12 23:36:17 +0000155/* Table of built-in functions (these are non-forking builtins, meaning they
156 * can change global variables in the parent shell process but they will not
157 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Eric Andersen86349772000-12-18 20:25:50 +0000158static struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000159 {"bg", "Resume a job in the background", builtin_fg_bg},
160 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000161 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000162 {"exit", "Exit from shell()", builtin_exit},
163 {"fg", "Bring job into the foreground", builtin_fg_bg},
164 {"jobs", "Lists the active jobs", builtin_jobs},
165 {"export", "Set environment variable", builtin_export},
166 {"unset", "Unset environment variable", builtin_unset},
167 {"read", "Input environment variable", builtin_read},
Matt Kraaidd450a02000-09-13 03:43:36 +0000168 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000169 /* to do: add ulimit */
Eric Andersenfad9c112000-07-25 18:06:52 +0000170 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000171};
172
Mark Whitley37653aa2000-07-12 23:36:17 +0000173/* Table of forking built-in functions (things that fork cannot change global
174 * variables in the parent process, such as the current working directory) */
Eric Andersen86349772000-12-18 20:25:50 +0000175static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000176 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000177 {"help", "List shell built-in commands", builtin_help},
178 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000179};
180
Eric Andersen0bcc8132001-01-05 19:37:32 +0000181
Eric Andersen7467c8d2001-07-12 20:26:32 +0000182static int shell_context; /* Type prompt trigger (PS1 or PS2) */
Eric Andersen0bcc8132001-01-05 19:37:32 +0000183
184
185/* Globals that are static to this file */
Eric Andersencfa88ec2001-05-11 18:08:16 +0000186static const char *cwd;
Eric Andersen744b0642001-01-05 21:23:44 +0000187static char *local_pending_command = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000188static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000189static int argc;
190static char **argv;
Eric Andersen702ec592001-03-06 22:17:29 +0000191static struct close_me *close_me_head;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000192static int last_return_code;
193static int last_bg_pid;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000194static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000195static int shell_terminal;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000196static char *PS1;
Eric Andersene5dfced2001-04-09 22:48:12 +0000197static char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000198
199
Eric Andersenb558e762000-11-30 22:43:16 +0000200#ifdef DEBUG_SHELL
201static inline void debug_printf(const char *format, ...)
202{
203 va_list args;
204 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000205 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000206 va_end(args);
207}
208#else
209static inline void debug_printf(const char *format, ...) { }
210#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000211
Eric Andersen86349772000-12-18 20:25:50 +0000212/*
213 Most builtins need access to the struct child_prog that has
214 their arguments, previously coded as cmd->progs[0]. That coding
215 can exhibit a bug, if the builtin is not the first command in
216 a pipeline: "echo foo | exec sort" will attempt to exec foo.
217
218builtin previous use notes
219------ ----------------- ---------
220cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000221exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
222exit cmd->progs[0]
223fg_bg cmd->progs[0], job_list->head, job_list->fg
224help 0
225jobs job_list->head
226pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000227export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000228source cmd->progs[0]
229unset cmd->progs[0]
230read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000231
232I added "struct job *family;" to struct child_prog,
233and switched API to builtin_foo(struct child_prog *child);
234So cmd->text becomes child->family->text
235 cmd->job_context becomes child->family->job_context
236 cmd->progs[0] becomes *child
237 job_list becomes child->family->job_list
238 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000239
Erik Andersend75af992000-03-16 08:09:09 +0000240/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000241static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000242{
Erik Andersen161220c2000-03-16 08:12:48 +0000243 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000244
Eric Andersen86349772000-12-18 20:25:50 +0000245 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000246 newdir = getenv("HOME");
247 else
Eric Andersen86349772000-12-18 20:25:50 +0000248 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000249 if (chdir(newdir)) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000250 printf("cd: %s: %m\n", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000251 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000252 }
Eric Andersencfa88ec2001-05-11 18:08:16 +0000253 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000254 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000255 cwd = bb_msg_unknown;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000256 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000257}
258
Eric Andersend2f56772000-09-21 02:48:07 +0000259/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000260static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000261{
Eric Andersen86349772000-12-18 20:25:50 +0000262 if (child->argv[1] == NULL)
263 return EXIT_SUCCESS; /* Really? */
264 child->argv++;
Eric Andersen07f2f392001-03-06 20:28:22 +0000265 close_all();
Eric Andersen86349772000-12-18 20:25:50 +0000266 pseudo_exec(child);
267 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000268}
269
Erik Andersen3522eb12000-03-12 23:49:18 +0000270/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000271static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000272{
Eric Andersen86349772000-12-18 20:25:50 +0000273 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000274 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000275
Eric Andersen86349772000-12-18 20:25:50 +0000276 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000277}
278
279/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000280static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000281{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000282 int i, jobnum;
Erik Andersen6273f652000-03-17 01:12:41 +0000283 struct job *job=NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000284
Eric Andersen8a646dd2001-06-21 16:38:11 +0000285 /* If they gave us no args, assume they want the last backgrounded task */
286 if (!child->argv[1]) {
287 for (job = child->family->job_list->head; job; job = job->next) {
288 if (job->jobid == last_jobid) {
289 break;
290 }
Erik Andersen161220c2000-03-16 08:12:48 +0000291 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000292 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000293 bb_error_msg("%s: no current job", child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000294 return EXIT_FAILURE;
295 }
296 } else {
297 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000298 bb_error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000299 return EXIT_FAILURE;
300 }
301 for (job = child->family->job_list->head; job; job = job->next) {
302 if (job->jobid == jobnum) {
303 break;
304 }
305 }
306 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000307 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000308 return EXIT_FAILURE;
309 }
Erik Andersend75af992000-03-16 08:09:09 +0000310 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000311
Eric Andersen86349772000-12-18 20:25:50 +0000312 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000313 /* Put the job into the foreground. */
314 tcsetpgrp(shell_terminal, job->pgrp);
315
Eric Andersen86349772000-12-18 20:25:50 +0000316 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000317 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000318
Erik Andersen161220c2000-03-16 08:12:48 +0000319 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000320 for (i = 0; i < job->num_progs; i++)
321 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000322
Eric Andersen86349772000-12-18 20:25:50 +0000323 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000324
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000325 if ( (i=kill(- job->pgrp, SIGCONT)) < 0) {
326 if (i == ESRCH) {
327 remove_job(&job_list, job);
Eric Andersen07abfe22001-06-27 17:29:11 +0000328 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000329 bb_perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000330 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000331 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000332
Matt Kraai3e856ce2000-12-01 02:55:13 +0000333 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000334}
335
336/* built-in 'help' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000337static int builtin_help(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000338{
Eric Andersen86349772000-12-18 20:25:50 +0000339 struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000340
Eric Andersen86349772000-12-18 20:25:50 +0000341 printf("\nBuilt-in commands:\n");
342 printf("-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000343 for (x = bltins; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000344 if (x->descr==NULL)
345 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000346 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000347 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000348 for (x = bltins_forking; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000349 if (x->descr==NULL)
350 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000351 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000352 }
Eric Andersen86349772000-12-18 20:25:50 +0000353 printf("\n\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000354 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000355}
356
357/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000358static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000359{
Erik Andersen161220c2000-03-16 08:12:48 +0000360 struct job *job;
Eric Andersen86349772000-12-18 20:25:50 +0000361 char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000362
Eric Andersen86349772000-12-18 20:25:50 +0000363 for (job = child->family->job_list->head; job; job = job->next) {
364 if (job->running_progs == job->stopped_progs)
365 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000366 else
Eric Andersen86349772000-12-18 20:25:50 +0000367 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000368
Eric Andersen86349772000-12-18 20:25:50 +0000369 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000370 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000371 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000372}
373
374
375/* built-in 'pwd' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000376static int builtin_pwd(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000377{
Eric Andersencfa88ec2001-05-11 18:08:16 +0000378 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000379 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000380 cwd = bb_msg_unknown;
Matt Kraai59df6f72001-05-16 14:21:09 +0000381 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000382 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000383}
384
Erik Andersen6273f652000-03-17 01:12:41 +0000385/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000386static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000387{
Erik Andersen161220c2000-03-16 08:12:48 +0000388 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000389 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000390
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000391 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000392 char **e;
393 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000394 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000395 }
Matt Kraai2129f972001-04-04 17:50:04 +0000396 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000397 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000398 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000399 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000400 fprintf(stderr, "export: %m\n");
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000401#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000402 if (strncmp(v, "PS1=", 4)==0)
403 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000404#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000405
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000406#ifdef CONFIG_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +0000407 if(strncmp(v, "LC_ALL=", 7)==0)
408 setlocale(LC_ALL, getenv("LC_ALL"));
Mark Whitleya82a0032001-03-27 17:07:15 +0000409 if(strncmp(v, "LC_CTYPE=", 9)==0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000410 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000411#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000412
Erik Andersen161220c2000-03-16 08:12:48 +0000413 return (res);
Erik Andersen3522eb12000-03-12 23:49:18 +0000414}
415
Eric Andersenb54833c2000-07-03 23:56:26 +0000416/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000417static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000418{
419 int res = 0, len, newlen;
420 char *s;
421 char string[MAX_READ];
422
Eric Andersen86349772000-12-18 20:25:50 +0000423 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000424 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen0d2d1eb2002-06-06 13:33:01 +0000425 safe_strncpy(string, child->argv[1], MAX_READ-1);
Eric Andersenb54833c2000-07-03 23:56:26 +0000426 len = strlen(string);
427 string[len++] = '=';
428 string[len] = '\0';
429 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
430 newlen = strlen(string);
431 if(newlen > len)
432 string[--newlen] = '\0'; /* chomp trailing newline */
433 /*
434 ** string should now contain "VAR=<value>"
435 ** copy it (putenv() won't do that, so we must make sure
436 ** the string resides in a static buffer!)
437 */
438 res = -1;
439 if((s = strdup(string)))
440 res = putenv(s);
441 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000442 fprintf(stderr, "read: %m\n");
Eric Andersenb54833c2000-07-03 23:56:26 +0000443 }
444 else
445 fgets(string, sizeof(string), stdin);
446
447 return (res);
448}
449
Erik Andersen3522eb12000-03-12 23:49:18 +0000450/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000451static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000452{
Erik Andersen161220c2000-03-16 08:12:48 +0000453 FILE *input;
454 int status;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000455 int fd;
Erik Andersen3522eb12000-03-12 23:49:18 +0000456
Eric Andersen86349772000-12-18 20:25:50 +0000457 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000458 return EXIT_FAILURE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000459
Eric Andersen86349772000-12-18 20:25:50 +0000460 input = fopen(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000461 if (!input) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000462 printf( "Couldn't open file '%s'\n", child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000463 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000464 }
Erik Andersend75af992000-03-16 08:09:09 +0000465
Eric Andersen8ea28be2001-01-05 20:58:22 +0000466 fd=fileno(input);
467 mark_open(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000468 /* Now run the file */
469 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000470 fclose(input);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000471 mark_closed(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000472 return (status);
Erik Andersen3522eb12000-03-12 23:49:18 +0000473}
474
475/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000476static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000477{
Eric Andersen86349772000-12-18 20:25:50 +0000478 if (child->argv[1] == NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000479 printf( "unset: parameter required.\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000480 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000481 }
Eric Andersen86349772000-12-18 20:25:50 +0000482 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000483 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000484}
485
Eric Andersen8ea28be2001-01-05 20:58:22 +0000486static void mark_open(int fd)
487{
488 struct close_me *new = xmalloc(sizeof(struct close_me));
489 new->fd = fd;
490 new->next = close_me_head;
491 close_me_head = new;
492}
493
494static void mark_closed(int fd)
495{
496 struct close_me *tmp;
497 if (close_me_head == NULL || close_me_head->fd != fd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000498 bb_error_msg_and_die("corrupt close_me");
Eric Andersen8ea28be2001-01-05 20:58:22 +0000499 tmp = close_me_head;
500 close_me_head = close_me_head->next;
501 free(tmp);
502}
503
504static void close_all()
505{
Eric Andersen702ec592001-03-06 22:17:29 +0000506 struct close_me *c, *tmp;
507 for (c=close_me_head; c; c=tmp) {
508 close(c->fd);
509 tmp=c->next;
510 free(c);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000511 }
512 close_me_head = NULL;
513}
514
515
Eric Andersen37032b42004-02-10 01:28:36 +0000516#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen3522eb12000-03-12 23:49:18 +0000517/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000518static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000519{
Erik Andersen161220c2000-03-16 08:12:48 +0000520 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000521 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000522
Eric Andersen86349772000-12-18 20:25:50 +0000523 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000524 free(cmd->progs[i].argv);
Eric Andersenff9ad472004-02-10 01:07:45 +0000525#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000526 if (cmd->progs[i].redirects)
527 free(cmd->progs[i].redirects);
Eric Andersenff9ad472004-02-10 01:07:45 +0000528#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000529 }
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000530 free(cmd->progs);
531 free(cmd->text);
532 free(cmd->cmdbuf);
Mark Whitley44a99142001-03-14 17:26:37 +0000533 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000534 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000535 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000536}
537
Eric Andersen1ca20a72001-03-21 07:34:27 +0000538/* remove a job from a jobset */
539static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000540{
Eric Andersen86349772000-12-18 20:25:50 +0000541 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000542
Eric Andersen86349772000-12-18 20:25:50 +0000543 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000544 if (job == j_list->head) {
545 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000546 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000547 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000548 while (prevjob->next != job)
549 prevjob = prevjob->next;
550 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000551 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000552
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000553 if (j_list->head)
554 last_jobid = j_list->head->jobid;
555 else
556 last_jobid = 0;
557
Erik Andersen161220c2000-03-16 08:12:48 +0000558 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000559}
560
Eric Andersenff9ad472004-02-10 01:07:45 +0000561/* Checks to see if any background processes have exited -- if they
Erik Andersen3522eb12000-03-12 23:49:18 +0000562 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000563static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000564{
Erik Andersen161220c2000-03-16 08:12:48 +0000565 struct job *job;
566 pid_t childpid;
567 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000568 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000569
Erik Andersen161220c2000-03-16 08:12:48 +0000570 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000571 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000572 prognum = 0;
573 while (prognum < job->num_progs &&
574 job->progs[prognum].pid != childpid) prognum++;
575 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000576 break;
577 }
578
Eric Andersena1d187a2000-07-17 19:14:41 +0000579 /* This happens on backticked commands */
580 if(job==NULL)
581 return;
582
Erik Andersen161220c2000-03-16 08:12:48 +0000583 if (WIFEXITED(status) || WIFSIGNALED(status)) {
584 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000585 job->running_progs--;
586 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000587
Eric Andersen86349772000-12-18 20:25:50 +0000588 if (!job->running_progs) {
589 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000590 last_jobid=0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000591 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000592 }
593 } else {
594 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000595 job->stopped_progs++;
596 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000597
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000598#if 0
599 /* Printing this stuff is a pain, since it tends to
600 * overwrite the prompt an inconveinient moments. So
601 * don't do that. */
Eric Andersen86349772000-12-18 20:25:50 +0000602 if (job->stopped_progs == job->num_progs) {
603 printf(JOB_STATUS_FORMAT, job->jobid, "Stopped",
Erik Andersen161220c2000-03-16 08:12:48 +0000604 job->text);
605 }
Eric Andersenff9ad472004-02-10 01:07:45 +0000606#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000607 }
Erik Andersend75af992000-03-16 08:09:09 +0000608 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000609
Erik Andersen161220c2000-03-16 08:12:48 +0000610 if (childpid == -1 && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000611 bb_perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000612}
Eric Andersen37032b42004-02-10 01:28:36 +0000613#else
614static void checkjobs(struct jobset *j_list)
615{
616}
617static void free_job(struct job *cmd)
618{
619}
620static void remove_job(struct jobset *j_list, struct job *job)
621{
622}
623#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000624
Eric Andersenff9ad472004-02-10 01:07:45 +0000625#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersene9f07fb2000-12-21 18:31:36 +0000626/* squirrel != NULL means we squirrel away copies of stdin, stdout,
627 * and stderr if they are redirected. */
628static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000629{
630 int i;
631 int openfd;
632 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000633 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000634
Eric Andersen86349772000-12-18 20:25:50 +0000635 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000636 switch (redir->type) {
637 case REDIRECT_INPUT:
638 mode = O_RDONLY;
639 break;
640 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000641 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000642 break;
643 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000644 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000645 break;
646 }
647
648 openfd = open(redir->filename, mode, 0666);
649 if (openfd < 0) {
650 /* this could get lost if stderr has been redirected, but
651 bash and ash both lose it as well (though zsh doesn't!) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000652 bb_perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000653 return 1;
654 }
655
656 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000657 if (squirrel && redir->fd < 3) {
658 squirrel[redir->fd] = dup(redir->fd);
Eric Andersenacd647c2004-08-16 08:38:34 +0000659 fcntl (squirrel[redir->fd], F_SETFD, FD_CLOEXEC);
Eric Andersene9f07fb2000-12-21 18:31:36 +0000660 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000661 dup2(openfd, redir->fd);
662 close(openfd);
663 }
664 }
665
666 return 0;
667}
668
Eric Andersene9f07fb2000-12-21 18:31:36 +0000669static void restore_redirects(int squirrel[])
670{
671 int i, fd;
672 for (i=0; i<3; i++) {
673 fd = squirrel[i];
674 if (fd != -1) {
675 /* No error checking. I sure wouldn't know what
676 * to do with an error if I found one! */
677 dup2(fd, i);
678 close(fd);
679 }
680 }
681}
Eric Andersenff9ad472004-02-10 01:07:45 +0000682#else
683static inline int setup_redirects(struct child_prog *prog, int squirrel[])
684{
685 return 0;
686}
687static inline void restore_redirects(int squirrel[])
688{
689}
690#endif
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000691
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000692static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000693{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000694#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000695 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000696#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000697 PS1 = getenv("PS1");
Eric Andersene5dfced2001-04-09 22:48:12 +0000698 if(PS1==0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000699 PS1 = "\\w \\$ ";
Eric Andersenff9ad472004-02-10 01:07:45 +0000700#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000701}
702
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000703static inline void setup_prompt_string(char **prompt_str)
704{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000705#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000706 /* Set up the prompt */
707 if (shell_context == 0) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000708 free(PS1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000709 PS1=xmalloc(strlen(cwd)+4);
710 sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
711 *prompt_str = PS1;
712 } else {
713 *prompt_str = PS2;
714 }
715#else
716 *prompt_str = (shell_context==0)? PS1 : PS2;
Eric Andersenff9ad472004-02-10 01:07:45 +0000717#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000718}
Eric Andersen22332fd2001-01-30 23:40:39 +0000719
Eric Andersen09acc062001-01-04 11:10:38 +0000720static int get_command(FILE * source, char *command)
721{
722 char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000723
Eric Andersen1c314ad2000-06-28 16:56:25 +0000724 if (source == NULL) {
725 if (local_pending_command) {
726 /* a command specified (-c option): return it & mark it done */
727 strcpy(command, local_pending_command);
728 free(local_pending_command);
729 local_pending_command = NULL;
730 return 0;
731 }
732 return 1;
733 }
734
Erik Andersen161220c2000-03-16 08:12:48 +0000735 if (source == stdin) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000736 setup_prompt_string(&prompt_str);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000737
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000738#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000739 /*
740 ** enable command line editing only while a command line
741 ** is actually being read; otherwise, we'll end up bequeathing
742 ** atexit() handlers and other unwanted stuff to our
743 ** child processes (rob@sysgo.de)
744 */
Eric Andersen86349772000-12-18 20:25:50 +0000745 cmdedit_read_input(prompt_str, command);
Erik Andersen161220c2000-03-16 08:12:48 +0000746 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000747#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000748 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000749#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000750 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000751
Erik Andersen161220c2000-03-16 08:12:48 +0000752 if (!fgets(command, BUFSIZ - 2, source)) {
753 if (source == stdin)
754 printf("\n");
755 return 1;
756 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000757
Erik Andersen161220c2000-03-16 08:12:48 +0000758 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000759}
760
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000761static char* itoa(register int i)
762{
763 static char a[7]; /* Max 7 ints */
764 register char *b = a + sizeof(a) - 1;
765 int sign = (i < 0);
766
767 if (sign)
768 i = -i;
769 *b = 0;
770 do
771 {
772 *--b = '0' + (i % 10);
773 i /= 10;
774 }
775 while (i);
776 if (sign)
777 *--b = '-';
778 return b;
779}
780
781char * strsep_space( char *string, int * ix)
782{
783 char *token, *begin;
784
785 begin = string;
786
787 /* Short circuit the trivial case */
788 if ( !string || ! string[*ix])
789 return NULL;
790
791 /* Find the end of the token. */
792 while( string && string[*ix] && !isspace(string[*ix]) ) {
793 (*ix)++;
794 }
795
Eric Andersenff9ad472004-02-10 01:07:45 +0000796 /* Find the end of any whitespace trailing behind
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000797 * the token and let that be part of the token */
798 while( string && string[*ix] && isspace(string[*ix]) ) {
799 (*ix)++;
800 }
801
802 if (! string && *ix==0) {
803 /* Nothing useful was found */
804 return NULL;
805 }
806
807 token = xmalloc(*ix+1);
808 token[*ix] = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000809 strncpy(token, string, *ix);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000810
811 return token;
812}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000813
Eric Andersenca604592001-03-08 17:17:13 +0000814static int expand_arguments(char *command)
815{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000816 int total_length=0, length, i, retval, ix = 0;
817 expand_t expand_result;
818 char *tmpcmd, *cmd, *cmd_copy;
819 char *src, *dst, *var;
820 const char *out_of_space = "out of space during expansion";
821 int flags = GLOB_NOCHECK
822#ifdef GLOB_BRACE
823 | GLOB_BRACE
Eric Andersenff9ad472004-02-10 01:07:45 +0000824#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000825#ifdef GLOB_TILDE
826 | GLOB_TILDE
Eric Andersenff9ad472004-02-10 01:07:45 +0000827#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000828 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000829
Eric Andersenca604592001-03-08 17:17:13 +0000830 /* get rid of the terminating \n */
831 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000832
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000833 /* Fix up escape sequences to be the Real Thing(tm) */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000834 while( command && command[ix]) {
835 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000836 const char *tmp = command+ix+1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000837 command[ix] = bb_process_escape_sequence( &tmp );
Eric Andersen1ca20a72001-03-21 07:34:27 +0000838 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000839 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000840 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000841 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000842 /* Use glob and then fixup environment variables and such */
843
844 /* It turns out that glob is very stupid. We have to feed it one word at a
845 * time since it can't cope with a full string. Here we convert command
846 * (char*) into cmd (char**, one word per string) */
847
848 /* We need a clean copy, so strsep can mess up the copy while
849 * we write stuff into the original (in a minute) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000850 cmd = cmd_copy = bb_xstrdup(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000851 *command = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000852 for (ix = 0, tmpcmd = cmd;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000853 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
854 if (*tmpcmd == '\0')
855 break;
856 /* we need to trim() the result for glob! */
857 trim(tmpcmd);
858 retval = glob(tmpcmd, flags, NULL, &expand_result);
859 free(tmpcmd); /* Free mem allocated by strsep_space */
860 if (retval == GLOB_NOSPACE) {
861 /* Mem may have been allocated... */
862 globfree (&expand_result);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000863 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000864 return FALSE;
865 } else if (retval != 0) {
866 /* Some other error. GLOB_NOMATCH shouldn't
Eric Andersenff9ad472004-02-10 01:07:45 +0000867 * happen because of the GLOB_NOCHECK flag in
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000868 * the glob call. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000869 bb_error_msg("syntax error");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000870 return FALSE;
871 } else {
872 /* Convert from char** (one word per string) to a simple char*,
873 * but don't overflow command which is BUFSIZ in length */
874 for (i=0; i < expand_result.gl_pathc; i++) {
875 length=strlen(expand_result.gl_pathv[i]);
876 if (total_length+length+1 >= BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000877 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000878 return FALSE;
879 }
880 strcat(command+total_length, " ");
881 total_length+=1;
882 strcat(command+total_length, expand_result.gl_pathv[i]);
883 total_length+=length;
884 }
885 globfree (&expand_result);
886 }
887 }
888 free(cmd_copy);
889 trim(command);
890
Eric Andersenff9ad472004-02-10 01:07:45 +0000891 /* Now do the shell variable substitutions which
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000892 * wordexp can't do for us, namely $? and $! */
893 src = command;
894 while((dst = strchr(src,'$')) != NULL){
895 var = NULL;
896 switch(*(dst+1)) {
897 case '?':
898 var = itoa(last_return_code);
899 break;
900 case '!':
901 if (last_bg_pid==-1)
902 *(var)='\0';
903 else
904 var = itoa(last_bg_pid);
905 break;
906 /* Everything else like $$, $#, $[0-9], etc. should all be
907 * expanded by wordexp(), so we can in theory skip that stuff
908 * here, but just to be on the safe side (i.e., since uClibc
909 * wordexp doesn't do this stuff yet), lets leave it in for
910 * now. */
911 case '$':
912 var = itoa(getpid());
913 break;
914 case '#':
915 var = itoa(argc-1);
916 break;
917 case '0':case '1':case '2':case '3':case '4':
918 case '5':case '6':case '7':case '8':case '9':
919 {
Eric Andersen7ab93d92002-11-08 09:40:02 +0000920 int ixx=*(dst+1)-48+1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000921 if (ixx >= argc) {
922 var='\0';
923 } else {
924 var = argv[ixx];
925 }
926 }
927 break;
928
929 }
930 if (var) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000931 /* a single character construction was found, and
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000932 * already handled in the case statement */
933 src=dst+2;
934 } else {
935 /* Looks like an environment variable */
936 char delim_hold;
937 int num_skip_chars=0;
938 int dstlen = strlen(dst);
939 /* Is this a ${foo} type variable? */
940 if (dstlen >=2 && *(dst+1) == '{') {
941 src=strchr(dst+1, '}');
942 num_skip_chars=1;
943 } else {
944 src=dst+1;
945 while(isalnum(*src) || *src=='_') src++;
946 }
947 if (src == NULL) {
948 src = dst+dstlen;
949 }
950 delim_hold=*src;
951 *src='\0'; /* temporary */
952 var = getenv(dst + 1 + num_skip_chars);
953 *src=delim_hold;
954 src += num_skip_chars;
955 }
956 if (var == NULL) {
957 /* Seems we got an un-expandable variable. So delete it. */
958 var = "";
959 }
960 {
961 int subst_len = strlen(var);
962 int trail_len = strlen(src);
963 if (dst+subst_len+trail_len >= command+BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000964 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000965 return FALSE;
966 }
967 /* Move stuff to the end of the string to accommodate
968 * filling the created gap with the new stuff */
969 memmove(dst+subst_len, src, trail_len+1);
970 /* Now copy in the new stuff */
971 memcpy(dst, var, subst_len);
972 src = dst+subst_len;
973 }
974 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000975
Eric Andersenca604592001-03-08 17:17:13 +0000976 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000977}
978
Eric Andersen86349772000-12-18 20:25:50 +0000979/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
980 line). If a valid command is found, command_ptr is set to point to
Eric Andersenff9ad472004-02-10 01:07:45 +0000981 the beginning of the next command (if the original command had more
982 then one job associated with it) or NULL if no more commands are
Erik Andersen3522eb12000-03-12 23:49:18 +0000983 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000984static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000985{
Erik Andersen161220c2000-03-16 08:12:48 +0000986 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000987 char *return_command = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +0000988 char *src, *buf;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000989 int argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000990 int done = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000991 int argv_alloced;
Eric Andersenff9ad472004-02-10 01:07:45 +0000992 int saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000993 char quote = '\0';
994 int count;
Eric Andersen86349772000-12-18 20:25:50 +0000995 struct child_prog *prog;
Eric Andersenff9ad472004-02-10 01:07:45 +0000996#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
997 int i;
998 char *chptr;
999#endif
Erik Andersen3522eb12000-03-12 23:49:18 +00001000
Erik Andersen161220c2000-03-16 08:12:48 +00001001 /* skip leading white space */
Eric Andersen86349772000-12-18 20:25:50 +00001002 while (**command_ptr && isspace(**command_ptr))
1003 (*command_ptr)++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001004
Erik Andersen161220c2000-03-16 08:12:48 +00001005 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +00001006 if (!**command_ptr || (**command_ptr == '#')) {
1007 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001008 return 0;
1009 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001010
Eric Andersen86349772000-12-18 20:25:50 +00001011 *inbg = 0;
1012 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +00001013 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +00001014
Eric Andersenff9ad472004-02-10 01:07:45 +00001015 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +00001016 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +00001017 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +00001018
Eric Andersenff9ad472004-02-10 01:07:45 +00001019 Getting clean memory relieves us of the task of NULL
1020 terminating things and makes the rest of this look a bit
Erik Andersen161220c2000-03-16 08:12:48 +00001021 cleaner (though it is, admittedly, a tad less efficient) */
Eric Andersen86349772000-12-18 20:25:50 +00001022 job->cmdbuf = command = xcalloc(2*strlen(*command_ptr) + 1, sizeof(char));
Erik Andersen161220c2000-03-16 08:12:48 +00001023 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001024
Erik Andersen161220c2000-03-16 08:12:48 +00001025 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +00001026 prog->num_redirects = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001027 prog->is_stopped = 0;
1028 prog->family = job;
Eric Andersenff9ad472004-02-10 01:07:45 +00001029#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
1030 prog->redirects = NULL;
1031#endif
Erik Andersen3522eb12000-03-12 23:49:18 +00001032
Eric Andersen86349772000-12-18 20:25:50 +00001033 argv_alloced = 5;
1034 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
1035 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +00001036
Erik Andersen161220c2000-03-16 08:12:48 +00001037 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +00001038 src = *command_ptr;
Erik Andersen161220c2000-03-16 08:12:48 +00001039 while (*src && !done) {
1040 if (quote == *src) {
1041 quote = '\0';
1042 } else if (quote) {
1043 if (*src == '\\') {
1044 src++;
1045 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001046 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001047 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001048 return 1;
1049 }
1050
1051 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +00001052 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +00001053 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +00001054 *buf++ = '\\';
1055 }
Erik Andersen161220c2000-03-16 08:12:48 +00001056 } else if (*src == '*' || *src == '?' || *src == '[' ||
1057 *src == ']') *buf++ = '\\';
1058 *buf++ = *src;
1059 } else if (isspace(*src)) {
Matt Kraaibe66ad32001-04-12 15:42:17 +00001060 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001061 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001062 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +00001063 if ((argc_l + 1) == argv_alloced) {
1064 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +00001065 prog->argv = xrealloc(prog->argv,
1066 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +00001067 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001068 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001069 prog->argv[argc_l] = buf;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001070 saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001071 }
1072 } else
1073 switch (*src) {
1074 case '"':
1075 case '\'':
1076 quote = *src;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001077 saw_quote = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001078 break;
1079
1080 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001081 if (*(src-1)== '$')
1082 *buf++ = *src;
1083 else
1084 done = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001085 break;
1086
Eric Andersenff9ad472004-02-10 01:07:45 +00001087#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +00001088 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +00001089 case '<':
Eric Andersen86349772000-12-18 20:25:50 +00001090 i = prog->num_redirects++;
1091 prog->redirects = xrealloc(prog->redirects,
1092 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +00001093 (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +00001094
Eric Andersen86349772000-12-18 20:25:50 +00001095 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001096 if (buf != prog->argv[argc_l]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001097 /* the stuff before this character may be the file number
Erik Andersen161220c2000-03-16 08:12:48 +00001098 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +00001099 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001100 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +00001101
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001102 if (*chptr && *prog->argv[argc_l]) {
1103 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001104 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001105 }
1106 }
1107
Eric Andersen86349772000-12-18 20:25:50 +00001108 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001109 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001110 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001111 else
Eric Andersen86349772000-12-18 20:25:50 +00001112 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001113 }
1114
1115 if (*src++ == '>') {
1116 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001117 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001118 REDIRECT_APPEND, src++;
1119 else
Eric Andersen86349772000-12-18 20:25:50 +00001120 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001121 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001122 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001123 }
1124
1125 /* This isn't POSIX sh compliant. Oh well. */
1126 chptr = src;
1127 while (isspace(*chptr))
1128 chptr++;
1129
1130 if (!*chptr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001131 bb_error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001132 free_job(job);
1133 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001134 return 1;
1135 }
1136
Eric Andersen86349772000-12-18 20:25:50 +00001137 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001138 while (*chptr && !isspace(*chptr))
1139 *buf++ = *chptr++;
1140
1141 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001142 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001143 break;
1144
1145 case '|': /* pipe */
1146 /* finish this command */
Matt Kraaibe66ad32001-04-12 15:42:17 +00001147 if (*prog->argv[argc_l] || saw_quote)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001148 argc_l++;
1149 if (!argc_l) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001150 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001151 free_job(job);
1152 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001153 return 1;
1154 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001155 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001156
1157 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001158 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001159 job->progs = xrealloc(job->progs,
Eric Andersen86349772000-12-18 20:25:50 +00001160 sizeof(*job->progs) * job->num_progs);
1161 prog = job->progs + (job->num_progs - 1);
1162 prog->num_redirects = 0;
1163 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001164 prog->is_stopped = 0;
1165 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001166 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001167
Eric Andersen86349772000-12-18 20:25:50 +00001168 argv_alloced = 5;
1169 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001170 prog->argv[0] = ++buf;
1171
1172 src++;
1173 while (*src && isspace(*src))
1174 src++;
1175
1176 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001177 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001178 free_job(job);
1179 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001180 return 1;
1181 }
1182 src--; /* we'll ++ it at the end of the loop */
1183
1184 break;
Eric Andersenff9ad472004-02-10 01:07:45 +00001185#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001186
Eric Andersen37032b42004-02-10 01:28:36 +00001187#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen161220c2000-03-16 08:12:48 +00001188 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001189 *inbg = 1;
Eric Andersen37032b42004-02-10 01:28:36 +00001190#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001191 case ';': /* multiple commands */
1192 done = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001193 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001194 break;
1195
Matt Kraai131241f2000-09-14 00:43:20 +00001196 case '\\':
1197 src++;
1198 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001199 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001200 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001201 return 1;
1202 }
1203 if (*src == '*' || *src == '[' || *src == ']'
1204 || *src == '?') *buf++ = '\\';
1205 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001206 default:
1207 *buf++ = *src;
1208 }
1209
Erik Andersend75af992000-03-16 08:09:09 +00001210 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001211 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001212
Matt Kraaibe66ad32001-04-12 15:42:17 +00001213 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001214 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001215 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001216 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001217 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001218 return 0;
1219 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001220 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001221
Eric Andersen86349772000-12-18 20:25:50 +00001222 if (!return_command) {
1223 job->text = xmalloc(strlen(*command_ptr) + 1);
1224 strcpy(job->text, *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001225 } else {
1226 /* This leaves any trailing spaces, which is a bit sloppy */
Eric Andersen86349772000-12-18 20:25:50 +00001227 count = return_command - *command_ptr;
Eric Andersenec10b9d2000-07-14 01:13:11 +00001228 job->text = xmalloc(count + 1);
Eric Andersen86349772000-12-18 20:25:50 +00001229 strncpy(job->text, *command_ptr, count);
Erik Andersen161220c2000-03-16 08:12:48 +00001230 job->text[count] = '\0';
1231 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001232
Eric Andersen86349772000-12-18 20:25:50 +00001233 *command_ptr = return_command;
Eric Andersenff9ad472004-02-10 01:07:45 +00001234
Erik Andersend75af992000-03-16 08:09:09 +00001235 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001236}
1237
Eric Andersen86349772000-12-18 20:25:50 +00001238/* Run the child_prog, no matter what kind of command it uses.
1239 */
1240static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001241{
Eric Andersen86349772000-12-18 20:25:50 +00001242 struct built_in_command *x;
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001243#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
Eric Andersenaf4ac772001-02-01 22:43:49 +00001244 char *name;
Erik Andersenbcd61772000-05-13 06:33:19 +00001245#endif
Erik Andersen3522eb12000-03-12 23:49:18 +00001246
Eric Andersene9f07fb2000-12-21 18:31:36 +00001247 /* Check if the command matches any of the non-forking builtins.
1248 * Depending on context, this might be redundant. But it's
1249 * easier to waste a few CPU cycles than it is to figure out
1250 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001251 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001252 for (x = bltins; x->cmd; x++) {
1253 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001254 _exit(x->function(child));
Eric Andersene9f07fb2000-12-21 18:31:36 +00001255 }
1256 }
1257
1258 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001259 for (x = bltins_forking; x->cmd; x++) {
1260 if (strcmp(child->argv[0], x->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001261 bb_applet_name=x->cmd;
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001262 _exit (x->function(child));
Eric Andersen86349772000-12-18 20:25:50 +00001263 }
1264 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001265#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
Eric Andersen86349772000-12-18 20:25:50 +00001266 /* Check if the command matches any busybox internal
1267 * commands ("applets") here. Following discussions from
Eric Andersen2423b122001-12-08 01:56:15 +00001268 * November 2000 on busybox@busybox.net, don't use
Manuel Novoa III cad53642003-03-19 09:13:01 +00001269 * bb_get_last_path_component(). This way explicit (with
Eric Andersen86349772000-12-18 20:25:50 +00001270 * slashes) filenames will never be interpreted as an
1271 * applet, just like with builtins. This way the user can
1272 * override an applet with an explicit filename reference.
1273 * The only downside to this change is that an explicit
1274 * /bin/foo invocation will fork and exec /bin/foo, even if
1275 * /bin/foo is a symlink to busybox.
1276 */
Matt Kraaif2cc2762001-02-01 19:21:20 +00001277 name = child->argv[0];
Eric Andersen86349772000-12-18 20:25:50 +00001278
Eric Andersen67991cf2001-02-14 21:23:06 +00001279 {
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001280 char** argv_l=child->argv;
1281 int argc_l;
1282#ifdef _NEWLIB_VERSION
1283 /* newlib uses __getopt_initialized for getopt() in
1284 * addition to optind, see newlib/libc/sys/linux/getopt.c
1285 */
1286 extern int __getopt_initialized = 0;
1287#endif
1288 for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
1289 optind = 1;
1290 run_applet_by_name(name, argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001291 }
1292#endif
1293
1294 execvp(child->argv[0], child->argv);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001295
Eric Andersenff9ad472004-02-10 01:07:45 +00001296 /* Do not use bb_perror_msg_and_die() here, since we must not
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001297 * call exit() but should call _exit() instead */
Eric Andersenad082982002-09-30 20:20:20 +00001298 fprintf(stderr, "%s: %m\n", child->argv[0]);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001299 _exit(EXIT_FAILURE);
Eric Andersen86349772000-12-18 20:25:50 +00001300}
1301
1302static void insert_job(struct job *newjob, int inbg)
1303{
1304 struct job *thejob;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001305 struct jobset *j_list=newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001306
1307 /* find the ID for thejob to use */
1308 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001309 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001310 if (thejob->jobid >= newjob->jobid)
1311 newjob->jobid = thejob->jobid + 1;
1312
1313 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001314 if (!j_list->head) {
1315 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001316 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001317 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001318 thejob->next = xmalloc(sizeof(*thejob));
1319 thejob = thejob->next;
1320 }
1321
1322 *thejob = *newjob; /* physically copy the struct job */
1323 thejob->next = NULL;
1324 thejob->running_progs = thejob->num_progs;
1325 thejob->stopped_progs = 0;
1326
Eric Andersen37032b42004-02-10 01:28:36 +00001327#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen86349772000-12-18 20:25:50 +00001328 if (inbg) {
Eric Andersenff9ad472004-02-10 01:07:45 +00001329 /* we don't wait for background thejobs to return -- append it
Eric Andersen86349772000-12-18 20:25:50 +00001330 to the list of backgrounded thejobs and leave it alone */
1331 printf("[%d] %d\n", thejob->jobid,
1332 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001333 last_jobid = newjob->jobid;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001334 last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001335 } else {
1336 newjob->job_list->fg = thejob;
1337
1338 /* move the new process group into the foreground */
1339 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001340 if (tcsetpgrp(shell_terminal, newjob->pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001341 bb_perror_msg("tcsetpgrp");
Eric Andersen86349772000-12-18 20:25:50 +00001342 }
Eric Andersen37032b42004-02-10 01:28:36 +00001343#endif
Eric Andersen86349772000-12-18 20:25:50 +00001344}
1345
1346static int run_command(struct job *newjob, int inbg, int outpipe[2])
1347{
1348 /* struct job *thejob; */
1349 int i;
1350 int nextin, nextout;
1351 int pipefds[2]; /* pipefd[0] is for reading */
1352 struct built_in_command *x;
1353 struct child_prog *child;
1354
Eric Andersen6efc48c2000-07-18 08:16:39 +00001355 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001356 for (i = 0; i < newjob->num_progs; i++) {
1357 child = & (newjob->progs[i]);
1358
1359 if ((i + 1) < newjob->num_progs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001360 if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001361 nextout = pipefds[1];
1362 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001363 if (outpipe[1]!=-1) {
1364 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001365 } else {
1366 nextout = 1;
1367 }
Erik Andersen161220c2000-03-16 08:12:48 +00001368 }
1369
Eric Andersen501c88b2000-07-28 15:14:45 +00001370
Eric Andersene9f07fb2000-12-21 18:31:36 +00001371 /* Check if the command matches any non-forking builtins,
1372 * but only if this is a simple command.
1373 * Non-forking builtins within pipes have to fork anyway,
1374 * and are handled in pseudo_exec. "echo foo | read bar"
1375 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001376 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001377 if (newjob->num_progs == 1) {
1378 for (x = bltins; x->cmd; x++) {
1379 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersene9f07fb2000-12-21 18:31:36 +00001380 int rcode;
Eric Andersenff9ad472004-02-10 01:07:45 +00001381 int squirrel[] = {-1, -1, -1};
Eric Andersene9f07fb2000-12-21 18:31:36 +00001382 setup_redirects(child, squirrel);
1383 rcode = x->function(child);
1384 restore_redirects(squirrel);
1385 return rcode;
1386 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001387 }
1388 }
1389
Eric Andersene3efc922004-04-12 17:59:24 +00001390#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersenff9ad472004-02-10 01:07:45 +00001391 if (!(child->pid = fork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001392#else
Eric Andersenff9ad472004-02-10 01:07:45 +00001393 if (!(child->pid = vfork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001394#endif
1395 {
Eric Andersen2d848a42001-06-25 17:11:54 +00001396 /* Set the handling for job control signals back to the default. */
1397 signal(SIGINT, SIG_DFL);
1398 signal(SIGQUIT, SIG_DFL);
1399 signal(SIGTSTP, SIG_DFL);
1400 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001401 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001402 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001403
Eric Andersen8ea28be2001-01-05 20:58:22 +00001404 close_all();
1405
Eric Andersen86349772000-12-18 20:25:50 +00001406 if (outpipe[1]!=-1) {
1407 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001408 }
1409 if (nextin != 0) {
1410 dup2(nextin, 0);
1411 close(nextin);
1412 }
1413
1414 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001415 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001416 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001417 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001418 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001419 }
1420
Eric Andersen86349772000-12-18 20:25:50 +00001421 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001422 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001423
Eric Andersen86349772000-12-18 20:25:50 +00001424 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001425 }
Eric Andersen86349772000-12-18 20:25:50 +00001426 if (outpipe[1]!=-1) {
1427 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001428 }
Erik Andersen161220c2000-03-16 08:12:48 +00001429
1430 /* put our child in the process group whose leader is the
1431 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001432 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001433 if (nextin != 0)
1434 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001435 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001436 close(nextout);
1437
Eric Andersenff9ad472004-02-10 01:07:45 +00001438 /* If there isn't another process, nextin is garbage
Erik Andersen161220c2000-03-16 08:12:48 +00001439 but it doesn't matter */
1440 nextin = pipefds[0];
1441 }
1442
Eric Andersen86349772000-12-18 20:25:50 +00001443 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001444
Eric Andersen86349772000-12-18 20:25:50 +00001445 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001446
Erik Andersen161220c2000-03-16 08:12:48 +00001447 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001448}
1449
Erik Andersend75af992000-03-16 08:09:09 +00001450static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001451{
Erik Andersen161220c2000-03-16 08:12:48 +00001452 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001453 char *next_command = NULL;
1454 struct job newjob;
Erik Andersen161220c2000-03-16 08:12:48 +00001455 int i;
Eric Andersen86349772000-12-18 20:25:50 +00001456 int inbg;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001457 int status;
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001458#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersend20d3752004-03-12 22:08:42 +00001459 pid_t parent_pgrp;
1460 /* save current owner of TTY so we can restore it on exit */
1461 parent_pgrp = tcgetpgrp(shell_terminal);
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001462#endif
Eric Andersen86349772000-12-18 20:25:50 +00001463 newjob.job_list = &job_list;
1464 newjob.job_context = DEFAULT_CONTEXT;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001465
Matt Kraaib8907522000-09-13 02:08:21 +00001466 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Erik Andersend75af992000-03-16 08:09:09 +00001467
Erik Andersen161220c2000-03-16 08:12:48 +00001468 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001469 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001470 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001471
Erik Andersend75af992000-03-16 08:09:09 +00001472 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001473 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001474
Eric Andersen86349772000-12-18 20:25:50 +00001475 if (!next_command) {
1476 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001477 break;
Eric Andersen86349772000-12-18 20:25:50 +00001478 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001479 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001480
Matt Kraai1f0c4362001-12-20 23:13:26 +00001481 if (! expand_arguments(next_command)) {
Eric Andersenca604592001-03-08 17:17:13 +00001482 free(command);
1483 command = (char *) xcalloc(BUFSIZ, sizeof(char));
1484 next_command = NULL;
1485 continue;
1486 }
1487
Eric Andersen86349772000-12-18 20:25:50 +00001488 if (!parse_command(&next_command, &newjob, &inbg) &&
1489 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001490 int pipefds[2] = {-1,-1};
Eric Andersenff9ad472004-02-10 01:07:45 +00001491 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001492 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001493 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001494 }
1495 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001496 free(command);
Matt Kraaib8907522000-09-13 02:08:21 +00001497 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Eric Andersen86349772000-12-18 20:25:50 +00001498 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001499 }
1500 } else {
1501 /* a job is running in the foreground; wait for it */
1502 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001503 while (!job_list.fg->progs[i].pid ||
1504 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001505
Eric Andersen7373e482002-07-31 04:04:47 +00001506 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) {
1507 if (errno != ECHILD) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001508 bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
Eric Andersen7373e482002-07-31 04:04:47 +00001509 }
1510 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001511
Erik Andersend75af992000-03-16 08:09:09 +00001512 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001513 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001514 job_list.fg->running_progs--;
1515 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001516
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001517 last_return_code=WEXITSTATUS(status);
1518
Eric Andersen86349772000-12-18 20:25:50 +00001519 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001520 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001521 remove_job(&job_list, job_list.fg);
1522 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001523 }
Eric Andersen37032b42004-02-10 01:28:36 +00001524 }
1525#ifdef CONFIG_LASH_JOB_CONTROL
1526 else {
Erik Andersen161220c2000-03-16 08:12:48 +00001527 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001528 job_list.fg->stopped_progs++;
1529 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001530
Eric Andersen86349772000-12-18 20:25:50 +00001531 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1532 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1533 "Stopped", job_list.fg->text);
1534 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001535 }
Erik Andersend75af992000-03-16 08:09:09 +00001536 }
1537
Eric Andersen86349772000-12-18 20:25:50 +00001538 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001539 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001540 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001541 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Eric Andersenff9ad472004-02-10 01:07:45 +00001542 bb_perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001543 }
Eric Andersen37032b42004-02-10 01:28:36 +00001544#endif
Erik Andersend75af992000-03-16 08:09:09 +00001545 }
1546 }
Erik Andersen161220c2000-03-16 08:12:48 +00001547 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001548
Eric Andersen37032b42004-02-10 01:28:36 +00001549#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen1c314ad2000-06-28 16:56:25 +00001550 /* return controlling TTY back to parent process group before exiting */
Eric Andersen7373e482002-07-31 04:04:47 +00001551 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001552 bb_perror_msg("tcsetpgrp");
Eric Andersen37032b42004-02-10 01:28:36 +00001553#endif
Eric Andersenb54833c2000-07-03 23:56:26 +00001554
1555 /* return exit status if called with "-c" */
1556 if (input == NULL && WIFEXITED(status))
1557 return WEXITSTATUS(status);
Eric Andersenff9ad472004-02-10 01:07:45 +00001558
Erik Andersen161220c2000-03-16 08:12:48 +00001559 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001560}
1561
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001562#ifdef CONFIG_FEATURE_CLEAN_UP
Eric Andersenfad9c112000-07-25 18:06:52 +00001563void free_memory(void)
1564{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001565 if (cwd && cwd!=bb_msg_unknown) {
Eric Andersen5d60a462001-08-22 05:32:24 +00001566 free((char*)cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001567 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001568 if (local_pending_command)
1569 free(local_pending_command);
1570
Eric Andersen86349772000-12-18 20:25:50 +00001571 if (job_list.fg && !job_list.fg->running_progs) {
1572 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001573 }
1574}
1575#endif
1576
Eric Andersen37032b42004-02-10 01:28:36 +00001577#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen2d848a42001-06-25 17:11:54 +00001578/* Make sure we have a controlling tty. If we get started under a job
1579 * aware app (like bash for example), make sure we are now in charge so
1580 * we don't fight over who gets the foreground */
Eric Andersenece8fc22002-12-11 04:26:28 +00001581static void setup_job_control(void)
Eric Andersen2d848a42001-06-25 17:11:54 +00001582{
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001583 int status;
Eric Andersen37032b42004-02-10 01:28:36 +00001584 pid_t shell_pgrp;
Eric Andersenff9ad472004-02-10 01:07:45 +00001585
Eric Andersen2d848a42001-06-25 17:11:54 +00001586 /* Loop until we are in the foreground. */
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001587 while ((status = tcgetpgrp (shell_terminal)) >= 0) {
1588 if (status == (shell_pgrp = getpgrp ())) {
1589 break;
1590 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001591 kill (- shell_pgrp, SIGTTIN);
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001592 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001593
1594 /* Ignore interactive and job-control signals. */
1595 signal(SIGINT, SIG_IGN);
1596 signal(SIGQUIT, SIG_IGN);
1597 signal(SIGTSTP, SIG_IGN);
1598 signal(SIGTTIN, SIG_IGN);
1599 signal(SIGTTOU, SIG_IGN);
1600 signal(SIGCHLD, SIG_IGN);
1601
1602 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001603 setsid();
Eric Andersen2d848a42001-06-25 17:11:54 +00001604 shell_pgrp = getpid ();
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001605 setpgid (shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001606
1607 /* Grab control of the terminal. */
1608 tcsetpgrp(shell_terminal, shell_pgrp);
1609}
Eric Andersen37032b42004-02-10 01:28:36 +00001610#else
1611static inline void setup_job_control(void)
1612{
1613}
1614#endif
Eric Andersen6efc48c2000-07-18 08:16:39 +00001615
Matt Kraai2d91deb2001-08-01 17:21:35 +00001616int lash_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001617{
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001618 int opt, interactive=FALSE;
Erik Andersen161220c2000-03-16 08:12:48 +00001619 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001620 argc = argc_l;
1621 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001622
Eric Andersen702ec592001-03-06 22:17:29 +00001623 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001624 last_jobid = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001625 local_pending_command = NULL;
Eric Andersen702ec592001-03-06 22:17:29 +00001626 close_me_head = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001627 job_list.head = NULL;
1628 job_list.fg = NULL;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001629 last_return_code=1;
Eric Andersen501c88b2000-07-28 15:14:45 +00001630
Eric Andersenb558e762000-11-30 22:43:16 +00001631 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001632 FILE *prof_input;
1633 prof_input = fopen("/etc/profile", "r");
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001634 if (prof_input) {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001635 int tmp_fd = fileno(prof_input);
Eric Andersenff9ad472004-02-10 01:07:45 +00001636 mark_open(tmp_fd);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001637 /* Now run the file */
1638 busy_loop(prof_input);
1639 fclose(prof_input);
1640 mark_closed(tmp_fd);
1641 }
Eric Andersenb558e762000-11-30 22:43:16 +00001642 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001643
Eric Andersenf21aa842000-12-08 20:50:30 +00001644 while ((opt = getopt(argc_l, argv_l, "cxi")) > 0) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001645 switch (opt) {
1646 case 'c':
1647 input = NULL;
Matt Kraai6085c722000-09-06 01:46:18 +00001648 if (local_pending_command != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001649 bb_error_msg_and_die("multiple -c arguments");
1650 local_pending_command = bb_xstrdup(argv[optind]);
Matt Kraai6085c722000-09-06 01:46:18 +00001651 optind++;
1652 argv = argv+optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001653 break;
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001654 case 'i':
1655 interactive = TRUE;
1656 break;
Eric Andersen501c88b2000-07-28 15:14:45 +00001657 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001658 bb_show_usage();
Eric Andersen501c88b2000-07-28 15:14:45 +00001659 }
1660 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001661 /* A shell is interactive if the `-i' flag was given, or if all of
1662 * the following conditions are met:
1663 * no -c command
1664 * no arguments remaining or the -s flag given
1665 * standard input is a terminal
1666 * standard output is a terminal
1667 * Refer to Posix.2, the description of the `sh' utility. */
Eric Andersen86349772000-12-18 20:25:50 +00001668 if (argv[optind]==NULL && input==stdin &&
Eric Andersen70060d22004-03-27 10:02:48 +00001669 isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
Eric Andersen86349772000-12-18 20:25:50 +00001670 interactive=TRUE;
1671 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001672 setup_job_control();
Eric Andersen86349772000-12-18 20:25:50 +00001673 if (interactive==TRUE) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001674 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Eric Andersen501c88b2000-07-28 15:14:45 +00001675 /* Looks like they want an interactive shell */
Eric Andersen37032b42004-02-10 01:28:36 +00001676#ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
Matt Kraai4ef40c02001-04-12 20:44:21 +00001677 printf( "\n\n" BB_BANNER " Built-in shell (lash)\n");
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001678 printf( "Enter 'help' for a list of built-in commands.\n\n");
Eric Andersend63dee42001-10-19 00:22:23 +00001679#endif
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001680 } else if (local_pending_command==NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001681 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001682 input = bb_xfopen(argv[optind], "r");
Eric Andersen8ea28be2001-01-05 20:58:22 +00001683 mark_open(fileno(input)); /* be lazy, never mark this closed */
Eric Andersen501c88b2000-07-28 15:14:45 +00001684 }
1685
Eric Andersen6efc48c2000-07-18 08:16:39 +00001686 /* initialize the cwd -- this is never freed...*/
Eric Andersene5dfced2001-04-09 22:48:12 +00001687 cwd = xgetcwd(0);
Eric Andersen5f265b72001-05-11 16:58:46 +00001688 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001689 cwd = bb_msg_unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001690
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001691#ifdef CONFIG_FEATURE_CLEAN_UP
Eric Andersenfad9c112000-07-25 18:06:52 +00001692 atexit(free_memory);
1693#endif
1694
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001695#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001696 cmdedit_set_initial_prompt();
1697#else
1698 PS1 = NULL;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001699#endif
Eric Andersenff9ad472004-02-10 01:07:45 +00001700
Erik Andersen161220c2000-03-16 08:12:48 +00001701 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001702}