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