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