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