blob: 7c932a91021fdb6c377dcd6449eb35f2af6783cf [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);
Eric Andersen07abfe22001-06-27 17:29:11 +0000322 } else {
323 perror_msg("kill (SIGCONT)");
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000324 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000325 }
Eric Andersen2d848a42001-06-25 17:11:54 +0000326
Matt Kraai3e856ce2000-12-01 02:55:13 +0000327 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000328}
329
330/* built-in 'help' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000331static int builtin_help(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000332{
Eric Andersen86349772000-12-18 20:25:50 +0000333 struct built_in_command *x;
Erik Andersen3522eb12000-03-12 23:49:18 +0000334
Eric Andersen86349772000-12-18 20:25:50 +0000335 printf("\nBuilt-in commands:\n");
336 printf("-------------------\n");
Erik Andersen161220c2000-03-16 08:12:48 +0000337 for (x = bltins; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000338 if (x->descr==NULL)
339 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000340 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen161220c2000-03-16 08:12:48 +0000341 }
Erik Andersen330fd2b2000-05-19 05:35:19 +0000342 for (x = bltins_forking; x->cmd; x++) {
Eric Andersenfad9c112000-07-25 18:06:52 +0000343 if (x->descr==NULL)
344 continue;
Eric Andersen86349772000-12-18 20:25:50 +0000345 printf("%s\t%s\n", x->cmd, x->descr);
Erik Andersen330fd2b2000-05-19 05:35:19 +0000346 }
Eric Andersen86349772000-12-18 20:25:50 +0000347 printf("\n\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000348 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000349}
350
351/* built-in 'jobs' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000352static int builtin_jobs(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000353{
Erik Andersen161220c2000-03-16 08:12:48 +0000354 struct job *job;
Eric Andersen86349772000-12-18 20:25:50 +0000355 char *status_string;
Erik Andersen3522eb12000-03-12 23:49:18 +0000356
Eric Andersen86349772000-12-18 20:25:50 +0000357 for (job = child->family->job_list->head; job; job = job->next) {
358 if (job->running_progs == job->stopped_progs)
359 status_string = "Stopped";
Erik Andersen161220c2000-03-16 08:12:48 +0000360 else
Eric Andersen86349772000-12-18 20:25:50 +0000361 status_string = "Running";
Erik Andersen161220c2000-03-16 08:12:48 +0000362
Eric Andersen86349772000-12-18 20:25:50 +0000363 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
Erik Andersen161220c2000-03-16 08:12:48 +0000364 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000365 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000366}
367
368
369/* built-in 'pwd' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000370static int builtin_pwd(struct child_prog *dummy)
Erik Andersen3522eb12000-03-12 23:49:18 +0000371{
Eric Andersencfa88ec2001-05-11 18:08:16 +0000372 cwd = xgetcwd((char *)cwd);
Eric Andersen5f265b72001-05-11 16:58:46 +0000373 if (!cwd)
374 cwd = unknown;
Matt Kraai59df6f72001-05-16 14:21:09 +0000375 puts(cwd);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000376 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000377}
378
Erik Andersen6273f652000-03-17 01:12:41 +0000379/* built-in 'export VAR=value' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000380static int builtin_export(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000381{
Erik Andersen161220c2000-03-16 08:12:48 +0000382 int res;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000383 char *v = child->argv[1];
Erik Andersen3522eb12000-03-12 23:49:18 +0000384
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000385 if (v == NULL) {
Eric Andersen84e229c2001-03-29 22:48:33 +0000386 char **e;
387 for (e = environ; *e; e++) {
Matt Kraai59df6f72001-05-16 14:21:09 +0000388 puts(*e);
Eric Andersen84e229c2001-03-29 22:48:33 +0000389 }
Matt Kraai2129f972001-04-04 17:50:04 +0000390 return 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000391 }
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000392 res = putenv(v);
Erik Andersen161220c2000-03-16 08:12:48 +0000393 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000394 fprintf(stderr, "export: %m\n");
Eric Andersen004015e2001-05-21 20:30:51 +0000395#ifdef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000396 if (strncmp(v, "PS1=", 4)==0)
397 PS1 = getenv("PS1");
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000398#endif
Eric Andersene5dfced2001-04-09 22:48:12 +0000399
400#ifdef BB_LOCALE_SUPPORT
Mark Whitley1c6581a2001-03-27 16:35:16 +0000401 if(strncmp(v, "LC_ALL=", 7)==0)
402 setlocale(LC_ALL, getenv("LC_ALL"));
Mark Whitleya82a0032001-03-27 17:07:15 +0000403 if(strncmp(v, "LC_CTYPE=", 9)==0)
Mark Whitley1c6581a2001-03-27 16:35:16 +0000404 setlocale(LC_CTYPE, getenv("LC_CTYPE"));
Eric Andersene5dfced2001-04-09 22:48:12 +0000405#endif
Mark Whitley1c6581a2001-03-27 16:35:16 +0000406
Erik Andersen161220c2000-03-16 08:12:48 +0000407 return (res);
Erik Andersen3522eb12000-03-12 23:49:18 +0000408}
409
Eric Andersenb54833c2000-07-03 23:56:26 +0000410/* built-in 'read VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000411static int builtin_read(struct child_prog *child)
Eric Andersenb54833c2000-07-03 23:56:26 +0000412{
413 int res = 0, len, newlen;
414 char *s;
415 char string[MAX_READ];
416
Eric Andersen86349772000-12-18 20:25:50 +0000417 if (child->argv[1]) {
Eric Andersenb54833c2000-07-03 23:56:26 +0000418 /* argument (VAR) given: put "VAR=" into buffer */
Eric Andersen86349772000-12-18 20:25:50 +0000419 strcpy(string, child->argv[1]);
Eric Andersenb54833c2000-07-03 23:56:26 +0000420 len = strlen(string);
421 string[len++] = '=';
422 string[len] = '\0';
423 fgets(&string[len], sizeof(string) - len, stdin); /* read string */
424 newlen = strlen(string);
425 if(newlen > len)
426 string[--newlen] = '\0'; /* chomp trailing newline */
427 /*
428 ** string should now contain "VAR=<value>"
429 ** copy it (putenv() won't do that, so we must make sure
430 ** the string resides in a static buffer!)
431 */
432 res = -1;
433 if((s = strdup(string)))
434 res = putenv(s);
435 if (res)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000436 fprintf(stderr, "read: %m\n");
Eric Andersenb54833c2000-07-03 23:56:26 +0000437 }
438 else
439 fgets(string, sizeof(string), stdin);
440
441 return (res);
442}
443
Erik Andersen3522eb12000-03-12 23:49:18 +0000444/* Built-in '.' handler (read-in and execute commands from file) */
Eric Andersen86349772000-12-18 20:25:50 +0000445static int builtin_source(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000446{
Erik Andersen161220c2000-03-16 08:12:48 +0000447 FILE *input;
448 int status;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000449 int fd;
Erik Andersen3522eb12000-03-12 23:49:18 +0000450
Eric Andersen86349772000-12-18 20:25:50 +0000451 if (child->argv[1] == NULL)
Matt Kraai3e856ce2000-12-01 02:55:13 +0000452 return EXIT_FAILURE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000453
Eric Andersen86349772000-12-18 20:25:50 +0000454 input = fopen(child->argv[1], "r");
Erik Andersen161220c2000-03-16 08:12:48 +0000455 if (!input) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000456 printf( "Couldn't open file '%s'\n", child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000457 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000458 }
Erik Andersend75af992000-03-16 08:09:09 +0000459
Eric Andersen8ea28be2001-01-05 20:58:22 +0000460 fd=fileno(input);
461 mark_open(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000462 /* Now run the file */
463 status = busy_loop(input);
Matt Kraaidd450a02000-09-13 03:43:36 +0000464 fclose(input);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000465 mark_closed(fd);
Erik Andersen161220c2000-03-16 08:12:48 +0000466 return (status);
Erik Andersen3522eb12000-03-12 23:49:18 +0000467}
468
469/* built-in 'unset VAR' handler */
Eric Andersen86349772000-12-18 20:25:50 +0000470static int builtin_unset(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +0000471{
Eric Andersen86349772000-12-18 20:25:50 +0000472 if (child->argv[1] == NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +0000473 printf( "unset: parameter required.\n");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000474 return EXIT_FAILURE;
Erik Andersen161220c2000-03-16 08:12:48 +0000475 }
Eric Andersen86349772000-12-18 20:25:50 +0000476 unsetenv(child->argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000477 return EXIT_SUCCESS;
Erik Andersen3522eb12000-03-12 23:49:18 +0000478}
479
Eric Andersen8ea28be2001-01-05 20:58:22 +0000480static void mark_open(int fd)
481{
482 struct close_me *new = xmalloc(sizeof(struct close_me));
483 new->fd = fd;
484 new->next = close_me_head;
485 close_me_head = new;
486}
487
488static void mark_closed(int fd)
489{
490 struct close_me *tmp;
491 if (close_me_head == NULL || close_me_head->fd != fd)
492 error_msg_and_die("corrupt close_me");
493 tmp = close_me_head;
494 close_me_head = close_me_head->next;
495 free(tmp);
496}
497
498static void close_all()
499{
Eric Andersen702ec592001-03-06 22:17:29 +0000500 struct close_me *c, *tmp;
501 for (c=close_me_head; c; c=tmp) {
502 close(c->fd);
503 tmp=c->next;
504 free(c);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000505 }
506 close_me_head = NULL;
507}
508
509
Erik Andersen3522eb12000-03-12 23:49:18 +0000510/* free up all memory from a job */
Eric Andersen86349772000-12-18 20:25:50 +0000511static void free_job(struct job *cmd)
Erik Andersen3522eb12000-03-12 23:49:18 +0000512{
Erik Andersen161220c2000-03-16 08:12:48 +0000513 int i;
Mark Whitley44a99142001-03-14 17:26:37 +0000514 struct jobset *keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000515
Eric Andersen86349772000-12-18 20:25:50 +0000516 for (i = 0; i < cmd->num_progs; i++) {
Erik Andersen161220c2000-03-16 08:12:48 +0000517 free(cmd->progs[i].argv);
Eric Andersen86349772000-12-18 20:25:50 +0000518 if (cmd->progs[i].redirects)
519 free(cmd->progs[i].redirects);
Erik Andersen161220c2000-03-16 08:12:48 +0000520 }
Mark Whitley44a99142001-03-14 17:26:37 +0000521 if (cmd->progs)
522 free(cmd->progs);
Erik Andersen161220c2000-03-16 08:12:48 +0000523 if (cmd->text)
524 free(cmd->text);
Mark Whitley44a99142001-03-14 17:26:37 +0000525 if (cmd->cmdbuf)
526 free(cmd->cmdbuf);
527 keep = cmd->job_list;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000528 memset(cmd, 0, sizeof(struct job));
Mark Whitley44a99142001-03-14 17:26:37 +0000529 cmd->job_list = keep;
Erik Andersen3522eb12000-03-12 23:49:18 +0000530}
531
Eric Andersen1ca20a72001-03-21 07:34:27 +0000532/* remove a job from a jobset */
533static void remove_job(struct jobset *j_list, struct job *job)
Erik Andersen3522eb12000-03-12 23:49:18 +0000534{
Eric Andersen86349772000-12-18 20:25:50 +0000535 struct job *prevjob;
Erik Andersen3522eb12000-03-12 23:49:18 +0000536
Eric Andersen86349772000-12-18 20:25:50 +0000537 free_job(job);
Eric Andersen1ca20a72001-03-21 07:34:27 +0000538 if (job == j_list->head) {
539 j_list->head = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000540 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000541 prevjob = j_list->head;
Eric Andersen86349772000-12-18 20:25:50 +0000542 while (prevjob->next != job)
543 prevjob = prevjob->next;
544 prevjob->next = job->next;
Erik Andersen161220c2000-03-16 08:12:48 +0000545 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000546
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000547 if (j_list->head)
548 last_jobid = j_list->head->jobid;
549 else
550 last_jobid = 0;
551
Erik Andersen161220c2000-03-16 08:12:48 +0000552 free(job);
Erik Andersen3522eb12000-03-12 23:49:18 +0000553}
554
555/* Checks to see if any background processes have exited -- if they
556 have, figure out why and see if a job has completed */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000557static void checkjobs(struct jobset *j_list)
Erik Andersen3522eb12000-03-12 23:49:18 +0000558{
Erik Andersen161220c2000-03-16 08:12:48 +0000559 struct job *job;
560 pid_t childpid;
561 int status;
Eric Andersen86349772000-12-18 20:25:50 +0000562 int prognum = 0;
Erik Andersend75af992000-03-16 08:09:09 +0000563
Erik Andersen161220c2000-03-16 08:12:48 +0000564 while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000565 for (job = j_list->head; job; job = job->next) {
Eric Andersen86349772000-12-18 20:25:50 +0000566 prognum = 0;
567 while (prognum < job->num_progs &&
568 job->progs[prognum].pid != childpid) prognum++;
569 if (prognum < job->num_progs)
Erik Andersen161220c2000-03-16 08:12:48 +0000570 break;
571 }
572
Eric Andersena1d187a2000-07-17 19:14:41 +0000573 /* This happens on backticked commands */
574 if(job==NULL)
575 return;
576
Erik Andersen161220c2000-03-16 08:12:48 +0000577 if (WIFEXITED(status) || WIFSIGNALED(status)) {
578 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +0000579 job->running_progs--;
580 job->progs[prognum].pid = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000581
Eric Andersen86349772000-12-18 20:25:50 +0000582 if (!job->running_progs) {
583 printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text);
Eric Andersen8a646dd2001-06-21 16:38:11 +0000584 last_jobid=0;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000585 remove_job(j_list, job);
Erik Andersen161220c2000-03-16 08:12:48 +0000586 }
587 } else {
588 /* child stopped */
Eric Andersen86349772000-12-18 20:25:50 +0000589 job->stopped_progs++;
590 job->progs[prognum].is_stopped = 1;
Erik Andersen161220c2000-03-16 08:12:48 +0000591
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000592#if 0
593 /* Printing this stuff is a pain, since it tends to
594 * overwrite the prompt an inconveinient moments. So
595 * don't do that. */
Eric Andersen86349772000-12-18 20:25:50 +0000596 if (job->stopped_progs == job->num_progs) {
597 printf(JOB_STATUS_FORMAT, job->jobid, "Stopped",
Erik Andersen161220c2000-03-16 08:12:48 +0000598 job->text);
599 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000600#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000601 }
Erik Andersend75af992000-03-16 08:09:09 +0000602 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000603
Erik Andersen161220c2000-03-16 08:12:48 +0000604 if (childpid == -1 && errno != ECHILD)
Matt Kraaia9819b22000-12-22 01:48:07 +0000605 perror_msg("waitpid");
Erik Andersen3522eb12000-03-12 23:49:18 +0000606}
607
Eric Andersene9f07fb2000-12-21 18:31:36 +0000608/* squirrel != NULL means we squirrel away copies of stdin, stdout,
609 * and stderr if they are redirected. */
610static int setup_redirects(struct child_prog *prog, int squirrel[])
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000611{
612 int i;
613 int openfd;
614 int mode = O_RDONLY;
Eric Andersen86349772000-12-18 20:25:50 +0000615 struct redir_struct *redir = prog->redirects;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000616
Eric Andersen86349772000-12-18 20:25:50 +0000617 for (i = 0; i < prog->num_redirects; i++, redir++) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000618 switch (redir->type) {
619 case REDIRECT_INPUT:
620 mode = O_RDONLY;
621 break;
622 case REDIRECT_OVERWRITE:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000623 mode = O_WRONLY | O_CREAT | O_TRUNC;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000624 break;
625 case REDIRECT_APPEND:
Eric Andersen46f0beb2000-11-14 21:59:22 +0000626 mode = O_WRONLY | O_CREAT | O_APPEND;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000627 break;
628 }
629
630 openfd = open(redir->filename, mode, 0666);
631 if (openfd < 0) {
632 /* this could get lost if stderr has been redirected, but
633 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000634 perror_msg("error opening %s", redir->filename);
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000635 return 1;
636 }
637
638 if (openfd != redir->fd) {
Eric Andersene9f07fb2000-12-21 18:31:36 +0000639 if (squirrel && redir->fd < 3) {
640 squirrel[redir->fd] = dup(redir->fd);
641 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000642 dup2(openfd, redir->fd);
643 close(openfd);
644 }
645 }
646
647 return 0;
648}
649
Eric Andersene9f07fb2000-12-21 18:31:36 +0000650static void restore_redirects(int squirrel[])
651{
652 int i, fd;
653 for (i=0; i<3; i++) {
654 fd = squirrel[i];
655 if (fd != -1) {
656 /* No error checking. I sure wouldn't know what
657 * to do with an error if I found one! */
658 dup2(fd, i);
659 close(fd);
660 }
661 }
662}
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000663
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000664static inline void cmdedit_set_initial_prompt(void)
Eric Andersen22332fd2001-01-30 23:40:39 +0000665{
Eric Andersen004015e2001-05-21 20:30:51 +0000666#ifndef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000667 PS1 = NULL;
Eric Andersen22332fd2001-01-30 23:40:39 +0000668#else
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000669 PS1 = getenv("PS1");
Eric Andersene5dfced2001-04-09 22:48:12 +0000670 if(PS1==0)
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000671 PS1 = "\\w \\$ ";
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000672#endif
Eric Andersen09acc062001-01-04 11:10:38 +0000673}
674
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000675static inline void setup_prompt_string(char **prompt_str)
676{
Eric Andersen004015e2001-05-21 20:30:51 +0000677#ifndef BB_FEATURE_SH_FANCY_PROMPT
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000678 /* Set up the prompt */
679 if (shell_context == 0) {
680 if (PS1)
681 free(PS1);
682 PS1=xmalloc(strlen(cwd)+4);
683 sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# ");
684 *prompt_str = PS1;
685 } else {
686 *prompt_str = PS2;
687 }
688#else
689 *prompt_str = (shell_context==0)? PS1 : PS2;
690#endif
691}
Eric Andersen22332fd2001-01-30 23:40:39 +0000692
Eric Andersen09acc062001-01-04 11:10:38 +0000693static int get_command(FILE * source, char *command)
694{
695 char *prompt_str;
Eric Andersen8ea28be2001-01-05 20:58:22 +0000696
Eric Andersen1c314ad2000-06-28 16:56:25 +0000697 if (source == NULL) {
698 if (local_pending_command) {
699 /* a command specified (-c option): return it & mark it done */
700 strcpy(command, local_pending_command);
701 free(local_pending_command);
702 local_pending_command = NULL;
703 return 0;
704 }
705 return 1;
706 }
707
Erik Andersen161220c2000-03-16 08:12:48 +0000708 if (source == stdin) {
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000709 setup_prompt_string(&prompt_str);
Eric Andersen8ea28be2001-01-05 20:58:22 +0000710
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000711#ifdef BB_FEATURE_COMMAND_EDITING
Eric Andersen501c88b2000-07-28 15:14:45 +0000712 /*
713 ** enable command line editing only while a command line
714 ** is actually being read; otherwise, we'll end up bequeathing
715 ** atexit() handlers and other unwanted stuff to our
716 ** child processes (rob@sysgo.de)
717 */
Eric Andersen86349772000-12-18 20:25:50 +0000718 cmdedit_read_input(prompt_str, command);
Eric Andersen501c88b2000-07-28 15:14:45 +0000719 cmdedit_terminate();
Erik Andersen161220c2000-03-16 08:12:48 +0000720 return 0;
Erik Andersenc7c634b2000-03-19 05:28:55 +0000721#else
Eric Andersen8ea28be2001-01-05 20:58:22 +0000722 fputs(prompt_str, stdout);
Erik Andersend75af992000-03-16 08:09:09 +0000723#endif
Erik Andersen161220c2000-03-16 08:12:48 +0000724 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000725
Erik Andersen161220c2000-03-16 08:12:48 +0000726 if (!fgets(command, BUFSIZ - 2, source)) {
727 if (source == stdin)
728 printf("\n");
729 return 1;
730 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000731
Erik Andersen161220c2000-03-16 08:12:48 +0000732 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +0000733}
734
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000735static char* itoa(register int i)
736{
737 static char a[7]; /* Max 7 ints */
738 register char *b = a + sizeof(a) - 1;
739 int sign = (i < 0);
740
741 if (sign)
742 i = -i;
743 *b = 0;
744 do
745 {
746 *--b = '0' + (i % 10);
747 i /= 10;
748 }
749 while (i);
750 if (sign)
751 *--b = '-';
752 return b;
753}
754
755char * strsep_space( char *string, int * ix)
756{
757 char *token, *begin;
758
759 begin = string;
760
761 /* Short circuit the trivial case */
762 if ( !string || ! string[*ix])
763 return NULL;
764
765 /* Find the end of the token. */
766 while( string && string[*ix] && !isspace(string[*ix]) ) {
767 (*ix)++;
768 }
769
770 /* Find the end of any whitespace trailing behind
771 * the token and let that be part of the token */
772 while( string && string[*ix] && isspace(string[*ix]) ) {
773 (*ix)++;
774 }
775
776 if (! string && *ix==0) {
777 /* Nothing useful was found */
778 return NULL;
779 }
780
781 token = xmalloc(*ix+1);
782 token[*ix] = '\0';
783 strncpy(token, string, *ix);
784
785 return token;
786}
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000787
Eric Andersenca604592001-03-08 17:17:13 +0000788static int expand_arguments(char *command)
789{
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000790 int total_length=0, length, i, retval, ix = 0;
791 expand_t expand_result;
792 char *tmpcmd, *cmd, *cmd_copy;
793 char *src, *dst, *var;
794 const char *out_of_space = "out of space during expansion";
795 int flags = GLOB_NOCHECK
796#ifdef GLOB_BRACE
797 | GLOB_BRACE
798#endif
799#ifdef GLOB_TILDE
800 | GLOB_TILDE
801#endif
802 ;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000803
Eric Andersenca604592001-03-08 17:17:13 +0000804 /* get rid of the terminating \n */
805 chomp(command);
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000806
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000807 /* Fix up escape sequences to be the Real Thing(tm) */
Eric Andersen1ca20a72001-03-21 07:34:27 +0000808 while( command && command[ix]) {
809 if (command[ix] == '\\') {
Eric Andersene5dfced2001-04-09 22:48:12 +0000810 const char *tmp = command+ix+1;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000811 command[ix] = process_escape_sequence( &tmp );
812 memmove(command+ix + 1, tmp, strlen(tmp)+1);
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000813 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000814 ix++;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +0000815 }
Eric Andersen4b6b5e42001-06-27 04:30:11 +0000816 /* Use glob and then fixup environment variables and such */
817
818 /* It turns out that glob is very stupid. We have to feed it one word at a
819 * time since it can't cope with a full string. Here we convert command
820 * (char*) into cmd (char**, one word per string) */
821
822 /* We need a clean copy, so strsep can mess up the copy while
823 * we write stuff into the original (in a minute) */
824 cmd = cmd_copy = strdup(command);
825 *command = '\0';
826 for (ix = 0, tmpcmd = cmd;
827 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
828 if (*tmpcmd == '\0')
829 break;
830 /* we need to trim() the result for glob! */
831 trim(tmpcmd);
832 retval = glob(tmpcmd, flags, NULL, &expand_result);
833 free(tmpcmd); /* Free mem allocated by strsep_space */
834 if (retval == GLOB_NOSPACE) {
835 /* Mem may have been allocated... */
836 globfree (&expand_result);
837 error_msg(out_of_space);
838 return FALSE;
839 } else if (retval != 0) {
840 /* Some other error. GLOB_NOMATCH shouldn't
841 * happen because of the GLOB_NOCHECK flag in
842 * the glob call. */
843 error_msg("syntax error");
844 return FALSE;
845 } else {
846 /* Convert from char** (one word per string) to a simple char*,
847 * but don't overflow command which is BUFSIZ in length */
848 for (i=0; i < expand_result.gl_pathc; i++) {
849 length=strlen(expand_result.gl_pathv[i]);
850 if (total_length+length+1 >= BUFSIZ) {
851 error_msg(out_of_space);
852 return FALSE;
853 }
854 strcat(command+total_length, " ");
855 total_length+=1;
856 strcat(command+total_length, expand_result.gl_pathv[i]);
857 total_length+=length;
858 }
859 globfree (&expand_result);
860 }
861 }
862 free(cmd_copy);
863 trim(command);
864
865 /* Now do the shell variable substitutions which
866 * wordexp can't do for us, namely $? and $! */
867 src = command;
868 while((dst = strchr(src,'$')) != NULL){
869 var = NULL;
870 switch(*(dst+1)) {
871 case '?':
872 var = itoa(last_return_code);
873 break;
874 case '!':
875 if (last_bg_pid==-1)
876 *(var)='\0';
877 else
878 var = itoa(last_bg_pid);
879 break;
880 /* Everything else like $$, $#, $[0-9], etc. should all be
881 * expanded by wordexp(), so we can in theory skip that stuff
882 * here, but just to be on the safe side (i.e., since uClibc
883 * wordexp doesn't do this stuff yet), lets leave it in for
884 * now. */
885 case '$':
886 var = itoa(getpid());
887 break;
888 case '#':
889 var = itoa(argc-1);
890 break;
891 case '0':case '1':case '2':case '3':case '4':
892 case '5':case '6':case '7':case '8':case '9':
893 {
894 int ixx=*(dst + 1)-48;
895 if (ixx >= argc) {
896 var='\0';
897 } else {
898 var = argv[ixx];
899 }
900 }
901 break;
902
903 }
904 if (var) {
905 /* a single character construction was found, and
906 * already handled in the case statement */
907 src=dst+2;
908 } else {
909 /* Looks like an environment variable */
910 char delim_hold;
911 int num_skip_chars=0;
912 int dstlen = strlen(dst);
913 /* Is this a ${foo} type variable? */
914 if (dstlen >=2 && *(dst+1) == '{') {
915 src=strchr(dst+1, '}');
916 num_skip_chars=1;
917 } else {
918 src=dst+1;
919 while(isalnum(*src) || *src=='_') src++;
920 }
921 if (src == NULL) {
922 src = dst+dstlen;
923 }
924 delim_hold=*src;
925 *src='\0'; /* temporary */
926 var = getenv(dst + 1 + num_skip_chars);
927 *src=delim_hold;
928 src += num_skip_chars;
929 }
930 if (var == NULL) {
931 /* Seems we got an un-expandable variable. So delete it. */
932 var = "";
933 }
934 {
935 int subst_len = strlen(var);
936 int trail_len = strlen(src);
937 if (dst+subst_len+trail_len >= command+BUFSIZ) {
938 error_msg(out_of_space);
939 return FALSE;
940 }
941 /* Move stuff to the end of the string to accommodate
942 * filling the created gap with the new stuff */
943 memmove(dst+subst_len, src, trail_len+1);
944 /* Now copy in the new stuff */
945 memcpy(dst, var, subst_len);
946 src = dst+subst_len;
947 }
948 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000949
Eric Andersenca604592001-03-08 17:17:13 +0000950 return TRUE;
Erik Andersen3522eb12000-03-12 23:49:18 +0000951}
952
Eric Andersen86349772000-12-18 20:25:50 +0000953/* Return cmd->num_progs as 0 if no command is present (e.g. an empty
954 line). If a valid command is found, command_ptr is set to point to
Erik Andersen3522eb12000-03-12 23:49:18 +0000955 the beginning of the next command (if the original command had more
956 then one job associated with it) or NULL if no more commands are
957 present. */
Eric Andersen86349772000-12-18 20:25:50 +0000958static int parse_command(char **command_ptr, struct job *job, int *inbg)
Erik Andersen3522eb12000-03-12 23:49:18 +0000959{
Erik Andersen161220c2000-03-16 08:12:48 +0000960 char *command;
Eric Andersen86349772000-12-18 20:25:50 +0000961 char *return_command = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +0000962 char *src, *buf, *chptr;
Eric Andersen6a99aaf2000-07-27 00:15:20 +0000963 int argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000964 int done = 0;
Eric Andersen86349772000-12-18 20:25:50 +0000965 int argv_alloced;
Matt Kraaibe66ad32001-04-12 15:42:17 +0000966 int i, saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +0000967 char quote = '\0';
968 int count;
Eric Andersen86349772000-12-18 20:25:50 +0000969 struct child_prog *prog;
Erik Andersen3522eb12000-03-12 23:49:18 +0000970
Erik Andersen161220c2000-03-16 08:12:48 +0000971 /* skip leading white space */
Eric Andersen86349772000-12-18 20:25:50 +0000972 while (**command_ptr && isspace(**command_ptr))
973 (*command_ptr)++;
Erik Andersen3522eb12000-03-12 23:49:18 +0000974
Erik Andersen161220c2000-03-16 08:12:48 +0000975 /* this handles empty lines or leading '#' characters */
Eric Andersen86349772000-12-18 20:25:50 +0000976 if (!**command_ptr || (**command_ptr == '#')) {
977 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +0000978 return 0;
979 }
Erik Andersen3522eb12000-03-12 23:49:18 +0000980
Eric Andersen86349772000-12-18 20:25:50 +0000981 *inbg = 0;
982 job->num_progs = 1;
Eric Andersenec10b9d2000-07-14 01:13:11 +0000983 job->progs = xmalloc(sizeof(*job->progs));
Erik Andersen3522eb12000-03-12 23:49:18 +0000984
Erik Andersen161220c2000-03-16 08:12:48 +0000985 /* We set the argv elements to point inside of this string. The
Eric Andersen86349772000-12-18 20:25:50 +0000986 memory is freed by free_job(). Allocate twice the original
Eric Andersenb54833c2000-07-03 23:56:26 +0000987 length in case we need to quote every single character.
Erik Andersen3522eb12000-03-12 23:49:18 +0000988
Erik Andersen161220c2000-03-16 08:12:48 +0000989 Getting clean memory relieves us of the task of NULL
990 terminating things and makes the rest of this look a bit
991 cleaner (though it is, admittedly, a tad less efficient) */
Eric Andersen86349772000-12-18 20:25:50 +0000992 job->cmdbuf = command = xcalloc(2*strlen(*command_ptr) + 1, sizeof(char));
Erik Andersen161220c2000-03-16 08:12:48 +0000993 job->text = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +0000994
Erik Andersen161220c2000-03-16 08:12:48 +0000995 prog = job->progs;
Eric Andersen86349772000-12-18 20:25:50 +0000996 prog->num_redirects = 0;
997 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +0000998 prog->is_stopped = 0;
999 prog->family = job;
Erik Andersen3522eb12000-03-12 23:49:18 +00001000
Eric Andersen86349772000-12-18 20:25:50 +00001001 argv_alloced = 5;
1002 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
1003 prog->argv[0] = job->cmdbuf;
Erik Andersen3522eb12000-03-12 23:49:18 +00001004
Erik Andersen161220c2000-03-16 08:12:48 +00001005 buf = command;
Eric Andersen86349772000-12-18 20:25:50 +00001006 src = *command_ptr;
Erik Andersen161220c2000-03-16 08:12:48 +00001007 while (*src && !done) {
1008 if (quote == *src) {
1009 quote = '\0';
1010 } else if (quote) {
1011 if (*src == '\\') {
1012 src++;
1013 if (!*src) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001014 error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001015 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001016 return 1;
1017 }
1018
1019 /* in shell, "\'" should yield \' */
Eric Andersenb2356f62000-12-11 19:14:40 +00001020 if (*src != quote) {
Erik Andersen161220c2000-03-16 08:12:48 +00001021 *buf++ = '\\';
Eric Andersenb2356f62000-12-11 19:14:40 +00001022 *buf++ = '\\';
1023 }
Erik Andersen161220c2000-03-16 08:12:48 +00001024 } else if (*src == '*' || *src == '?' || *src == '[' ||
1025 *src == ']') *buf++ = '\\';
1026 *buf++ = *src;
1027 } else if (isspace(*src)) {
Matt Kraaibe66ad32001-04-12 15:42:17 +00001028 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001029 buf++, argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001030 /* +1 here leaves room for the NULL which ends argv */
Eric Andersen86349772000-12-18 20:25:50 +00001031 if ((argc_l + 1) == argv_alloced) {
1032 argv_alloced += 5;
Matt Kraaib8907522000-09-13 02:08:21 +00001033 prog->argv = xrealloc(prog->argv,
1034 sizeof(*prog->argv) *
Eric Andersen86349772000-12-18 20:25:50 +00001035 argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001036 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001037 prog->argv[argc_l] = buf;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001038 saw_quote = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001039 }
1040 } else
1041 switch (*src) {
1042 case '"':
1043 case '\'':
1044 quote = *src;
Matt Kraaibe66ad32001-04-12 15:42:17 +00001045 saw_quote = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001046 break;
1047
1048 case '#': /* comment */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001049 if (*(src-1)== '$')
1050 *buf++ = *src;
1051 else
1052 done = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001053 break;
1054
Eric Andersen86349772000-12-18 20:25:50 +00001055 case '>': /* redirects */
Erik Andersen161220c2000-03-16 08:12:48 +00001056 case '<':
Eric Andersen86349772000-12-18 20:25:50 +00001057 i = prog->num_redirects++;
1058 prog->redirects = xrealloc(prog->redirects,
1059 sizeof(*prog->redirects) *
Matt Kraaib8907522000-09-13 02:08:21 +00001060 (i + 1));
Erik Andersen161220c2000-03-16 08:12:48 +00001061
Eric Andersen86349772000-12-18 20:25:50 +00001062 prog->redirects[i].fd = -1;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001063 if (buf != prog->argv[argc_l]) {
Erik Andersen161220c2000-03-16 08:12:48 +00001064 /* the stuff before this character may be the file number
1065 being redirected */
Eric Andersen86349772000-12-18 20:25:50 +00001066 prog->redirects[i].fd =
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001067 strtol(prog->argv[argc_l], &chptr, 10);
Erik Andersen161220c2000-03-16 08:12:48 +00001068
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001069 if (*chptr && *prog->argv[argc_l]) {
1070 buf++, argc_l++;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001071 prog->argv[argc_l] = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001072 }
1073 }
1074
Eric Andersen86349772000-12-18 20:25:50 +00001075 if (prog->redirects[i].fd == -1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001076 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001077 prog->redirects[i].fd = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001078 else
Eric Andersen86349772000-12-18 20:25:50 +00001079 prog->redirects[i].fd = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001080 }
1081
1082 if (*src++ == '>') {
1083 if (*src == '>')
Eric Andersen86349772000-12-18 20:25:50 +00001084 prog->redirects[i].type =
Erik Andersen161220c2000-03-16 08:12:48 +00001085 REDIRECT_APPEND, src++;
1086 else
Eric Andersen86349772000-12-18 20:25:50 +00001087 prog->redirects[i].type = REDIRECT_OVERWRITE;
Erik Andersen161220c2000-03-16 08:12:48 +00001088 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001089 prog->redirects[i].type = REDIRECT_INPUT;
Erik Andersen161220c2000-03-16 08:12:48 +00001090 }
1091
1092 /* This isn't POSIX sh compliant. Oh well. */
1093 chptr = src;
1094 while (isspace(*chptr))
1095 chptr++;
1096
1097 if (!*chptr) {
Mark Whitley44a99142001-03-14 17:26:37 +00001098 error_msg("file name expected after %c", *(src-1));
Eric Andersen86349772000-12-18 20:25:50 +00001099 free_job(job);
1100 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001101 return 1;
1102 }
1103
Eric Andersen86349772000-12-18 20:25:50 +00001104 prog->redirects[i].filename = buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001105 while (*chptr && !isspace(*chptr))
1106 *buf++ = *chptr++;
1107
1108 src = chptr - 1; /* we src++ later */
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001109 prog->argv[argc_l] = ++buf;
Erik Andersen161220c2000-03-16 08:12:48 +00001110 break;
1111
1112 case '|': /* pipe */
1113 /* finish this command */
Matt Kraaibe66ad32001-04-12 15:42:17 +00001114 if (*prog->argv[argc_l] || saw_quote)
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001115 argc_l++;
1116 if (!argc_l) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001117 error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001118 free_job(job);
1119 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001120 return 1;
1121 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001122 prog->argv[argc_l] = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001123
1124 /* and start the next */
Eric Andersen86349772000-12-18 20:25:50 +00001125 job->num_progs++;
Matt Kraaib8907522000-09-13 02:08:21 +00001126 job->progs = xrealloc(job->progs,
Eric Andersen86349772000-12-18 20:25:50 +00001127 sizeof(*job->progs) * job->num_progs);
1128 prog = job->progs + (job->num_progs - 1);
1129 prog->num_redirects = 0;
1130 prog->redirects = NULL;
Eric Andersen86349772000-12-18 20:25:50 +00001131 prog->is_stopped = 0;
1132 prog->family = job;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001133 argc_l = 0;
Erik Andersen161220c2000-03-16 08:12:48 +00001134
Eric Andersen86349772000-12-18 20:25:50 +00001135 argv_alloced = 5;
1136 prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced);
Erik Andersen161220c2000-03-16 08:12:48 +00001137 prog->argv[0] = ++buf;
1138
1139 src++;
1140 while (*src && isspace(*src))
1141 src++;
1142
1143 if (!*src) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001144 error_msg("empty command in pipe");
Eric Andersen86349772000-12-18 20:25:50 +00001145 free_job(job);
1146 job->num_progs=0;
Erik Andersen161220c2000-03-16 08:12:48 +00001147 return 1;
1148 }
1149 src--; /* we'll ++ it at the end of the loop */
1150
1151 break;
1152
1153 case '&': /* background */
Eric Andersen86349772000-12-18 20:25:50 +00001154 *inbg = 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001155 case ';': /* multiple commands */
1156 done = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001157 return_command = *command_ptr + (src - *command_ptr) + 1;
Erik Andersen161220c2000-03-16 08:12:48 +00001158 break;
1159
Matt Kraai131241f2000-09-14 00:43:20 +00001160 case '\\':
1161 src++;
1162 if (!*src) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001163 error_msg("character expected after \\");
Eric Andersen86349772000-12-18 20:25:50 +00001164 free_job(job);
Matt Kraai131241f2000-09-14 00:43:20 +00001165 return 1;
1166 }
1167 if (*src == '*' || *src == '[' || *src == ']'
1168 || *src == '?') *buf++ = '\\';
1169 /* fallthrough */
Erik Andersen161220c2000-03-16 08:12:48 +00001170 default:
1171 *buf++ = *src;
1172 }
1173
Erik Andersend75af992000-03-16 08:09:09 +00001174 src++;
Erik Andersen161220c2000-03-16 08:12:48 +00001175 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001176
Matt Kraaibe66ad32001-04-12 15:42:17 +00001177 if (*prog->argv[argc_l] || saw_quote) {
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001178 argc_l++;
Erik Andersen161220c2000-03-16 08:12:48 +00001179 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001180 if (!argc_l) {
Eric Andersen86349772000-12-18 20:25:50 +00001181 free_job(job);
Erik Andersen161220c2000-03-16 08:12:48 +00001182 return 0;
1183 }
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001184 prog->argv[argc_l] = NULL;
Erik Andersen3522eb12000-03-12 23:49:18 +00001185
Eric Andersen86349772000-12-18 20:25:50 +00001186 if (!return_command) {
1187 job->text = xmalloc(strlen(*command_ptr) + 1);
1188 strcpy(job->text, *command_ptr);
Erik Andersen161220c2000-03-16 08:12:48 +00001189 } else {
1190 /* This leaves any trailing spaces, which is a bit sloppy */
Eric Andersen86349772000-12-18 20:25:50 +00001191 count = return_command - *command_ptr;
Eric Andersenec10b9d2000-07-14 01:13:11 +00001192 job->text = xmalloc(count + 1);
Eric Andersen86349772000-12-18 20:25:50 +00001193 strncpy(job->text, *command_ptr, count);
Erik Andersen161220c2000-03-16 08:12:48 +00001194 job->text[count] = '\0';
1195 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001196
Eric Andersen86349772000-12-18 20:25:50 +00001197 *command_ptr = return_command;
Eric Andersen46f0beb2000-11-14 21:59:22 +00001198
Erik Andersend75af992000-03-16 08:09:09 +00001199 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001200}
1201
Eric Andersen86349772000-12-18 20:25:50 +00001202/* Run the child_prog, no matter what kind of command it uses.
1203 */
1204static int pseudo_exec(struct child_prog *child)
Erik Andersen3522eb12000-03-12 23:49:18 +00001205{
Eric Andersen86349772000-12-18 20:25:50 +00001206 struct built_in_command *x;
Eric Andersenb54833c2000-07-03 23:56:26 +00001207#ifdef BB_FEATURE_SH_STANDALONE_SHELL
Eric Andersenaf4ac772001-02-01 22:43:49 +00001208 char *name;
Erik Andersenbcd61772000-05-13 06:33:19 +00001209#endif
Erik Andersen3522eb12000-03-12 23:49:18 +00001210
Eric Andersene9f07fb2000-12-21 18:31:36 +00001211 /* Check if the command matches any of the non-forking builtins.
1212 * Depending on context, this might be redundant. But it's
1213 * easier to waste a few CPU cycles than it is to figure out
1214 * if this is one of those cases.
Eric Andersen86349772000-12-18 20:25:50 +00001215 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001216 for (x = bltins; x->cmd; x++) {
1217 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1218 exit(x->function(child));
1219 }
1220 }
1221
1222 /* Check if the command matches any of the forking builtins. */
Eric Andersen86349772000-12-18 20:25:50 +00001223 for (x = bltins_forking; x->cmd; x++) {
1224 if (strcmp(child->argv[0], x->cmd) == 0) {
1225 applet_name=x->cmd;
1226 exit (x->function(child));
1227 }
1228 }
1229#ifdef BB_FEATURE_SH_STANDALONE_SHELL
1230 /* Check if the command matches any busybox internal
1231 * commands ("applets") here. Following discussions from
1232 * November 2000 on busybox@opensource.lineo.com, don't use
1233 * get_last_path_component(). This way explicit (with
1234 * slashes) filenames will never be interpreted as an
1235 * applet, just like with builtins. This way the user can
1236 * override an applet with an explicit filename reference.
1237 * The only downside to this change is that an explicit
1238 * /bin/foo invocation will fork and exec /bin/foo, even if
1239 * /bin/foo is a symlink to busybox.
1240 */
Matt Kraaif2cc2762001-02-01 19:21:20 +00001241 name = child->argv[0];
Eric Andersen86349772000-12-18 20:25:50 +00001242
1243#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
1244 /* If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then
1245 * if you run /bin/cat, it will use BusyBox cat even if
1246 * /bin/cat exists on the filesystem and is _not_ busybox.
1247 * Some systems want this, others do not. Choose wisely. :-)
1248 */
Matt Kraaif2cc2762001-02-01 19:21:20 +00001249 name = get_last_path_component(name);
Eric Andersen86349772000-12-18 20:25:50 +00001250#endif
1251
Eric Andersen67991cf2001-02-14 21:23:06 +00001252 {
Eric Andersen82ab8da2001-03-23 17:06:01 +00001253 char** argv_l=child->argv;
Eric Andersen67991cf2001-02-14 21:23:06 +00001254 int argc_l;
Eric Andersen82ab8da2001-03-23 17:06:01 +00001255 for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
Eric Andersen67991cf2001-02-14 21:23:06 +00001256 optind = 1;
1257 run_applet_by_name(name, argc_l, child->argv);
Eric Andersen86349772000-12-18 20:25:50 +00001258 }
1259#endif
1260
1261 execvp(child->argv[0], child->argv);
Eric Andersen8ea28be2001-01-05 20:58:22 +00001262 perror_msg_and_die("%s", child->argv[0]);
Eric Andersen86349772000-12-18 20:25:50 +00001263}
1264
1265static void insert_job(struct job *newjob, int inbg)
1266{
1267 struct job *thejob;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001268 struct jobset *j_list=newjob->job_list;
Eric Andersen86349772000-12-18 20:25:50 +00001269
1270 /* find the ID for thejob to use */
1271 newjob->jobid = 1;
Eric Andersen1ca20a72001-03-21 07:34:27 +00001272 for (thejob = j_list->head; thejob; thejob = thejob->next)
Eric Andersen86349772000-12-18 20:25:50 +00001273 if (thejob->jobid >= newjob->jobid)
1274 newjob->jobid = thejob->jobid + 1;
1275
1276 /* add thejob to the list of running jobs */
Eric Andersen1ca20a72001-03-21 07:34:27 +00001277 if (!j_list->head) {
1278 thejob = j_list->head = xmalloc(sizeof(*thejob));
Eric Andersen86349772000-12-18 20:25:50 +00001279 } else {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001280 for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */;
Eric Andersen86349772000-12-18 20:25:50 +00001281 thejob->next = xmalloc(sizeof(*thejob));
1282 thejob = thejob->next;
1283 }
1284
1285 *thejob = *newjob; /* physically copy the struct job */
1286 thejob->next = NULL;
1287 thejob->running_progs = thejob->num_progs;
1288 thejob->stopped_progs = 0;
1289
1290 if (inbg) {
1291 /* we don't wait for background thejobs to return -- append it
1292 to the list of backgrounded thejobs and leave it alone */
1293 printf("[%d] %d\n", thejob->jobid,
1294 newjob->progs[newjob->num_progs - 1].pid);
Eric Andersen8a646dd2001-06-21 16:38:11 +00001295 last_jobid = newjob->jobid;
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001296 last_bg_pid=newjob->progs[newjob->num_progs - 1].pid;
Eric Andersen86349772000-12-18 20:25:50 +00001297 } else {
1298 newjob->job_list->fg = thejob;
1299
1300 /* move the new process group into the foreground */
1301 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001302 if (tcsetpgrp(shell_terminal, newjob->pgrp) && errno != ENOTTY)
Matt Kraaia9819b22000-12-22 01:48:07 +00001303 perror_msg("tcsetpgrp");
Eric Andersen86349772000-12-18 20:25:50 +00001304 }
1305}
1306
1307static int run_command(struct job *newjob, int inbg, int outpipe[2])
1308{
1309 /* struct job *thejob; */
1310 int i;
1311 int nextin, nextout;
1312 int pipefds[2]; /* pipefd[0] is for reading */
1313 struct built_in_command *x;
1314 struct child_prog *child;
1315
Eric Andersen6efc48c2000-07-18 08:16:39 +00001316 nextin = 0, nextout = 1;
Eric Andersen86349772000-12-18 20:25:50 +00001317 for (i = 0; i < newjob->num_progs; i++) {
1318 child = & (newjob->progs[i]);
1319
1320 if ((i + 1) < newjob->num_progs) {
1321 if (pipe(pipefds)<0) perror_msg_and_die("pipe");
Erik Andersen161220c2000-03-16 08:12:48 +00001322 nextout = pipefds[1];
1323 } else {
Eric Andersen86349772000-12-18 20:25:50 +00001324 if (outpipe[1]!=-1) {
1325 nextout = outpipe[1];
Eric Andersen6efc48c2000-07-18 08:16:39 +00001326 } else {
1327 nextout = 1;
1328 }
Erik Andersen161220c2000-03-16 08:12:48 +00001329 }
1330
Eric Andersen501c88b2000-07-28 15:14:45 +00001331
Eric Andersene9f07fb2000-12-21 18:31:36 +00001332 /* Check if the command matches any non-forking builtins,
1333 * but only if this is a simple command.
1334 * Non-forking builtins within pipes have to fork anyway,
1335 * and are handled in pseudo_exec. "echo foo | read bar"
1336 * is doomed to failure, and doesn't work on bash, either.
Eric Andersen86349772000-12-18 20:25:50 +00001337 */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001338 if (newjob->num_progs == 1) {
1339 for (x = bltins; x->cmd; x++) {
1340 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1341 int squirrel[] = {-1, -1, -1};
1342 int rcode;
1343 setup_redirects(child, squirrel);
1344 rcode = x->function(child);
1345 restore_redirects(squirrel);
1346 return rcode;
1347 }
Erik Andersen330fd2b2000-05-19 05:35:19 +00001348 }
1349 }
1350
Eric Andersen86349772000-12-18 20:25:50 +00001351 if (!(child->pid = fork())) {
Eric Andersen2d848a42001-06-25 17:11:54 +00001352 /* Set the handling for job control signals back to the default. */
1353 signal(SIGINT, SIG_DFL);
1354 signal(SIGQUIT, SIG_DFL);
1355 signal(SIGTSTP, SIG_DFL);
1356 signal(SIGTTIN, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001357 signal(SIGTTOU, SIG_DFL);
Eric Andersen2d848a42001-06-25 17:11:54 +00001358 signal(SIGCHLD, SIG_DFL);
Erik Andersen161220c2000-03-16 08:12:48 +00001359
Eric Andersen8ea28be2001-01-05 20:58:22 +00001360 close_all();
1361
Eric Andersen86349772000-12-18 20:25:50 +00001362 if (outpipe[1]!=-1) {
1363 close(outpipe[0]);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001364 }
1365 if (nextin != 0) {
1366 dup2(nextin, 0);
1367 close(nextin);
1368 }
1369
1370 if (nextout != 1) {
Erik Andersen161220c2000-03-16 08:12:48 +00001371 dup2(nextout, 1);
Eric Andersen86349772000-12-18 20:25:50 +00001372 dup2(nextout, 2); /* Really? */
Erik Andersen161220c2000-03-16 08:12:48 +00001373 close(nextout);
Eric Andersen501c88b2000-07-28 15:14:45 +00001374 close(pipefds[0]);
Erik Andersen161220c2000-03-16 08:12:48 +00001375 }
1376
Eric Andersen86349772000-12-18 20:25:50 +00001377 /* explicit redirects override pipes */
Eric Andersene9f07fb2000-12-21 18:31:36 +00001378 setup_redirects(child,NULL);
Erik Andersen161220c2000-03-16 08:12:48 +00001379
Eric Andersen86349772000-12-18 20:25:50 +00001380 pseudo_exec(child);
Erik Andersen161220c2000-03-16 08:12:48 +00001381 }
Eric Andersen86349772000-12-18 20:25:50 +00001382 if (outpipe[1]!=-1) {
1383 close(outpipe[1]);
Eric Andersena1d187a2000-07-17 19:14:41 +00001384 }
Erik Andersen161220c2000-03-16 08:12:48 +00001385
1386 /* put our child in the process group whose leader is the
1387 first process in this pipe */
Eric Andersen86349772000-12-18 20:25:50 +00001388 setpgid(child->pid, newjob->progs[0].pid);
Erik Andersen161220c2000-03-16 08:12:48 +00001389 if (nextin != 0)
1390 close(nextin);
Eric Andersen6efc48c2000-07-18 08:16:39 +00001391 if (nextout != 1)
Erik Andersen161220c2000-03-16 08:12:48 +00001392 close(nextout);
1393
1394 /* If there isn't another process, nextin is garbage
1395 but it doesn't matter */
1396 nextin = pipefds[0];
1397 }
1398
Eric Andersen86349772000-12-18 20:25:50 +00001399 newjob->pgrp = newjob->progs[0].pid;
Erik Andersen161220c2000-03-16 08:12:48 +00001400
Eric Andersen86349772000-12-18 20:25:50 +00001401 insert_job(newjob, inbg);
Erik Andersen3522eb12000-03-12 23:49:18 +00001402
Erik Andersen161220c2000-03-16 08:12:48 +00001403 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001404}
1405
Erik Andersend75af992000-03-16 08:09:09 +00001406static int busy_loop(FILE * input)
Erik Andersen3522eb12000-03-12 23:49:18 +00001407{
Erik Andersen161220c2000-03-16 08:12:48 +00001408 char *command;
Eric Andersen86349772000-12-18 20:25:50 +00001409 char *next_command = NULL;
1410 struct job newjob;
Eric Andersen1c314ad2000-06-28 16:56:25 +00001411 pid_t parent_pgrp;
Erik Andersen161220c2000-03-16 08:12:48 +00001412 int i;
Eric Andersen86349772000-12-18 20:25:50 +00001413 int inbg;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001414 int status;
Eric Andersen86349772000-12-18 20:25:50 +00001415 newjob.job_list = &job_list;
1416 newjob.job_context = DEFAULT_CONTEXT;
Erik Andersen3522eb12000-03-12 23:49:18 +00001417
Eric Andersen1c314ad2000-06-28 16:56:25 +00001418 /* save current owner of TTY so we can restore it on exit */
Eric Andersen2d848a42001-06-25 17:11:54 +00001419 parent_pgrp = tcgetpgrp(shell_terminal);
Eric Andersen1c314ad2000-06-28 16:56:25 +00001420
Matt Kraaib8907522000-09-13 02:08:21 +00001421 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Erik Andersend75af992000-03-16 08:09:09 +00001422
Erik Andersen161220c2000-03-16 08:12:48 +00001423 while (1) {
Eric Andersen86349772000-12-18 20:25:50 +00001424 if (!job_list.fg) {
Erik Andersend75af992000-03-16 08:09:09 +00001425 /* no job is in the foreground */
Erik Andersen3522eb12000-03-12 23:49:18 +00001426
Erik Andersend75af992000-03-16 08:09:09 +00001427 /* see if any background processes have exited */
Eric Andersen86349772000-12-18 20:25:50 +00001428 checkjobs(&job_list);
Erik Andersen3522eb12000-03-12 23:49:18 +00001429
Eric Andersen86349772000-12-18 20:25:50 +00001430 if (!next_command) {
1431 if (get_command(input, command))
Erik Andersen161220c2000-03-16 08:12:48 +00001432 break;
Eric Andersen86349772000-12-18 20:25:50 +00001433 next_command = command;
Erik Andersend75af992000-03-16 08:09:09 +00001434 }
Erik Andersen3522eb12000-03-12 23:49:18 +00001435
Eric Andersenca604592001-03-08 17:17:13 +00001436 if (expand_arguments(next_command) == FALSE) {
1437 free(command);
1438 command = (char *) xcalloc(BUFSIZ, sizeof(char));
1439 next_command = NULL;
1440 continue;
1441 }
1442
Eric Andersen86349772000-12-18 20:25:50 +00001443 if (!parse_command(&next_command, &newjob, &inbg) &&
1444 newjob.num_progs) {
Eric Andersena1d187a2000-07-17 19:14:41 +00001445 int pipefds[2] = {-1,-1};
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001446 debug_printf( "job=%p fed to run_command by busy_loop()'\n",
1447 &newjob);
Eric Andersen86349772000-12-18 20:25:50 +00001448 run_command(&newjob, inbg, pipefds);
Eric Andersenfad9c112000-07-25 18:06:52 +00001449 }
1450 else {
Eric Andersena1d187a2000-07-17 19:14:41 +00001451 free(command);
Matt Kraaib8907522000-09-13 02:08:21 +00001452 command = (char *) xcalloc(BUFSIZ, sizeof(char));
Eric Andersen86349772000-12-18 20:25:50 +00001453 next_command = NULL;
Erik Andersend75af992000-03-16 08:09:09 +00001454 }
1455 } else {
1456 /* a job is running in the foreground; wait for it */
1457 i = 0;
Eric Andersen86349772000-12-18 20:25:50 +00001458 while (!job_list.fg->progs[i].pid ||
1459 job_list.fg->progs[i].is_stopped == 1) i++;
Erik Andersen3522eb12000-03-12 23:49:18 +00001460
Eric Andersen8ea28be2001-01-05 20:58:22 +00001461 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0)
1462 perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
Erik Andersen3522eb12000-03-12 23:49:18 +00001463
Erik Andersend75af992000-03-16 08:09:09 +00001464 if (WIFEXITED(status) || WIFSIGNALED(status)) {
Erik Andersen161220c2000-03-16 08:12:48 +00001465 /* the child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001466 job_list.fg->running_progs--;
1467 job_list.fg->progs[i].pid = 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001468
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001469 last_return_code=WEXITSTATUS(status);
1470
Eric Andersen86349772000-12-18 20:25:50 +00001471 if (!job_list.fg->running_progs) {
Erik Andersen161220c2000-03-16 08:12:48 +00001472 /* child exited */
Eric Andersen86349772000-12-18 20:25:50 +00001473 remove_job(&job_list, job_list.fg);
1474 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001475 }
Erik Andersend75af992000-03-16 08:09:09 +00001476 } else {
Erik Andersen161220c2000-03-16 08:12:48 +00001477 /* the child was stopped */
Eric Andersen86349772000-12-18 20:25:50 +00001478 job_list.fg->stopped_progs++;
1479 job_list.fg->progs[i].is_stopped = 1;
Erik Andersen3522eb12000-03-12 23:49:18 +00001480
Eric Andersen86349772000-12-18 20:25:50 +00001481 if (job_list.fg->stopped_progs == job_list.fg->running_progs) {
1482 printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid,
1483 "Stopped", job_list.fg->text);
1484 job_list.fg = NULL;
Erik Andersen161220c2000-03-16 08:12:48 +00001485 }
Erik Andersend75af992000-03-16 08:09:09 +00001486 }
1487
Eric Andersen86349772000-12-18 20:25:50 +00001488 if (!job_list.fg) {
Erik Andersen161220c2000-03-16 08:12:48 +00001489 /* move the shell to the foreground */
Eric Andersen1c314ad2000-06-28 16:56:25 +00001490 /* suppress messages when run from /linuxrc mag@sysgo.de */
Eric Andersen2d848a42001-06-25 17:11:54 +00001491 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
Matt Kraaia9819b22000-12-22 01:48:07 +00001492 perror_msg("tcsetpgrp");
Erik Andersend75af992000-03-16 08:09:09 +00001493 }
1494 }
1495 }
Erik Andersen161220c2000-03-16 08:12:48 +00001496 free(command);
Erik Andersen3522eb12000-03-12 23:49:18 +00001497
Eric Andersen1c314ad2000-06-28 16:56:25 +00001498 /* return controlling TTY back to parent process group before exiting */
Eric Andersen2d848a42001-06-25 17:11:54 +00001499 if (tcsetpgrp(shell_terminal, parent_pgrp))
Matt Kraaia9819b22000-12-22 01:48:07 +00001500 perror_msg("tcsetpgrp");
Eric Andersenb54833c2000-07-03 23:56:26 +00001501
1502 /* return exit status if called with "-c" */
1503 if (input == NULL && WIFEXITED(status))
1504 return WEXITSTATUS(status);
1505
Erik Andersen161220c2000-03-16 08:12:48 +00001506 return 0;
Erik Andersen3522eb12000-03-12 23:49:18 +00001507}
1508
1509
Eric Andersenfad9c112000-07-25 18:06:52 +00001510#ifdef BB_FEATURE_CLEAN_UP
1511void free_memory(void)
1512{
Eric Andersene5dfced2001-04-09 22:48:12 +00001513 if (cwd) {
Eric Andersenfad9c112000-07-25 18:06:52 +00001514 free(cwd);
Eric Andersene5dfced2001-04-09 22:48:12 +00001515 }
Eric Andersenfad9c112000-07-25 18:06:52 +00001516 if (local_pending_command)
1517 free(local_pending_command);
1518
Eric Andersen86349772000-12-18 20:25:50 +00001519 if (job_list.fg && !job_list.fg->running_progs) {
1520 remove_job(&job_list, job_list.fg);
Eric Andersenfad9c112000-07-25 18:06:52 +00001521 }
1522}
1523#endif
1524
Eric Andersen2d848a42001-06-25 17:11:54 +00001525/* Make sure we have a controlling tty. If we get started under a job
1526 * aware app (like bash for example), make sure we are now in charge so
1527 * we don't fight over who gets the foreground */
1528static void setup_job_control()
1529{
1530 /* Loop until we are in the foreground. */
1531 while (tcgetpgrp (shell_terminal) != (shell_pgrp = getpgrp ()))
1532 kill (- shell_pgrp, SIGTTIN);
1533
1534 /* Ignore interactive and job-control signals. */
1535 signal(SIGINT, SIG_IGN);
1536 signal(SIGQUIT, SIG_IGN);
1537 signal(SIGTSTP, SIG_IGN);
1538 signal(SIGTTIN, SIG_IGN);
1539 signal(SIGTTOU, SIG_IGN);
1540 signal(SIGCHLD, SIG_IGN);
1541
1542 /* Put ourselves in our own process group. */
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001543 setsid();
Eric Andersen2d848a42001-06-25 17:11:54 +00001544 shell_pgrp = getpid ();
Eric Andersen4b6b5e42001-06-27 04:30:11 +00001545 setpgid (shell_pgrp, shell_pgrp);
Eric Andersen2d848a42001-06-25 17:11:54 +00001546
1547 /* Grab control of the terminal. */
1548 tcsetpgrp(shell_terminal, shell_pgrp);
1549}
Eric Andersen6efc48c2000-07-18 08:16:39 +00001550
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001551int shell_main(int argc_l, char **argv_l)
Erik Andersen3522eb12000-03-12 23:49:18 +00001552{
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001553 int opt, interactive=FALSE;
Erik Andersen161220c2000-03-16 08:12:48 +00001554 FILE *input = stdin;
Eric Andersen6a99aaf2000-07-27 00:15:20 +00001555 argc = argc_l;
1556 argv = argv_l;
Erik Andersen3522eb12000-03-12 23:49:18 +00001557
Eric Andersen702ec592001-03-06 22:17:29 +00001558 /* These variables need re-initializing when recursing */
Eric Andersen8a646dd2001-06-21 16:38:11 +00001559 last_jobid = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001560 shell_context = 0;
Eric Andersenb0970d42001-01-05 19:34:52 +00001561 local_pending_command = NULL;
Eric Andersen702ec592001-03-06 22:17:29 +00001562 close_me_head = 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) {
Eric Andersen8ea28be2001-01-05 20:58:22 +00001571 int tmp_fd = fileno(prof_input);
1572 mark_open(tmp_fd);
1573 /* Now run the file */
1574 busy_loop(prof_input);
1575 fclose(prof_input);
1576 mark_closed(tmp_fd);
1577 }
Eric Andersenb558e762000-11-30 22:43:16 +00001578 }
Eric Andersen501c88b2000-07-28 15:14:45 +00001579
Eric Andersenf21aa842000-12-08 20:50:30 +00001580 while ((opt = getopt(argc_l, argv_l, "cxi")) > 0) {
Eric Andersen501c88b2000-07-28 15:14:45 +00001581 switch (opt) {
1582 case 'c':
1583 input = NULL;
Matt Kraai6085c722000-09-06 01:46:18 +00001584 if (local_pending_command != 0)
Matt Kraaidd19c692001-01-31 19:00:21 +00001585 error_msg_and_die("multiple -c arguments");
Matt Kraai6085c722000-09-06 01:46:18 +00001586 local_pending_command = xstrdup(argv[optind]);
1587 optind++;
1588 argv = argv+optind;
Eric Andersen501c88b2000-07-28 15:14:45 +00001589 break;
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001590 case 'i':
1591 interactive = TRUE;
1592 break;
Eric Andersen501c88b2000-07-28 15:14:45 +00001593 default:
Eric Andersen67991cf2001-02-14 21:23:06 +00001594 show_usage();
Eric Andersen501c88b2000-07-28 15:14:45 +00001595 }
1596 }
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001597 /* A shell is interactive if the `-i' flag was given, or if all of
1598 * the following conditions are met:
1599 * no -c command
1600 * no arguments remaining or the -s flag given
1601 * standard input is a terminal
1602 * standard output is a terminal
1603 * Refer to Posix.2, the description of the `sh' utility. */
Eric Andersen86349772000-12-18 20:25:50 +00001604 if (argv[optind]==NULL && input==stdin &&
1605 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
1606 interactive=TRUE;
1607 }
Eric Andersen2d848a42001-06-25 17:11:54 +00001608 setup_job_control();
Eric Andersen86349772000-12-18 20:25:50 +00001609 if (interactive==TRUE) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001610 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Eric Andersen501c88b2000-07-28 15:14:45 +00001611 /* Looks like they want an interactive shell */
Matt Kraai4ef40c02001-04-12 20:44:21 +00001612 printf( "\n\n" BB_BANNER " Built-in shell (lash)\n");
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001613 printf( "Enter 'help' for a list of built-in commands.\n\n");
Eric Andersen6a4c33c2000-07-28 17:08:36 +00001614 } else if (local_pending_command==NULL) {
Eric Andersen6f65a3a2001-01-20 01:10:07 +00001615 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
Matt Kraaibbaef662000-09-27 02:43:35 +00001616 input = xfopen(argv[optind], "r");
Eric Andersen8ea28be2001-01-05 20:58:22 +00001617 mark_open(fileno(input)); /* be lazy, never mark this closed */
Eric Andersen501c88b2000-07-28 15:14:45 +00001618 }
1619
Eric Andersen6efc48c2000-07-18 08:16:39 +00001620 /* initialize the cwd -- this is never freed...*/
Eric Andersene5dfced2001-04-09 22:48:12 +00001621 cwd = xgetcwd(0);
Eric Andersen5f265b72001-05-11 16:58:46 +00001622 if (!cwd)
1623 cwd = unknown;
Erik Andersen3522eb12000-03-12 23:49:18 +00001624
Eric Andersenfad9c112000-07-25 18:06:52 +00001625#ifdef BB_FEATURE_CLEAN_UP
1626 atexit(free_memory);
1627#endif
1628
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001629#ifdef BB_FEATURE_COMMAND_EDITING
1630 cmdedit_set_initial_prompt();
1631#else
1632 PS1 = NULL;
Eric Andersenb3d6e2d2001-03-13 22:57:56 +00001633#endif
1634
Erik Andersen161220c2000-03-16 08:12:48 +00001635 return (busy_loop(input));
Erik Andersen3522eb12000-03-12 23:49:18 +00001636}