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