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