blob: a209348347d4a5e120c48d0c7d00fad1051fb442 [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 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000011 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Erik Andersen3522eb12000-03-12 23:49:18 +000012 */
13
Eric Andersenbdfd0d72001-10-24 05:00:29 +000014/* This shell's parsing engine is officially at a dead-end. Future
15 * work shell work should be done using hush, msh, or ash. This is
16 * still a very useful, small shell -- it just don't need any more
17 * features beyond what it already has...
Eric Andersen8ea28be2001-01-05 20:58:22 +000018 */
Eric Andersen8a646dd2001-06-21 16:38:11 +000019
Eric Andersen1e7cea92000-12-06 23:47:38 +000020//For debugging/development on the shell only...
Eric Andersen501c88b2000-07-28 15:14:45 +000021//#define DEBUG_SHELL
Eric Andersena1d187a2000-07-17 19:14:41 +000022
23
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000024#include "busybox.h"
Erik Andersen3522eb12000-03-12 23:49:18 +000025#include <stdio.h>
26#include <stdlib.h>
27#include <ctype.h>
28#include <errno.h>
29#include <fcntl.h>
Erik Andersen3522eb12000-03-12 23:49:18 +000030#include <signal.h>
31#include <string.h>
32#include <sys/ioctl.h>
33#include <sys/wait.h>
34#include <unistd.h>
Eric Andersen501c88b2000-07-28 15:14:45 +000035#include <getopt.h>
Eric Andersen2d848a42001-06-25 17:11:54 +000036#include <termios.h>
Mark Whitley4b541a82001-04-25 17:10:30 +000037#include "cmdedit.h"
Eric Andersene5dfced2001-04-09 22:48:12 +000038
Eric Andersenbdfd0d72001-10-24 05:00:29 +000039#ifdef CONFIG_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +000040#include <locale.h>
Eric Andersene5dfced2001-04-09 22:48:12 +000041#endif
Eric Andersen13d1fa12001-03-08 23:59:45 +000042
Eric Andersen13d1fa12001-03-08 23:59:45 +000043#include <glob.h>
44#define expand_t glob_t
Erik Andersen3522eb12000-03-12 23:49:18 +000045
Eric Andersenff9ad472004-02-10 01:07:45 +000046/* Always enable for the moment... */
Eric Andersen95b26252004-02-10 01:30:21 +000047#define CONFIG_LASH_PIPE_N_REDIRECTS
48#define CONFIG_LASH_JOB_CONTROL
Eric Andersen13d1fa12001-03-08 23:59:45 +000049
Mark Whitley59ab0252001-01-23 22:30:04 +000050static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000051#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
52
Erik Andersend75af992000-03-16 08:09:09 +000053
Eric Andersenff9ad472004-02-10 01:07:45 +000054#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000055enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000056 REDIRECT_APPEND
57};
Eric Andersenff9ad472004-02-10 01:07:45 +000058#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000059
Rob Landleybc68cd12006-03-10 19:22:06 +000060enum {
61 DEFAULT_CONTEXT = 0x1,
62 IF_TRUE_CONTEXT = 0x2,
63 IF_FALSE_CONTEXT = 0x4,
64 THEN_EXP_CONTEXT = 0x8,
65 ELSE_EXP_CONTEXT = 0x10
66};
Eric Andersenfad9c112000-07-25 18:06:52 +000067
Eric Andersenff9ad472004-02-10 01:07:45 +000068#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +000069struct redir_struct {
70 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000071 int fd; /* file descriptor being redirected */
72 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000073};
Eric Andersenff9ad472004-02-10 01:07:45 +000074#endif
Erik Andersen3522eb12000-03-12 23:49:18 +000075
Eric Andersen86349772000-12-18 20:25:50 +000076struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000077 pid_t pid; /* 0 if exited */
78 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000079 int num_redirects; /* elements in redirection array */
Eric Andersen86349772000-12-18 20:25:50 +000080 int is_stopped; /* is the program currently running? */
81 struct job *family; /* pointer back to the child's parent job */
Eric Andersenff9ad472004-02-10 01:07:45 +000082#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
83 struct redir_struct *redirects; /* I/O redirects */
84#endif
85};
86
87struct jobset {
88 struct job *head; /* head of list of running jobs */
89 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000090};
91
92struct job {
Eric Andersen86349772000-12-18 20:25:50 +000093 int jobid; /* job number */
94 int num_progs; /* total number of programs in job */
95 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000096 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +000097 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +000098 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +000099 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +0000100 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +0000101 int stopped_progs; /* number of programs alive, but stopped */
102 unsigned int job_context; /* bitmask defining current context */
103 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +0000104};
105
Eric Andersen86349772000-12-18 20:25:50 +0000106struct built_in_command {
Erik Andersen161220c2000-03-16 08:12:48 +0000107 char *cmd; /* name */
108 char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +0000109 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +0000110};
111
Eric Andersen34e19412000-07-10 18:47:24 +0000112/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000113static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000114static int builtin_exec(struct child_prog *cmd);
115static int builtin_exit(struct child_prog *cmd);
116static int builtin_fg_bg(struct child_prog *cmd);
117static int builtin_help(struct child_prog *cmd);
118static int builtin_jobs(struct child_prog *dummy);
119static int builtin_pwd(struct child_prog *dummy);
120static int builtin_export(struct child_prog *cmd);
121static int builtin_source(struct child_prog *cmd);
122static int builtin_unset(struct child_prog *cmd);
123static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000124
Eric Andersen34e19412000-07-10 18:47:24 +0000125
126/* function prototypes for shell stuff */
Eric Andersen86349772000-12-18 20:25:50 +0000127static void checkjobs(struct jobset *job_list);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000128static void remove_job(struct jobset *j_list, struct job *job);
Eric Andersen86349772000-12-18 20:25:50 +0000129static int get_command(FILE * source, char *command);
130static int parse_command(char **command_ptr, struct job *job, int *inbg);
131static int run_command(struct job *newjob, int inbg, int outpipe[2]);
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000132static int pseudo_exec(struct child_prog *cmd) ATTRIBUTE_NORETURN;
Erik Andersen3522eb12000-03-12 23:49:18 +0000133static int busy_loop(FILE * input);
134
Erik Andersend75af992000-03-16 08:09:09 +0000135
Mark Whitley37653aa2000-07-12 23:36:17 +0000136/* Table of built-in functions (these are non-forking builtins, meaning they
137 * can change global variables in the parent shell process but they will not
138 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Eric Andersen86349772000-12-18 20:25:50 +0000139static struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000140 {"bg", "Resume a job in the background", builtin_fg_bg},
141 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000142 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000143 {"exit", "Exit from shell()", builtin_exit},
144 {"fg", "Bring job into the foreground", builtin_fg_bg},
145 {"jobs", "Lists the active jobs", builtin_jobs},
146 {"export", "Set environment variable", builtin_export},
147 {"unset", "Unset environment variable", builtin_unset},
148 {"read", "Input environment variable", builtin_read},
Matt Kraaidd450a02000-09-13 03:43:36 +0000149 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000150 /* to do: add ulimit */
Eric Andersenfad9c112000-07-25 18:06:52 +0000151 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000152};
153
Mark Whitley37653aa2000-07-12 23:36:17 +0000154/* Table of forking built-in functions (things that fork cannot change global
155 * variables in the parent process, such as the current working directory) */
Eric Andersen86349772000-12-18 20:25:50 +0000156static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000157 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000158 {"help", "List shell built-in commands", builtin_help},
159 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000160};
161
Eric Andersen0bcc8132001-01-05 19:37:32 +0000162
Eric Andersen7467c8d2001-07-12 20:26:32 +0000163static int shell_context; /* Type prompt trigger (PS1 or PS2) */
Eric Andersen0bcc8132001-01-05 19:37:32 +0000164
165
166/* Globals that are static to this file */
Eric Andersencfa88ec2001-05-11 18:08:16 +0000167static const char *cwd;
Eric Andersen744b0642001-01-05 21:23:44 +0000168static char *local_pending_command = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000169static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000170static int argc;
171static char **argv;
Rob Landleyf9463792006-06-13 18:50:20 +0000172static llist_t *close_me_list;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000173static int last_return_code;
174static int last_bg_pid;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000175static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000176static int shell_terminal;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000177static char *PS1;
Eric Andersene5dfced2001-04-09 22:48:12 +0000178static char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000179
180
Eric Andersenb558e762000-11-30 22:43:16 +0000181#ifdef DEBUG_SHELL
182static inline void debug_printf(const char *format, ...)
183{
184 va_list args;
185 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000186 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000187 va_end(args);
188}
189#else
190static inline void debug_printf(const char *format, ...) { }
191#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000192
Eric Andersen86349772000-12-18 20:25:50 +0000193/*
194 Most builtins need access to the struct child_prog that has
195 their arguments, previously coded as cmd->progs[0]. That coding
196 can exhibit a bug, if the builtin is not the first command in
197 a pipeline: "echo foo | exec sort" will attempt to exec foo.
198
199builtin previous use notes
200------ ----------------- ---------
201cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000202exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
203exit cmd->progs[0]
204fg_bg cmd->progs[0], job_list->head, job_list->fg
205help 0
206jobs job_list->head
207pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000208export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000209source cmd->progs[0]
210unset cmd->progs[0]
211read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000212
213I added "struct job *family;" to struct child_prog,
214and switched API to builtin_foo(struct child_prog *child);
215So cmd->text becomes child->family->text
216 cmd->job_context becomes child->family->job_context
217 cmd->progs[0] becomes *child
218 job_list becomes child->family->job_list
219 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000220
Erik Andersend75af992000-03-16 08:09:09 +0000221/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000222static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000223{
Erik Andersen161220c2000-03-16 08:12:48 +0000224 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000225
Eric Andersen86349772000-12-18 20:25:50 +0000226 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000227 newdir = getenv("HOME");
228 else
Eric Andersen86349772000-12-18 20:25:50 +0000229 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000230 if (chdir(newdir)) {
Rob Landley5483de12006-06-20 21:35:26 +0000231 bb_perror_msg("cd: %s", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000232 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000233 }
Eric Andersencfa88ec2001-05-11 18:08:16 +0000234 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000235 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000236 cwd = bb_msg_unknown;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000237 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000238}
239
Eric Andersend2f56772000-09-21 02:48:07 +0000240/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000241static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000242{
Eric Andersen86349772000-12-18 20:25:50 +0000243 if (child->argv[1] == NULL)
244 return EXIT_SUCCESS; /* Really? */
245 child->argv++;
Rob Landley7a260f02006-06-19 03:20:03 +0000246 while(close_me_list) close((long)llist_pop(&close_me_list));
Eric Andersen86349772000-12-18 20:25:50 +0000247 pseudo_exec(child);
248 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000249}
250
Erik Andersen3522eb12000-03-12 23:49:18 +0000251/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000252static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000253{
Eric Andersen86349772000-12-18 20:25:50 +0000254 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000255 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000256
Eric Andersen86349772000-12-18 20:25:50 +0000257 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000258}
259
260/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000261static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000262{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000263 int i, jobnum;
Erik Andersen6273f652000-03-17 01:12:41 +0000264 struct job *job=NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000265
Eric Andersen8a646dd2001-06-21 16:38:11 +0000266 /* If they gave us no args, assume they want the last backgrounded task */
267 if (!child->argv[1]) {
268 for (job = child->family->job_list->head; job; job = job->next) {
269 if (job->jobid == last_jobid) {
270 break;
271 }
Erik Andersen161220c2000-03-16 08:12:48 +0000272 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000273 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000274 bb_error_msg("%s: no current job", child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000275 return EXIT_FAILURE;
276 }
277 } else {
278 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000279 bb_error_msg(bb_msg_invalid_arg, child->argv[1], child->argv[0]);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000280 return EXIT_FAILURE;
281 }
282 for (job = child->family->job_list->head; job; job = job->next) {
283 if (job->jobid == jobnum) {
284 break;
285 }
286 }
287 if (!job) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000288 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000289 return EXIT_FAILURE;
290 }
Erik Andersend75af992000-03-16 08:09:09 +0000291 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000292
Eric Andersen86349772000-12-18 20:25:50 +0000293 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000294 /* Put the job into the foreground. */
295 tcsetpgrp(shell_terminal, job->pgrp);
296
Eric Andersen86349772000-12-18 20:25:50 +0000297 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000298 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000299
Erik Andersen161220c2000-03-16 08:12:48 +0000300 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000301 for (i = 0; i < job->num_progs; i++)
302 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000303
Eric Andersen86349772000-12-18 20:25:50 +0000304 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000305
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000306 if ( (i=kill(- job->pgrp, SIGCONT)) < 0) {
307 if (i == ESRCH) {
308 remove_job(&job_list, job);
Eric Andersen07abfe22001-06-27 17:29:11 +0000309 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000310 bb_perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000311 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000312 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000313
Matt Kraai3e856ce2000-12-01 02:55:13 +0000314 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000315}
316
317/* built-in 'help' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000318static int builtin_help(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000319{
Eric Andersen86349772000-12-18 20:25:50 +0000320 struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000321
Eric Andersen86349772000-12-18 20:25:50 +0000322 printf("\nBuilt-in commands:\n");
323 printf("-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000324 for (x = bltins; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000325 if (x->descr==NULL)
326 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000327 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000328 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000329 for (x = bltins_forking; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000330 if (x->descr==NULL)
331 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000332 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000333 }
Eric Andersen86349772000-12-18 20:25:50 +0000334 printf("\n\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000335 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000336}
337
338/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000339static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000340{
Erik Andersen161220c2000-03-16 08:12:48 +0000341 struct job *job;
Eric Andersen86349772000-12-18 20:25:50 +0000342 char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000343
Eric Andersen86349772000-12-18 20:25:50 +0000344 for (job = child->family->job_list->head; job; job = job->next) {
345 if (job->running_progs == job->stopped_progs)
346 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000347 else
Eric Andersen86349772000-12-18 20:25:50 +0000348 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000349
Eric Andersen86349772000-12-18 20:25:50 +0000350 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000351 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000352 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000353}
354
355
356/* built-in 'pwd' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000357static int builtin_pwd(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000358{
Eric Andersencfa88ec2001-05-11 18:08:16 +0000359 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000360 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000361 cwd = bb_msg_unknown;
Matt Kraai59df6f72001-05-16 14:21:09 +0000362 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000363 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000364}
365
Erik Andersen6273f652000-03-17 01:12:41 +0000366/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000367static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000368{
Erik Andersen161220c2000-03-16 08:12:48 +0000369 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000370 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000371
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000372 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000373 char **e;
374 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000375 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000376 }
Matt Kraai2129f972001-04-04 17:50:04 +0000377 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000378 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000379 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000380 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000381 bb_perror_msg("export");
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000382#ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000383 if (strncmp(v, "PS1=", 4)==0)
384 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000385#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000386
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000387#ifdef CONFIG_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +0000388 if(strncmp(v, "LC_ALL=", 7)==0)
389 setlocale(LC_ALL, getenv("LC_ALL"));
Mark Whitleya82a0032001-03-27 17:07:15 +0000390 if(strncmp(v, "LC_CTYPE=", 9)==0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000391 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000392#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000393
Erik Andersen161220c2000-03-16 08:12:48 +0000394 return (res);
Erik Andersen3522eb12000-03-12 23:49:18 +0000395}
396
Eric Andersenb54833c2000-07-03 23:56:26 +0000397/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000398static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000399{
400 int res = 0, len, newlen;
401 char *s;
402 char string[MAX_READ];
403
Eric Andersen86349772000-12-18 20:25:50 +0000404 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000405 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen0d2d1eb2002-06-06 13:33:01 +0000406 safe_strncpy(string, child->argv[1], MAX_READ-1);
Eric Andersenb54833c2000-07-03 23:56:26 +0000407 len = strlen(string);
408 string[len++] = '=';
409 string[len] = '\0';
410 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
411 newlen = strlen(string);
412 if(newlen > len)
413 string[--newlen] = '\0'; /* chomp trailing newline */
414 /*
415 ** string should now contain "VAR=<value>"
416 ** copy it (putenv() won't do that, so we must make sure
417 ** the string resides in a static buffer!)
418 */
419 res = -1;
420 if((s = strdup(string)))
421 res = putenv(s);
422 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000423 bb_perror_msg("read");
Eric Andersenb54833c2000-07-03 23:56:26 +0000424 }
425 else
426 fgets(string, sizeof(string), stdin);
427
428 return (res);
429}
430
Erik Andersen3522eb12000-03-12 23:49:18 +0000431/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000432static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000433{
Erik Andersen161220c2000-03-16 08:12:48 +0000434 FILE *input;
435 int status;
Erik Andersen3522eb12000-03-12 23:49:18 +0000436
Eric Andersen86349772000-12-18 20:25:50 +0000437 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000438 return EXIT_FAILURE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000439
Eric Andersen86349772000-12-18 20:25:50 +0000440 input = fopen(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000441 if (!input) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000442 printf( "Couldn't open file '%s'\n", child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000443 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000444 }
Erik Andersend75af992000-03-16 08:09:09 +0000445
Rob Landley7a260f02006-06-19 03:20:03 +0000446 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Erik Andersen161220c2000-03-16 08:12:48 +0000447 /* Now run the file */
448 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000449 fclose(input);
Rob Landleyf9463792006-06-13 18:50:20 +0000450 llist_pop(&close_me_list);
Erik Andersen161220c2000-03-16 08:12:48 +0000451 return (status);
Erik Andersen3522eb12000-03-12 23:49:18 +0000452}
453
454/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000455static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000456{
Eric Andersen86349772000-12-18 20:25:50 +0000457 if (child->argv[1] == NULL) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000458 printf(bb_msg_requires_arg, "unset");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000459 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000460 }
Eric Andersen86349772000-12-18 20:25:50 +0000461 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000462 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000463}
464
Eric Andersen37032b42004-02-10 01:28:36 +0000465#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen3522eb12000-03-12 23:49:18 +0000466/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000467static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000468{
Erik Andersen161220c2000-03-16 08:12:48 +0000469 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000470 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000471
Eric Andersen86349772000-12-18 20:25:50 +0000472 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000473 free(cmd->progs[i].argv);
Eric Andersenff9ad472004-02-10 01:07:45 +0000474#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +0000475 if (cmd->progs[i].redirects)
476 free(cmd->progs[i].redirects);
Eric Andersenff9ad472004-02-10 01:07:45 +0000477#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000478 }
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000479 free(cmd->progs);
480 free(cmd->text);
481 free(cmd->cmdbuf);
Mark Whitley44a99142001-03-14 17:26:37 +0000482 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000483 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000484 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000485}
486
Eric Andersen1ca20a72001-03-21 07:34:27 +0000487/* remove a job from a jobset */
488static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000489{
Eric Andersen86349772000-12-18 20:25:50 +0000490 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000491
Eric Andersen86349772000-12-18 20:25:50 +0000492 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000493 if (job == j_list->head) {
494 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000495 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000496 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000497 while (prevjob->next != job)
498 prevjob = prevjob->next;
499 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000500 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000501
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000502 if (j_list->head)
503 last_jobid = j_list->head->jobid;
504 else
505 last_jobid = 0;
506
Erik Andersen161220c2000-03-16 08:12:48 +0000507 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000508}
509
Eric Andersenff9ad472004-02-10 01:07:45 +0000510/* Checks to see if any background processes have exited -- if they
Erik Andersen3522eb12000-03-12 23:49:18 +0000511 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000512static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000513{
Erik Andersen161220c2000-03-16 08:12:48 +0000514 struct job *job;
515 pid_t childpid;
516 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000517 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000518
Erik Andersen161220c2000-03-16 08:12:48 +0000519 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000520 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000521 prognum = 0;
522 while (prognum < job->num_progs &&
523 job->progs[prognum].pid != childpid) prognum++;
524 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000525 break;
526 }
527
Eric Andersena1d187a2000-07-17 19:14:41 +0000528 /* This happens on backticked commands */
529 if(job==NULL)
530 return;
531
Erik Andersen161220c2000-03-16 08:12:48 +0000532 if (WIFEXITED(status) || WIFSIGNALED(status)) {
533 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000534 job->running_progs--;
535 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000536
Eric Andersen86349772000-12-18 20:25:50 +0000537 if (!job->running_progs) {
538 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000539 last_jobid=0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000540 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000541 }
542 } else {
543 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000544 job->stopped_progs++;
545 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000546
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000547#if 0
548 /* Printing this stuff is a pain, since it tends to
549 * overwrite the prompt an inconveinient moments. So
550 * don't do that. */
Eric Andersen86349772000-12-18 20:25:50 +0000551 if (job->stopped_progs == job->num_progs) {
552 printf(JOB_STATUS_FORMAT, job->jobid, "Stopped",
Erik Andersen161220c2000-03-16 08:12:48 +0000553 job->text);
554 }
Eric Andersenff9ad472004-02-10 01:07:45 +0000555#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000556 }
Erik Andersend75af992000-03-16 08:09:09 +0000557 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000558
Erik Andersen161220c2000-03-16 08:12:48 +0000559 if (childpid == -1 && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000560 bb_perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000561}
Eric Andersen37032b42004-02-10 01:28:36 +0000562#else
563static void checkjobs(struct jobset *j_list)
564{
565}
566static void free_job(struct job *cmd)
567{
568}
569static void remove_job(struct jobset *j_list, struct job *job)
570{
571}
572#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000573
Eric Andersenff9ad472004-02-10 01:07:45 +0000574#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersene9f07fb2000-12-21 18:31:36 +0000575/* squirrel != NULL means we squirrel away copies of stdin, stdout,
576 * and stderr if they are redirected. */
577static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000578{
579 int i;
580 int openfd;
581 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000582 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000583
Eric Andersen86349772000-12-18 20:25:50 +0000584 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000585 switch (redir->type) {
586 case REDIRECT_INPUT:
587 mode = O_RDONLY;
588 break;
589 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000590 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000591 break;
592 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000593 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000594 break;
595 }
596
597 openfd = open(redir->filename, mode, 0666);
598 if (openfd < 0) {
599 /* this could get lost if stderr has been redirected, but
600 bash and ash both lose it as well (though zsh doesn't!) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000601 bb_perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000602 return 1;
603 }
604
605 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000606 if (squirrel && redir->fd < 3) {
607 squirrel[redir->fd] = dup(redir->fd);
Eric Andersenacd647c2004-08-16 08:38:34 +0000608 fcntl (squirrel[redir->fd], F_SETFD, FD_CLOEXEC);
Eric Andersene9f07fb2000-12-21 18:31:36 +0000609 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000610 dup2(openfd, redir->fd);
611 close(openfd);
612 }
613 }
614
615 return 0;
616}
617
Eric Andersene9f07fb2000-12-21 18:31:36 +0000618static void restore_redirects(int squirrel[])
619{
620 int i, fd;
621 for (i=0; i<3; i++) {
622 fd = squirrel[i];
623 if (fd != -1) {
624 /* No error checking. I sure wouldn't know what
625 * to do with an error if I found one! */
626 dup2(fd, i);
627 close(fd);
628 }
629 }
630}
Eric Andersenff9ad472004-02-10 01:07:45 +0000631#else
632static inline int setup_redirects(struct child_prog *prog, int squirrel[])
633{
634 return 0;
635}
636static inline void restore_redirects(int squirrel[])
637{
638}
639#endif
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000640
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000641static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000642{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000643#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000644 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000645#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000646 PS1 = getenv("PS1");
Eric Andersene5dfced2001-04-09 22:48:12 +0000647 if(PS1==0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000648 PS1 = "\\w \\$ ";
Eric Andersenff9ad472004-02-10 01:07:45 +0000649#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000650}
651
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000652static inline void setup_prompt_string(char **prompt_str)
653{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000654#ifndef CONFIG_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000655 /* Set up the prompt */
656 if (shell_context == 0) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000657 free(PS1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000658 PS1=xmalloc(strlen(cwd)+4);
659 sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
660 *prompt_str = PS1;
661 } else {
662 *prompt_str = PS2;
663 }
664#else
665 *prompt_str = (shell_context==0)? PS1 : PS2;
Eric Andersenff9ad472004-02-10 01:07:45 +0000666#endif
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000667}
Eric Andersen22332fd2001-01-30 23:40:39 +0000668
Eric Andersen09acc062001-01-04 11:10:38 +0000669static int get_command(FILE * source, char *command)
670{
671 char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000672
Eric Andersen1c314ad2000-06-28 16:56:25 +0000673 if (source == NULL) {
674 if (local_pending_command) {
675 /* a command specified (-c option): return it & mark it done */
676 strcpy(command, local_pending_command);
677 free(local_pending_command);
678 local_pending_command = NULL;
679 return 0;
680 }
681 return 1;
682 }
683
Erik Andersen161220c2000-03-16 08:12:48 +0000684 if (source == stdin) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000685 setup_prompt_string(&prompt_str);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000686
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000687#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000688 /*
689 ** enable command line editing only while a command line
690 ** is actually being read; otherwise, we'll end up bequeathing
691 ** atexit() handlers and other unwanted stuff to our
692 ** child processes (rob@sysgo.de)
693 */
Eric Andersen86349772000-12-18 20:25:50 +0000694 cmdedit_read_input(prompt_str, command);
Erik Andersen161220c2000-03-16 08:12:48 +0000695 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000696#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000697 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000698#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000699 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000700
Erik Andersen161220c2000-03-16 08:12:48 +0000701 if (!fgets(command, BUFSIZ - 2, source)) {
702 if (source == stdin)
703 printf("\n");
704 return 1;
705 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000706
Erik Andersen161220c2000-03-16 08:12:48 +0000707 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000708}
709
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000710static char* itoa(register int i)
711{
712 static char a[7]; /* Max 7 ints */
713 register char *b = a + sizeof(a) - 1;
714 int sign = (i < 0);
715
716 if (sign)
717 i = -i;
718 *b = 0;
719 do
720 {
721 *--b = '0' + (i % 10);
722 i /= 10;
723 }
724 while (i);
725 if (sign)
726 *--b = '-';
727 return b;
728}
729
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +0000730static char * strsep_space( char *string, int * ix)
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000731{
Rob Landleyf9463792006-06-13 18:50:20 +0000732 char *token;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000733
734 /* Short circuit the trivial case */
735 if ( !string || ! string[*ix])
736 return NULL;
737
738 /* Find the end of the token. */
Rob Landleyf9463792006-06-13 18:50:20 +0000739 while( string[*ix] && !isspace(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000740 (*ix)++;
741 }
742
Eric Andersenff9ad472004-02-10 01:07:45 +0000743 /* Find the end of any whitespace trailing behind
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000744 * the token and let that be part of the token */
Rob Landleyf9463792006-06-13 18:50:20 +0000745 while( string[*ix] && isspace(string[*ix]) ) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000746 (*ix)++;
747 }
748
Rob Landleyf9463792006-06-13 18:50:20 +0000749 if (!*ix) {
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000750 /* Nothing useful was found */
751 return NULL;
752 }
753
Rob Landleyf9463792006-06-13 18:50:20 +0000754 token = bb_xstrndup(string, *ix);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000755
756 return token;
757}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000758
Eric Andersenca604592001-03-08 17:17:13 +0000759static int expand_arguments(char *command)
760{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000761 int total_length=0, length, i, retval, ix = 0;
762 expand_t expand_result;
763 char *tmpcmd, *cmd, *cmd_copy;
764 char *src, *dst, *var;
765 const char *out_of_space = "out of space during expansion";
766 int flags = GLOB_NOCHECK
767#ifdef GLOB_BRACE
768 | GLOB_BRACE
Eric Andersenff9ad472004-02-10 01:07:45 +0000769#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000770#ifdef GLOB_TILDE
771 | GLOB_TILDE
Eric Andersenff9ad472004-02-10 01:07:45 +0000772#endif
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000773 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000774
Eric Andersenca604592001-03-08 17:17:13 +0000775 /* get rid of the terminating \n */
776 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000777
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000778 /* Fix up escape sequences to be the Real Thing(tm) */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000779 while( command && command[ix]) {
780 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000781 const char *tmp = command+ix+1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000782 command[ix] = bb_process_escape_sequence( &tmp );
Eric Andersen1ca20a72001-03-21 07:34:27 +0000783 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000784 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000785 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000786 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000787 /* Use glob and then fixup environment variables and such */
788
789 /* It turns out that glob is very stupid. We have to feed it one word at a
790 * time since it can't cope with a full string. Here we convert command
791 * (char*) into cmd (char**, one word per string) */
792
793 /* We need a clean copy, so strsep can mess up the copy while
794 * we write stuff into the original (in a minute) */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000795 cmd = cmd_copy = bb_xstrdup(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000796 *command = '\0';
Eric Andersenff9ad472004-02-10 01:07:45 +0000797 for (ix = 0, tmpcmd = cmd;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000798 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
799 if (*tmpcmd == '\0')
800 break;
801 /* we need to trim() the result for glob! */
802 trim(tmpcmd);
803 retval = glob(tmpcmd, flags, NULL, &expand_result);
804 free(tmpcmd); /* Free mem allocated by strsep_space */
805 if (retval == GLOB_NOSPACE) {
806 /* Mem may have been allocated... */
807 globfree (&expand_result);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000808 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000809 return FALSE;
810 } else if (retval != 0) {
811 /* Some other error. GLOB_NOMATCH shouldn't
Eric Andersenff9ad472004-02-10 01:07:45 +0000812 * happen because of the GLOB_NOCHECK flag in
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000813 * the glob call. */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000814 bb_error_msg("syntax error");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000815 return FALSE;
816 } else {
817 /* Convert from char** (one word per string) to a simple char*,
818 * but don't overflow command which is BUFSIZ in length */
819 for (i=0; i < expand_result.gl_pathc; i++) {
820 length=strlen(expand_result.gl_pathv[i]);
821 if (total_length+length+1 >= BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000822 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000823 return FALSE;
824 }
825 strcat(command+total_length, " ");
826 total_length+=1;
827 strcat(command+total_length, expand_result.gl_pathv[i]);
828 total_length+=length;
829 }
830 globfree (&expand_result);
831 }
832 }
833 free(cmd_copy);
834 trim(command);
835
Eric Andersenff9ad472004-02-10 01:07:45 +0000836 /* Now do the shell variable substitutions which
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000837 * wordexp can't do for us, namely $? and $! */
838 src = command;
839 while((dst = strchr(src,'$')) != NULL){
840 var = NULL;
841 switch(*(dst+1)) {
842 case '?':
843 var = itoa(last_return_code);
844 break;
845 case '!':
846 if (last_bg_pid==-1)
847 *(var)='\0';
848 else
849 var = itoa(last_bg_pid);
850 break;
851 /* Everything else like $$, $#, $[0-9], etc. should all be
852 * expanded by wordexp(), so we can in theory skip that stuff
853 * here, but just to be on the safe side (i.e., since uClibc
854 * wordexp doesn't do this stuff yet), lets leave it in for
855 * now. */
856 case '$':
857 var = itoa(getpid());
858 break;
859 case '#':
860 var = itoa(argc-1);
861 break;
862 case '0':case '1':case '2':case '3':case '4':
863 case '5':case '6':case '7':case '8':case '9':
864 {
Eric Andersen7ab93d92002-11-08 09:40:02 +0000865 int ixx=*(dst+1)-48+1;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000866 if (ixx >= argc) {
867 var='\0';
868 } else {
869 var = argv[ixx];
870 }
871 }
872 break;
873
874 }
875 if (var) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000876 /* a single character construction was found, and
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000877 * already handled in the case statement */
878 src=dst+2;
879 } else {
880 /* Looks like an environment variable */
881 char delim_hold;
882 int num_skip_chars=0;
883 int dstlen = strlen(dst);
884 /* Is this a ${foo} type variable? */
885 if (dstlen >=2 && *(dst+1) == '{') {
886 src=strchr(dst+1, '}');
887 num_skip_chars=1;
888 } else {
889 src=dst+1;
890 while(isalnum(*src) || *src=='_') src++;
891 }
892 if (src == NULL) {
893 src = dst+dstlen;
894 }
895 delim_hold=*src;
896 *src='\0'; /* temporary */
897 var = getenv(dst + 1 + num_skip_chars);
898 *src=delim_hold;
899 src += num_skip_chars;
900 }
901 if (var == NULL) {
902 /* Seems we got an un-expandable variable. So delete it. */
903 var = "";
904 }
905 {
906 int subst_len = strlen(var);
907 int trail_len = strlen(src);
908 if (dst+subst_len+trail_len >= command+BUFSIZ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000909 bb_error_msg(out_of_space);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000910 return FALSE;
911 }
912 /* Move stuff to the end of the string to accommodate
913 * filling the created gap with the new stuff */
914 memmove(dst+subst_len, src, trail_len+1);
915 /* Now copy in the new stuff */
916 memcpy(dst, var, subst_len);
917 src = dst+subst_len;
918 }
919 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000920
Eric Andersenca604592001-03-08 17:17:13 +0000921 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000922}
923
Eric Andersen86349772000-12-18 20:25:50 +0000924/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
925 line). If a valid command is found, command_ptr is set to point to
Eric Andersenff9ad472004-02-10 01:07:45 +0000926 the beginning of the next command (if the original command had more
927 then one job associated with it) or NULL if no more commands are
Erik Andersen3522eb12000-03-12 23:49:18 +0000928 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000929static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000930{
Erik Andersen161220c2000-03-16 08:12:48 +0000931 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000932 char *return_command = NULL;
Eric Andersenff9ad472004-02-10 01:07:45 +0000933 char *src, *buf;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000934 int argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000935 int done = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000936 int argv_alloced;
Eric Andersenff9ad472004-02-10 01:07:45 +0000937 int saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000938 char quote = '\0';
Eric Andersen86349772000-12-18 20:25:50 +0000939 struct child_prog *prog;
Eric Andersenff9ad472004-02-10 01:07:45 +0000940#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
941 int i;
942 char *chptr;
943#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000944
Erik Andersen161220c2000-03-16 08:12:48 +0000945 /* skip leading white space */
Eric Andersen86349772000-12-18 20:25:50 +0000946 while (**command_ptr && isspace(**command_ptr))
947 (*command_ptr)++;
Erik Andersen3522eb12000-03-12 23:49:18 +0000948
Erik Andersen161220c2000-03-16 08:12:48 +0000949 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000950 if (!**command_ptr || (**command_ptr == '#')) {
951 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000952 return 0;
953 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000954
Eric Andersen86349772000-12-18 20:25:50 +0000955 *inbg = 0;
956 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000957 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000958
Eric Andersenff9ad472004-02-10 01:07:45 +0000959 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000960 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000961 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000962
Eric Andersenff9ad472004-02-10 01:07:45 +0000963 Getting clean memory relieves us of the task of NULL
964 terminating things and makes the rest of this look a bit
Erik Andersen161220c2000-03-16 08:12:48 +0000965 cleaner (though it is, admittedly, a tad less efficient) */
Rob Landleyf9463792006-06-13 18:50:20 +0000966 job->cmdbuf = command = xzalloc(2*strlen(*command_ptr) + 1);
Erik Andersen161220c2000-03-16 08:12:48 +0000967 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000968
Erik Andersen161220c2000-03-16 08:12:48 +0000969 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000970 prog->num_redirects = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000971 prog->is_stopped = 0;
972 prog->family = job;
Eric Andersenff9ad472004-02-10 01:07:45 +0000973#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
974 prog->redirects = NULL;
975#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000976
Eric Andersen86349772000-12-18 20:25:50 +0000977 argv_alloced = 5;
978 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
979 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +0000980
Erik Andersen161220c2000-03-16 08:12:48 +0000981 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +0000982 src = *command_ptr;
Erik Andersen161220c2000-03-16 08:12:48 +0000983 while (*src && !done) {
984 if (quote == *src) {
985 quote = '\0';
986 } else if (quote) {
987 if (*src == '\\') {
988 src++;
989 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000990 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +0000991 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +0000992 return 1;
993 }
994
995 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +0000996 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +0000997 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +0000998 *buf++ = '\\';
999 }
Erik Andersen161220c2000-03-16 08:12:48 +00001000 } else if (*src == '*' || *src == '?' || *src == '[' ||
1001 *src == ']') *buf++ = '\\';
1002 *buf++ = *src;
1003 } else if (isspace(*src)) {
Matt Kraaibe66ad32001-04-12 15:42:17 +00001004 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001005 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001006 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +00001007 if ((argc_l + 1) == argv_alloced) {
1008 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +00001009 prog->argv = xrealloc(prog->argv,
1010 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +00001011 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001012 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001013 prog->argv[argc_l] = buf;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001014 saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001015 }
1016 } else
1017 switch (*src) {
1018 case '"':
1019 case '\'':
1020 quote = *src;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001021 saw_quote = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001022 break;
1023
1024 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001025 if (*(src-1)== '$')
1026 *buf++ = *src;
1027 else
1028 done = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001029 break;
1030
Eric Andersenff9ad472004-02-10 01:07:45 +00001031#ifdef CONFIG_LASH_PIPE_N_REDIRECTS
Eric Andersen86349772000-12-18 20:25:50 +00001032 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +00001033 case '<':
Eric Andersen86349772000-12-18 20:25:50 +00001034 i = prog->num_redirects++;
1035 prog->redirects = xrealloc(prog->redirects,
1036 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +00001037 (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +00001038
Eric Andersen86349772000-12-18 20:25:50 +00001039 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001040 if (buf != prog->argv[argc_l]) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001041 /* the stuff before this character may be the file number
Erik Andersen161220c2000-03-16 08:12:48 +00001042 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +00001043 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001044 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +00001045
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001046 if (*chptr && *prog->argv[argc_l]) {
1047 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001048 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001049 }
1050 }
1051
Eric Andersen86349772000-12-18 20:25:50 +00001052 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001053 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001054 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001055 else
Eric Andersen86349772000-12-18 20:25:50 +00001056 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001057 }
1058
1059 if (*src++ == '>') {
1060 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001061 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001062 REDIRECT_APPEND, src++;
1063 else
Eric Andersen86349772000-12-18 20:25:50 +00001064 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001065 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001066 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001067 }
1068
1069 /* This isn't POSIX sh compliant. Oh well. */
1070 chptr = src;
1071 while (isspace(*chptr))
1072 chptr++;
1073
1074 if (!*chptr) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001075 bb_error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001076 free_job(job);
1077 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001078 return 1;
1079 }
1080
Eric Andersen86349772000-12-18 20:25:50 +00001081 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001082 while (*chptr && !isspace(*chptr))
1083 *buf++ = *chptr++;
1084
1085 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001086 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001087 break;
1088
1089 case '|': /* pipe */
1090 /* finish this command */
Matt Kraaibe66ad32001-04-12 15:42:17 +00001091 if (*prog->argv[argc_l] || saw_quote)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001092 argc_l++;
1093 if (!argc_l) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001094 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001095 free_job(job);
1096 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001097 return 1;
1098 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001099 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001100
1101 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001102 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001103 job->progs = xrealloc(job->progs,
Eric Andersen86349772000-12-18 20:25:50 +00001104 sizeof(*job->progs) * job->num_progs);
1105 prog = job->progs + (job->num_progs - 1);
1106 prog->num_redirects = 0;
1107 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001108 prog->is_stopped = 0;
1109 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001110 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001111
Eric Andersen86349772000-12-18 20:25:50 +00001112 argv_alloced = 5;
1113 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001114 prog->argv[0] = ++buf;
1115
1116 src++;
1117 while (*src && isspace(*src))
1118 src++;
1119
1120 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001121 bb_error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001122 free_job(job);
1123 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001124 return 1;
1125 }
1126 src--; /* we'll ++ it at the end of the loop */
1127
1128 break;
Eric Andersenff9ad472004-02-10 01:07:45 +00001129#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001130
Eric Andersen37032b42004-02-10 01:28:36 +00001131#ifdef CONFIG_LASH_JOB_CONTROL
Erik Andersen161220c2000-03-16 08:12:48 +00001132 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001133 *inbg = 1;
Eric Andersen37032b42004-02-10 01:28:36 +00001134#endif
Erik Andersen161220c2000-03-16 08:12:48 +00001135 case ';': /* multiple commands */
1136 done = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001137 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001138 break;
1139
Matt Kraai131241f2000-09-14 00:43:20 +00001140 case '\\':
1141 src++;
1142 if (!*src) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001143 bb_error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001144 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001145 return 1;
1146 }
1147 if (*src == '*' || *src == '[' || *src == ']'
1148 || *src == '?') *buf++ = '\\';
1149 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001150 default:
1151 *buf++ = *src;
1152 }
1153
Erik Andersend75af992000-03-16 08:09:09 +00001154 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001155 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001156
Matt Kraaibe66ad32001-04-12 15:42:17 +00001157 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001158 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001159 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001160 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001161 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001162 return 0;
1163 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001164 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001165
Eric Andersen86349772000-12-18 20:25:50 +00001166 if (!return_command) {
Rob Landleyf9463792006-06-13 18:50:20 +00001167 job->text = bb_xstrdup(*command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001168 } else {
1169 /* This leaves any trailing spaces, which is a bit sloppy */
Rob Landleyf9463792006-06-13 18:50:20 +00001170 job->text = bb_xstrndup(*command_ptr, return_command - *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001171 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001172
Eric Andersen86349772000-12-18 20:25:50 +00001173 *command_ptr = return_command;
Eric Andersenff9ad472004-02-10 01:07:45 +00001174
Erik Andersend75af992000-03-16 08:09:09 +00001175 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001176}
1177
Eric Andersen86349772000-12-18 20:25:50 +00001178/* Run the child_prog, no matter what kind of command it uses.
1179 */
1180static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001181{
Eric Andersen86349772000-12-18 20:25:50 +00001182 struct built_in_command *x;
Rob Landley68286772006-06-13 19:01:30 +00001183
1184 /* Check if the command sets an environment variable. */
1185 if( strchr(child->argv[0], '=') != NULL ) {
1186 child->argv[1] = child->argv[0];
1187 _exit(builtin_export(child));
1188 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001189
Eric Andersene9f07fb2000-12-21 18:31:36 +00001190 /* Check if the command matches any of the non-forking builtins.
1191 * Depending on context, this might be redundant. But it's
1192 * easier to waste a few CPU cycles than it is to figure out
1193 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001194 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001195 for (x = bltins; x->cmd; x++) {
1196 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001197 _exit(x->function(child));
Eric Andersene9f07fb2000-12-21 18:31:36 +00001198 }
1199 }
1200
1201 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001202 for (x = bltins_forking; x->cmd; x++) {
1203 if (strcmp(child->argv[0], x->cmd) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001204 bb_applet_name=x->cmd;
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001205 _exit (x->function(child));
Eric Andersen86349772000-12-18 20:25:50 +00001206 }
1207 }
Rob Landley68286772006-06-13 19:01:30 +00001208
Eric Andersen86349772000-12-18 20:25:50 +00001209 /* Check if the command matches any busybox internal
1210 * commands ("applets") here. Following discussions from
Eric Andersen2423b122001-12-08 01:56:15 +00001211 * November 2000 on busybox@busybox.net, don't use
Manuel Novoa III cad53642003-03-19 09:13:01 +00001212 * bb_get_last_path_component(). This way explicit (with
Eric Andersen86349772000-12-18 20:25:50 +00001213 * slashes) filenames will never be interpreted as an
1214 * applet, just like with builtins. This way the user can
1215 * override an applet with an explicit filename reference.
1216 * The only downside to this change is that an explicit
1217 * /bin/foo invocation will fork and exec /bin/foo, even if
1218 * /bin/foo is a symlink to busybox.
1219 */
Eric Andersen86349772000-12-18 20:25:50 +00001220
Rob Landley68286772006-06-13 19:01:30 +00001221 if (ENABLE_FEATURE_SH_STANDALONE_SHELL) {
1222 char **argv_l = child->argv;
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001223 int argc_l;
Bernhard Reutner-Fischer62558762006-06-03 10:28:25 +00001224
Rob Landley68286772006-06-13 19:01:30 +00001225 for(argc_l=0; *argv_l; argv_l++, argc_l++);
Mike Frysingerdcc40b72005-03-04 01:33:17 +00001226 optind = 1;
Rob Landley68286772006-06-13 19:01:30 +00001227 run_applet_by_name(child->argv[0], argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001228 }
Eric Andersen86349772000-12-18 20:25:50 +00001229
1230 execvp(child->argv[0], child->argv);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001231
Eric Andersenff9ad472004-02-10 01:07:45 +00001232 /* Do not use bb_perror_msg_and_die() here, since we must not
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001233 * call exit() but should call _exit() instead */
Rob Landley5483de12006-06-20 21:35:26 +00001234 bb_perror_msg("%s", child->argv[0]);
Eric Andersen6f2ebca2002-09-26 13:59:40 +00001235 _exit(EXIT_FAILURE);
Eric Andersen86349772000-12-18 20:25:50 +00001236}
1237
1238static void insert_job(struct job *newjob, int inbg)
1239{
1240 struct job *thejob;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001241 struct jobset *j_list=newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001242
1243 /* find the ID for thejob to use */
1244 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001245 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001246 if (thejob->jobid >= newjob->jobid)
1247 newjob->jobid = thejob->jobid + 1;
1248
1249 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001250 if (!j_list->head) {
1251 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001252 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001253 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001254 thejob->next = xmalloc(sizeof(*thejob));
1255 thejob = thejob->next;
1256 }
1257
1258 *thejob = *newjob; /* physically copy the struct job */
1259 thejob->next = NULL;
1260 thejob->running_progs = thejob->num_progs;
1261 thejob->stopped_progs = 0;
1262
Eric Andersen37032b42004-02-10 01:28:36 +00001263#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen86349772000-12-18 20:25:50 +00001264 if (inbg) {
Eric Andersenff9ad472004-02-10 01:07:45 +00001265 /* we don't wait for background thejobs to return -- append it
Eric Andersen86349772000-12-18 20:25:50 +00001266 to the list of backgrounded thejobs and leave it alone */
1267 printf("[%d] %d\n", thejob->jobid,
1268 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001269 last_jobid = newjob->jobid;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001270 last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001271 } else {
1272 newjob->job_list->fg = thejob;
1273
1274 /* move the new process group into the foreground */
Rob Landleyf9463792006-06-13 18:50:20 +00001275 /* Ignore errors since child could have already exited */
1276 tcsetpgrp(shell_terminal, newjob->pgrp);
Eric Andersen86349772000-12-18 20:25:50 +00001277 }
Eric Andersen37032b42004-02-10 01:28:36 +00001278#endif
Eric Andersen86349772000-12-18 20:25:50 +00001279}
1280
1281static int run_command(struct job *newjob, int inbg, int outpipe[2])
1282{
1283 /* struct job *thejob; */
1284 int i;
1285 int nextin, nextout;
1286 int pipefds[2]; /* pipefd[0] is for reading */
1287 struct built_in_command *x;
1288 struct child_prog *child;
1289
Eric Andersen6efc48c2000-07-18 08:16:39 +00001290 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001291 for (i = 0; i < newjob->num_progs; i++) {
1292 child = & (newjob->progs[i]);
1293
1294 if ((i + 1) < newjob->num_progs) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001295 if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001296 nextout = pipefds[1];
1297 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001298 if (outpipe[1]!=-1) {
1299 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001300 } else {
1301 nextout = 1;
1302 }
Erik Andersen161220c2000-03-16 08:12:48 +00001303 }
1304
Eric Andersen501c88b2000-07-28 15:14:45 +00001305
Eric Andersene9f07fb2000-12-21 18:31:36 +00001306 /* Check if the command matches any non-forking builtins,
1307 * but only if this is a simple command.
1308 * Non-forking builtins within pipes have to fork anyway,
1309 * and are handled in pseudo_exec. "echo foo | read bar"
1310 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001311 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001312 if (newjob->num_progs == 1) {
1313 for (x = bltins; x->cmd; x++) {
1314 if (strcmp(child->argv[0], x->cmd) == 0 ) {
Eric Andersene9f07fb2000-12-21 18:31:36 +00001315 int rcode;
Eric Andersenff9ad472004-02-10 01:07:45 +00001316 int squirrel[] = {-1, -1, -1};
Eric Andersene9f07fb2000-12-21 18:31:36 +00001317 setup_redirects(child, squirrel);
1318 rcode = x->function(child);
1319 restore_redirects(squirrel);
1320 return rcode;
1321 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001322 }
1323 }
1324
Eric Andersene3efc922004-04-12 17:59:24 +00001325#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersenff9ad472004-02-10 01:07:45 +00001326 if (!(child->pid = fork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001327#else
Eric Andersenff9ad472004-02-10 01:07:45 +00001328 if (!(child->pid = vfork()))
Eric Andersen72f9a422001-10-28 05:12:20 +00001329#endif
1330 {
Eric Andersen2d848a42001-06-25 17:11:54 +00001331 /* Set the handling for job control signals back to the default. */
1332 signal(SIGINT, SIG_DFL);
1333 signal(SIGQUIT, SIG_DFL);
1334 signal(SIGTSTP, SIG_DFL);
1335 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001336 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001337 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001338
Rob Landleyf9463792006-06-13 18:50:20 +00001339 // Close all open filehandles.
Rob Landley7a260f02006-06-19 03:20:03 +00001340 while(close_me_list) close((long)llist_pop(&close_me_list));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001341
Eric Andersen86349772000-12-18 20:25:50 +00001342 if (outpipe[1]!=-1) {
1343 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001344 }
1345 if (nextin != 0) {
1346 dup2(nextin, 0);
1347 close(nextin);
1348 }
1349
1350 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001351 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001352 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001353 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001354 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001355 }
1356
Eric Andersen86349772000-12-18 20:25:50 +00001357 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001358 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001359
Eric Andersen86349772000-12-18 20:25:50 +00001360 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001361 }
Eric Andersen86349772000-12-18 20:25:50 +00001362 if (outpipe[1]!=-1) {
1363 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001364 }
Erik Andersen161220c2000-03-16 08:12:48 +00001365
1366 /* put our child in the process group whose leader is the
1367 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001368 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001369 if (nextin != 0)
1370 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001371 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001372 close(nextout);
1373
Eric Andersenff9ad472004-02-10 01:07:45 +00001374 /* If there isn't another process, nextin is garbage
Erik Andersen161220c2000-03-16 08:12:48 +00001375 but it doesn't matter */
1376 nextin = pipefds[0];
1377 }
1378
Eric Andersen86349772000-12-18 20:25:50 +00001379 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001380
Eric Andersen86349772000-12-18 20:25:50 +00001381 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001382
Erik Andersen161220c2000-03-16 08:12:48 +00001383 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001384}
1385
Erik Andersend75af992000-03-16 08:09:09 +00001386static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001387{
Erik Andersen161220c2000-03-16 08:12:48 +00001388 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001389 char *next_command = NULL;
1390 struct job newjob;
Erik Andersen161220c2000-03-16 08:12:48 +00001391 int i;
Eric Andersena68ea1c2006-01-30 22:48:39 +00001392 int inbg = 0;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001393 int status;
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001394#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersend20d3752004-03-12 22:08:42 +00001395 pid_t parent_pgrp;
1396 /* save current owner of TTY so we can restore it on exit */
1397 parent_pgrp = tcgetpgrp(shell_terminal);
Glenn L McGrathc45146c2004-03-05 12:55:30 +00001398#endif
Eric Andersen86349772000-12-18 20:25:50 +00001399 newjob.job_list = &job_list;
1400 newjob.job_context = DEFAULT_CONTEXT;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001401
Rob Landleyf9463792006-06-13 18:50:20 +00001402 command = xzalloc(BUFSIZ);
Erik Andersend75af992000-03-16 08:09:09 +00001403
Erik Andersen161220c2000-03-16 08:12:48 +00001404 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001405 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001406 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001407
Erik Andersend75af992000-03-16 08:09:09 +00001408 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001409 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001410
Eric Andersen86349772000-12-18 20:25:50 +00001411 if (!next_command) {
1412 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001413 break;
Eric Andersen86349772000-12-18 20:25:50 +00001414 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001415 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001416
Matt Kraai1f0c4362001-12-20 23:13:26 +00001417 if (! expand_arguments(next_command)) {
Eric Andersenca604592001-03-08 17:17:13 +00001418 free(command);
Rob Landleyf9463792006-06-13 18:50:20 +00001419 command = xzalloc(BUFSIZ);
Eric Andersenca604592001-03-08 17:17:13 +00001420 next_command = NULL;
1421 continue;
1422 }
1423
Eric Andersen86349772000-12-18 20:25:50 +00001424 if (!parse_command(&next_command, &newjob, &inbg) &&
1425 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001426 int pipefds[2] = {-1,-1};
Eric Andersenff9ad472004-02-10 01:07:45 +00001427 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001428 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001429 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001430 }
1431 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001432 free(command);
Rob Landleyf9463792006-06-13 18:50:20 +00001433 command = (char *) xzalloc(BUFSIZ);
Eric Andersen86349772000-12-18 20:25:50 +00001434 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001435 }
1436 } else {
1437 /* a job is running in the foreground; wait for it */
1438 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001439 while (!job_list.fg->progs[i].pid ||
1440 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001441
Eric Andersen7373e482002-07-31 04:04:47 +00001442 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) {
1443 if (errno != ECHILD) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001444 bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
Eric Andersen7373e482002-07-31 04:04:47 +00001445 }
1446 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001447
Erik Andersend75af992000-03-16 08:09:09 +00001448 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001449 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001450 job_list.fg->running_progs--;
1451 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001452
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001453 last_return_code=WEXITSTATUS(status);
1454
Eric Andersen86349772000-12-18 20:25:50 +00001455 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001456 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001457 remove_job(&job_list, job_list.fg);
1458 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001459 }
Eric Andersen37032b42004-02-10 01:28:36 +00001460 }
1461#ifdef CONFIG_LASH_JOB_CONTROL
1462 else {
Erik Andersen161220c2000-03-16 08:12:48 +00001463 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001464 job_list.fg->stopped_progs++;
1465 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001466
Eric Andersen86349772000-12-18 20:25:50 +00001467 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1468 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1469 "Stopped", job_list.fg->text);
1470 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001471 }
Erik Andersend75af992000-03-16 08:09:09 +00001472 }
1473
Eric Andersen86349772000-12-18 20:25:50 +00001474 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001475 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001476 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001477 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Eric Andersenff9ad472004-02-10 01:07:45 +00001478 bb_perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001479 }
Eric Andersen37032b42004-02-10 01:28:36 +00001480#endif
Erik Andersend75af992000-03-16 08:09:09 +00001481 }
1482 }
Erik Andersen161220c2000-03-16 08:12:48 +00001483 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001484
Eric Andersen37032b42004-02-10 01:28:36 +00001485#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen1c314ad2000-06-28 16:56:25 +00001486 /* return controlling TTY back to parent process group before exiting */
Eric Andersen7373e482002-07-31 04:04:47 +00001487 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001488 bb_perror_msg("tcsetpgrp");
Eric Andersen37032b42004-02-10 01:28:36 +00001489#endif
Eric Andersenb54833c2000-07-03 23:56:26 +00001490
1491 /* return exit status if called with "-c" */
1492 if (input == NULL && WIFEXITED(status))
1493 return WEXITSTATUS(status);
Eric Andersenff9ad472004-02-10 01:07:45 +00001494
Erik Andersen161220c2000-03-16 08:12:48 +00001495 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001496}
1497
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001498#ifdef CONFIG_FEATURE_CLEAN_UP
"Vladimir N. Oleynik"264e7712005-09-22 14:35:29 +00001499static void free_memory(void)
Eric Andersenfad9c112000-07-25 18:06:52 +00001500{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001501 if (cwd && cwd!=bb_msg_unknown) {
Eric Andersen5d60a462001-08-22 05:32:24 +00001502 free((char*)cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001503 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001504 if (local_pending_command)
1505 free(local_pending_command);
1506
Eric Andersen86349772000-12-18 20:25:50 +00001507 if (job_list.fg && !job_list.fg->running_progs) {
1508 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001509 }
1510}
1511#endif
1512
Eric Andersen37032b42004-02-10 01:28:36 +00001513#ifdef CONFIG_LASH_JOB_CONTROL
Eric Andersen2d848a42001-06-25 17:11:54 +00001514/* Make sure we have a controlling tty. If we get started under a job
1515 * aware app (like bash for example), make sure we are now in charge so
1516 * we don't fight over who gets the foreground */
Eric Andersenece8fc22002-12-11 04:26:28 +00001517static void setup_job_control(void)
Eric Andersen2d848a42001-06-25 17:11:54 +00001518{
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001519 int status;
Eric Andersen37032b42004-02-10 01:28:36 +00001520 pid_t shell_pgrp;
Eric Andersenff9ad472004-02-10 01:07:45 +00001521
Eric Andersen2d848a42001-06-25 17:11:54 +00001522 /* Loop until we are in the foreground. */
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001523 while ((status = tcgetpgrp (shell_terminal)) >= 0) {
1524 if (status == (shell_pgrp = getpgrp ())) {
1525 break;
1526 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001527 kill (- shell_pgrp, SIGTTIN);
Eric Andersenf0a4ac82001-10-03 11:23:42 +00001528 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001529
1530 /* Ignore interactive and job-control signals. */
1531 signal(SIGINT, SIG_IGN);
1532 signal(SIGQUIT, SIG_IGN);
1533 signal(SIGTSTP, SIG_IGN);
1534 signal(SIGTTIN, SIG_IGN);
1535 signal(SIGTTOU, SIG_IGN);
1536 signal(SIGCHLD, SIG_IGN);
1537
1538 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001539 setsid();
Eric Andersen2d848a42001-06-25 17:11:54 +00001540 shell_pgrp = getpid ();
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001541 setpgid (shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001542
1543 /* Grab control of the terminal. */
1544 tcsetpgrp(shell_terminal, shell_pgrp);
1545}
Eric Andersen37032b42004-02-10 01:28:36 +00001546#else
1547static inline void setup_job_control(void)
1548{
1549}
1550#endif
Eric Andersen6efc48c2000-07-18 08:16:39 +00001551
Matt Kraai2d91deb2001-08-01 17:21:35 +00001552int lash_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001553{
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001554 int opt, interactive=FALSE;
Erik Andersen161220c2000-03-16 08:12:48 +00001555 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001556 argc = argc_l;
1557 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001558
Eric Andersen702ec592001-03-06 22:17:29 +00001559 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001560 last_jobid = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001561 local_pending_command = NULL;
Rob Landleyf9463792006-06-13 18:50:20 +00001562 close_me_list = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001563 job_list.head = NULL;
1564 job_list.fg = NULL;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001565 last_return_code=1;
Eric Andersen501c88b2000-07-28 15:14:45 +00001566
Eric Andersenb558e762000-11-30 22:43:16 +00001567 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001568 FILE *prof_input;
1569 prof_input = fopen("/etc/profile", "r");
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001570 if (prof_input) {
Rob Landley7a260f02006-06-19 03:20:03 +00001571 llist_add_to(&close_me_list, (void *)(long)fileno(prof_input));
Eric Andersen8ea28be2001-01-05 20:58:22 +00001572 /* Now run the file */
1573 busy_loop(prof_input);
1574 fclose(prof_input);
Rob Landleyf9463792006-06-13 18:50:20 +00001575 llist_pop(&close_me_list);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001576 }
Eric Andersenb558e762000-11-30 22:43:16 +00001577 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001578
Eric Andersenf21aa842000-12-08 20:50:30 +00001579 while ((opt = getopt(argc_l, argv_l, "cxi")) > 0) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001580 switch (opt) {
1581 case 'c':
1582 input = NULL;
Matt Kraai6085c722000-09-06 01:46:18 +00001583 if (local_pending_command != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001584 bb_error_msg_and_die("multiple -c arguments");
1585 local_pending_command = bb_xstrdup(argv[optind]);
Matt Kraai6085c722000-09-06 01:46:18 +00001586 optind++;
1587 argv = argv+optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001588 break;
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001589 case 'i':
1590 interactive = TRUE;
1591 break;
Eric Andersen501c88b2000-07-28 15:14:45 +00001592 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001593 bb_show_usage();
Eric Andersen501c88b2000-07-28 15:14:45 +00001594 }
1595 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001596 /* A shell is interactive if the `-i' flag was given, or if all of
1597 * the following conditions are met:
1598 * no -c command
1599 * no arguments remaining or the -s flag given
1600 * standard input is a terminal
1601 * standard output is a terminal
1602 * Refer to Posix.2, the description of the `sh' utility. */
Eric Andersen86349772000-12-18 20:25:50 +00001603 if (argv[optind]==NULL && input==stdin &&
Eric Andersen70060d22004-03-27 10:02:48 +00001604 isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
Eric Andersen86349772000-12-18 20:25:50 +00001605 interactive=TRUE;
1606 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001607 setup_job_control();
Eric Andersen86349772000-12-18 20:25:50 +00001608 if (interactive==TRUE) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001609 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Eric Andersen501c88b2000-07-28 15:14:45 +00001610 /* Looks like they want an interactive shell */
Eric Andersen37032b42004-02-10 01:28:36 +00001611#ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +00001612 printf( "\n\n%s Built-in shell (lash)\n", BB_BANNER);
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001613 printf( "Enter 'help' for a list of built-in commands.\n\n");
Eric Andersend63dee42001-10-19 00:22:23 +00001614#endif
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001615 } else if (local_pending_command==NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001616 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001617 input = bb_xfopen(argv[optind], "r");
Rob Landleyf9463792006-06-13 18:50:20 +00001618 /* be lazy, never mark this closed */
Rob Landley7a260f02006-06-19 03:20:03 +00001619 llist_add_to(&close_me_list, (void *)(long)fileno(input));
Eric Andersen501c88b2000-07-28 15:14:45 +00001620 }
1621
Eric Andersen6efc48c2000-07-18 08:16:39 +00001622 /* initialize the cwd -- this is never freed...*/
Eric Andersene5dfced2001-04-09 22:48:12 +00001623 cwd = xgetcwd(0);
Eric Andersen5f265b72001-05-11 16:58:46 +00001624 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001625 cwd = bb_msg_unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001626
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001627#ifdef CONFIG_FEATURE_CLEAN_UP
Eric Andersenfad9c112000-07-25 18:06:52 +00001628 atexit(free_memory);
1629#endif
1630
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001631#ifdef CONFIG_FEATURE_COMMAND_EDITING
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001632 cmdedit_set_initial_prompt();
1633#else
1634 PS1 = NULL;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001635#endif
Eric Andersenff9ad472004-02-10 01:07:45 +00001636
Erik Andersen161220c2000-03-16 08:12:48 +00001637 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001638}