Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Erik Andersen | 6acaa40 | 2000-03-26 14:03:20 +0000 | [diff] [blame] | 3 | * lash -- the BusyBox Lame-Ass SHell |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org> |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 6 | * |
| 7 | * Based in part on ladsh.c by Michael K. Johnson and Erik W. Troan, which is |
| 8 | * under the following liberal license: "We have placed this source code in the |
| 9 | * public domain. Use it in any project, free or commercial." |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | * |
| 25 | */ |
| 26 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 27 | /* This shell's parsing engine is officially at a dead-end. Future |
| 28 | * work shell work should be done using hush, msh, or ash. This is |
| 29 | * still a very useful, small shell -- it just don't need any more |
| 30 | * features beyond what it already has... |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 31 | */ |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 32 | |
Eric Andersen | 1e7cea9 | 2000-12-06 23:47:38 +0000 | [diff] [blame] | 33 | //For debugging/development on the shell only... |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 34 | //#define DEBUG_SHELL |
Eric Andersen | a1d187a | 2000-07-17 19:14:41 +0000 | [diff] [blame] | 35 | |
| 36 | |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <ctype.h> |
| 40 | #include <errno.h> |
| 41 | #include <fcntl.h> |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 42 | #include <signal.h> |
| 43 | #include <string.h> |
| 44 | #include <sys/ioctl.h> |
| 45 | #include <sys/wait.h> |
| 46 | #include <unistd.h> |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 47 | #include <getopt.h> |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 48 | #include <termios.h> |
Mark Whitley | 4b541a8 | 2001-04-25 17:10:30 +0000 | [diff] [blame] | 49 | #include "busybox.h" |
| 50 | #include "cmdedit.h" |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 51 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 52 | #ifdef CONFIG_LOCALE_SUPPORT |
Mark Whitley | 1c6581a | 2001-03-27 16:35:16 +0000 | [diff] [blame] | 53 | #include <locale.h> |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 54 | #endif |
Eric Andersen | 13d1fa1 | 2001-03-08 23:59:45 +0000 | [diff] [blame] | 55 | |
Eric Andersen | 13d1fa1 | 2001-03-08 23:59:45 +0000 | [diff] [blame] | 56 | #include <glob.h> |
| 57 | #define expand_t glob_t |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 58 | |
Eric Andersen | 13d1fa1 | 2001-03-08 23:59:45 +0000 | [diff] [blame] | 59 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 60 | static const int MAX_READ = 128; /* size of input buffer for `read' builtin */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 61 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
| 62 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 63 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 64 | enum redir_type { REDIRECT_INPUT, REDIRECT_OVERWRITE, |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 65 | REDIRECT_APPEND |
| 66 | }; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 67 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 68 | static const unsigned int DEFAULT_CONTEXT=0x1; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 69 | static const unsigned int IF_TRUE_CONTEXT=0x2; |
| 70 | static const unsigned int IF_FALSE_CONTEXT=0x4; |
| 71 | static const unsigned int THEN_EXP_CONTEXT=0x8; |
| 72 | static const unsigned int ELSE_EXP_CONTEXT=0x10; |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 73 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 74 | |
| 75 | struct jobset { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 76 | struct job *head; /* head of list of running jobs */ |
| 77 | struct job *fg; /* current foreground job */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 80 | struct redir_struct { |
| 81 | enum redir_type type; /* type of redirection */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 82 | int fd; /* file descriptor being redirected */ |
| 83 | char *filename; /* file to redirect fd to */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 86 | struct child_prog { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 87 | pid_t pid; /* 0 if exited */ |
| 88 | char **argv; /* program name and arguments */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 89 | int num_redirects; /* elements in redirection array */ |
| 90 | struct redir_struct *redirects; /* I/O redirects */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 91 | int is_stopped; /* is the program currently running? */ |
| 92 | struct job *family; /* pointer back to the child's parent job */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | struct job { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 96 | int jobid; /* job number */ |
| 97 | int num_progs; /* total number of programs in job */ |
| 98 | int running_progs; /* number of programs running */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 99 | char *text; /* name of job */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 100 | char *cmdbuf; /* buffer various argv's point into */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 101 | pid_t pgrp; /* process group ID for the job */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 102 | struct child_prog *progs; /* array of programs in job */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 103 | struct job *next; /* to track background commands */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 104 | int stopped_progs; /* number of programs alive, but stopped */ |
| 105 | unsigned int job_context; /* bitmask defining current context */ |
| 106 | struct jobset *job_list; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 109 | struct built_in_command { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 110 | char *cmd; /* name */ |
| 111 | char *descr; /* description */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 112 | int (*function) (struct child_prog *); /* function ptr */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 115 | struct close_me { |
| 116 | int fd; |
| 117 | struct close_me *next; |
| 118 | }; |
| 119 | |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 120 | /* function prototypes for builtins */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 121 | static int builtin_cd(struct child_prog *cmd); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 122 | static int builtin_exec(struct child_prog *cmd); |
| 123 | static int builtin_exit(struct child_prog *cmd); |
| 124 | static int builtin_fg_bg(struct child_prog *cmd); |
| 125 | static int builtin_help(struct child_prog *cmd); |
| 126 | static int builtin_jobs(struct child_prog *dummy); |
| 127 | static int builtin_pwd(struct child_prog *dummy); |
| 128 | static int builtin_export(struct child_prog *cmd); |
| 129 | static int builtin_source(struct child_prog *cmd); |
| 130 | static int builtin_unset(struct child_prog *cmd); |
| 131 | static int builtin_read(struct child_prog *cmd); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 132 | |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 133 | |
| 134 | /* function prototypes for shell stuff */ |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 135 | static void mark_open(int fd); |
| 136 | static void mark_closed(int fd); |
Eric Andersen | 36278b9 | 2001-03-06 20:47:31 +0000 | [diff] [blame] | 137 | static void close_all(void); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 138 | static void checkjobs(struct jobset *job_list); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 139 | static void remove_job(struct jobset *j_list, struct job *job); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 140 | static int get_command(FILE * source, char *command); |
| 141 | static int parse_command(char **command_ptr, struct job *job, int *inbg); |
| 142 | static int run_command(struct job *newjob, int inbg, int outpipe[2]); |
| 143 | static int pseudo_exec(struct child_prog *cmd) __attribute__ ((noreturn)); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 144 | static int busy_loop(FILE * input); |
| 145 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 146 | |
Mark Whitley | 37653aa | 2000-07-12 23:36:17 +0000 | [diff] [blame] | 147 | /* Table of built-in functions (these are non-forking builtins, meaning they |
| 148 | * can change global variables in the parent shell process but they will not |
| 149 | * work with pipes and redirects; 'unset foo | whatever' will not work) */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 150 | static struct built_in_command bltins[] = { |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 151 | {"bg", "Resume a job in the background", builtin_fg_bg}, |
| 152 | {"cd", "Change working directory", builtin_cd}, |
Eric Andersen | d2f5677 | 2000-09-21 02:48:07 +0000 | [diff] [blame] | 153 | {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec}, |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 154 | {"exit", "Exit from shell()", builtin_exit}, |
| 155 | {"fg", "Bring job into the foreground", builtin_fg_bg}, |
| 156 | {"jobs", "Lists the active jobs", builtin_jobs}, |
| 157 | {"export", "Set environment variable", builtin_export}, |
| 158 | {"unset", "Unset environment variable", builtin_unset}, |
| 159 | {"read", "Input environment variable", builtin_read}, |
Matt Kraai | dd450a0 | 2000-09-13 03:43:36 +0000 | [diff] [blame] | 160 | {".", "Source-in and run commands in a file", builtin_source}, |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 161 | /* to do: add ulimit */ |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 162 | {NULL, NULL, NULL} |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
Mark Whitley | 37653aa | 2000-07-12 23:36:17 +0000 | [diff] [blame] | 165 | /* Table of forking built-in functions (things that fork cannot change global |
| 166 | * variables in the parent process, such as the current working directory) */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 167 | static struct built_in_command bltins_forking[] = { |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 168 | {"pwd", "Print current directory", builtin_pwd}, |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 169 | {"help", "List shell built-in commands", builtin_help}, |
| 170 | {NULL, NULL, NULL} |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
Eric Andersen | 0bcc813 | 2001-01-05 19:37:32 +0000 | [diff] [blame] | 173 | |
Eric Andersen | 7467c8d | 2001-07-12 20:26:32 +0000 | [diff] [blame] | 174 | static int shell_context; /* Type prompt trigger (PS1 or PS2) */ |
Eric Andersen | 0bcc813 | 2001-01-05 19:37:32 +0000 | [diff] [blame] | 175 | |
| 176 | |
| 177 | /* Globals that are static to this file */ |
Eric Andersen | cfa88ec | 2001-05-11 18:08:16 +0000 | [diff] [blame] | 178 | static const char *cwd; |
Eric Andersen | 744b064 | 2001-01-05 21:23:44 +0000 | [diff] [blame] | 179 | static char *local_pending_command = NULL; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 180 | static struct jobset job_list = { NULL, NULL }; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 181 | static int argc; |
| 182 | static char **argv; |
Eric Andersen | 702ec59 | 2001-03-06 22:17:29 +0000 | [diff] [blame] | 183 | static struct close_me *close_me_head; |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 184 | static int last_return_code; |
| 185 | static int last_bg_pid; |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 186 | static unsigned int last_jobid; |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 187 | static int shell_terminal; |
| 188 | static pid_t shell_pgrp; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 189 | static char *PS1; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 190 | static char *PS2 = "> "; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 191 | |
| 192 | |
Eric Andersen | b558e76 | 2000-11-30 22:43:16 +0000 | [diff] [blame] | 193 | #ifdef DEBUG_SHELL |
| 194 | static inline void debug_printf(const char *format, ...) |
| 195 | { |
| 196 | va_list args; |
| 197 | va_start(args, format); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 198 | vfprintf(stderr, format, args); |
Eric Andersen | b558e76 | 2000-11-30 22:43:16 +0000 | [diff] [blame] | 199 | va_end(args); |
| 200 | } |
| 201 | #else |
| 202 | static inline void debug_printf(const char *format, ...) { } |
| 203 | #endif |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 204 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 205 | /* |
| 206 | Most builtins need access to the struct child_prog that has |
| 207 | their arguments, previously coded as cmd->progs[0]. That coding |
| 208 | can exhibit a bug, if the builtin is not the first command in |
| 209 | a pipeline: "echo foo | exec sort" will attempt to exec foo. |
| 210 | |
| 211 | builtin previous use notes |
| 212 | ------ ----------------- --------- |
| 213 | cd cmd->progs[0] |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 214 | exec cmd->progs[0] squashed bug: didn't look for applets or forking builtins |
| 215 | exit cmd->progs[0] |
| 216 | fg_bg cmd->progs[0], job_list->head, job_list->fg |
| 217 | help 0 |
| 218 | jobs job_list->head |
| 219 | pwd 0 |
Eric Andersen | 84e229c | 2001-03-29 22:48:33 +0000 | [diff] [blame] | 220 | export cmd->progs[0] |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 221 | source cmd->progs[0] |
| 222 | unset cmd->progs[0] |
| 223 | read cmd->progs[0] |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 224 | |
| 225 | I added "struct job *family;" to struct child_prog, |
| 226 | and switched API to builtin_foo(struct child_prog *child); |
| 227 | So cmd->text becomes child->family->text |
| 228 | cmd->job_context becomes child->family->job_context |
| 229 | cmd->progs[0] becomes *child |
| 230 | job_list becomes child->family->job_list |
| 231 | */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 232 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 233 | /* built-in 'cd <path>' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 234 | static int builtin_cd(struct child_prog *child) |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 235 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 236 | char *newdir; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 237 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 238 | if (child->argv[1] == NULL) |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 239 | newdir = getenv("HOME"); |
| 240 | else |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 241 | newdir = child->argv[1]; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 242 | if (chdir(newdir)) { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 243 | printf("cd: %s: %m\n", newdir); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 244 | return EXIT_FAILURE; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 245 | } |
Eric Andersen | cfa88ec | 2001-05-11 18:08:16 +0000 | [diff] [blame] | 246 | cwd = xgetcwd((char *)cwd); |
Eric Andersen | 5f265b7 | 2001-05-11 16:58:46 +0000 | [diff] [blame] | 247 | if (!cwd) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 248 | cwd = bb_msg_unknown; |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 249 | return EXIT_SUCCESS; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Eric Andersen | d2f5677 | 2000-09-21 02:48:07 +0000 | [diff] [blame] | 252 | /* built-in 'exec' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 253 | static int builtin_exec(struct child_prog *child) |
Eric Andersen | d2f5677 | 2000-09-21 02:48:07 +0000 | [diff] [blame] | 254 | { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 255 | if (child->argv[1] == NULL) |
| 256 | return EXIT_SUCCESS; /* Really? */ |
| 257 | child->argv++; |
Eric Andersen | 07f2f39 | 2001-03-06 20:28:22 +0000 | [diff] [blame] | 258 | close_all(); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 259 | pseudo_exec(child); |
| 260 | /* never returns */ |
Eric Andersen | d2f5677 | 2000-09-21 02:48:07 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 263 | /* built-in 'exit' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 264 | static int builtin_exit(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 265 | { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 266 | if (child->argv[1] == NULL) |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 267 | exit(EXIT_SUCCESS); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 268 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 269 | exit (atoi(child->argv[1])); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* built-in 'fg' and 'bg' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 273 | static int builtin_fg_bg(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 274 | { |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 275 | int i, jobnum; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 276 | struct job *job=NULL; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 277 | |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 278 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 279 | if (!child->argv[1]) { |
| 280 | for (job = child->family->job_list->head; job; job = job->next) { |
| 281 | if (job->jobid == last_jobid) { |
| 282 | break; |
| 283 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 284 | } |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 285 | if (!job) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 286 | bb_error_msg("%s: no current job", child->argv[0]); |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 287 | return EXIT_FAILURE; |
| 288 | } |
| 289 | } else { |
| 290 | if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 291 | bb_error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]); |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 292 | return EXIT_FAILURE; |
| 293 | } |
| 294 | for (job = child->family->job_list->head; job; job = job->next) { |
| 295 | if (job->jobid == jobnum) { |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | if (!job) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 300 | bb_error_msg("%s: %d: no such job", child->argv[0], jobnum); |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 301 | return EXIT_FAILURE; |
| 302 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 303 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 304 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 305 | if (*child->argv[0] == 'f') { |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 306 | /* Put the job into the foreground. */ |
| 307 | tcsetpgrp(shell_terminal, job->pgrp); |
| 308 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 309 | child->family->job_list->fg = job; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 310 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 311 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 312 | /* Restart the processes in the job */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 313 | for (i = 0; i < job->num_progs; i++) |
| 314 | job->progs[i].is_stopped = 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 315 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 316 | job->stopped_progs = 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 317 | |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 318 | if ( (i=kill(- job->pgrp, SIGCONT)) < 0) { |
| 319 | if (i == ESRCH) { |
| 320 | remove_job(&job_list, job); |
Eric Andersen | 07abfe2 | 2001-06-27 17:29:11 +0000 | [diff] [blame] | 321 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 322 | bb_perror_msg("kill (SIGCONT)"); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 323 | } |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 324 | } |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 325 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 326 | return EXIT_SUCCESS; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* built-in 'help' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 330 | static int builtin_help(struct child_prog *dummy) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 331 | { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 332 | struct built_in_command *x; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 333 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 334 | printf("\nBuilt-in commands:\n"); |
| 335 | printf("-------------------\n"); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 336 | for (x = bltins; x->cmd; x++) { |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 337 | if (x->descr==NULL) |
| 338 | continue; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 339 | printf("%s\t%s\n", x->cmd, x->descr); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 340 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 341 | for (x = bltins_forking; x->cmd; x++) { |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 342 | if (x->descr==NULL) |
| 343 | continue; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 344 | printf("%s\t%s\n", x->cmd, x->descr); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 345 | } |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 346 | printf("\n\n"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 347 | return EXIT_SUCCESS; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* built-in 'jobs' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 351 | static int builtin_jobs(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 352 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 353 | struct job *job; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 354 | char *status_string; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 355 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 356 | for (job = child->family->job_list->head; job; job = job->next) { |
| 357 | if (job->running_progs == job->stopped_progs) |
| 358 | status_string = "Stopped"; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 359 | else |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 360 | status_string = "Running"; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 361 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 362 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 363 | } |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 364 | return EXIT_SUCCESS; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | |
| 368 | /* built-in 'pwd' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 369 | static int builtin_pwd(struct child_prog *dummy) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 370 | { |
Eric Andersen | cfa88ec | 2001-05-11 18:08:16 +0000 | [diff] [blame] | 371 | cwd = xgetcwd((char *)cwd); |
Eric Andersen | 5f265b7 | 2001-05-11 16:58:46 +0000 | [diff] [blame] | 372 | if (!cwd) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 373 | cwd = bb_msg_unknown; |
Matt Kraai | 59df6f7 | 2001-05-16 14:21:09 +0000 | [diff] [blame] | 374 | puts(cwd); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 375 | return EXIT_SUCCESS; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 378 | /* built-in 'export VAR=value' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 379 | static int builtin_export(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 380 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 381 | int res; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 382 | char *v = child->argv[1]; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 383 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 384 | if (v == NULL) { |
Eric Andersen | 84e229c | 2001-03-29 22:48:33 +0000 | [diff] [blame] | 385 | char **e; |
| 386 | for (e = environ; *e; e++) { |
Matt Kraai | 59df6f7 | 2001-05-16 14:21:09 +0000 | [diff] [blame] | 387 | puts(*e); |
Eric Andersen | 84e229c | 2001-03-29 22:48:33 +0000 | [diff] [blame] | 388 | } |
Matt Kraai | 2129f97 | 2001-04-04 17:50:04 +0000 | [diff] [blame] | 389 | return 0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 390 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 391 | res = putenv(v); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 392 | if (res) |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 393 | fprintf(stderr, "export: %m\n"); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 394 | #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 395 | if (strncmp(v, "PS1=", 4)==0) |
| 396 | PS1 = getenv("PS1"); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 397 | #endif |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 398 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 399 | #ifdef CONFIG_LOCALE_SUPPORT |
Mark Whitley | 1c6581a | 2001-03-27 16:35:16 +0000 | [diff] [blame] | 400 | if(strncmp(v, "LC_ALL=", 7)==0) |
| 401 | setlocale(LC_ALL, getenv("LC_ALL")); |
Mark Whitley | a82a003 | 2001-03-27 17:07:15 +0000 | [diff] [blame] | 402 | if(strncmp(v, "LC_CTYPE=", 9)==0) |
Mark Whitley | 1c6581a | 2001-03-27 16:35:16 +0000 | [diff] [blame] | 403 | setlocale(LC_CTYPE, getenv("LC_CTYPE")); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 404 | #endif |
Mark Whitley | 1c6581a | 2001-03-27 16:35:16 +0000 | [diff] [blame] | 405 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 406 | return (res); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 409 | /* built-in 'read VAR' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 410 | static int builtin_read(struct child_prog *child) |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 411 | { |
| 412 | int res = 0, len, newlen; |
| 413 | char *s; |
| 414 | char string[MAX_READ]; |
| 415 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 416 | if (child->argv[1]) { |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 417 | /* argument (VAR) given: put "VAR=" into buffer */ |
Eric Andersen | 0d2d1eb | 2002-06-06 13:33:01 +0000 | [diff] [blame] | 418 | safe_strncpy(string, child->argv[1], MAX_READ-1); |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 419 | 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 Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 435 | fprintf(stderr, "read: %m\n"); |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 436 | } |
| 437 | else |
| 438 | fgets(string, sizeof(string), stdin); |
| 439 | |
| 440 | return (res); |
| 441 | } |
| 442 | |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 443 | /* Built-in '.' handler (read-in and execute commands from file) */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 444 | static int builtin_source(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 445 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 446 | FILE *input; |
| 447 | int status; |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 448 | int fd; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 449 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 450 | if (child->argv[1] == NULL) |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 451 | return EXIT_FAILURE; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 452 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 453 | input = fopen(child->argv[1], "r"); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 454 | if (!input) { |
Eric Andersen | 6f65a3a | 2001-01-20 01:10:07 +0000 | [diff] [blame] | 455 | printf( "Couldn't open file '%s'\n", child->argv[1]); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 456 | return EXIT_FAILURE; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 457 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 458 | |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 459 | fd=fileno(input); |
| 460 | mark_open(fd); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 461 | /* Now run the file */ |
| 462 | status = busy_loop(input); |
Matt Kraai | dd450a0 | 2000-09-13 03:43:36 +0000 | [diff] [blame] | 463 | fclose(input); |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 464 | mark_closed(fd); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 465 | return (status); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* built-in 'unset VAR' handler */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 469 | static int builtin_unset(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 470 | { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 471 | if (child->argv[1] == NULL) { |
Eric Andersen | 6f65a3a | 2001-01-20 01:10:07 +0000 | [diff] [blame] | 472 | printf( "unset: parameter required.\n"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 473 | return EXIT_FAILURE; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 474 | } |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 475 | unsetenv(child->argv[1]); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 476 | return EXIT_SUCCESS; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 479 | static 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 | |
| 487 | static void mark_closed(int fd) |
| 488 | { |
| 489 | struct close_me *tmp; |
| 490 | if (close_me_head == NULL || close_me_head->fd != fd) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 491 | bb_error_msg_and_die("corrupt close_me"); |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 492 | tmp = close_me_head; |
| 493 | close_me_head = close_me_head->next; |
| 494 | free(tmp); |
| 495 | } |
| 496 | |
| 497 | static void close_all() |
| 498 | { |
Eric Andersen | 702ec59 | 2001-03-06 22:17:29 +0000 | [diff] [blame] | 499 | 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 Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 504 | } |
| 505 | close_me_head = NULL; |
| 506 | } |
| 507 | |
| 508 | |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 509 | /* free up all memory from a job */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 510 | static void free_job(struct job *cmd) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 511 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 512 | int i; |
Mark Whitley | 44a9914 | 2001-03-14 17:26:37 +0000 | [diff] [blame] | 513 | struct jobset *keep; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 514 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 515 | for (i = 0; i < cmd->num_progs; i++) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 516 | free(cmd->progs[i].argv); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 517 | if (cmd->progs[i].redirects) |
| 518 | free(cmd->progs[i].redirects); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 519 | } |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 520 | free(cmd->progs); |
| 521 | free(cmd->text); |
| 522 | free(cmd->cmdbuf); |
Mark Whitley | 44a9914 | 2001-03-14 17:26:37 +0000 | [diff] [blame] | 523 | keep = cmd->job_list; |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 524 | memset(cmd, 0, sizeof(struct job)); |
Mark Whitley | 44a9914 | 2001-03-14 17:26:37 +0000 | [diff] [blame] | 525 | cmd->job_list = keep; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 528 | /* remove a job from a jobset */ |
| 529 | static void remove_job(struct jobset *j_list, struct job *job) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 530 | { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 531 | struct job *prevjob; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 532 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 533 | free_job(job); |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 534 | if (job == j_list->head) { |
| 535 | j_list->head = job->next; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 536 | } else { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 537 | prevjob = j_list->head; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 538 | while (prevjob->next != job) |
| 539 | prevjob = prevjob->next; |
| 540 | prevjob->next = job->next; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 541 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 542 | |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 543 | if (j_list->head) |
| 544 | last_jobid = j_list->head->jobid; |
| 545 | else |
| 546 | last_jobid = 0; |
| 547 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 548 | free(job); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* Checks to see if any background processes have exited -- if they |
| 552 | have, figure out why and see if a job has completed */ |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 553 | static void checkjobs(struct jobset *j_list) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 554 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 555 | struct job *job; |
| 556 | pid_t childpid; |
| 557 | int status; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 558 | int prognum = 0; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 559 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 560 | while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 561 | for (job = j_list->head; job; job = job->next) { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 562 | prognum = 0; |
| 563 | while (prognum < job->num_progs && |
| 564 | job->progs[prognum].pid != childpid) prognum++; |
| 565 | if (prognum < job->num_progs) |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 566 | break; |
| 567 | } |
| 568 | |
Eric Andersen | a1d187a | 2000-07-17 19:14:41 +0000 | [diff] [blame] | 569 | /* This happens on backticked commands */ |
| 570 | if(job==NULL) |
| 571 | return; |
| 572 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 573 | if (WIFEXITED(status) || WIFSIGNALED(status)) { |
| 574 | /* child exited */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 575 | job->running_progs--; |
| 576 | job->progs[prognum].pid = 0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 577 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 578 | if (!job->running_progs) { |
| 579 | printf(JOB_STATUS_FORMAT, job->jobid, "Done", job->text); |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 580 | last_jobid=0; |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 581 | remove_job(j_list, job); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 582 | } |
| 583 | } else { |
| 584 | /* child stopped */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 585 | job->stopped_progs++; |
| 586 | job->progs[prognum].is_stopped = 1; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 587 | |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 588 | #if 0 |
| 589 | /* Printing this stuff is a pain, since it tends to |
| 590 | * overwrite the prompt an inconveinient moments. So |
| 591 | * don't do that. */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 592 | if (job->stopped_progs == job->num_progs) { |
| 593 | printf(JOB_STATUS_FORMAT, job->jobid, "Stopped", |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 594 | job->text); |
| 595 | } |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 596 | #endif |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 597 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 598 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 599 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 600 | if (childpid == -1 && errno != ECHILD) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 601 | bb_perror_msg("waitpid"); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 604 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 605 | * and stderr if they are redirected. */ |
| 606 | static int setup_redirects(struct child_prog *prog, int squirrel[]) |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 607 | { |
| 608 | int i; |
| 609 | int openfd; |
| 610 | int mode = O_RDONLY; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 611 | struct redir_struct *redir = prog->redirects; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 612 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 613 | for (i = 0; i < prog->num_redirects; i++, redir++) { |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 614 | switch (redir->type) { |
| 615 | case REDIRECT_INPUT: |
| 616 | mode = O_RDONLY; |
| 617 | break; |
| 618 | case REDIRECT_OVERWRITE: |
Eric Andersen | 46f0beb | 2000-11-14 21:59:22 +0000 | [diff] [blame] | 619 | mode = O_WRONLY | O_CREAT | O_TRUNC; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 620 | break; |
| 621 | case REDIRECT_APPEND: |
Eric Andersen | 46f0beb | 2000-11-14 21:59:22 +0000 | [diff] [blame] | 622 | mode = O_WRONLY | O_CREAT | O_APPEND; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 623 | break; |
| 624 | } |
| 625 | |
| 626 | openfd = open(redir->filename, mode, 0666); |
| 627 | if (openfd < 0) { |
| 628 | /* this could get lost if stderr has been redirected, but |
| 629 | bash and ash both lose it as well (though zsh doesn't!) */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 630 | bb_perror_msg("error opening %s", redir->filename); |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 631 | return 1; |
| 632 | } |
| 633 | |
| 634 | if (openfd != redir->fd) { |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 635 | if (squirrel && redir->fd < 3) { |
| 636 | squirrel[redir->fd] = dup(redir->fd); |
| 637 | } |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 638 | dup2(openfd, redir->fd); |
| 639 | close(openfd); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 646 | static void restore_redirects(int squirrel[]) |
| 647 | { |
| 648 | int i, fd; |
| 649 | for (i=0; i<3; i++) { |
| 650 | fd = squirrel[i]; |
| 651 | if (fd != -1) { |
| 652 | /* No error checking. I sure wouldn't know what |
| 653 | * to do with an error if I found one! */ |
| 654 | dup2(fd, i); |
| 655 | close(fd); |
| 656 | } |
| 657 | } |
| 658 | } |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 659 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 660 | static inline void cmdedit_set_initial_prompt(void) |
Eric Andersen | 22332fd | 2001-01-30 23:40:39 +0000 | [diff] [blame] | 661 | { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 662 | #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 663 | PS1 = NULL; |
Eric Andersen | 22332fd | 2001-01-30 23:40:39 +0000 | [diff] [blame] | 664 | #else |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 665 | PS1 = getenv("PS1"); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 666 | if(PS1==0) |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 667 | PS1 = "\\w \\$ "; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 668 | #endif |
Eric Andersen | 09acc06 | 2001-01-04 11:10:38 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 671 | static inline void setup_prompt_string(char **prompt_str) |
| 672 | { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 673 | #ifndef CONFIG_FEATURE_SH_FANCY_PROMPT |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 674 | /* Set up the prompt */ |
| 675 | if (shell_context == 0) { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 676 | free(PS1); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 677 | PS1=xmalloc(strlen(cwd)+4); |
| 678 | sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ? "$ ":"# "); |
| 679 | *prompt_str = PS1; |
| 680 | } else { |
| 681 | *prompt_str = PS2; |
| 682 | } |
| 683 | #else |
| 684 | *prompt_str = (shell_context==0)? PS1 : PS2; |
| 685 | #endif |
| 686 | } |
Eric Andersen | 22332fd | 2001-01-30 23:40:39 +0000 | [diff] [blame] | 687 | |
Eric Andersen | 09acc06 | 2001-01-04 11:10:38 +0000 | [diff] [blame] | 688 | static int get_command(FILE * source, char *command) |
| 689 | { |
| 690 | char *prompt_str; |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 691 | |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 692 | if (source == NULL) { |
| 693 | if (local_pending_command) { |
| 694 | /* a command specified (-c option): return it & mark it done */ |
| 695 | strcpy(command, local_pending_command); |
| 696 | free(local_pending_command); |
| 697 | local_pending_command = NULL; |
| 698 | return 0; |
| 699 | } |
| 700 | return 1; |
| 701 | } |
| 702 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 703 | if (source == stdin) { |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 704 | setup_prompt_string(&prompt_str); |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 705 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 706 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 707 | /* |
| 708 | ** enable command line editing only while a command line |
| 709 | ** is actually being read; otherwise, we'll end up bequeathing |
| 710 | ** atexit() handlers and other unwanted stuff to our |
| 711 | ** child processes (rob@sysgo.de) |
| 712 | */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 713 | cmdedit_read_input(prompt_str, command); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 714 | return 0; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 715 | #else |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 716 | fputs(prompt_str, stdout); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 717 | #endif |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 718 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 719 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 720 | if (!fgets(command, BUFSIZ - 2, source)) { |
| 721 | if (source == stdin) |
| 722 | printf("\n"); |
| 723 | return 1; |
| 724 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 725 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 726 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 729 | static char* itoa(register int i) |
| 730 | { |
| 731 | static char a[7]; /* Max 7 ints */ |
| 732 | register char *b = a + sizeof(a) - 1; |
| 733 | int sign = (i < 0); |
| 734 | |
| 735 | if (sign) |
| 736 | i = -i; |
| 737 | *b = 0; |
| 738 | do |
| 739 | { |
| 740 | *--b = '0' + (i % 10); |
| 741 | i /= 10; |
| 742 | } |
| 743 | while (i); |
| 744 | if (sign) |
| 745 | *--b = '-'; |
| 746 | return b; |
| 747 | } |
| 748 | |
| 749 | char * strsep_space( char *string, int * ix) |
| 750 | { |
| 751 | char *token, *begin; |
| 752 | |
| 753 | begin = string; |
| 754 | |
| 755 | /* Short circuit the trivial case */ |
| 756 | if ( !string || ! string[*ix]) |
| 757 | return NULL; |
| 758 | |
| 759 | /* Find the end of the token. */ |
| 760 | while( string && string[*ix] && !isspace(string[*ix]) ) { |
| 761 | (*ix)++; |
| 762 | } |
| 763 | |
| 764 | /* Find the end of any whitespace trailing behind |
| 765 | * the token and let that be part of the token */ |
| 766 | while( string && string[*ix] && isspace(string[*ix]) ) { |
| 767 | (*ix)++; |
| 768 | } |
| 769 | |
| 770 | if (! string && *ix==0) { |
| 771 | /* Nothing useful was found */ |
| 772 | return NULL; |
| 773 | } |
| 774 | |
| 775 | token = xmalloc(*ix+1); |
| 776 | token[*ix] = '\0'; |
| 777 | strncpy(token, string, *ix); |
| 778 | |
| 779 | return token; |
| 780 | } |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 781 | |
Eric Andersen | ca60459 | 2001-03-08 17:17:13 +0000 | [diff] [blame] | 782 | static int expand_arguments(char *command) |
| 783 | { |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 784 | int total_length=0, length, i, retval, ix = 0; |
| 785 | expand_t expand_result; |
| 786 | char *tmpcmd, *cmd, *cmd_copy; |
| 787 | char *src, *dst, *var; |
| 788 | const char *out_of_space = "out of space during expansion"; |
| 789 | int flags = GLOB_NOCHECK |
| 790 | #ifdef GLOB_BRACE |
| 791 | | GLOB_BRACE |
| 792 | #endif |
| 793 | #ifdef GLOB_TILDE |
| 794 | | GLOB_TILDE |
| 795 | #endif |
| 796 | ; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 797 | |
Eric Andersen | ca60459 | 2001-03-08 17:17:13 +0000 | [diff] [blame] | 798 | /* get rid of the terminating \n */ |
| 799 | chomp(command); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 800 | |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 801 | /* Fix up escape sequences to be the Real Thing(tm) */ |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 802 | while( command && command[ix]) { |
| 803 | if (command[ix] == '\\') { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 804 | const char *tmp = command+ix+1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 805 | command[ix] = bb_process_escape_sequence( &tmp ); |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 806 | memmove(command+ix + 1, tmp, strlen(tmp)+1); |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 807 | } |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 808 | ix++; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 809 | } |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 810 | /* Use glob and then fixup environment variables and such */ |
| 811 | |
| 812 | /* It turns out that glob is very stupid. We have to feed it one word at a |
| 813 | * time since it can't cope with a full string. Here we convert command |
| 814 | * (char*) into cmd (char**, one word per string) */ |
| 815 | |
| 816 | /* We need a clean copy, so strsep can mess up the copy while |
| 817 | * we write stuff into the original (in a minute) */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 818 | cmd = cmd_copy = bb_xstrdup(command); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 819 | *command = '\0'; |
| 820 | for (ix = 0, tmpcmd = cmd; |
| 821 | (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) { |
| 822 | if (*tmpcmd == '\0') |
| 823 | break; |
| 824 | /* we need to trim() the result for glob! */ |
| 825 | trim(tmpcmd); |
| 826 | retval = glob(tmpcmd, flags, NULL, &expand_result); |
| 827 | free(tmpcmd); /* Free mem allocated by strsep_space */ |
| 828 | if (retval == GLOB_NOSPACE) { |
| 829 | /* Mem may have been allocated... */ |
| 830 | globfree (&expand_result); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 831 | bb_error_msg(out_of_space); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 832 | return FALSE; |
| 833 | } else if (retval != 0) { |
| 834 | /* Some other error. GLOB_NOMATCH shouldn't |
| 835 | * happen because of the GLOB_NOCHECK flag in |
| 836 | * the glob call. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 837 | bb_error_msg("syntax error"); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 838 | return FALSE; |
| 839 | } else { |
| 840 | /* Convert from char** (one word per string) to a simple char*, |
| 841 | * but don't overflow command which is BUFSIZ in length */ |
| 842 | for (i=0; i < expand_result.gl_pathc; i++) { |
| 843 | length=strlen(expand_result.gl_pathv[i]); |
| 844 | if (total_length+length+1 >= BUFSIZ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 845 | bb_error_msg(out_of_space); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 846 | return FALSE; |
| 847 | } |
| 848 | strcat(command+total_length, " "); |
| 849 | total_length+=1; |
| 850 | strcat(command+total_length, expand_result.gl_pathv[i]); |
| 851 | total_length+=length; |
| 852 | } |
| 853 | globfree (&expand_result); |
| 854 | } |
| 855 | } |
| 856 | free(cmd_copy); |
| 857 | trim(command); |
| 858 | |
| 859 | /* Now do the shell variable substitutions which |
| 860 | * wordexp can't do for us, namely $? and $! */ |
| 861 | src = command; |
| 862 | while((dst = strchr(src,'$')) != NULL){ |
| 863 | var = NULL; |
| 864 | switch(*(dst+1)) { |
| 865 | case '?': |
| 866 | var = itoa(last_return_code); |
| 867 | break; |
| 868 | case '!': |
| 869 | if (last_bg_pid==-1) |
| 870 | *(var)='\0'; |
| 871 | else |
| 872 | var = itoa(last_bg_pid); |
| 873 | break; |
| 874 | /* Everything else like $$, $#, $[0-9], etc. should all be |
| 875 | * expanded by wordexp(), so we can in theory skip that stuff |
| 876 | * here, but just to be on the safe side (i.e., since uClibc |
| 877 | * wordexp doesn't do this stuff yet), lets leave it in for |
| 878 | * now. */ |
| 879 | case '$': |
| 880 | var = itoa(getpid()); |
| 881 | break; |
| 882 | case '#': |
| 883 | var = itoa(argc-1); |
| 884 | break; |
| 885 | case '0':case '1':case '2':case '3':case '4': |
| 886 | case '5':case '6':case '7':case '8':case '9': |
| 887 | { |
Eric Andersen | 7ab93d9 | 2002-11-08 09:40:02 +0000 | [diff] [blame] | 888 | int ixx=*(dst+1)-48+1; |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 889 | if (ixx >= argc) { |
| 890 | var='\0'; |
| 891 | } else { |
| 892 | var = argv[ixx]; |
| 893 | } |
| 894 | } |
| 895 | break; |
| 896 | |
| 897 | } |
| 898 | if (var) { |
| 899 | /* a single character construction was found, and |
| 900 | * already handled in the case statement */ |
| 901 | src=dst+2; |
| 902 | } else { |
| 903 | /* Looks like an environment variable */ |
| 904 | char delim_hold; |
| 905 | int num_skip_chars=0; |
| 906 | int dstlen = strlen(dst); |
| 907 | /* Is this a ${foo} type variable? */ |
| 908 | if (dstlen >=2 && *(dst+1) == '{') { |
| 909 | src=strchr(dst+1, '}'); |
| 910 | num_skip_chars=1; |
| 911 | } else { |
| 912 | src=dst+1; |
| 913 | while(isalnum(*src) || *src=='_') src++; |
| 914 | } |
| 915 | if (src == NULL) { |
| 916 | src = dst+dstlen; |
| 917 | } |
| 918 | delim_hold=*src; |
| 919 | *src='\0'; /* temporary */ |
| 920 | var = getenv(dst + 1 + num_skip_chars); |
| 921 | *src=delim_hold; |
| 922 | src += num_skip_chars; |
| 923 | } |
| 924 | if (var == NULL) { |
| 925 | /* Seems we got an un-expandable variable. So delete it. */ |
| 926 | var = ""; |
| 927 | } |
| 928 | { |
| 929 | int subst_len = strlen(var); |
| 930 | int trail_len = strlen(src); |
| 931 | if (dst+subst_len+trail_len >= command+BUFSIZ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 932 | bb_error_msg(out_of_space); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 933 | return FALSE; |
| 934 | } |
| 935 | /* Move stuff to the end of the string to accommodate |
| 936 | * filling the created gap with the new stuff */ |
| 937 | memmove(dst+subst_len, src, trail_len+1); |
| 938 | /* Now copy in the new stuff */ |
| 939 | memcpy(dst, var, subst_len); |
| 940 | src = dst+subst_len; |
| 941 | } |
| 942 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 943 | |
Eric Andersen | ca60459 | 2001-03-08 17:17:13 +0000 | [diff] [blame] | 944 | return TRUE; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 947 | /* Return cmd->num_progs as 0 if no command is present (e.g. an empty |
| 948 | line). If a valid command is found, command_ptr is set to point to |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 949 | the beginning of the next command (if the original command had more |
| 950 | then one job associated with it) or NULL if no more commands are |
| 951 | present. */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 952 | static int parse_command(char **command_ptr, struct job *job, int *inbg) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 953 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 954 | char *command; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 955 | char *return_command = NULL; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 956 | char *src, *buf, *chptr; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 957 | int argc_l = 0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 958 | int done = 0; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 959 | int argv_alloced; |
Matt Kraai | be66ad3 | 2001-04-12 15:42:17 +0000 | [diff] [blame] | 960 | int i, saw_quote = 0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 961 | char quote = '\0'; |
| 962 | int count; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 963 | struct child_prog *prog; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 964 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 965 | /* skip leading white space */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 966 | while (**command_ptr && isspace(**command_ptr)) |
| 967 | (*command_ptr)++; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 968 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 969 | /* this handles empty lines or leading '#' characters */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 970 | if (!**command_ptr || (**command_ptr == '#')) { |
| 971 | job->num_progs=0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 972 | return 0; |
| 973 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 974 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 975 | *inbg = 0; |
| 976 | job->num_progs = 1; |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 977 | job->progs = xmalloc(sizeof(*job->progs)); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 978 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 979 | /* We set the argv elements to point inside of this string. The |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 980 | memory is freed by free_job(). Allocate twice the original |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 981 | length in case we need to quote every single character. |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 982 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 983 | Getting clean memory relieves us of the task of NULL |
| 984 | terminating things and makes the rest of this look a bit |
| 985 | cleaner (though it is, admittedly, a tad less efficient) */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 986 | job->cmdbuf = command = xcalloc(2*strlen(*command_ptr) + 1, sizeof(char)); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 987 | job->text = NULL; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 988 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 989 | prog = job->progs; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 990 | prog->num_redirects = 0; |
| 991 | prog->redirects = NULL; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 992 | prog->is_stopped = 0; |
| 993 | prog->family = job; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 994 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 995 | argv_alloced = 5; |
| 996 | prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced); |
| 997 | prog->argv[0] = job->cmdbuf; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 998 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 999 | buf = command; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1000 | src = *command_ptr; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1001 | while (*src && !done) { |
| 1002 | if (quote == *src) { |
| 1003 | quote = '\0'; |
| 1004 | } else if (quote) { |
| 1005 | if (*src == '\\') { |
| 1006 | src++; |
| 1007 | if (!*src) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1008 | bb_error_msg("character expected after \\"); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1009 | free_job(job); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1010 | return 1; |
| 1011 | } |
| 1012 | |
| 1013 | /* in shell, "\'" should yield \' */ |
Eric Andersen | b2356f6 | 2000-12-11 19:14:40 +0000 | [diff] [blame] | 1014 | if (*src != quote) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1015 | *buf++ = '\\'; |
Eric Andersen | b2356f6 | 2000-12-11 19:14:40 +0000 | [diff] [blame] | 1016 | *buf++ = '\\'; |
| 1017 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1018 | } else if (*src == '*' || *src == '?' || *src == '[' || |
| 1019 | *src == ']') *buf++ = '\\'; |
| 1020 | *buf++ = *src; |
| 1021 | } else if (isspace(*src)) { |
Matt Kraai | be66ad3 | 2001-04-12 15:42:17 +0000 | [diff] [blame] | 1022 | if (*prog->argv[argc_l] || saw_quote) { |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1023 | buf++, argc_l++; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1024 | /* +1 here leaves room for the NULL which ends argv */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1025 | if ((argc_l + 1) == argv_alloced) { |
| 1026 | argv_alloced += 5; |
Matt Kraai | b890752 | 2000-09-13 02:08:21 +0000 | [diff] [blame] | 1027 | prog->argv = xrealloc(prog->argv, |
| 1028 | sizeof(*prog->argv) * |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1029 | argv_alloced); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1030 | } |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1031 | prog->argv[argc_l] = buf; |
Matt Kraai | be66ad3 | 2001-04-12 15:42:17 +0000 | [diff] [blame] | 1032 | saw_quote = 0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1033 | } |
| 1034 | } else |
| 1035 | switch (*src) { |
| 1036 | case '"': |
| 1037 | case '\'': |
| 1038 | quote = *src; |
Matt Kraai | be66ad3 | 2001-04-12 15:42:17 +0000 | [diff] [blame] | 1039 | saw_quote = 1; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1040 | break; |
| 1041 | |
| 1042 | case '#': /* comment */ |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1043 | if (*(src-1)== '$') |
| 1044 | *buf++ = *src; |
| 1045 | else |
| 1046 | done = 1; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1047 | break; |
| 1048 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1049 | case '>': /* redirects */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1050 | case '<': |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1051 | i = prog->num_redirects++; |
| 1052 | prog->redirects = xrealloc(prog->redirects, |
| 1053 | sizeof(*prog->redirects) * |
Matt Kraai | b890752 | 2000-09-13 02:08:21 +0000 | [diff] [blame] | 1054 | (i + 1)); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1055 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1056 | prog->redirects[i].fd = -1; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1057 | if (buf != prog->argv[argc_l]) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1058 | /* the stuff before this character may be the file number |
| 1059 | being redirected */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1060 | prog->redirects[i].fd = |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1061 | strtol(prog->argv[argc_l], &chptr, 10); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1062 | |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1063 | if (*chptr && *prog->argv[argc_l]) { |
| 1064 | buf++, argc_l++; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1065 | prog->argv[argc_l] = buf; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1069 | if (prog->redirects[i].fd == -1) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1070 | if (*src == '>') |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1071 | prog->redirects[i].fd = 1; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1072 | else |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1073 | prog->redirects[i].fd = 0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | if (*src++ == '>') { |
| 1077 | if (*src == '>') |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1078 | prog->redirects[i].type = |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1079 | REDIRECT_APPEND, src++; |
| 1080 | else |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1081 | prog->redirects[i].type = REDIRECT_OVERWRITE; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1082 | } else { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1083 | prog->redirects[i].type = REDIRECT_INPUT; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /* This isn't POSIX sh compliant. Oh well. */ |
| 1087 | chptr = src; |
| 1088 | while (isspace(*chptr)) |
| 1089 | chptr++; |
| 1090 | |
| 1091 | if (!*chptr) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1092 | bb_error_msg("file name expected after %c", *(src-1)); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1093 | free_job(job); |
| 1094 | job->num_progs=0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1095 | return 1; |
| 1096 | } |
| 1097 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1098 | prog->redirects[i].filename = buf; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1099 | while (*chptr && !isspace(*chptr)) |
| 1100 | *buf++ = *chptr++; |
| 1101 | |
| 1102 | src = chptr - 1; /* we src++ later */ |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1103 | prog->argv[argc_l] = ++buf; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1104 | break; |
| 1105 | |
| 1106 | case '|': /* pipe */ |
| 1107 | /* finish this command */ |
Matt Kraai | be66ad3 | 2001-04-12 15:42:17 +0000 | [diff] [blame] | 1108 | if (*prog->argv[argc_l] || saw_quote) |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1109 | argc_l++; |
| 1110 | if (!argc_l) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1111 | bb_error_msg("empty command in pipe"); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1112 | free_job(job); |
| 1113 | job->num_progs=0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1114 | return 1; |
| 1115 | } |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1116 | prog->argv[argc_l] = NULL; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1117 | |
| 1118 | /* and start the next */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1119 | job->num_progs++; |
Matt Kraai | b890752 | 2000-09-13 02:08:21 +0000 | [diff] [blame] | 1120 | job->progs = xrealloc(job->progs, |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1121 | sizeof(*job->progs) * job->num_progs); |
| 1122 | prog = job->progs + (job->num_progs - 1); |
| 1123 | prog->num_redirects = 0; |
| 1124 | prog->redirects = NULL; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1125 | prog->is_stopped = 0; |
| 1126 | prog->family = job; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1127 | argc_l = 0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1128 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1129 | argv_alloced = 5; |
| 1130 | prog->argv = xmalloc(sizeof(*prog->argv) * argv_alloced); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1131 | prog->argv[0] = ++buf; |
| 1132 | |
| 1133 | src++; |
| 1134 | while (*src && isspace(*src)) |
| 1135 | src++; |
| 1136 | |
| 1137 | if (!*src) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1138 | bb_error_msg("empty command in pipe"); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1139 | free_job(job); |
| 1140 | job->num_progs=0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1141 | return 1; |
| 1142 | } |
| 1143 | src--; /* we'll ++ it at the end of the loop */ |
| 1144 | |
| 1145 | break; |
| 1146 | |
| 1147 | case '&': /* background */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1148 | *inbg = 1; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1149 | case ';': /* multiple commands */ |
| 1150 | done = 1; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1151 | return_command = *command_ptr + (src - *command_ptr) + 1; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1152 | break; |
| 1153 | |
Matt Kraai | 131241f | 2000-09-14 00:43:20 +0000 | [diff] [blame] | 1154 | case '\\': |
| 1155 | src++; |
| 1156 | if (!*src) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1157 | bb_error_msg("character expected after \\"); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1158 | free_job(job); |
Matt Kraai | 131241f | 2000-09-14 00:43:20 +0000 | [diff] [blame] | 1159 | return 1; |
| 1160 | } |
| 1161 | if (*src == '*' || *src == '[' || *src == ']' |
| 1162 | || *src == '?') *buf++ = '\\'; |
| 1163 | /* fallthrough */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1164 | default: |
| 1165 | *buf++ = *src; |
| 1166 | } |
| 1167 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1168 | src++; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1169 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1170 | |
Matt Kraai | be66ad3 | 2001-04-12 15:42:17 +0000 | [diff] [blame] | 1171 | if (*prog->argv[argc_l] || saw_quote) { |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1172 | argc_l++; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1173 | } |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1174 | if (!argc_l) { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1175 | free_job(job); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1178 | prog->argv[argc_l] = NULL; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1179 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1180 | if (!return_command) { |
| 1181 | job->text = xmalloc(strlen(*command_ptr) + 1); |
| 1182 | strcpy(job->text, *command_ptr); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1183 | } else { |
| 1184 | /* This leaves any trailing spaces, which is a bit sloppy */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1185 | count = return_command - *command_ptr; |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 1186 | job->text = xmalloc(count + 1); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1187 | strncpy(job->text, *command_ptr, count); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1188 | job->text[count] = '\0'; |
| 1189 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1190 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1191 | *command_ptr = return_command; |
Eric Andersen | 46f0beb | 2000-11-14 21:59:22 +0000 | [diff] [blame] | 1192 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1193 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1196 | /* Run the child_prog, no matter what kind of command it uses. |
| 1197 | */ |
| 1198 | static int pseudo_exec(struct child_prog *child) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1199 | { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1200 | struct built_in_command *x; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1201 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | af4ac77 | 2001-02-01 22:43:49 +0000 | [diff] [blame] | 1202 | char *name; |
Erik Andersen | bcd6177 | 2000-05-13 06:33:19 +0000 | [diff] [blame] | 1203 | #endif |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1204 | |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1205 | /* Check if the command matches any of the non-forking builtins. |
| 1206 | * Depending on context, this might be redundant. But it's |
| 1207 | * easier to waste a few CPU cycles than it is to figure out |
| 1208 | * if this is one of those cases. |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1209 | */ |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1210 | for (x = bltins; x->cmd; x++) { |
| 1211 | if (strcmp(child->argv[0], x->cmd) == 0 ) { |
Eric Andersen | 6f2ebca | 2002-09-26 13:59:40 +0000 | [diff] [blame] | 1212 | _exit(x->function(child)); |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | /* Check if the command matches any of the forking builtins. */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1217 | for (x = bltins_forking; x->cmd; x++) { |
| 1218 | if (strcmp(child->argv[0], x->cmd) == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1219 | bb_applet_name=x->cmd; |
Eric Andersen | 6f2ebca | 2002-09-26 13:59:40 +0000 | [diff] [blame] | 1220 | _exit (x->function(child)); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1221 | } |
| 1222 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1223 | #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1224 | /* Check if the command matches any busybox internal |
| 1225 | * commands ("applets") here. Following discussions from |
Eric Andersen | 2423b12 | 2001-12-08 01:56:15 +0000 | [diff] [blame] | 1226 | * November 2000 on busybox@busybox.net, don't use |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1227 | * bb_get_last_path_component(). This way explicit (with |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1228 | * slashes) filenames will never be interpreted as an |
| 1229 | * applet, just like with builtins. This way the user can |
| 1230 | * override an applet with an explicit filename reference. |
| 1231 | * The only downside to this change is that an explicit |
| 1232 | * /bin/foo invocation will fork and exec /bin/foo, even if |
| 1233 | * /bin/foo is a symlink to busybox. |
| 1234 | */ |
Matt Kraai | f2cc276 | 2001-02-01 19:21:20 +0000 | [diff] [blame] | 1235 | name = child->argv[0]; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1236 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1237 | #ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN |
| 1238 | /* If you enable CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN, then |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1239 | * if you run /bin/cat, it will use BusyBox cat even if |
| 1240 | * /bin/cat exists on the filesystem and is _not_ busybox. |
| 1241 | * Some systems want this, others do not. Choose wisely. :-) |
| 1242 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1243 | name = bb_get_last_path_component(name); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1244 | #endif |
| 1245 | |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 1246 | { |
Eric Andersen | 82ab8da | 2001-03-23 17:06:01 +0000 | [diff] [blame] | 1247 | char** argv_l=child->argv; |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 1248 | int argc_l; |
Eric Andersen | 82ab8da | 2001-03-23 17:06:01 +0000 | [diff] [blame] | 1249 | for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++); |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 1250 | optind = 1; |
| 1251 | run_applet_by_name(name, argc_l, child->argv); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1252 | } |
| 1253 | #endif |
| 1254 | |
| 1255 | execvp(child->argv[0], child->argv); |
Eric Andersen | 6f2ebca | 2002-09-26 13:59:40 +0000 | [diff] [blame] | 1256 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1257 | /* Do not use bb_perror_msg_and_die() here, since we must not |
Eric Andersen | 6f2ebca | 2002-09-26 13:59:40 +0000 | [diff] [blame] | 1258 | * call exit() but should call _exit() instead */ |
Eric Andersen | ad08298 | 2002-09-30 20:20:20 +0000 | [diff] [blame] | 1259 | fprintf(stderr, "%s: %m\n", child->argv[0]); |
Eric Andersen | 6f2ebca | 2002-09-26 13:59:40 +0000 | [diff] [blame] | 1260 | _exit(EXIT_FAILURE); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | static void insert_job(struct job *newjob, int inbg) |
| 1264 | { |
| 1265 | struct job *thejob; |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1266 | struct jobset *j_list=newjob->job_list; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1267 | |
| 1268 | /* find the ID for thejob to use */ |
| 1269 | newjob->jobid = 1; |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1270 | for (thejob = j_list->head; thejob; thejob = thejob->next) |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1271 | if (thejob->jobid >= newjob->jobid) |
| 1272 | newjob->jobid = thejob->jobid + 1; |
| 1273 | |
| 1274 | /* add thejob to the list of running jobs */ |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1275 | if (!j_list->head) { |
| 1276 | thejob = j_list->head = xmalloc(sizeof(*thejob)); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1277 | } else { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1278 | for (thejob = j_list->head; thejob->next; thejob = thejob->next) /* nothing */; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1279 | thejob->next = xmalloc(sizeof(*thejob)); |
| 1280 | thejob = thejob->next; |
| 1281 | } |
| 1282 | |
| 1283 | *thejob = *newjob; /* physically copy the struct job */ |
| 1284 | thejob->next = NULL; |
| 1285 | thejob->running_progs = thejob->num_progs; |
| 1286 | thejob->stopped_progs = 0; |
| 1287 | |
| 1288 | if (inbg) { |
| 1289 | /* we don't wait for background thejobs to return -- append it |
| 1290 | to the list of backgrounded thejobs and leave it alone */ |
| 1291 | printf("[%d] %d\n", thejob->jobid, |
| 1292 | newjob->progs[newjob->num_progs - 1].pid); |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 1293 | last_jobid = newjob->jobid; |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 1294 | last_bg_pid=newjob->progs[newjob->num_progs - 1].pid; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1295 | } else { |
| 1296 | newjob->job_list->fg = thejob; |
| 1297 | |
| 1298 | /* move the new process group into the foreground */ |
| 1299 | /* suppress messages when run from /linuxrc mag@sysgo.de */ |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1300 | if (tcsetpgrp(shell_terminal, newjob->pgrp) && errno != ENOTTY) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1301 | bb_perror_msg("tcsetpgrp"); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | static int run_command(struct job *newjob, int inbg, int outpipe[2]) |
| 1306 | { |
| 1307 | /* struct job *thejob; */ |
| 1308 | int i; |
| 1309 | int nextin, nextout; |
| 1310 | int pipefds[2]; /* pipefd[0] is for reading */ |
| 1311 | struct built_in_command *x; |
| 1312 | struct child_prog *child; |
| 1313 | |
Eric Andersen | 6efc48c | 2000-07-18 08:16:39 +0000 | [diff] [blame] | 1314 | nextin = 0, nextout = 1; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1315 | for (i = 0; i < newjob->num_progs; i++) { |
| 1316 | child = & (newjob->progs[i]); |
| 1317 | |
| 1318 | if ((i + 1) < newjob->num_progs) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1319 | if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe"); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1320 | nextout = pipefds[1]; |
| 1321 | } else { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1322 | if (outpipe[1]!=-1) { |
| 1323 | nextout = outpipe[1]; |
Eric Andersen | 6efc48c | 2000-07-18 08:16:39 +0000 | [diff] [blame] | 1324 | } else { |
| 1325 | nextout = 1; |
| 1326 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1329 | |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1330 | /* Check if the command matches any non-forking builtins, |
| 1331 | * but only if this is a simple command. |
| 1332 | * Non-forking builtins within pipes have to fork anyway, |
| 1333 | * and are handled in pseudo_exec. "echo foo | read bar" |
| 1334 | * is doomed to failure, and doesn't work on bash, either. |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1335 | */ |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1336 | if (newjob->num_progs == 1) { |
| 1337 | for (x = bltins; x->cmd; x++) { |
| 1338 | if (strcmp(child->argv[0], x->cmd) == 0 ) { |
| 1339 | int squirrel[] = {-1, -1, -1}; |
| 1340 | int rcode; |
| 1341 | setup_redirects(child, squirrel); |
| 1342 | rcode = x->function(child); |
| 1343 | restore_redirects(squirrel); |
| 1344 | return rcode; |
| 1345 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | |
Eric Andersen | 72f9a42 | 2001-10-28 05:12:20 +0000 | [diff] [blame] | 1349 | #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__) |
| 1350 | if (!(child->pid = fork())) |
| 1351 | #else |
| 1352 | if (!(child->pid = vfork())) |
| 1353 | #endif |
| 1354 | { |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1355 | /* Set the handling for job control signals back to the default. */ |
| 1356 | signal(SIGINT, SIG_DFL); |
| 1357 | signal(SIGQUIT, SIG_DFL); |
| 1358 | signal(SIGTSTP, SIG_DFL); |
| 1359 | signal(SIGTTIN, SIG_DFL); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1360 | signal(SIGTTOU, SIG_DFL); |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1361 | signal(SIGCHLD, SIG_DFL); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1362 | |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 1363 | close_all(); |
| 1364 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1365 | if (outpipe[1]!=-1) { |
| 1366 | close(outpipe[0]); |
Eric Andersen | 6efc48c | 2000-07-18 08:16:39 +0000 | [diff] [blame] | 1367 | } |
| 1368 | if (nextin != 0) { |
| 1369 | dup2(nextin, 0); |
| 1370 | close(nextin); |
| 1371 | } |
| 1372 | |
| 1373 | if (nextout != 1) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1374 | dup2(nextout, 1); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1375 | dup2(nextout, 2); /* Really? */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1376 | close(nextout); |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1377 | close(pipefds[0]); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1380 | /* explicit redirects override pipes */ |
Eric Andersen | e9f07fb | 2000-12-21 18:31:36 +0000 | [diff] [blame] | 1381 | setup_redirects(child,NULL); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1382 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1383 | pseudo_exec(child); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1384 | } |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1385 | if (outpipe[1]!=-1) { |
| 1386 | close(outpipe[1]); |
Eric Andersen | a1d187a | 2000-07-17 19:14:41 +0000 | [diff] [blame] | 1387 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1388 | |
| 1389 | /* put our child in the process group whose leader is the |
| 1390 | first process in this pipe */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1391 | setpgid(child->pid, newjob->progs[0].pid); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1392 | if (nextin != 0) |
| 1393 | close(nextin); |
Eric Andersen | 6efc48c | 2000-07-18 08:16:39 +0000 | [diff] [blame] | 1394 | if (nextout != 1) |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1395 | close(nextout); |
| 1396 | |
| 1397 | /* If there isn't another process, nextin is garbage |
| 1398 | but it doesn't matter */ |
| 1399 | nextin = pipefds[0]; |
| 1400 | } |
| 1401 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1402 | newjob->pgrp = newjob->progs[0].pid; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1403 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1404 | insert_job(newjob, inbg); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1405 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1406 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1409 | static int busy_loop(FILE * input) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1410 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1411 | char *command; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1412 | char *next_command = NULL; |
| 1413 | struct job newjob; |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1414 | pid_t parent_pgrp; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1415 | int i; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1416 | int inbg; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1417 | int status; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1418 | newjob.job_list = &job_list; |
| 1419 | newjob.job_context = DEFAULT_CONTEXT; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1420 | |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1421 | /* save current owner of TTY so we can restore it on exit */ |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1422 | parent_pgrp = tcgetpgrp(shell_terminal); |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1423 | |
Matt Kraai | b890752 | 2000-09-13 02:08:21 +0000 | [diff] [blame] | 1424 | command = (char *) xcalloc(BUFSIZ, sizeof(char)); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1425 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1426 | while (1) { |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1427 | if (!job_list.fg) { |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1428 | /* no job is in the foreground */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1429 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1430 | /* see if any background processes have exited */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1431 | checkjobs(&job_list); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1432 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1433 | if (!next_command) { |
| 1434 | if (get_command(input, command)) |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1435 | break; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1436 | next_command = command; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1437 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1438 | |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 1439 | if (! expand_arguments(next_command)) { |
Eric Andersen | ca60459 | 2001-03-08 17:17:13 +0000 | [diff] [blame] | 1440 | free(command); |
| 1441 | command = (char *) xcalloc(BUFSIZ, sizeof(char)); |
| 1442 | next_command = NULL; |
| 1443 | continue; |
| 1444 | } |
| 1445 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1446 | if (!parse_command(&next_command, &newjob, &inbg) && |
| 1447 | newjob.num_progs) { |
Eric Andersen | a1d187a | 2000-07-17 19:14:41 +0000 | [diff] [blame] | 1448 | int pipefds[2] = {-1,-1}; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1449 | debug_printf( "job=%p fed to run_command by busy_loop()'\n", |
| 1450 | &newjob); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1451 | run_command(&newjob, inbg, pipefds); |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 1452 | } |
| 1453 | else { |
Eric Andersen | a1d187a | 2000-07-17 19:14:41 +0000 | [diff] [blame] | 1454 | free(command); |
Matt Kraai | b890752 | 2000-09-13 02:08:21 +0000 | [diff] [blame] | 1455 | command = (char *) xcalloc(BUFSIZ, sizeof(char)); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1456 | next_command = NULL; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1457 | } |
| 1458 | } else { |
| 1459 | /* a job is running in the foreground; wait for it */ |
| 1460 | i = 0; |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1461 | while (!job_list.fg->progs[i].pid || |
| 1462 | job_list.fg->progs[i].is_stopped == 1) i++; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1463 | |
Eric Andersen | 7373e48 | 2002-07-31 04:04:47 +0000 | [diff] [blame] | 1464 | if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) { |
| 1465 | if (errno != ECHILD) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1466 | bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid); |
Eric Andersen | 7373e48 | 2002-07-31 04:04:47 +0000 | [diff] [blame] | 1467 | } |
| 1468 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1469 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1470 | if (WIFEXITED(status) || WIFSIGNALED(status)) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1471 | /* the child exited */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1472 | job_list.fg->running_progs--; |
| 1473 | job_list.fg->progs[i].pid = 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1474 | |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 1475 | last_return_code=WEXITSTATUS(status); |
| 1476 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1477 | if (!job_list.fg->running_progs) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1478 | /* child exited */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1479 | remove_job(&job_list, job_list.fg); |
| 1480 | job_list.fg = NULL; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1481 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1482 | } else { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1483 | /* the child was stopped */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1484 | job_list.fg->stopped_progs++; |
| 1485 | job_list.fg->progs[i].is_stopped = 1; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1486 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1487 | if (job_list.fg->stopped_progs == job_list.fg->running_progs) { |
| 1488 | printf("\n" JOB_STATUS_FORMAT, job_list.fg->jobid, |
| 1489 | "Stopped", job_list.fg->text); |
| 1490 | job_list.fg = NULL; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1491 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1494 | if (!job_list.fg) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1495 | /* move the shell to the foreground */ |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1496 | /* suppress messages when run from /linuxrc mag@sysgo.de */ |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1497 | if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1498 | bb_perror_msg("tcsetpgrp"); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1502 | free(command); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1503 | |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1504 | /* return controlling TTY back to parent process group before exiting */ |
Eric Andersen | 7373e48 | 2002-07-31 04:04:47 +0000 | [diff] [blame] | 1505 | if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1506 | bb_perror_msg("tcsetpgrp"); |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 1507 | |
| 1508 | /* return exit status if called with "-c" */ |
| 1509 | if (input == NULL && WIFEXITED(status)) |
| 1510 | return WEXITSTATUS(status); |
| 1511 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1512 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1516 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 1517 | void free_memory(void) |
| 1518 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1519 | if (cwd && cwd!=bb_msg_unknown) { |
Eric Andersen | 5d60a46 | 2001-08-22 05:32:24 +0000 | [diff] [blame] | 1520 | free((char*)cwd); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 1521 | } |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 1522 | if (local_pending_command) |
| 1523 | free(local_pending_command); |
| 1524 | |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1525 | if (job_list.fg && !job_list.fg->running_progs) { |
| 1526 | remove_job(&job_list, job_list.fg); |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 1527 | } |
| 1528 | } |
| 1529 | #endif |
| 1530 | |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1531 | /* Make sure we have a controlling tty. If we get started under a job |
| 1532 | * aware app (like bash for example), make sure we are now in charge so |
| 1533 | * we don't fight over who gets the foreground */ |
Eric Andersen | ece8fc2 | 2002-12-11 04:26:28 +0000 | [diff] [blame] | 1534 | static void setup_job_control(void) |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1535 | { |
Eric Andersen | f0a4ac8 | 2001-10-03 11:23:42 +0000 | [diff] [blame] | 1536 | int status; |
| 1537 | |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1538 | /* Loop until we are in the foreground. */ |
Eric Andersen | f0a4ac8 | 2001-10-03 11:23:42 +0000 | [diff] [blame] | 1539 | while ((status = tcgetpgrp (shell_terminal)) >= 0) { |
| 1540 | if (status == (shell_pgrp = getpgrp ())) { |
| 1541 | break; |
| 1542 | } |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1543 | kill (- shell_pgrp, SIGTTIN); |
Eric Andersen | f0a4ac8 | 2001-10-03 11:23:42 +0000 | [diff] [blame] | 1544 | } |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1545 | |
| 1546 | /* Ignore interactive and job-control signals. */ |
| 1547 | signal(SIGINT, SIG_IGN); |
| 1548 | signal(SIGQUIT, SIG_IGN); |
| 1549 | signal(SIGTSTP, SIG_IGN); |
| 1550 | signal(SIGTTIN, SIG_IGN); |
| 1551 | signal(SIGTTOU, SIG_IGN); |
| 1552 | signal(SIGCHLD, SIG_IGN); |
| 1553 | |
| 1554 | /* Put ourselves in our own process group. */ |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 1555 | setsid(); |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1556 | shell_pgrp = getpid (); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 1557 | setpgid (shell_pgrp, shell_pgrp); |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1558 | |
| 1559 | /* Grab control of the terminal. */ |
| 1560 | tcsetpgrp(shell_terminal, shell_pgrp); |
| 1561 | } |
Eric Andersen | 6efc48c | 2000-07-18 08:16:39 +0000 | [diff] [blame] | 1562 | |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 1563 | int lash_main(int argc_l, char **argv_l) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1564 | { |
Eric Andersen | 6a4c33c | 2000-07-28 17:08:36 +0000 | [diff] [blame] | 1565 | int opt, interactive=FALSE; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1566 | FILE *input = stdin; |
Eric Andersen | 6a99aaf | 2000-07-27 00:15:20 +0000 | [diff] [blame] | 1567 | argc = argc_l; |
| 1568 | argv = argv_l; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1569 | |
Eric Andersen | 702ec59 | 2001-03-06 22:17:29 +0000 | [diff] [blame] | 1570 | /* These variables need re-initializing when recursing */ |
Eric Andersen | 8a646dd | 2001-06-21 16:38:11 +0000 | [diff] [blame] | 1571 | last_jobid = 0; |
Eric Andersen | b0970d4 | 2001-01-05 19:34:52 +0000 | [diff] [blame] | 1572 | local_pending_command = NULL; |
Eric Andersen | 702ec59 | 2001-03-06 22:17:29 +0000 | [diff] [blame] | 1573 | close_me_head = NULL; |
Eric Andersen | b0970d4 | 2001-01-05 19:34:52 +0000 | [diff] [blame] | 1574 | job_list.head = NULL; |
| 1575 | job_list.fg = NULL; |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 1576 | last_return_code=1; |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1577 | |
Eric Andersen | b558e76 | 2000-11-30 22:43:16 +0000 | [diff] [blame] | 1578 | if (argv[0] && argv[0][0] == '-') { |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 1579 | FILE *prof_input; |
| 1580 | prof_input = fopen("/etc/profile", "r"); |
Eric Andersen | 4b6b5e4 | 2001-06-27 04:30:11 +0000 | [diff] [blame] | 1581 | if (prof_input) { |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 1582 | int tmp_fd = fileno(prof_input); |
| 1583 | mark_open(tmp_fd); |
| 1584 | /* Now run the file */ |
| 1585 | busy_loop(prof_input); |
| 1586 | fclose(prof_input); |
| 1587 | mark_closed(tmp_fd); |
| 1588 | } |
Eric Andersen | b558e76 | 2000-11-30 22:43:16 +0000 | [diff] [blame] | 1589 | } |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1590 | |
Eric Andersen | f21aa84 | 2000-12-08 20:50:30 +0000 | [diff] [blame] | 1591 | while ((opt = getopt(argc_l, argv_l, "cxi")) > 0) { |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1592 | switch (opt) { |
| 1593 | case 'c': |
| 1594 | input = NULL; |
Matt Kraai | 6085c72 | 2000-09-06 01:46:18 +0000 | [diff] [blame] | 1595 | if (local_pending_command != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1596 | bb_error_msg_and_die("multiple -c arguments"); |
| 1597 | local_pending_command = bb_xstrdup(argv[optind]); |
Matt Kraai | 6085c72 | 2000-09-06 01:46:18 +0000 | [diff] [blame] | 1598 | optind++; |
| 1599 | argv = argv+optind; |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1600 | break; |
Eric Andersen | 6a4c33c | 2000-07-28 17:08:36 +0000 | [diff] [blame] | 1601 | case 'i': |
| 1602 | interactive = TRUE; |
| 1603 | break; |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1604 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1605 | bb_show_usage(); |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1606 | } |
| 1607 | } |
Eric Andersen | 6a4c33c | 2000-07-28 17:08:36 +0000 | [diff] [blame] | 1608 | /* A shell is interactive if the `-i' flag was given, or if all of |
| 1609 | * the following conditions are met: |
| 1610 | * no -c command |
| 1611 | * no arguments remaining or the -s flag given |
| 1612 | * standard input is a terminal |
| 1613 | * standard output is a terminal |
| 1614 | * Refer to Posix.2, the description of the `sh' utility. */ |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1615 | if (argv[optind]==NULL && input==stdin && |
| 1616 | isatty(fileno(stdin)) && isatty(fileno(stdout))) { |
| 1617 | interactive=TRUE; |
| 1618 | } |
Eric Andersen | 2d848a4 | 2001-06-25 17:11:54 +0000 | [diff] [blame] | 1619 | setup_job_control(); |
Eric Andersen | 8634977 | 2000-12-18 20:25:50 +0000 | [diff] [blame] | 1620 | if (interactive==TRUE) { |
Eric Andersen | 6f65a3a | 2001-01-20 01:10:07 +0000 | [diff] [blame] | 1621 | //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]); |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1622 | /* Looks like they want an interactive shell */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1623 | #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET |
Matt Kraai | 4ef40c0 | 2001-04-12 20:44:21 +0000 | [diff] [blame] | 1624 | printf( "\n\n" BB_BANNER " Built-in shell (lash)\n"); |
Eric Andersen | 6f65a3a | 2001-01-20 01:10:07 +0000 | [diff] [blame] | 1625 | printf( "Enter 'help' for a list of built-in commands.\n\n"); |
Eric Andersen | d63dee4 | 2001-10-19 00:22:23 +0000 | [diff] [blame] | 1626 | #endif |
Eric Andersen | 6a4c33c | 2000-07-28 17:08:36 +0000 | [diff] [blame] | 1627 | } else if (local_pending_command==NULL) { |
Eric Andersen | 6f65a3a | 2001-01-20 01:10:07 +0000 | [diff] [blame] | 1628 | //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1629 | input = bb_xfopen(argv[optind], "r"); |
Eric Andersen | 8ea28be | 2001-01-05 20:58:22 +0000 | [diff] [blame] | 1630 | mark_open(fileno(input)); /* be lazy, never mark this closed */ |
Eric Andersen | 501c88b | 2000-07-28 15:14:45 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Eric Andersen | 6efc48c | 2000-07-18 08:16:39 +0000 | [diff] [blame] | 1633 | /* initialize the cwd -- this is never freed...*/ |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 1634 | cwd = xgetcwd(0); |
Eric Andersen | 5f265b7 | 2001-05-11 16:58:46 +0000 | [diff] [blame] | 1635 | if (!cwd) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1636 | cwd = bb_msg_unknown; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1637 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1638 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Eric Andersen | fad9c11 | 2000-07-25 18:06:52 +0000 | [diff] [blame] | 1639 | atexit(free_memory); |
| 1640 | #endif |
| 1641 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1642 | #ifdef CONFIG_FEATURE_COMMAND_EDITING |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1643 | cmdedit_set_initial_prompt(); |
| 1644 | #else |
| 1645 | PS1 = NULL; |
Eric Andersen | b3d6e2d | 2001-03-13 22:57:56 +0000 | [diff] [blame] | 1646 | #endif |
| 1647 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1648 | return (busy_loop(input)); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1649 | } |