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