blob: e4ca5f67b9b5b9a5079b06664934686c2de879d4 [file] [log] [blame]
Erik Andersen3522eb12000-03-12 23:49:18 +00001/* vi: set sw=4 ts=4: */
2/*
Erik Andersen6acaa402000-03-26 14:03:20 +00003 * lash -- the BusyBox Lame-Ass SHell
Erik Andersen3522eb12000-03-12 23:49:18 +00004 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Erik Andersen3522eb12000-03-12 23:49:18 +00006 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 *
8 * Based in part on ladsh.c by Michael K. Johnson and Erik W. Troan, which is
9 * under the following liberal license: "We have placed this source code in the
10 * public domain. Use it in any project, free or commercial."
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
Eric Andersen8a646dd2001-06-21 16:38:11 +000028/* This shell's parsing engine is officially at a dead-end.
29 * Future work shell work should be done using hush.c
Eric Andersen8ea28be2001-01-05 20:58:22 +000030 */
Eric Andersen8a646dd2001-06-21 16:38:11 +000031
Eric Andersen1e7cea92000-12-06 23:47:38 +000032//For debugging/development on the shell only...
Eric Andersen501c88b2000-07-28 15:14:45 +000033//#define DEBUG_SHELL
Eric Andersena1d187a2000-07-17 19:14:41 +000034
35
Erik Andersen3522eb12000-03-12 23:49:18 +000036#include <stdio.h>
37#include <stdlib.h>
38#include <ctype.h>
39#include <errno.h>
40#include <fcntl.h>
Erik Andersen3522eb12000-03-12 23:49:18 +000041#include <signal.h>
42#include <string.h>
43#include <sys/ioctl.h>
44#include <sys/wait.h>
45#include <unistd.h>
Eric Andersen501c88b2000-07-28 15:14:45 +000046#include <getopt.h>
Eric Andersen2d848a42001-06-25 17:11:54 +000047#include <termios.h>
Mark Whitley4b541a82001-04-25 17:10:30 +000048#include "busybox.h"
49#include "cmdedit.h"
Eric Andersene5dfced2001-04-09 22:48:12 +000050
51#ifdef BB_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +000052#include <locale.h>
Eric Andersene5dfced2001-04-09 22:48:12 +000053#endif
Eric Andersen13d1fa12001-03-08 23:59:45 +000054
Eric Andersen13d1fa12001-03-08 23:59:45 +000055#include <glob.h>
56#define expand_t glob_t
Erik Andersen3522eb12000-03-12 23:49:18 +000057
Eric Andersen13d1fa12001-03-08 23:59:45 +000058
Mark Whitley59ab0252001-01-23 22:30:04 +000059static const int MAX_READ = 128; /* size of input buffer for `read' builtin */
Erik Andersen3522eb12000-03-12 23:49:18 +000060#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
61
Erik Andersend75af992000-03-16 08:09:09 +000062
Eric Andersen86349772000-12-18 20:25:50 +000063enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE,
Erik Andersen161220c2000-03-16 08:12:48 +000064 REDIRECT_APPEND
65};
Erik Andersen3522eb12000-03-12 23:49:18 +000066
Eric Andersen86349772000-12-18 20:25:50 +000067static const unsigned int DEFAULT_CONTEXT=0x1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +000068static const unsigned int IF_TRUE_CONTEXT=0x2;
69static const unsigned int IF_FALSE_CONTEXT=0x4;
70static const unsigned int THEN_EXP_CONTEXT=0x8;
71static const unsigned int ELSE_EXP_CONTEXT=0x10;
Eric Andersenfad9c112000-07-25 18:06:52 +000072
Eric Andersen86349772000-12-18 20:25:50 +000073
74struct jobset {
Erik Andersen161220c2000-03-16 08:12:48 +000075 struct job *head; /* head of list of running jobs */
76 struct job *fg; /* current foreground job */
Erik Andersen3522eb12000-03-12 23:49:18 +000077};
78
Eric Andersen86349772000-12-18 20:25:50 +000079struct redir_struct {
80 enum redir_type type; /* type of redirection */
Erik Andersen161220c2000-03-16 08:12:48 +000081 int fd; /* file descriptor being redirected */
82 char *filename; /* file to redirect fd to */
Erik Andersen3522eb12000-03-12 23:49:18 +000083};
84
Eric Andersen86349772000-12-18 20:25:50 +000085struct child_prog {
Erik Andersen161220c2000-03-16 08:12:48 +000086 pid_t pid; /* 0 if exited */
87 char **argv; /* program name and arguments */
Eric Andersen86349772000-12-18 20:25:50 +000088 int num_redirects; /* elements in redirection array */
89 struct redir_struct *redirects; /* I/O redirects */
Eric Andersen86349772000-12-18 20:25:50 +000090 int is_stopped; /* is the program currently running? */
91 struct job *family; /* pointer back to the child's parent job */
Erik Andersen3522eb12000-03-12 23:49:18 +000092};
93
94struct job {
Eric Andersen86349772000-12-18 20:25:50 +000095 int jobid; /* job number */
96 int num_progs; /* total number of programs in job */
97 int running_progs; /* number of programs running */
Erik Andersen161220c2000-03-16 08:12:48 +000098 char *text; /* name of job */
Eric Andersen86349772000-12-18 20:25:50 +000099 char *cmdbuf; /* buffer various argv's point into */
Erik Andersen161220c2000-03-16 08:12:48 +0000100 pid_t pgrp; /* process group ID for the job */
Eric Andersen86349772000-12-18 20:25:50 +0000101 struct child_prog *progs; /* array of programs in job */
Erik Andersen161220c2000-03-16 08:12:48 +0000102 struct job *next; /* to track background commands */
Eric Andersen86349772000-12-18 20:25:50 +0000103 int stopped_progs; /* number of programs alive, but stopped */
104 unsigned int job_context; /* bitmask defining current context */
105 struct jobset *job_list;
Erik Andersen3522eb12000-03-12 23:49:18 +0000106};
107
Eric Andersen86349772000-12-18 20:25:50 +0000108struct built_in_command {
Erik Andersen161220c2000-03-16 08:12:48 +0000109 char *cmd; /* name */
110 char *descr; /* description */
Eric Andersen86349772000-12-18 20:25:50 +0000111 int (*function) (struct child_prog *); /* function ptr */
Erik Andersen3522eb12000-03-12 23:49:18 +0000112};
113
Eric Andersen8ea28be2001-01-05 20:58:22 +0000114struct close_me {
115 int fd;
116 struct close_me *next;
117};
118
Eric Andersen34e19412000-07-10 18:47:24 +0000119/* function prototypes for builtins */
Eric Andersen86349772000-12-18 20:25:50 +0000120static int builtin_cd(struct child_prog *cmd);
Eric Andersen86349772000-12-18 20:25:50 +0000121static int builtin_exec(struct child_prog *cmd);
122static int builtin_exit(struct child_prog *cmd);
123static int builtin_fg_bg(struct child_prog *cmd);
124static int builtin_help(struct child_prog *cmd);
125static int builtin_jobs(struct child_prog *dummy);
126static int builtin_pwd(struct child_prog *dummy);
127static int builtin_export(struct child_prog *cmd);
128static int builtin_source(struct child_prog *cmd);
129static int builtin_unset(struct child_prog *cmd);
130static int builtin_read(struct child_prog *cmd);
Erik Andersen3522eb12000-03-12 23:49:18 +0000131
Eric Andersen34e19412000-07-10 18:47:24 +0000132
133/* function prototypes for shell stuff */
Eric Andersen8ea28be2001-01-05 20:58:22 +0000134static void mark_open(int fd);
135static void mark_closed(int fd);
Eric Andersen36278b92001-03-06 20:47:31 +0000136static void close_all(void);
Eric Andersen86349772000-12-18 20:25:50 +0000137static void checkjobs(struct jobset *job_list);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000138static void remove_job(struct jobset *j_list, struct job *job);
Eric Andersen86349772000-12-18 20:25:50 +0000139static int get_command(FILE * source, char *command);
140static int parse_command(char **command_ptr, struct job *job, int *inbg);
141static int run_command(struct job *newjob, int inbg, int outpipe[2]);
142static int pseudo_exec(struct child_prog *cmd) __attribute__ ((noreturn));
Erik Andersen3522eb12000-03-12 23:49:18 +0000143static int busy_loop(FILE * input);
144
Erik Andersend75af992000-03-16 08:09:09 +0000145
Mark Whitley37653aa2000-07-12 23:36:17 +0000146/* Table of built-in functions (these are non-forking builtins, meaning they
147 * can change global variables in the parent shell process but they will not
148 * work with pipes and redirects; 'unset foo | whatever' will not work) */
Eric Andersen86349772000-12-18 20:25:50 +0000149static struct built_in_command bltins[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000150 {"bg", "Resume a job in the background", builtin_fg_bg},
151 {"cd", "Change working directory", builtin_cd},
Eric Andersend2f56772000-09-21 02:48:07 +0000152 {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
Eric Andersenfad9c112000-07-25 18:06:52 +0000153 {"exit", "Exit from shell()", builtin_exit},
154 {"fg", "Bring job into the foreground", builtin_fg_bg},
155 {"jobs", "Lists the active jobs", builtin_jobs},
156 {"export", "Set environment variable", builtin_export},
157 {"unset", "Unset environment variable", builtin_unset},
158 {"read", "Input environment variable", builtin_read},
Matt Kraaidd450a02000-09-13 03:43:36 +0000159 {".", "Source-in and run commands in a file", builtin_source},
Eric Andersen86349772000-12-18 20:25:50 +0000160 /* to do: add ulimit */
Eric Andersenfad9c112000-07-25 18:06:52 +0000161 {NULL, NULL, NULL}
Erik Andersen330fd2b2000-05-19 05:35:19 +0000162};
163
Mark Whitley37653aa2000-07-12 23:36:17 +0000164/* Table of forking built-in functions (things that fork cannot change global
165 * variables in the parent process, such as the current working directory) */
Eric Andersen86349772000-12-18 20:25:50 +0000166static struct built_in_command bltins_forking[] = {
Eric Andersenfad9c112000-07-25 18:06:52 +0000167 {"pwd", "Print current directory", builtin_pwd},
Eric Andersenfad9c112000-07-25 18:06:52 +0000168 {"help", "List shell built-in commands", builtin_help},
169 {NULL, NULL, NULL}
Erik Andersen3522eb12000-03-12 23:49:18 +0000170};
171
Eric Andersen0bcc8132001-01-05 19:37:32 +0000172
173/* Variables we export */
174unsigned int shell_context; /* Used in cmdedit.c to reset the
175 context when someone hits ^C */
176
177
178/* Globals that are static to this file */
Eric Andersencfa88ec2001-05-11 18:08:16 +0000179static const char *cwd;
Eric Andersen744b0642001-01-05 21:23:44 +0000180static char *local_pending_command = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000181static struct jobset job_list = { NULL, NULL };
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000182static int argc;
183static char **argv;
Eric Andersen702ec592001-03-06 22:17:29 +0000184static struct close_me *close_me_head;
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000185static int last_return_code;
186static int last_bg_pid;
Eric Andersen8a646dd2001-06-21 16:38:11 +0000187static unsigned int last_jobid;
Eric Andersen2d848a42001-06-25 17:11:54 +0000188static int shell_terminal;
189static pid_t shell_pgrp;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000190static char *PS1;
Eric Andersene5dfced2001-04-09 22:48:12 +0000191static char *PS2 = "> ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000192
193
Eric Andersenb558e762000-11-30 22:43:16 +0000194#ifdef DEBUG_SHELL
195static inline void debug_printf(const char *format, ...)
196{
197 va_list args;
198 va_start(args, format);
Eric Andersen86349772000-12-18 20:25:50 +0000199 vfprintf(stderr, format, args);
Eric Andersenb558e762000-11-30 22:43:16 +0000200 va_end(args);
201}
202#else
203static inline void debug_printf(const char *format, ...) { }
204#endif
Erik Andersen3522eb12000-03-12 23:49:18 +0000205
Eric Andersen86349772000-12-18 20:25:50 +0000206/*
207 Most builtins need access to the struct child_prog that has
208 their arguments, previously coded as cmd->progs[0]. That coding
209 can exhibit a bug, if the builtin is not the first command in
210 a pipeline: "echo foo | exec sort" will attempt to exec foo.
211
212builtin previous use notes
213------ ----------------- ---------
214cd cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000215exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins
216exit cmd->progs[0]
217fg_bg cmd->progs[0], job_list->head, job_list->fg
218help 0
219jobs job_list->head
220pwd 0
Eric Andersen84e229c2001-03-29 22:48:33 +0000221export cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000222source cmd->progs[0]
223unset cmd->progs[0]
224read cmd->progs[0]
Eric Andersen86349772000-12-18 20:25:50 +0000225
226I added "struct job *family;" to struct child_prog,
227and switched API to builtin_foo(struct child_prog *child);
228So cmd->text becomes child->family->text
229 cmd->job_context becomes child->family->job_context
230 cmd->progs[0] becomes *child
231 job_list becomes child->family->job_list
232 */
Erik Andersen3522eb12000-03-12 23:49:18 +0000233
Erik Andersend75af992000-03-16 08:09:09 +0000234/* built-in 'cd <path>' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000235static int builtin_cd(struct child_prog *child)
Erik Andersend75af992000-03-16 08:09:09 +0000236{
Erik Andersen161220c2000-03-16 08:12:48 +0000237 char *newdir;
Erik Andersend75af992000-03-16 08:09:09 +0000238
Eric Andersen86349772000-12-18 20:25:50 +0000239 if (child->argv[1] == NULL)
Erik Andersen161220c2000-03-16 08:12:48 +0000240 newdir = getenv("HOME");
241 else
Eric Andersen86349772000-12-18 20:25:50 +0000242 newdir = child->argv[1];
Erik Andersen161220c2000-03-16 08:12:48 +0000243 if (chdir(newdir)) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000244 printf("cd: %s: %m\n", newdir);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000245 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000246 }
Eric Andersencfa88ec2001-05-11 18:08:16 +0000247 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000248 if (!cwd)
249 cwd = unknown;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000250 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000251}
252
Eric Andersend2f56772000-09-21 02:48:07 +0000253/* built-in 'exec' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000254static int builtin_exec(struct child_prog *child)
Eric Andersend2f56772000-09-21 02:48:07 +0000255{
Eric Andersen86349772000-12-18 20:25:50 +0000256 if (child->argv[1] == NULL)
257 return EXIT_SUCCESS; /* Really? */
258 child->argv++;
Eric Andersen07f2f392001-03-06 20:28:22 +0000259 close_all();
Eric Andersen86349772000-12-18 20:25:50 +0000260 pseudo_exec(child);
261 /* never returns */
Eric Andersend2f56772000-09-21 02:48:07 +0000262}
263
Erik Andersen3522eb12000-03-12 23:49:18 +0000264/* built-in 'exit' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000265static int builtin_exit(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000266{
Eric Andersen86349772000-12-18 20:25:50 +0000267 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000268 exit(EXIT_SUCCESS);
Erik Andersen161220c2000-03-16 08:12:48 +0000269
Eric Andersen86349772000-12-18 20:25:50 +0000270 exit (atoi(child->argv[1]));
Erik Andersen3522eb12000-03-12 23:49:18 +0000271}
272
273/* built-in 'fg' and 'bg' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000274static int builtin_fg_bg(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000275{
Eric Andersen8a646dd2001-06-21 16:38:11 +0000276 int i, jobnum;
Erik Andersen6273f652000-03-17 01:12:41 +0000277 struct job *job=NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000278
Eric Andersen8a646dd2001-06-21 16:38:11 +0000279 /* If they gave us no args, assume they want the last backgrounded task */
280 if (!child->argv[1]) {
281 for (job = child->family->job_list->head; job; job = job->next) {
282 if (job->jobid == last_jobid) {
283 break;
284 }
Erik Andersen161220c2000-03-16 08:12:48 +0000285 }
Eric Andersen8a646dd2001-06-21 16:38:11 +0000286 if (!job) {
287 error_msg("%s: no current job", child->argv[0]);
288 return EXIT_FAILURE;
289 }
290 } else {
291 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
292 error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]);
293 return EXIT_FAILURE;
294 }
295 for (job = child->family->job_list->head; job; job = job->next) {
296 if (job->jobid == jobnum) {
297 break;
298 }
299 }
300 if (!job) {
301 error_msg("%s: %d: no such job", child->argv[0], jobnum);
302 return EXIT_FAILURE;
303 }
Erik Andersend75af992000-03-16 08:09:09 +0000304 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000305
Eric Andersen86349772000-12-18 20:25:50 +0000306 if (*child->argv[0] == 'f') {
Eric Andersen2d848a42001-06-25 17:11:54 +0000307 /* Put the job into the foreground. */
308 tcsetpgrp(shell_terminal, job->pgrp);
309
Eric Andersen86349772000-12-18 20:25:50 +0000310 child->family->job_list->fg = job;
Erik Andersen161220c2000-03-16 08:12:48 +0000311 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000312
Erik Andersen161220c2000-03-16 08:12:48 +0000313 /* Restart the processes in the job */
Eric Andersen86349772000-12-18 20:25:50 +0000314 for (i = 0; i < job->num_progs; i++)
315 job->progs[i].is_stopped = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000316
Eric Andersen86349772000-12-18 20:25:50 +0000317 job->stopped_progs = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000318
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000319 if ( (i=kill(- job->pgrp, SIGCONT)) < 0) {
320 if (i == ESRCH) {
321 remove_job(&job_list, job);
322 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000323 perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000324 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000325
Matt Kraai3e856ce2000-12-01 02:55:13 +0000326 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000327}
328
329/* built-in 'help' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000330static int builtin_help(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000331{
Eric Andersen86349772000-12-18 20:25:50 +0000332 struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000333
Eric Andersen86349772000-12-18 20:25:50 +0000334 printf("\nBuilt-in commands:\n");
335 printf("-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000336 for (x = bltins; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000337 if (x->descr==NULL)
338 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000339 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000340 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000341 for (x = bltins_forking; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000342 if (x->descr==NULL)
343 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000344 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000345 }
Eric Andersen86349772000-12-18 20:25:50 +0000346 printf("\n\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000347 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000348}
349
350/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000351static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000352{
Erik Andersen161220c2000-03-16 08:12:48 +0000353 struct job *job;
Eric Andersen86349772000-12-18 20:25:50 +0000354 char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000355
Eric Andersen86349772000-12-18 20:25:50 +0000356 for (job = child->family->job_list->head; job; job = job->next) {
357 if (job->running_progs == job->stopped_progs)
358 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000359 else
Eric Andersen86349772000-12-18 20:25:50 +0000360 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000361
Eric Andersen86349772000-12-18 20:25:50 +0000362 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000363 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000364 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000365}
366
367
368/* built-in 'pwd' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000369static int builtin_pwd(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000370{
Eric Andersencfa88ec2001-05-11 18:08:16 +0000371 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000372 if (!cwd)
373 cwd = unknown;
Matt Kraai59df6f72001-05-16 14:21:09 +0000374 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000375 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000376}
377
Erik Andersen6273f652000-03-17 01:12:41 +0000378/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000379static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000380{
Erik Andersen161220c2000-03-16 08:12:48 +0000381 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000382 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000383
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000384 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000385 char **e;
386 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000387 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000388 }
Matt Kraai2129f972001-04-04 17:50:04 +0000389 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000390 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000391 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000392 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000393 fprintf(stderr, "export: %m\n");
Eric Andersen004015e2001-05-21 20:30:51 +0000394#ifdef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000395 if (strncmp(v, "PS1=", 4)==0)
396 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000397#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000398
399#ifdef BB_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +0000400 if(strncmp(v, "LC_ALL=", 7)==0)
401 setlocale(LC_ALL, getenv("LC_ALL"));
Mark Whitleya82a0032001-03-27 17:07:15 +0000402 if(strncmp(v, "LC_CTYPE=", 9)==0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000403 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000404#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000405
Erik Andersen161220c2000-03-16 08:12:48 +0000406 return (res);
Erik Andersen3522eb12000-03-12 23:49:18 +0000407}
408
Eric Andersenb54833c2000-07-03 23:56:26 +0000409/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000410static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000411{
412 int res = 0, len, newlen;
413 char *s;
414 char string[MAX_READ];
415
Eric Andersen86349772000-12-18 20:25:50 +0000416 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000417 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen86349772000-12-18 20:25:50 +0000418 strcpy(string, child->argv[1]);
Eric Andersenb54833c2000-07-03 23:56:26 +0000419 len = strlen(string);
420 string[len++] = '=';
421 string[len] = '\0';
422 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
423 newlen = strlen(string);
424 if(newlen > len)
425 string[--newlen] = '\0'; /* chomp trailing newline */
426 /*
427 ** string should now contain "VAR=<value>"
428 ** copy it (putenv() won't do that, so we must make sure
429 ** the string resides in a static buffer!)
430 */
431 res = -1;
432 if((s = strdup(string)))
433 res = putenv(s);
434 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000435 fprintf(stderr, "read: %m\n");
Eric Andersenb54833c2000-07-03 23:56:26 +0000436 }
437 else
438 fgets(string, sizeof(string), stdin);
439
440 return (res);
441}
442
Erik Andersen3522eb12000-03-12 23:49:18 +0000443/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000444static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000445{
Erik Andersen161220c2000-03-16 08:12:48 +0000446 FILE *input;
447 int status;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000448 int fd;
Erik Andersen3522eb12000-03-12 23:49:18 +0000449
Eric Andersen86349772000-12-18 20:25:50 +0000450 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000451 return EXIT_FAILURE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000452
Eric Andersen86349772000-12-18 20:25:50 +0000453 input = fopen(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000454 if (!input) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000455 printf( "Couldn't open file '%s'\n", child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000456 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000457 }
Erik Andersend75af992000-03-16 08:09:09 +0000458
Eric Andersen8ea28be2001-01-05 20:58:22 +0000459 fd=fileno(input);
460 mark_open(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000461 /* Now run the file */
462 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000463 fclose(input);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000464 mark_closed(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000465 return (status);
Erik Andersen3522eb12000-03-12 23:49:18 +0000466}
467
468/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000469static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000470{
Eric Andersen86349772000-12-18 20:25:50 +0000471 if (child->argv[1] == NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000472 printf( "unset: parameter required.\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000473 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000474 }
Eric Andersen86349772000-12-18 20:25:50 +0000475 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000476 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000477}
478
Eric Andersen8ea28be2001-01-05 20:58:22 +0000479static void mark_open(int fd)
480{
481 struct close_me *new = xmalloc(sizeof(struct close_me));
482 new->fd = fd;
483 new->next = close_me_head;
484 close_me_head = new;
485}
486
487static void mark_closed(int fd)
488{
489 struct close_me *tmp;
490 if (close_me_head == NULL || close_me_head->fd != fd)
491 error_msg_and_die("corrupt close_me");
492 tmp = close_me_head;
493 close_me_head = close_me_head->next;
494 free(tmp);
495}
496
497static void close_all()
498{
Eric Andersen702ec592001-03-06 22:17:29 +0000499 struct close_me *c, *tmp;
500 for (c=close_me_head; c; c=tmp) {
501 close(c->fd);
502 tmp=c->next;
503 free(c);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000504 }
505 close_me_head = NULL;
506}
507
508
Erik Andersen3522eb12000-03-12 23:49:18 +0000509/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000510static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000511{
Erik Andersen161220c2000-03-16 08:12:48 +0000512 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000513 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000514
Eric Andersen86349772000-12-18 20:25:50 +0000515 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000516 free(cmd->progs[i].argv);
Eric Andersen86349772000-12-18 20:25:50 +0000517 if (cmd->progs[i].redirects)
518 free(cmd->progs[i].redirects);
Erik Andersen161220c2000-03-16 08:12:48 +0000519 }
Mark Whitley44a99142001-03-14 17:26:37 +0000520 if (cmd->progs)
521 free(cmd->progs);
Erik Andersen161220c2000-03-16 08:12:48 +0000522 if (cmd->text)
523 free(cmd->text);
Mark Whitley44a99142001-03-14 17:26:37 +0000524 if (cmd->cmdbuf)
525 free(cmd->cmdbuf);
526 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000527 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000528 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000529}
530
Eric Andersen1ca20a72001-03-21 07:34:27 +0000531/* remove a job from a jobset */
532static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000533{
Eric Andersen86349772000-12-18 20:25:50 +0000534 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000535
Eric Andersen86349772000-12-18 20:25:50 +0000536 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000537 if (job == j_list->head) {
538 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000539 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000540 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000541 while (prevjob->next != job)
542 prevjob = prevjob->next;
543 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000544 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000545
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000546 if (j_list->head)
547 last_jobid = j_list->head->jobid;
548 else
549 last_jobid = 0;
550
Erik Andersen161220c2000-03-16 08:12:48 +0000551 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000552}
553
554/* Checks to see if any background processes have exited -- if they
555 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000556static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000557{
Erik Andersen161220c2000-03-16 08:12:48 +0000558 struct job *job;
559 pid_t childpid;
560 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000561 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000562
Erik Andersen161220c2000-03-16 08:12:48 +0000563 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000564 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000565 prognum = 0;
566 while (prognum < job->num_progs &&
567 job->progs[prognum].pid != childpid) prognum++;
568 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000569 break;
570 }
571
Eric Andersena1d187a2000-07-17 19:14:41 +0000572 /* This happens on backticked commands */
573 if(job==NULL)
574 return;
575
Erik Andersen161220c2000-03-16 08:12:48 +0000576 if (WIFEXITED(status) || WIFSIGNALED(status)) {
577 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000578 job->running_progs--;
579 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000580
Eric Andersen86349772000-12-18 20:25:50 +0000581 if (!job->running_progs) {
582 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000583 last_jobid=0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000584 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000585 }
586 } else {
587 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000588 job->stopped_progs++;
589 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000590
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000591#if 0
592 /* Printing this stuff is a pain, since it tends to
593 * overwrite the prompt an inconveinient moments. So
594 * don't do that. */
Eric Andersen86349772000-12-18 20:25:50 +0000595 if (job->stopped_progs == job->num_progs) {
596 printf(JOB_STATUS_FORMAT, job->jobid, "Stopped",
Erik Andersen161220c2000-03-16 08:12:48 +0000597 job->text);
598 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000599#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000600 }
Erik Andersend75af992000-03-16 08:09:09 +0000601 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000602
Erik Andersen161220c2000-03-16 08:12:48 +0000603 if (childpid == -1 && errno != ECHILD)
Matt Kraaia9819b22000-12-22 01:48:07 +0000604 perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000605}
606
Eric Andersene9f07fb2000-12-21 18:31:36 +0000607/* squirrel != NULL means we squirrel away copies of stdin, stdout,
608 * and stderr if they are redirected. */
609static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000610{
611 int i;
612 int openfd;
613 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000614 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000615
Eric Andersen86349772000-12-18 20:25:50 +0000616 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000617 switch (redir->type) {
618 case REDIRECT_INPUT:
619 mode = O_RDONLY;
620 break;
621 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000622 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000623 break;
624 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000625 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000626 break;
627 }
628
629 openfd = open(redir->filename, mode, 0666);
630 if (openfd < 0) {
631 /* this could get lost if stderr has been redirected, but
632 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000633 perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000634 return 1;
635 }
636
637 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000638 if (squirrel && redir->fd < 3) {
639 squirrel[redir->fd] = dup(redir->fd);
640 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000641 dup2(openfd, redir->fd);
642 close(openfd);
643 }
644 }
645
646 return 0;
647}
648
Eric Andersene9f07fb2000-12-21 18:31:36 +0000649static void restore_redirects(int squirrel[])
650{
651 int i, fd;
652 for (i=0; i<3; i++) {
653 fd = squirrel[i];
654 if (fd != -1) {
655 /* No error checking. I sure wouldn't know what
656 * to do with an error if I found one! */
657 dup2(fd, i);
658 close(fd);
659 }
660 }
661}
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000662
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000663static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000664{
Eric Andersen004015e2001-05-21 20:30:51 +0000665#ifndef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000666 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000667#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000668 PS1 = getenv("PS1");
Eric Andersene5dfced2001-04-09 22:48:12 +0000669 if(PS1==0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000670 PS1 = "\\w \\$ ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000671#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000672}
673
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000674static inline void setup_prompt_string(char **prompt_str)
675{
Eric Andersen004015e2001-05-21 20:30:51 +0000676#ifndef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000677 /* Set up the prompt */
678 if (shell_context == 0) {
679 if (PS1)
680 free(PS1);
681 PS1=xmalloc(strlen(cwd)+4);
682 sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
683 *prompt_str = PS1;
684 } else {
685 *prompt_str = PS2;
686 }
687#else
688 *prompt_str = (shell_context==0)? PS1 : PS2;
689#endif
690}
Eric Andersen22332fd2001-01-30 23:40:39 +0000691
Eric Andersen09acc062001-01-04 11:10:38 +0000692static int get_command(FILE * source, char *command)
693{
694 char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000695
Eric Andersen1c314ad2000-06-28 16:56:25 +0000696 if (source == NULL) {
697 if (local_pending_command) {
698 /* a command specified (-c option): return it & mark it done */
699 strcpy(command, local_pending_command);
700 free(local_pending_command);
701 local_pending_command = NULL;
702 return 0;
703 }
704 return 1;
705 }
706
Erik Andersen161220c2000-03-16 08:12:48 +0000707 if (source == stdin) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000708 setup_prompt_string(&prompt_str);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000709
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000710#ifdef BB_FEATURE_COMMAND_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000711 /*
712 ** enable command line editing only while a command line
713 ** is actually being read; otherwise, we'll end up bequeathing
714 ** atexit() handlers and other unwanted stuff to our
715 ** child processes (rob@sysgo.de)
716 */
Eric Andersen86349772000-12-18 20:25:50 +0000717 cmdedit_read_input(prompt_str, command);
Eric Andersen501c88b2000-07-28 15:14:45 +0000718 cmdedit_terminate();
Erik Andersen161220c2000-03-16 08:12:48 +0000719 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000720#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000721 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000722#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000723 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000724
Erik Andersen161220c2000-03-16 08:12:48 +0000725 if (!fgets(command, BUFSIZ - 2, source)) {
726 if (source == stdin)
727 printf("\n");
728 return 1;
729 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000730
Erik Andersen161220c2000-03-16 08:12:48 +0000731 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000732}
733
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000734static char* itoa(register int i)
735{
736 static char a[7]; /* Max 7 ints */
737 register char *b = a + sizeof(a) - 1;
738 int sign = (i < 0);
739
740 if (sign)
741 i = -i;
742 *b = 0;
743 do
744 {
745 *--b = '0' + (i % 10);
746 i /= 10;
747 }
748 while (i);
749 if (sign)
750 *--b = '-';
751 return b;
752}
753
754char * strsep_space( char *string, int * ix)
755{
756 char *token, *begin;
757
758 begin = string;
759
760 /* Short circuit the trivial case */
761 if ( !string || ! string[*ix])
762 return NULL;
763
764 /* Find the end of the token. */
765 while( string && string[*ix] && !isspace(string[*ix]) ) {
766 (*ix)++;
767 }
768
769 /* Find the end of any whitespace trailing behind
770 * the token and let that be part of the token */
771 while( string && string[*ix] && isspace(string[*ix]) ) {
772 (*ix)++;
773 }
774
775 if (! string && *ix==0) {
776 /* Nothing useful was found */
777 return NULL;
778 }
779
780 token = xmalloc(*ix+1);
781 token[*ix] = '\0';
782 strncpy(token, string, *ix);
783
784 return token;
785}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000786
Eric Andersenca604592001-03-08 17:17:13 +0000787static int expand_arguments(char *command)
788{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000789 int total_length=0, length, i, retval, ix = 0;
790 expand_t expand_result;
791 char *tmpcmd, *cmd, *cmd_copy;
792 char *src, *dst, *var;
793 const char *out_of_space = "out of space during expansion";
794 int flags = GLOB_NOCHECK
795#ifdef GLOB_BRACE
796 | GLOB_BRACE
797#endif
798#ifdef GLOB_TILDE
799 | GLOB_TILDE
800#endif
801 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000802
Eric Andersenca604592001-03-08 17:17:13 +0000803 /* get rid of the terminating \n */
804 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000805
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000806 /* Fix up escape sequences to be the Real Thing(tm) */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000807 while( command && command[ix]) {
808 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000809 const char *tmp = command+ix+1;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000810 command[ix] = process_escape_sequence( &tmp );
811 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000812 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000813 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000814 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000815 /* Use glob and then fixup environment variables and such */
816
817 /* It turns out that glob is very stupid. We have to feed it one word at a
818 * time since it can't cope with a full string. Here we convert command
819 * (char*) into cmd (char**, one word per string) */
820
821 /* We need a clean copy, so strsep can mess up the copy while
822 * we write stuff into the original (in a minute) */
823 cmd = cmd_copy = strdup(command);
824 *command = '\0';
825 for (ix = 0, tmpcmd = cmd;
826 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
827 if (*tmpcmd == '\0')
828 break;
829 /* we need to trim() the result for glob! */
830 trim(tmpcmd);
831 retval = glob(tmpcmd, flags, NULL, &expand_result);
832 free(tmpcmd); /* Free mem allocated by strsep_space */
833 if (retval == GLOB_NOSPACE) {
834 /* Mem may have been allocated... */
835 globfree (&expand_result);
836 error_msg(out_of_space);
837 return FALSE;
838 } else if (retval != 0) {
839 /* Some other error. GLOB_NOMATCH shouldn't
840 * happen because of the GLOB_NOCHECK flag in
841 * the glob call. */
842 error_msg("syntax error");
843 return FALSE;
844 } else {
845 /* Convert from char** (one word per string) to a simple char*,
846 * but don't overflow command which is BUFSIZ in length */
847 for (i=0; i < expand_result.gl_pathc; i++) {
848 length=strlen(expand_result.gl_pathv[i]);
849 if (total_length+length+1 >= BUFSIZ) {
850 error_msg(out_of_space);
851 return FALSE;
852 }
853 strcat(command+total_length, " ");
854 total_length+=1;
855 strcat(command+total_length, expand_result.gl_pathv[i]);
856 total_length+=length;
857 }
858 globfree (&expand_result);
859 }
860 }
861 free(cmd_copy);
862 trim(command);
863
864 /* Now do the shell variable substitutions which
865 * wordexp can't do for us, namely $? and $! */
866 src = command;
867 while((dst = strchr(src,'$')) != NULL){
868 var = NULL;
869 switch(*(dst+1)) {
870 case '?':
871 var = itoa(last_return_code);
872 break;
873 case '!':
874 if (last_bg_pid==-1)
875 *(var)='\0';
876 else
877 var = itoa(last_bg_pid);
878 break;
879 /* Everything else like $$, $#, $[0-9], etc. should all be
880 * expanded by wordexp(), so we can in theory skip that stuff
881 * here, but just to be on the safe side (i.e., since uClibc
882 * wordexp doesn't do this stuff yet), lets leave it in for
883 * now. */
884 case '$':
885 var = itoa(getpid());
886 break;
887 case '#':
888 var = itoa(argc-1);
889 break;
890 case '0':case '1':case '2':case '3':case '4':
891 case '5':case '6':case '7':case '8':case '9':
892 {
893 int ixx=*(dst + 1)-48;
894 if (ixx >= argc) {
895 var='\0';
896 } else {
897 var = argv[ixx];
898 }
899 }
900 break;
901
902 }
903 if (var) {
904 /* a single character construction was found, and
905 * already handled in the case statement */
906 src=dst+2;
907 } else {
908 /* Looks like an environment variable */
909 char delim_hold;
910 int num_skip_chars=0;
911 int dstlen = strlen(dst);
912 /* Is this a ${foo} type variable? */
913 if (dstlen >=2 && *(dst+1) == '{') {
914 src=strchr(dst+1, '}');
915 num_skip_chars=1;
916 } else {
917 src=dst+1;
918 while(isalnum(*src) || *src=='_') src++;
919 }
920 if (src == NULL) {
921 src = dst+dstlen;
922 }
923 delim_hold=*src;
924 *src='\0'; /* temporary */
925 var = getenv(dst + 1 + num_skip_chars);
926 *src=delim_hold;
927 src += num_skip_chars;
928 }
929 if (var == NULL) {
930 /* Seems we got an un-expandable variable. So delete it. */
931 var = "";
932 }
933 {
934 int subst_len = strlen(var);
935 int trail_len = strlen(src);
936 if (dst+subst_len+trail_len >= command+BUFSIZ) {
937 error_msg(out_of_space);
938 return FALSE;
939 }
940 /* Move stuff to the end of the string to accommodate
941 * filling the created gap with the new stuff */
942 memmove(dst+subst_len, src, trail_len+1);
943 /* Now copy in the new stuff */
944 memcpy(dst, var, subst_len);
945 src = dst+subst_len;
946 }
947 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000948
Eric Andersenca604592001-03-08 17:17:13 +0000949 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000950}
951
Eric Andersen86349772000-12-18 20:25:50 +0000952/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
953 line). If a valid command is found, command_ptr is set to point to
Erik Andersen3522eb12000-03-12 23:49:18 +0000954 the beginning of the next command (if the original command had more
955 then one job associated with it) or NULL if no more commands are
956 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000957static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000958{
Erik Andersen161220c2000-03-16 08:12:48 +0000959 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000960 char *return_command = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +0000961 char *src, *buf, *chptr;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000962 int argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000963 int done = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000964 int argv_alloced;
Matt Kraaibe66ad32001-04-12 15:42:17 +0000965 int i, saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000966 char quote = '\0';
967 int count;
Eric Andersen86349772000-12-18 20:25:50 +0000968 struct child_prog *prog;
Erik Andersen3522eb12000-03-12 23:49:18 +0000969
Erik Andersen161220c2000-03-16 08:12:48 +0000970 /* skip leading white space */
Eric Andersen86349772000-12-18 20:25:50 +0000971 while (**command_ptr && isspace(**command_ptr))
972 (*command_ptr)++;
Erik Andersen3522eb12000-03-12 23:49:18 +0000973
Erik Andersen161220c2000-03-16 08:12:48 +0000974 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000975 if (!**command_ptr || (**command_ptr == '#')) {
976 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000977 return 0;
978 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000979
Eric Andersen86349772000-12-18 20:25:50 +0000980 *inbg = 0;
981 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000982 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000983
Erik Andersen161220c2000-03-16 08:12:48 +0000984 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000985 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000986 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000987
Erik Andersen161220c2000-03-16 08:12:48 +0000988 Getting clean memory relieves us of the task of NULL
989 terminating things and makes the rest of this look a bit
990 cleaner (though it is, admittedly, a tad less efficient) */
Eric Andersen86349772000-12-18 20:25:50 +0000991 job->cmdbuf = command = xcalloc(2*strlen(*command_ptr) + 1, sizeof(char));
Erik Andersen161220c2000-03-16 08:12:48 +0000992 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000993
Erik Andersen161220c2000-03-16 08:12:48 +0000994 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000995 prog->num_redirects = 0;
996 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000997 prog->is_stopped = 0;
998 prog->family = job;
Erik Andersen3522eb12000-03-12 23:49:18 +0000999
Eric Andersen86349772000-12-18 20:25:50 +00001000 argv_alloced = 5;
1001 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
1002 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +00001003
Erik Andersen161220c2000-03-16 08:12:48 +00001004 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +00001005 src = *command_ptr;
Erik Andersen161220c2000-03-16 08:12:48 +00001006 while (*src && !done) {
1007 if (quote == *src) {
1008 quote = '\0';
1009 } else if (quote) {
1010 if (*src == '\\') {
1011 src++;
1012 if (!*src) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001013 error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001014 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001015 return 1;
1016 }
1017
1018 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +00001019 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +00001020 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +00001021 *buf++ = '\\';
1022 }
Erik Andersen161220c2000-03-16 08:12:48 +00001023 } else if (*src == '*' || *src == '?' || *src == '[' ||
1024 *src == ']') *buf++ = '\\';
1025 *buf++ = *src;
1026 } else if (isspace(*src)) {
Matt Kraaibe66ad32001-04-12 15:42:17 +00001027 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001028 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001029 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +00001030 if ((argc_l + 1) == argv_alloced) {
1031 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +00001032 prog->argv = xrealloc(prog->argv,
1033 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +00001034 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001035 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001036 prog->argv[argc_l] = buf;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001037 saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001038 }
1039 } else
1040 switch (*src) {
1041 case '"':
1042 case '\'':
1043 quote = *src;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001044 saw_quote = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001045 break;
1046
1047 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001048 if (*(src-1)== '$')
1049 *buf++ = *src;
1050 else
1051 done = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001052 break;
1053
Eric Andersen86349772000-12-18 20:25:50 +00001054 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +00001055 case '<':
Eric Andersen86349772000-12-18 20:25:50 +00001056 i = prog->num_redirects++;
1057 prog->redirects = xrealloc(prog->redirects,
1058 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +00001059 (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +00001060
Eric Andersen86349772000-12-18 20:25:50 +00001061 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001062 if (buf != prog->argv[argc_l]) {
Erik Andersen161220c2000-03-16 08:12:48 +00001063 /* the stuff before this character may be the file number
1064 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +00001065 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001066 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +00001067
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001068 if (*chptr && *prog->argv[argc_l]) {
1069 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001070 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001071 }
1072 }
1073
Eric Andersen86349772000-12-18 20:25:50 +00001074 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001075 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001076 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001077 else
Eric Andersen86349772000-12-18 20:25:50 +00001078 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001079 }
1080
1081 if (*src++ == '>') {
1082 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001083 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001084 REDIRECT_APPEND, src++;
1085 else
Eric Andersen86349772000-12-18 20:25:50 +00001086 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001087 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001088 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001089 }
1090
1091 /* This isn't POSIX sh compliant. Oh well. */
1092 chptr = src;
1093 while (isspace(*chptr))
1094 chptr++;
1095
1096 if (!*chptr) {
Mark Whitley44a99142001-03-14 17:26:37 +00001097 error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001098 free_job(job);
1099 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001100 return 1;
1101 }
1102
Eric Andersen86349772000-12-18 20:25:50 +00001103 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001104 while (*chptr && !isspace(*chptr))
1105 *buf++ = *chptr++;
1106
1107 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001108 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001109 break;
1110
1111 case '|': /* pipe */
1112 /* finish this command */
Matt Kraaibe66ad32001-04-12 15:42:17 +00001113 if (*prog->argv[argc_l] || saw_quote)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001114 argc_l++;
1115 if (!argc_l) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001116 error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001117 free_job(job);
1118 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001119 return 1;
1120 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001121 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001122
1123 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001124 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001125 job->progs = xrealloc(job->progs,
Eric Andersen86349772000-12-18 20:25:50 +00001126 sizeof(*job->progs) * job->num_progs);
1127 prog = job->progs + (job->num_progs - 1);
1128 prog->num_redirects = 0;
1129 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001130 prog->is_stopped = 0;
1131 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001132 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001133
Eric Andersen86349772000-12-18 20:25:50 +00001134 argv_alloced = 5;
1135 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001136 prog->argv[0] = ++buf;
1137
1138 src++;
1139 while (*src && isspace(*src))
1140 src++;
1141
1142 if (!*src) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001143 error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001144 free_job(job);
1145 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001146 return 1;
1147 }
1148 src--; /* we'll ++ it at the end of the loop */
1149
1150 break;
1151
1152 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001153 *inbg = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001154 case ';': /* multiple commands */
1155 done = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001156 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001157 break;
1158
Matt Kraai131241f2000-09-14 00:43:20 +00001159 case '\\':
1160 src++;
1161 if (!*src) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001162 error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001163 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001164 return 1;
1165 }
1166 if (*src == '*' || *src == '[' || *src == ']'
1167 || *src == '?') *buf++ = '\\';
1168 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001169 default:
1170 *buf++ = *src;
1171 }
1172
Erik Andersend75af992000-03-16 08:09:09 +00001173 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001174 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001175
Matt Kraaibe66ad32001-04-12 15:42:17 +00001176 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001177 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001178 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001179 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001180 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001181 return 0;
1182 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001183 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001184
Eric Andersen86349772000-12-18 20:25:50 +00001185 if (!return_command) {
1186 job->text = xmalloc(strlen(*command_ptr) + 1);
1187 strcpy(job->text, *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001188 } else {
1189 /* This leaves any trailing spaces, which is a bit sloppy */
Eric Andersen86349772000-12-18 20:25:50 +00001190 count = return_command - *command_ptr;
Eric Andersenec10b9d2000-07-14 01:13:11 +00001191 job->text = xmalloc(count + 1);
Eric Andersen86349772000-12-18 20:25:50 +00001192 strncpy(job->text, *command_ptr, count);
Erik Andersen161220c2000-03-16 08:12:48 +00001193 job->text[count] = '\0';
1194 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001195
Eric Andersen86349772000-12-18 20:25:50 +00001196 *command_ptr = return_command;
Eric Andersen46f0beb2000-11-14 21:59:22 +00001197
Erik Andersend75af992000-03-16 08:09:09 +00001198 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001199}
1200
Eric Andersen86349772000-12-18 20:25:50 +00001201/* Run the child_prog, no matter what kind of command it uses.
1202 */
1203static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001204{
Eric Andersen86349772000-12-18 20:25:50 +00001205 struct built_in_command *x;
Eric Andersenb54833c2000-07-03 23:56:26 +00001206#ifdef BB_FEATURE_SH_STANDALONE_SHELL
Eric Andersenaf4ac772001-02-01 22:43:49 +00001207 char *name;
Erik Andersenbcd61772000-05-13 06:33:19 +00001208#endif
Erik Andersen3522eb12000-03-12 23:49:18 +00001209
Eric Andersene9f07fb2000-12-21 18:31:36 +00001210 /* Check if the command matches any of the non-forking builtins.
1211 * Depending on context, this might be redundant. But it's
1212 * easier to waste a few CPU cycles than it is to figure out
1213 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001214 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001215 for (x = bltins; x->cmd; x++) {
1216 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1217 exit(x->function(child));
1218 }
1219 }
1220
1221 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001222 for (x = bltins_forking; x->cmd; x++) {
1223 if (strcmp(child->argv[0], x->cmd) == 0) {
1224 applet_name=x->cmd;
1225 exit (x->function(child));
1226 }
1227 }
1228#ifdef BB_FEATURE_SH_STANDALONE_SHELL
1229 /* Check if the command matches any busybox internal
1230 * commands ("applets") here. Following discussions from
1231 * November 2000 on busybox@opensource.lineo.com, don't use
1232 * get_last_path_component(). This way explicit (with
1233 * slashes) filenames will never be interpreted as an
1234 * applet, just like with builtins. This way the user can
1235 * override an applet with an explicit filename reference.
1236 * The only downside to this change is that an explicit
1237 * /bin/foo invocation will fork and exec /bin/foo, even if
1238 * /bin/foo is a symlink to busybox.
1239 */
Matt Kraaif2cc2762001-02-01 19:21:20 +00001240 name = child->argv[0];
Eric Andersen86349772000-12-18 20:25:50 +00001241
1242#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
1243 /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then
1244 * if you run /bin/cat, it will use BusyBox cat even if
1245 * /bin/cat exists on the filesystem and is _not_ busybox.
1246 * Some systems want this, others do not. Choose wisely. :-)
1247 */
Matt Kraaif2cc2762001-02-01 19:21:20 +00001248 name = get_last_path_component(name);
Eric Andersen86349772000-12-18 20:25:50 +00001249#endif
1250
Eric Andersen67991cf2001-02-14 21:23:06 +00001251 {
Eric Andersen82ab8da2001-03-23 17:06:01 +00001252 char** argv_l=child->argv;
Eric Andersen67991cf2001-02-14 21:23:06 +00001253 int argc_l;
Eric Andersen82ab8da2001-03-23 17:06:01 +00001254 for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
Eric Andersen67991cf2001-02-14 21:23:06 +00001255 optind = 1;
1256 run_applet_by_name(name, argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001257 }
1258#endif
1259
1260 execvp(child->argv[0], child->argv);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001261 perror_msg_and_die("%s", child->argv[0]);
Eric Andersen86349772000-12-18 20:25:50 +00001262}
1263
1264static void insert_job(struct job *newjob, int inbg)
1265{
1266 struct job *thejob;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001267 struct jobset *j_list=newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001268
1269 /* find the ID for thejob to use */
1270 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001271 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001272 if (thejob->jobid >= newjob->jobid)
1273 newjob->jobid = thejob->jobid + 1;
1274
1275 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001276 if (!j_list->head) {
1277 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001278 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001279 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001280 thejob->next = xmalloc(sizeof(*thejob));
1281 thejob = thejob->next;
1282 }
1283
1284 *thejob = *newjob; /* physically copy the struct job */
1285 thejob->next = NULL;
1286 thejob->running_progs = thejob->num_progs;
1287 thejob->stopped_progs = 0;
1288
1289 if (inbg) {
1290 /* we don't wait for background thejobs to return -- append it
1291 to the list of backgrounded thejobs and leave it alone */
1292 printf("[%d] %d\n", thejob->jobid,
1293 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001294 last_jobid = newjob->jobid;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001295 last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001296 } else {
1297 newjob->job_list->fg = thejob;
1298
1299 /* move the new process group into the foreground */
1300 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001301 if (tcsetpgrp(shell_terminal, newjob->pgrp) && errno != ENOTTY)
Matt Kraaia9819b22000-12-22 01:48:07 +00001302 perror_msg("tcsetpgrp");
Eric Andersen86349772000-12-18 20:25:50 +00001303 }
1304}
1305
1306static int run_command(struct job *newjob, int inbg, int outpipe[2])
1307{
1308 /* struct job *thejob; */
1309 int i;
1310 int nextin, nextout;
1311 int pipefds[2]; /* pipefd[0] is for reading */
1312 struct built_in_command *x;
1313 struct child_prog *child;
1314
Eric Andersen6efc48c2000-07-18 08:16:39 +00001315 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001316 for (i = 0; i < newjob->num_progs; i++) {
1317 child = & (newjob->progs[i]);
1318
1319 if ((i + 1) < newjob->num_progs) {
1320 if (pipe(pipefds)<0) perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001321 nextout = pipefds[1];
1322 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001323 if (outpipe[1]!=-1) {
1324 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001325 } else {
1326 nextout = 1;
1327 }
Erik Andersen161220c2000-03-16 08:12:48 +00001328 }
1329
Eric Andersen501c88b2000-07-28 15:14:45 +00001330
Eric Andersene9f07fb2000-12-21 18:31:36 +00001331 /* Check if the command matches any non-forking builtins,
1332 * but only if this is a simple command.
1333 * Non-forking builtins within pipes have to fork anyway,
1334 * and are handled in pseudo_exec. "echo foo | read bar"
1335 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001336 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001337 if (newjob->num_progs == 1) {
1338 for (x = bltins; x->cmd; x++) {
1339 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1340 int squirrel[] = {-1, -1, -1};
1341 int rcode;
1342 setup_redirects(child, squirrel);
1343 rcode = x->function(child);
1344 restore_redirects(squirrel);
1345 return rcode;
1346 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001347 }
1348 }
1349
Eric Andersen86349772000-12-18 20:25:50 +00001350 if (!(child->pid = fork())) {
Eric Andersen2d848a42001-06-25 17:11:54 +00001351 /* Set the handling for job control signals back to the default. */
1352 signal(SIGINT, SIG_DFL);
1353 signal(SIGQUIT, SIG_DFL);
1354 signal(SIGTSTP, SIG_DFL);
1355 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001356 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001357 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001358
Eric Andersen8ea28be2001-01-05 20:58:22 +00001359 close_all();
1360
Eric Andersen86349772000-12-18 20:25:50 +00001361 if (outpipe[1]!=-1) {
1362 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001363 }
1364 if (nextin != 0) {
1365 dup2(nextin, 0);
1366 close(nextin);
1367 }
1368
1369 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001370 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001371 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001372 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001373 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001374 }
1375
Eric Andersen86349772000-12-18 20:25:50 +00001376 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001377 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001378
Eric Andersen86349772000-12-18 20:25:50 +00001379 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001380 }
Eric Andersen86349772000-12-18 20:25:50 +00001381 if (outpipe[1]!=-1) {
1382 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001383 }
Erik Andersen161220c2000-03-16 08:12:48 +00001384
1385 /* put our child in the process group whose leader is the
1386 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001387 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001388 if (nextin != 0)
1389 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001390 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001391 close(nextout);
1392
1393 /* If there isn't another process, nextin is garbage
1394 but it doesn't matter */
1395 nextin = pipefds[0];
1396 }
1397
Eric Andersen86349772000-12-18 20:25:50 +00001398 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001399
Eric Andersen86349772000-12-18 20:25:50 +00001400 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001401
Erik Andersen161220c2000-03-16 08:12:48 +00001402 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001403}
1404
Erik Andersend75af992000-03-16 08:09:09 +00001405static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001406{
Erik Andersen161220c2000-03-16 08:12:48 +00001407 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001408 char *next_command = NULL;
1409 struct job newjob;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001410 pid_t parent_pgrp;
Erik Andersen161220c2000-03-16 08:12:48 +00001411 int i;
Eric Andersen86349772000-12-18 20:25:50 +00001412 int inbg;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001413 int status;
Eric Andersen86349772000-12-18 20:25:50 +00001414 newjob.job_list = &job_list;
1415 newjob.job_context = DEFAULT_CONTEXT;
Erik Andersen3522eb12000-03-12 23:49:18 +00001416
Eric Andersen1c314ad2000-06-28 16:56:25 +00001417 /* save current owner of TTY so we can restore it on exit */
Eric Andersen2d848a42001-06-25 17:11:54 +00001418 parent_pgrp = tcgetpgrp(shell_terminal);
Eric Andersen1c314ad2000-06-28 16:56:25 +00001419
Matt Kraaib8907522000-09-13 02:08:21 +00001420 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Erik Andersend75af992000-03-16 08:09:09 +00001421
Erik Andersen161220c2000-03-16 08:12:48 +00001422 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001423 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001424 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001425
Erik Andersend75af992000-03-16 08:09:09 +00001426 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001427 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001428
Eric Andersen86349772000-12-18 20:25:50 +00001429 if (!next_command) {
1430 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001431 break;
Eric Andersen86349772000-12-18 20:25:50 +00001432 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001433 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001434
Eric Andersenca604592001-03-08 17:17:13 +00001435 if (expand_arguments(next_command) == FALSE) {
1436 free(command);
1437 command = (char *) xcalloc(BUFSIZ, sizeof(char));
1438 next_command = NULL;
1439 continue;
1440 }
1441
Eric Andersen86349772000-12-18 20:25:50 +00001442 if (!parse_command(&next_command, &newjob, &inbg) &&
1443 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001444 int pipefds[2] = {-1,-1};
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001445 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
1446 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001447 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001448 }
1449 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001450 free(command);
Matt Kraaib8907522000-09-13 02:08:21 +00001451 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Eric Andersen86349772000-12-18 20:25:50 +00001452 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001453 }
1454 } else {
1455 /* a job is running in the foreground; wait for it */
1456 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001457 while (!job_list.fg->progs[i].pid ||
1458 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001459
Eric Andersen8ea28be2001-01-05 20:58:22 +00001460 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0)
1461 perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
Erik Andersen3522eb12000-03-12 23:49:18 +00001462
Erik Andersend75af992000-03-16 08:09:09 +00001463 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001464 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001465 job_list.fg->running_progs--;
1466 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001467
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001468 last_return_code=WEXITSTATUS(status);
1469
Eric Andersen86349772000-12-18 20:25:50 +00001470 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001471 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001472 remove_job(&job_list, job_list.fg);
1473 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001474 }
Erik Andersend75af992000-03-16 08:09:09 +00001475 } else {
Erik Andersen161220c2000-03-16 08:12:48 +00001476 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001477 job_list.fg->stopped_progs++;
1478 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001479
Eric Andersen86349772000-12-18 20:25:50 +00001480 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1481 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1482 "Stopped", job_list.fg->text);
1483 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001484 }
Erik Andersend75af992000-03-16 08:09:09 +00001485 }
1486
Eric Andersen86349772000-12-18 20:25:50 +00001487 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001488 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001489 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001490 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Matt Kraaia9819b22000-12-22 01:48:07 +00001491 perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001492 }
1493 }
1494 }
Erik Andersen161220c2000-03-16 08:12:48 +00001495 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001496
Eric Andersen1c314ad2000-06-28 16:56:25 +00001497 /* return controlling TTY back to parent process group before exiting */
Eric Andersen2d848a42001-06-25 17:11:54 +00001498 if (tcsetpgrp(shell_terminal, parent_pgrp))
Matt Kraaia9819b22000-12-22 01:48:07 +00001499 perror_msg("tcsetpgrp");
Eric Andersenb54833c2000-07-03 23:56:26 +00001500
1501 /* return exit status if called with "-c" */
1502 if (input == NULL && WIFEXITED(status))
1503 return WEXITSTATUS(status);
1504
Erik Andersen161220c2000-03-16 08:12:48 +00001505 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001506}
1507
1508
Eric Andersenfad9c112000-07-25 18:06:52 +00001509#ifdef BB_FEATURE_CLEAN_UP
1510void free_memory(void)
1511{
Eric Andersene5dfced2001-04-09 22:48:12 +00001512 if (cwd) {
Eric Andersenfad9c112000-07-25 18:06:52 +00001513 free(cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001514 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001515 if (local_pending_command)
1516 free(local_pending_command);
1517
Eric Andersen86349772000-12-18 20:25:50 +00001518 if (job_list.fg && !job_list.fg->running_progs) {
1519 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001520 }
1521}
1522#endif
1523
Eric Andersen2d848a42001-06-25 17:11:54 +00001524/* Make sure we have a controlling tty. If we get started under a job
1525 * aware app (like bash for example), make sure we are now in charge so
1526 * we don't fight over who gets the foreground */
1527static void setup_job_control()
1528{
1529 /* Loop until we are in the foreground. */
1530 while (tcgetpgrp (shell_terminal) != (shell_pgrp = getpgrp ()))
1531 kill (- shell_pgrp, SIGTTIN);
1532
1533 /* Ignore interactive and job-control signals. */
1534 signal(SIGINT, SIG_IGN);
1535 signal(SIGQUIT, SIG_IGN);
1536 signal(SIGTSTP, SIG_IGN);
1537 signal(SIGTTIN, SIG_IGN);
1538 signal(SIGTTOU, SIG_IGN);
1539 signal(SIGCHLD, SIG_IGN);
1540
1541 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001542 setsid();
Eric Andersen2d848a42001-06-25 17:11:54 +00001543 shell_pgrp = getpid ();
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001544 setpgid (shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001545
1546 /* Grab control of the terminal. */
1547 tcsetpgrp(shell_terminal, shell_pgrp);
1548}
Eric Andersen6efc48c2000-07-18 08:16:39 +00001549
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001550int shell_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001551{
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001552 int opt, interactive=FALSE;
Erik Andersen161220c2000-03-16 08:12:48 +00001553 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001554 argc = argc_l;
1555 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001556
Eric Andersen702ec592001-03-06 22:17:29 +00001557 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001558 last_jobid = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001559 shell_context = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001560 local_pending_command = NULL;
Eric Andersen702ec592001-03-06 22:17:29 +00001561 close_me_head = NULL;
Eric Andersenb0970d42001-01-05 19:34:52 +00001562 job_list.head = NULL;
1563 job_list.fg = NULL;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001564 last_return_code=1;
Eric Andersen501c88b2000-07-28 15:14:45 +00001565
Eric Andersenb558e762000-11-30 22:43:16 +00001566 if (argv[0] && argv[0][0] == '-') {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001567 FILE *prof_input;
1568 prof_input = fopen("/etc/profile", "r");
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001569 if (prof_input) {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001570 int tmp_fd = fileno(prof_input);
1571 mark_open(tmp_fd);
1572 /* Now run the file */
1573 busy_loop(prof_input);
1574 fclose(prof_input);
1575 mark_closed(tmp_fd);
1576 }
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)
Matt Kraaidd19c692001-01-31 19:00:21 +00001584 error_msg_and_die("multiple -c arguments");
Matt Kraai6085c722000-09-06 01:46:18 +00001585 local_pending_command = xstrdup(argv[optind]);
1586 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:
Eric Andersen67991cf2001-02-14 21:23:06 +00001593 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 &&
1604 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
1605 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 */
Matt Kraai4ef40c02001-04-12 20:44:21 +00001611 printf( "\n\n" BB_BANNER " Built-in shell (lash)\n");
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001612 printf( "Enter 'help' for a list of built-in commands.\n\n");
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001613 } else if (local_pending_command==NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001614 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Matt Kraaibbaef662000-09-27 02:43:35 +00001615 input = xfopen(argv[optind], "r");
Eric Andersen8ea28be2001-01-05 20:58:22 +00001616 mark_open(fileno(input)); /* be lazy, never mark this closed */
Eric Andersen501c88b2000-07-28 15:14:45 +00001617 }
1618
Eric Andersen6efc48c2000-07-18 08:16:39 +00001619 /* initialize the cwd -- this is never freed...*/
Eric Andersene5dfced2001-04-09 22:48:12 +00001620 cwd = xgetcwd(0);
Eric Andersen5f265b72001-05-11 16:58:46 +00001621 if (!cwd)
1622 cwd = unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001623
Eric Andersenfad9c112000-07-25 18:06:52 +00001624#ifdef BB_FEATURE_CLEAN_UP
1625 atexit(free_memory);
1626#endif
1627
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001628#ifdef BB_FEATURE_COMMAND_EDITING
1629 cmdedit_set_initial_prompt();
1630#else
1631 PS1 = NULL;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001632#endif
1633
Erik Andersen161220c2000-03-16 08:12:48 +00001634 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001635}