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