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