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 | |
| 28 | #include "internal.h" |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <ctype.h> |
| 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <glob.h> |
| 35 | #include <signal.h> |
| 36 | #include <string.h> |
| 37 | #include <sys/ioctl.h> |
| 38 | #include <sys/wait.h> |
| 39 | #include <unistd.h> |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 40 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
| 41 | #include "cmdedit.h" |
| 42 | #endif |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 43 | |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 44 | #define MAX_READ 128 /* size of input buffer for `read' builtin */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 45 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
| 46 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 47 | |
| 48 | enum redirectionType { REDIRECT_INPUT, REDIRECT_OVERWRITE, |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 49 | REDIRECT_APPEND |
| 50 | }; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 51 | |
| 52 | struct jobSet { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 53 | struct job *head; /* head of list of running jobs */ |
| 54 | struct job *fg; /* current foreground job */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | struct redirectionSpecifier { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 58 | enum redirectionType type; /* type of redirection */ |
| 59 | int fd; /* file descriptor being redirected */ |
| 60 | char *filename; /* file to redirect fd to */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | struct childProgram { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 64 | pid_t pid; /* 0 if exited */ |
| 65 | char **argv; /* program name and arguments */ |
| 66 | int numRedirections; /* elements in redirection array */ |
| 67 | struct redirectionSpecifier *redirections; /* I/O redirections */ |
| 68 | glob_t globResult; /* result of parameter globbing */ |
| 69 | int freeGlob; /* should we globfree(&globResult)? */ |
| 70 | int isStopped; /* is the program currently running? */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | struct job { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 74 | int jobId; /* job number */ |
| 75 | int numProgs; /* total number of programs in job */ |
| 76 | int runningProgs; /* number of programs running */ |
| 77 | char *text; /* name of job */ |
| 78 | char *cmdBuf; /* buffer various argv's point into */ |
| 79 | pid_t pgrp; /* process group ID for the job */ |
| 80 | struct childProgram *progs; /* array of programs in job */ |
| 81 | struct job *next; /* to track background commands */ |
| 82 | int stoppedProgs; /* number of programs alive, but stopped */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | struct builtInCommand { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 86 | char *cmd; /* name */ |
| 87 | char *descr; /* description */ |
| 88 | char *usage; /* usage */ |
| 89 | int (*function) (struct job *, struct jobSet * jobList); /* function ptr */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 92 | /* function prototypes for builtins */ |
| 93 | static int builtin_cd(struct job *cmd, struct jobSet *junk); |
| 94 | static int builtin_env(struct job *dummy, struct jobSet *junk); |
| 95 | static int builtin_exit(struct job *cmd, struct jobSet *junk); |
| 96 | static int builtin_fg_bg(struct job *cmd, struct jobSet *jobList); |
| 97 | static int builtin_help(struct job *cmd, struct jobSet *junk); |
| 98 | static int builtin_jobs(struct job *dummy, struct jobSet *jobList); |
| 99 | static int builtin_pwd(struct job *dummy, struct jobSet *junk); |
| 100 | static int builtin_export(struct job *cmd, struct jobSet *junk); |
| 101 | static int builtin_source(struct job *cmd, struct jobSet *jobList); |
| 102 | static int builtin_unset(struct job *cmd, struct jobSet *junk); |
| 103 | static int builtin_read(struct job *cmd, struct jobSet *junk); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 104 | |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 105 | |
| 106 | /* function prototypes for shell stuff */ |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 107 | static void checkJobs(struct jobSet *jobList); |
| 108 | static int getCommand(FILE * source, char *command); |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 109 | static int parseCommand(char **commandPtr, struct job *job, struct jobSet *jobList, int *isBg); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 110 | static int setupRedirections(struct childProgram *prog); |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 111 | static int runCommand(struct job *newJob, struct jobSet *jobList, int inBg); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 112 | static int busy_loop(FILE * input); |
| 113 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 114 | |
Mark Whitley | 37653aa | 2000-07-12 23:36:17 +0000 | [diff] [blame] | 115 | /* Table of built-in functions (these are non-forking builtins, meaning they |
| 116 | * can change global variables in the parent shell process but they will not |
| 117 | * work with pipes and redirects; 'unset foo | whatever' will not work) */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 118 | static struct builtInCommand bltins[] = { |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 119 | {"bg", "Resume a job in the background", "bg [%%job]", builtin_fg_bg}, |
| 120 | {"cd", "Change working directory", "cd [dir]", builtin_cd}, |
| 121 | {"exit", "Exit from shell()", "exit", builtin_exit}, |
| 122 | {"fg", "Bring job into the foreground", "fg [%%job]", builtin_fg_bg}, |
| 123 | {"jobs", "Lists the active jobs", "jobs", builtin_jobs}, |
| 124 | {"export", "Set environment variable", "export [VAR=value]", builtin_export}, |
| 125 | {"unset", "Unset environment variable", "unset VAR", builtin_unset}, |
| 126 | {"read", "Input environment variable", "read [VAR]", builtin_read}, |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 127 | {NULL, NULL, NULL, NULL} |
| 128 | }; |
| 129 | |
Mark Whitley | 37653aa | 2000-07-12 23:36:17 +0000 | [diff] [blame] | 130 | /* Table of forking built-in functions (things that fork cannot change global |
| 131 | * variables in the parent process, such as the current working directory) */ |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 132 | static struct builtInCommand bltins_forking[] = { |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 133 | {"env", "Print all environment variables", "env", builtin_env}, |
| 134 | {"pwd", "Print current directory", "pwd", builtin_pwd}, |
| 135 | {".", "Source-in and run commands in a file", ". filename", builtin_source}, |
| 136 | {"help", "List shell built-in commands", "help", builtin_help}, |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 137 | {NULL, NULL, NULL, NULL} |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | static const char shell_usage[] = |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 141 | "sh [FILE]...\n" |
| 142 | " or: sh -c command [args]...\n" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 143 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 144 | "\nlash: The BusyBox command interpreter (shell).\n\n" |
| 145 | #endif |
| 146 | ; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 147 | |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 148 | static char *prompt = "# "; |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 149 | static char *cwd = NULL; |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 150 | static char *local_pending_command = NULL; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 151 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 152 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
| 153 | void win_changed(int sig) |
| 154 | { |
| 155 | struct winsize win = { 0, 0 }; |
| 156 | ioctl(0, TIOCGWINSZ, &win); |
| 157 | if (win.ws_col > 0) { |
| 158 | cmdedit_setwidth( win.ws_col - 1); |
| 159 | } |
| 160 | } |
| 161 | #endif |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 162 | |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 163 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 164 | /* built-in 'cd <path>' handler */ |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 165 | static int builtin_cd(struct job *cmd, struct jobSet *junk) |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 166 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 167 | char *newdir; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 168 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 169 | if (!cmd->progs[0].argv[1] == 1) |
| 170 | newdir = getenv("HOME"); |
| 171 | else |
| 172 | newdir = cmd->progs[0].argv[1]; |
| 173 | if (chdir(newdir)) { |
| 174 | printf("cd: %s: %s\n", newdir, strerror(errno)); |
| 175 | return FALSE; |
| 176 | } |
| 177 | getcwd(cwd, sizeof(cwd)); |
| 178 | |
| 179 | return TRUE; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* built-in 'env' handler */ |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 183 | static int builtin_env(struct job *dummy, struct jobSet *junk) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 184 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 185 | char **e; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 186 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 187 | for (e = environ; *e; e++) { |
| 188 | fprintf(stdout, "%s\n", *e); |
| 189 | } |
| 190 | return (0); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* built-in 'exit' handler */ |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 194 | static int builtin_exit(struct job *cmd, struct jobSet *junk) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 195 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 196 | if (!cmd->progs[0].argv[1] == 1) |
| 197 | exit TRUE; |
| 198 | |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 199 | return(atoi(cmd->progs[0].argv[1])); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* built-in 'fg' and 'bg' handler */ |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 203 | static int builtin_fg_bg(struct job *cmd, struct jobSet *jobList) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 204 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 205 | int i, jobNum; |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 206 | struct job *job=NULL; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 207 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 208 | if (!jobList->head) { |
| 209 | if (!cmd->progs[0].argv[1] || cmd->progs[0].argv[2]) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 210 | errorMsg("%s: exactly one argument is expected\n", |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 211 | cmd->progs[0].argv[0]); |
| 212 | return FALSE; |
| 213 | } |
| 214 | if (sscanf(cmd->progs[0].argv[1], "%%%d", &jobNum) != 1) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 215 | errorMsg("%s: bad argument '%s'\n", |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 216 | cmd->progs[0].argv[0], cmd->progs[0].argv[1]); |
| 217 | return FALSE; |
| 218 | for (job = jobList->head; job; job = job->next) { |
| 219 | if (job->jobId == jobNum) { |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } else { |
| 225 | job = jobList->head; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 226 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 227 | |
| 228 | if (!job) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 229 | errorMsg("%s: unknown job %d\n", |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 230 | cmd->progs[0].argv[0], jobNum); |
| 231 | return FALSE; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 232 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 233 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 234 | if (*cmd->progs[0].argv[0] == 'f') { |
| 235 | /* Make this job the foreground job */ |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 236 | /* suppress messages when run from /linuxrc mag@sysgo.de */ |
| 237 | if (tcsetpgrp(0, job->pgrp) && errno != ENOTTY) |
| 238 | perror("tcsetpgrp"); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 239 | jobList->fg = job; |
| 240 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 241 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 242 | /* Restart the processes in the job */ |
| 243 | for (i = 0; i < job->numProgs; i++) |
| 244 | job->progs[i].isStopped = 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 245 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 246 | kill(-job->pgrp, SIGCONT); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 247 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 248 | job->stoppedProgs = 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 249 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 250 | return TRUE; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* built-in 'help' handler */ |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 254 | static int builtin_help(struct job *cmd, struct jobSet *junk) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 255 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 256 | struct builtInCommand *x; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 257 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 258 | fprintf(stdout, "\nBuilt-in commands:\n"); |
| 259 | fprintf(stdout, "-------------------\n"); |
| 260 | for (x = bltins; x->cmd; x++) { |
| 261 | fprintf(stdout, "%s\t%s\n", x->cmd, x->descr); |
| 262 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 263 | for (x = bltins_forking; x->cmd; x++) { |
| 264 | fprintf(stdout, "%s\t%s\n", x->cmd, x->descr); |
| 265 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 266 | fprintf(stdout, "\n\n"); |
| 267 | return TRUE; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* built-in 'jobs' handler */ |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 271 | static int builtin_jobs(struct job *dummy, struct jobSet *jobList) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 272 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 273 | struct job *job; |
| 274 | char *statusString; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 275 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 276 | for (job = jobList->head; job; job = job->next) { |
| 277 | if (job->runningProgs == job->stoppedProgs) |
| 278 | statusString = "Stopped"; |
| 279 | else |
| 280 | statusString = "Running"; |
| 281 | |
| 282 | printf(JOB_STATUS_FORMAT, job->jobId, statusString, job->text); |
| 283 | } |
| 284 | return TRUE; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
| 288 | /* built-in 'pwd' handler */ |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 289 | static int builtin_pwd(struct job *dummy, struct jobSet *junk) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 290 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 291 | getcwd(cwd, sizeof(cwd)); |
| 292 | fprintf(stdout, "%s\n", cwd); |
| 293 | return TRUE; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 296 | /* built-in 'export VAR=value' handler */ |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 297 | static int builtin_export(struct job *cmd, struct jobSet *junk) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 298 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 299 | int res; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 300 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 301 | if (!cmd->progs[0].argv[1] == 1) { |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 302 | return (builtin_env(cmd, junk)); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 303 | } |
| 304 | res = putenv(cmd->progs[0].argv[1]); |
| 305 | if (res) |
Erik Andersen | 6273f65 | 2000-03-17 01:12:41 +0000 | [diff] [blame] | 306 | fprintf(stdout, "export: %s\n", strerror(errno)); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 307 | return (res); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 310 | /* built-in 'read VAR' handler */ |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 311 | static int builtin_read(struct job *cmd, struct jobSet *junk) |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 312 | { |
| 313 | int res = 0, len, newlen; |
| 314 | char *s; |
| 315 | char string[MAX_READ]; |
| 316 | |
| 317 | if (cmd->progs[0].argv[1]) { |
| 318 | /* argument (VAR) given: put "VAR=" into buffer */ |
| 319 | strcpy(string, cmd->progs[0].argv[1]); |
| 320 | len = strlen(string); |
| 321 | string[len++] = '='; |
| 322 | string[len] = '\0'; |
| 323 | fgets(&string[len], sizeof(string) - len, stdin); /* read string */ |
| 324 | newlen = strlen(string); |
| 325 | if(newlen > len) |
| 326 | string[--newlen] = '\0'; /* chomp trailing newline */ |
| 327 | /* |
| 328 | ** string should now contain "VAR=<value>" |
| 329 | ** copy it (putenv() won't do that, so we must make sure |
| 330 | ** the string resides in a static buffer!) |
| 331 | */ |
| 332 | res = -1; |
| 333 | if((s = strdup(string))) |
| 334 | res = putenv(s); |
| 335 | if (res) |
| 336 | fprintf(stdout, "read: %s\n", strerror(errno)); |
| 337 | } |
| 338 | else |
| 339 | fgets(string, sizeof(string), stdin); |
| 340 | |
| 341 | return (res); |
| 342 | } |
| 343 | |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 344 | /* Built-in '.' handler (read-in and execute commands from file) */ |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 345 | static int builtin_source(struct job *cmd, struct jobSet *junk) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 346 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 347 | FILE *input; |
| 348 | int status; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 349 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 350 | if (!cmd->progs[0].argv[1] == 1) |
| 351 | return FALSE; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 352 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 353 | input = fopen(cmd->progs[0].argv[1], "r"); |
| 354 | if (!input) { |
| 355 | fprintf(stdout, "Couldn't open file '%s'\n", |
| 356 | cmd->progs[0].argv[1]); |
| 357 | return FALSE; |
| 358 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 359 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 360 | /* Now run the file */ |
| 361 | status = busy_loop(input); |
| 362 | return (status); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* built-in 'unset VAR' handler */ |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 366 | static int builtin_unset(struct job *cmd, struct jobSet *junk) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 367 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 368 | if (!cmd->progs[0].argv[1] == 1) { |
| 369 | fprintf(stdout, "unset: parameter required.\n"); |
| 370 | return FALSE; |
| 371 | } |
| 372 | unsetenv(cmd->progs[0].argv[1]); |
| 373 | return TRUE; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* free up all memory from a job */ |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 377 | static void freeJob(struct job *cmd) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 378 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 379 | int i; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 380 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 381 | for (i = 0; i < cmd->numProgs; i++) { |
| 382 | free(cmd->progs[i].argv); |
| 383 | if (cmd->progs[i].redirections) |
| 384 | free(cmd->progs[i].redirections); |
| 385 | if (cmd->progs[i].freeGlob) |
| 386 | globfree(&cmd->progs[i].globResult); |
| 387 | } |
| 388 | free(cmd->progs); |
| 389 | if (cmd->text) |
| 390 | free(cmd->text); |
| 391 | free(cmd->cmdBuf); |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 392 | memset(cmd, 0, sizeof(struct job)); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* remove a job from the jobList */ |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 396 | static void removeJob(struct jobSet *jobList, struct job *job) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 397 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 398 | struct job *prevJob; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 399 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 400 | freeJob(job); |
| 401 | if (job == jobList->head) { |
| 402 | jobList->head = job->next; |
| 403 | } else { |
| 404 | prevJob = jobList->head; |
| 405 | while (prevJob->next != job) |
| 406 | prevJob = prevJob->next; |
| 407 | prevJob->next = job->next; |
| 408 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 409 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 410 | free(job); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /* Checks to see if any background processes have exited -- if they |
| 414 | have, figure out why and see if a job has completed */ |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 415 | static void checkJobs(struct jobSet *jobList) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 416 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 417 | struct job *job; |
| 418 | pid_t childpid; |
| 419 | int status; |
| 420 | int progNum = 0; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 421 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 422 | while ((childpid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) { |
| 423 | for (job = jobList->head; job; job = job->next) { |
| 424 | progNum = 0; |
| 425 | while (progNum < job->numProgs && |
| 426 | job->progs[progNum].pid != childpid) progNum++; |
| 427 | if (progNum < job->numProgs) |
| 428 | break; |
| 429 | } |
| 430 | |
| 431 | if (WIFEXITED(status) || WIFSIGNALED(status)) { |
| 432 | /* child exited */ |
| 433 | job->runningProgs--; |
| 434 | job->progs[progNum].pid = 0; |
| 435 | |
| 436 | if (!job->runningProgs) { |
| 437 | printf(JOB_STATUS_FORMAT, job->jobId, "Done", job->text); |
| 438 | removeJob(jobList, job); |
| 439 | } |
| 440 | } else { |
| 441 | /* child stopped */ |
| 442 | job->stoppedProgs++; |
| 443 | job->progs[progNum].isStopped = 1; |
| 444 | |
| 445 | if (job->stoppedProgs == job->numProgs) { |
| 446 | printf(JOB_STATUS_FORMAT, job->jobId, "Stopped", |
| 447 | job->text); |
| 448 | } |
| 449 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 450 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 451 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 452 | if (childpid == -1 && errno != ECHILD) |
| 453 | perror("waitpid"); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 456 | static int getCommand(FILE * source, char *command) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 457 | { |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 458 | if (source == NULL) { |
| 459 | if (local_pending_command) { |
| 460 | /* a command specified (-c option): return it & mark it done */ |
| 461 | strcpy(command, local_pending_command); |
| 462 | free(local_pending_command); |
| 463 | local_pending_command = NULL; |
| 464 | return 0; |
| 465 | } |
| 466 | return 1; |
| 467 | } |
| 468 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 469 | if (source == stdin) { |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 470 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 471 | int len; |
| 472 | char *promptStr; |
Erik Andersen | d4bc1fc | 2000-04-05 05:19:03 +0000 | [diff] [blame] | 473 | len=fprintf(stdout, "%s %s", cwd, prompt); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 474 | fflush(stdout); |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 475 | promptStr=(char*)xmalloc(sizeof(char)*(len+1)); |
Erik Andersen | d4bc1fc | 2000-04-05 05:19:03 +0000 | [diff] [blame] | 476 | sprintf(promptStr, "%s %s", cwd, prompt); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 477 | cmdedit_read_input(promptStr, command); |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 478 | free( promptStr); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 479 | return 0; |
Erik Andersen | c7c634b | 2000-03-19 05:28:55 +0000 | [diff] [blame] | 480 | #else |
| 481 | fprintf(stdout, "%s %s", cwd, prompt); |
| 482 | fflush(stdout); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 483 | #endif |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 484 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 485 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 486 | if (!fgets(command, BUFSIZ - 2, source)) { |
| 487 | if (source == stdin) |
| 488 | printf("\n"); |
| 489 | return 1; |
| 490 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 491 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 492 | /* remove trailing newline */ |
| 493 | command[strlen(command) - 1] = '\0'; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 494 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 495 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 498 | static void globLastArgument(struct childProgram *prog, int *argcPtr, |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 499 | int *argcAllocedPtr) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 500 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 501 | int argc = *argcPtr; |
| 502 | int argcAlloced = *argcAllocedPtr; |
| 503 | int rc; |
| 504 | int flags; |
| 505 | int i; |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 506 | char *src, *dst, *var; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 507 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 508 | if (argc > 1) { /* cmd->globResult is already initialized */ |
| 509 | flags = GLOB_APPEND; |
| 510 | i = prog->globResult.gl_pathc; |
| 511 | } else { |
| 512 | prog->freeGlob = 1; |
| 513 | flags = 0; |
| 514 | i = 0; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 515 | } |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 516 | /* do shell variable substitution */ |
| 517 | if(*prog->argv[argc - 1] == '$' && (var = getenv(prog->argv[argc - 1] + 1))) |
| 518 | prog->argv[argc - 1] = var; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 519 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 520 | rc = glob(prog->argv[argc - 1], flags, NULL, &prog->globResult); |
| 521 | if (rc == GLOB_NOSPACE) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 522 | errorMsg("out of space during glob operation\n"); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 523 | return; |
| 524 | } else if (rc == GLOB_NOMATCH || |
| 525 | (!rc && (prog->globResult.gl_pathc - i) == 1 && |
| 526 | !strcmp(prog->argv[argc - 1], |
| 527 | prog->globResult.gl_pathv[i]))) { |
| 528 | /* we need to remove whatever \ quoting is still present */ |
| 529 | src = dst = prog->argv[argc - 1]; |
| 530 | while (*src) { |
| 531 | if (*src != '\\') |
| 532 | *dst++ = *src; |
| 533 | src++; |
| 534 | } |
| 535 | *dst = '\0'; |
| 536 | } else if (!rc) { |
| 537 | argcAlloced += (prog->globResult.gl_pathc - i); |
| 538 | prog->argv = |
| 539 | realloc(prog->argv, argcAlloced * sizeof(*prog->argv)); |
| 540 | memcpy(prog->argv + (argc - 1), prog->globResult.gl_pathv + i, |
| 541 | sizeof(*(prog->argv)) * (prog->globResult.gl_pathc - i)); |
| 542 | argc += (prog->globResult.gl_pathc - i - 1); |
| 543 | } |
| 544 | |
| 545 | *argcAllocedPtr = argcAlloced; |
| 546 | *argcPtr = argc; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /* Return cmd->numProgs as 0 if no command is present (e.g. an empty |
| 550 | line). If a valid command is found, commandPtr is set to point to |
| 551 | the beginning of the next command (if the original command had more |
| 552 | then one job associated with it) or NULL if no more commands are |
| 553 | present. */ |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 554 | static int parseCommand(char **commandPtr, struct job *job, struct jobSet *jobList, int *isBg) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 555 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 556 | char *command; |
| 557 | char *returnCommand = NULL; |
| 558 | char *src, *buf, *chptr; |
| 559 | int argc = 0; |
| 560 | int done = 0; |
| 561 | int argvAlloced; |
| 562 | int i; |
| 563 | char quote = '\0'; |
| 564 | int count; |
| 565 | struct childProgram *prog; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 566 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 567 | /* skip leading white space */ |
| 568 | while (**commandPtr && isspace(**commandPtr)) |
| 569 | (*commandPtr)++; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 570 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 571 | /* this handles empty lines or leading '#' characters */ |
| 572 | if (!**commandPtr || (**commandPtr == '#')) { |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 573 | job->numProgs=0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 574 | return 0; |
| 575 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 576 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 577 | *isBg = 0; |
| 578 | job->numProgs = 1; |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 579 | job->progs = xmalloc(sizeof(*job->progs)); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 580 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 581 | /* We set the argv elements to point inside of this string. The |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 582 | memory is freed by freeJob(). Allocate twice the original |
| 583 | length in case we need to quote every single character. |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 584 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 585 | Getting clean memory relieves us of the task of NULL |
| 586 | terminating things and makes the rest of this look a bit |
| 587 | cleaner (though it is, admittedly, a tad less efficient) */ |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 588 | job->cmdBuf = command = calloc(1, 2*strlen(*commandPtr) + 1); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 589 | job->text = NULL; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 590 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 591 | prog = job->progs; |
| 592 | prog->numRedirections = 0; |
| 593 | prog->redirections = NULL; |
| 594 | prog->freeGlob = 0; |
| 595 | prog->isStopped = 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 596 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 597 | argvAlloced = 5; |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 598 | prog->argv = xmalloc(sizeof(*prog->argv) * argvAlloced); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 599 | prog->argv[0] = job->cmdBuf; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 600 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 601 | buf = command; |
| 602 | src = *commandPtr; |
| 603 | while (*src && !done) { |
| 604 | if (quote == *src) { |
| 605 | quote = '\0'; |
| 606 | } else if (quote) { |
| 607 | if (*src == '\\') { |
| 608 | src++; |
| 609 | if (!*src) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 610 | errorMsg("character expected after \\\n"); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 611 | freeJob(job); |
| 612 | return 1; |
| 613 | } |
| 614 | |
| 615 | /* in shell, "\'" should yield \' */ |
| 616 | if (*src != quote) |
| 617 | *buf++ = '\\'; |
| 618 | } else if (*src == '*' || *src == '?' || *src == '[' || |
| 619 | *src == ']') *buf++ = '\\'; |
| 620 | *buf++ = *src; |
| 621 | } else if (isspace(*src)) { |
| 622 | if (*prog->argv[argc]) { |
| 623 | buf++, argc++; |
| 624 | /* +1 here leaves room for the NULL which ends argv */ |
| 625 | if ((argc + 1) == argvAlloced) { |
| 626 | argvAlloced += 5; |
| 627 | prog->argv = realloc(prog->argv, |
| 628 | sizeof(*prog->argv) * |
| 629 | argvAlloced); |
| 630 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 631 | globLastArgument(prog, &argc, &argvAlloced); |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 632 | prog->argv[argc] = buf; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 633 | } |
| 634 | } else |
| 635 | switch (*src) { |
| 636 | case '"': |
| 637 | case '\'': |
| 638 | quote = *src; |
| 639 | break; |
| 640 | |
| 641 | case '#': /* comment */ |
| 642 | done = 1; |
| 643 | break; |
| 644 | |
| 645 | case '>': /* redirections */ |
| 646 | case '<': |
| 647 | i = prog->numRedirections++; |
| 648 | prog->redirections = realloc(prog->redirections, |
| 649 | sizeof(*prog->redirections) * |
| 650 | (i + 1)); |
| 651 | |
| 652 | prog->redirections[i].fd = -1; |
| 653 | if (buf != prog->argv[argc]) { |
| 654 | /* the stuff before this character may be the file number |
| 655 | being redirected */ |
| 656 | prog->redirections[i].fd = |
| 657 | strtol(prog->argv[argc], &chptr, 10); |
| 658 | |
| 659 | if (*chptr && *prog->argv[argc]) { |
| 660 | buf++, argc++; |
| 661 | globLastArgument(prog, &argc, &argvAlloced); |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 662 | prog->argv[argc] = buf; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
| 666 | if (prog->redirections[i].fd == -1) { |
| 667 | if (*src == '>') |
| 668 | prog->redirections[i].fd = 1; |
| 669 | else |
| 670 | prog->redirections[i].fd = 0; |
| 671 | } |
| 672 | |
| 673 | if (*src++ == '>') { |
| 674 | if (*src == '>') |
| 675 | prog->redirections[i].type = |
| 676 | REDIRECT_APPEND, src++; |
| 677 | else |
| 678 | prog->redirections[i].type = REDIRECT_OVERWRITE; |
| 679 | } else { |
| 680 | prog->redirections[i].type = REDIRECT_INPUT; |
| 681 | } |
| 682 | |
| 683 | /* This isn't POSIX sh compliant. Oh well. */ |
| 684 | chptr = src; |
| 685 | while (isspace(*chptr)) |
| 686 | chptr++; |
| 687 | |
| 688 | if (!*chptr) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 689 | errorMsg("file name expected after %c\n", *src); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 690 | freeJob(job); |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 691 | job->numProgs=0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 692 | return 1; |
| 693 | } |
| 694 | |
| 695 | prog->redirections[i].filename = buf; |
| 696 | while (*chptr && !isspace(*chptr)) |
| 697 | *buf++ = *chptr++; |
| 698 | |
| 699 | src = chptr - 1; /* we src++ later */ |
| 700 | prog->argv[argc] = ++buf; |
| 701 | break; |
| 702 | |
| 703 | case '|': /* pipe */ |
| 704 | /* finish this command */ |
| 705 | if (*prog->argv[argc]) |
| 706 | argc++; |
| 707 | if (!argc) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 708 | errorMsg("empty command in pipe1\n"); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 709 | freeJob(job); |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 710 | job->numProgs=0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 711 | return 1; |
| 712 | } |
| 713 | prog->argv[argc] = NULL; |
| 714 | |
| 715 | /* and start the next */ |
| 716 | job->numProgs++; |
| 717 | job->progs = realloc(job->progs, |
| 718 | sizeof(*job->progs) * job->numProgs); |
| 719 | prog = job->progs + (job->numProgs - 1); |
| 720 | prog->numRedirections = 0; |
| 721 | prog->redirections = NULL; |
| 722 | prog->freeGlob = 0; |
| 723 | argc = 0; |
| 724 | |
| 725 | argvAlloced = 5; |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 726 | prog->argv = xmalloc(sizeof(*prog->argv) * argvAlloced); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 727 | prog->argv[0] = ++buf; |
| 728 | |
| 729 | src++; |
| 730 | while (*src && isspace(*src)) |
| 731 | src++; |
| 732 | |
| 733 | if (!*src) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 734 | errorMsg("empty command in pipe2\n"); |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 735 | freeJob(job); |
| 736 | job->numProgs=0; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 737 | return 1; |
| 738 | } |
| 739 | src--; /* we'll ++ it at the end of the loop */ |
| 740 | |
| 741 | break; |
| 742 | |
| 743 | case '&': /* background */ |
| 744 | *isBg = 1; |
| 745 | case ';': /* multiple commands */ |
| 746 | done = 1; |
| 747 | returnCommand = *commandPtr + (src - *commandPtr) + 1; |
| 748 | break; |
| 749 | |
| 750 | case '\\': |
| 751 | src++; |
| 752 | if (!*src) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 753 | errorMsg("character expected after \\\n"); |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 754 | freeJob(job); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 755 | return 1; |
| 756 | } |
| 757 | if (*src == '*' || *src == '[' || *src == ']' |
| 758 | || *src == '?') *buf++ = '\\'; |
| 759 | /* fallthrough */ |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 760 | case '`': |
| 761 | /* Exec a backtick-ed command */ |
| 762 | { |
| 763 | char* newcmd=NULL; |
| 764 | char* ptr=NULL; |
| 765 | struct job newJob; |
| 766 | |
| 767 | ptr=strchr(++src, '`'); |
| 768 | if (ptr==NULL) { |
| 769 | fprintf(stderr, "Unmatched '`' in command\n"); |
| 770 | freeJob(job); |
| 771 | return 1; |
| 772 | } |
| 773 | |
| 774 | newcmd = xmalloc(1+ptr-src); |
| 775 | snprintf(newcmd, 1+ptr-src, src); |
| 776 | |
| 777 | if (!parseCommand(&newcmd, &newJob, jobList, isBg) && |
| 778 | newJob.numProgs) { |
| 779 | runCommand(&newJob, jobList, *isBg); |
| 780 | } |
| 781 | |
| 782 | /* Clip out the the backticked command from the string */ |
| 783 | memmove(--src, ptr, strlen(ptr)+1); |
| 784 | free(newcmd); |
| 785 | } |
| 786 | break; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 787 | default: |
| 788 | *buf++ = *src; |
| 789 | } |
| 790 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 791 | src++; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 792 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 793 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 794 | if (*prog->argv[argc]) { |
| 795 | argc++; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 796 | globLastArgument(prog, &argc, &argvAlloced); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 797 | } |
| 798 | if (!argc) { |
| 799 | freeJob(job); |
| 800 | return 0; |
| 801 | } |
| 802 | prog->argv[argc] = NULL; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 803 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 804 | if (!returnCommand) { |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 805 | job->text = xmalloc(strlen(*commandPtr) + 1); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 806 | strcpy(job->text, *commandPtr); |
| 807 | } else { |
| 808 | /* This leaves any trailing spaces, which is a bit sloppy */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 809 | count = returnCommand - *commandPtr; |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 810 | job->text = xmalloc(count + 1); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 811 | strncpy(job->text, *commandPtr, count); |
| 812 | job->text[count] = '\0'; |
| 813 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 814 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 815 | *commandPtr = returnCommand; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 816 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 817 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Erik Andersen | bcd6177 | 2000-05-13 06:33:19 +0000 | [diff] [blame] | 820 | |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 821 | static int runCommand(struct job *newJob, struct jobSet *jobList, int inBg) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 822 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 823 | struct job *job; |
| 824 | int i; |
| 825 | int nextin, nextout; |
| 826 | int pipefds[2]; /* pipefd[0] is for reading */ |
| 827 | struct builtInCommand *x; |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 828 | #ifdef BB_FEATURE_SH_STANDALONE_SHELL |
Erik Andersen | bcd6177 | 2000-05-13 06:33:19 +0000 | [diff] [blame] | 829 | const struct BB_applet *a = applets; |
| 830 | #endif |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 831 | |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 832 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 833 | nextin = 0, nextout = 1; |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 834 | for (i = 0; i < newJob->numProgs; i++) { |
| 835 | if ((i + 1) < newJob->numProgs) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 836 | pipe(pipefds); |
| 837 | nextout = pipefds[1]; |
| 838 | } else { |
| 839 | nextout = 1; |
| 840 | } |
| 841 | |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 842 | /* Check if the command matches any non-forking builtins */ |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 843 | for (x = bltins; x->cmd; x++) { |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 844 | if (!strcmp(newJob->progs[i].argv[0], x->cmd)) { |
| 845 | return (x->function(newJob, jobList)); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 849 | if (!(newJob->progs[i].pid = fork())) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 850 | signal(SIGTTOU, SIG_DFL); |
| 851 | |
| 852 | if (nextin != 0) { |
| 853 | dup2(nextin, 0); |
| 854 | close(nextin); |
| 855 | } |
| 856 | |
| 857 | if (nextout != 1) { |
| 858 | dup2(nextout, 1); |
| 859 | close(nextout); |
| 860 | } |
| 861 | |
| 862 | /* explicit redirections override pipes */ |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 863 | setupRedirections(newJob->progs + i); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 864 | |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 865 | /* Check if the command matches any of the other builtins */ |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 866 | for (x = bltins_forking; x->cmd; x++) { |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 867 | if (!strcmp(newJob->progs[i].argv[0], x->cmd)) { |
| 868 | exit (x->function(newJob, jobList)); |
Erik Andersen | bcd6177 | 2000-05-13 06:33:19 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 871 | #ifdef BB_FEATURE_SH_STANDALONE_SHELL |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 872 | /* Check if the command matches any busybox internal commands here */ |
| 873 | /* TODO: Add matching when paths are appended (i.e. 'cat' currently |
| 874 | * works, but '/bin/cat' doesn't ) */ |
Erik Andersen | bcd6177 | 2000-05-13 06:33:19 +0000 | [diff] [blame] | 875 | while (a->name != 0) { |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 876 | if (strcmp(newJob->progs[i].argv[0], a->name) == 0) { |
Erik Andersen | bcd6177 | 2000-05-13 06:33:19 +0000 | [diff] [blame] | 877 | int argc; |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 878 | char** argv=newJob->progs[i].argv; |
Erik Andersen | c3f5c9c | 2000-05-13 19:00:07 +0000 | [diff] [blame] | 879 | for(argc=0;*argv!=NULL; argv++, argc++); |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 880 | exit((*(a->main)) (argc, newJob->progs[i].argv)); |
Erik Andersen | bcd6177 | 2000-05-13 06:33:19 +0000 | [diff] [blame] | 881 | } |
| 882 | a++; |
| 883 | } |
| 884 | #endif |
| 885 | |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 886 | execvp(newJob->progs[i].argv[0], newJob->progs[i].argv); |
| 887 | fatalError("%s: %s\n", newJob->progs[i].argv[0], |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 888 | strerror(errno)); |
| 889 | } |
| 890 | |
| 891 | /* put our child in the process group whose leader is the |
| 892 | first process in this pipe */ |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 893 | setpgid(newJob->progs[i].pid, newJob->progs[0].pid); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 894 | |
| 895 | if (nextin != 0) |
| 896 | close(nextin); |
| 897 | if (nextout != 1) |
| 898 | close(nextout); |
| 899 | |
| 900 | /* If there isn't another process, nextin is garbage |
| 901 | but it doesn't matter */ |
| 902 | nextin = pipefds[0]; |
| 903 | } |
| 904 | |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 905 | newJob->pgrp = newJob->progs[0].pid; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 906 | |
| 907 | /* find the ID for the job to use */ |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 908 | newJob->jobId = 1; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 909 | for (job = jobList->head; job; job = job->next) |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 910 | if (job->jobId >= newJob->jobId) |
| 911 | newJob->jobId = job->jobId + 1; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 912 | |
| 913 | /* add the job to the list of running jobs */ |
| 914 | if (!jobList->head) { |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 915 | job = jobList->head = xmalloc(sizeof(*job)); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 916 | } else { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 917 | for (job = jobList->head; job->next; job = job->next); |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 918 | job->next = xmalloc(sizeof(*job)); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 919 | job = job->next; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 920 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 921 | |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 922 | *job = *newJob; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 923 | job->next = NULL; |
| 924 | job->runningProgs = job->numProgs; |
| 925 | job->stoppedProgs = 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 926 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 927 | if (inBg) { |
| 928 | /* we don't wait for background jobs to return -- append it |
| 929 | to the list of backgrounded jobs and leave it alone */ |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 930 | printf("[%d] %d\n", job->jobId, |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 931 | newJob->progs[newJob->numProgs - 1].pid); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 932 | } else { |
| 933 | jobList->fg = job; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 934 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 935 | /* move the new process group into the foreground */ |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 936 | /* suppress messages when run from /linuxrc mag@sysgo.de */ |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 937 | if (tcsetpgrp(0, newJob->pgrp) && errno != ENOTTY) |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 938 | perror("tcsetpgrp"); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 939 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 940 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 941 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 944 | static int setupRedirections(struct childProgram *prog) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 945 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 946 | int i; |
| 947 | int openfd; |
| 948 | int mode = O_RDONLY; |
| 949 | struct redirectionSpecifier *redir = prog->redirections; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 950 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 951 | for (i = 0; i < prog->numRedirections; i++, redir++) { |
| 952 | switch (redir->type) { |
| 953 | case REDIRECT_INPUT: |
| 954 | mode = O_RDONLY; |
| 955 | break; |
| 956 | case REDIRECT_OVERWRITE: |
| 957 | mode = O_RDWR | O_CREAT | O_TRUNC; |
| 958 | break; |
| 959 | case REDIRECT_APPEND: |
| 960 | mode = O_RDWR | O_CREAT | O_APPEND; |
| 961 | break; |
| 962 | } |
| 963 | |
| 964 | openfd = open(redir->filename, mode, 0666); |
| 965 | if (openfd < 0) { |
| 966 | /* this could get lost if stderr has been redirected, but |
| 967 | bash and ash both lose it as well (though zsh doesn't!) */ |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 968 | errorMsg("error opening %s: %s\n", redir->filename, |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 969 | strerror(errno)); |
| 970 | return 1; |
| 971 | } |
| 972 | |
| 973 | if (openfd != redir->fd) { |
| 974 | dup2(openfd, redir->fd); |
| 975 | close(openfd); |
| 976 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 977 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 978 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 979 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 983 | static int busy_loop(FILE * input) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 984 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 985 | char *command; |
| 986 | char *nextCommand = NULL; |
| 987 | struct jobSet jobList = { NULL, NULL }; |
| 988 | struct job newJob; |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 989 | pid_t parent_pgrp; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 990 | int i; |
| 991 | int status; |
| 992 | int inBg; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 993 | |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 994 | /* save current owner of TTY so we can restore it on exit */ |
| 995 | parent_pgrp = tcgetpgrp(0); |
| 996 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 997 | command = (char *) calloc(BUFSIZ, sizeof(char)); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 998 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 999 | /* don't pay any attention to this signal; it just confuses |
| 1000 | things and isn't really meant for shells anyway */ |
| 1001 | signal(SIGTTOU, SIG_IGN); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1002 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1003 | while (1) { |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1004 | if (!jobList.fg) { |
| 1005 | /* no job is in the foreground */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1006 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1007 | /* see if any background processes have exited */ |
| 1008 | checkJobs(&jobList); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1009 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1010 | if (!nextCommand) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1011 | if (getCommand(input, command)) |
| 1012 | break; |
| 1013 | nextCommand = command; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1014 | } |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1015 | |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 1016 | if (!parseCommand(&nextCommand, &newJob, &jobList, &inBg) && |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1017 | newJob.numProgs) { |
Eric Andersen | ec10b9d | 2000-07-14 01:13:11 +0000 | [diff] [blame] | 1018 | runCommand(&newJob, &jobList, inBg); |
| 1019 | } else { |
| 1020 | nextCommand=NULL; |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } else { |
| 1023 | /* a job is running in the foreground; wait for it */ |
| 1024 | i = 0; |
| 1025 | while (!jobList.fg->progs[i].pid || |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1026 | jobList.fg->progs[i].isStopped) i++; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1027 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1028 | waitpid(jobList.fg->progs[i].pid, &status, WUNTRACED); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1029 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1030 | if (WIFEXITED(status) || WIFSIGNALED(status)) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1031 | /* the child exited */ |
| 1032 | jobList.fg->runningProgs--; |
| 1033 | jobList.fg->progs[i].pid = 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1034 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1035 | if (!jobList.fg->runningProgs) { |
| 1036 | /* child exited */ |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1037 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1038 | removeJob(&jobList, jobList.fg); |
| 1039 | jobList.fg = NULL; |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1040 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1041 | } else { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1042 | /* the child was stopped */ |
| 1043 | jobList.fg->stoppedProgs++; |
| 1044 | jobList.fg->progs[i].isStopped = 1; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1045 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1046 | if (jobList.fg->stoppedProgs == jobList.fg->runningProgs) { |
| 1047 | printf("\n" JOB_STATUS_FORMAT, jobList.fg->jobId, |
| 1048 | "Stopped", jobList.fg->text); |
| 1049 | jobList.fg = NULL; |
| 1050 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | if (!jobList.fg) { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1054 | /* move the shell to the foreground */ |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1055 | /* suppress messages when run from /linuxrc mag@sysgo.de */ |
| 1056 | if (tcsetpgrp(0, getpid()) && errno != ENOTTY) |
| 1057 | perror("tcsetpgrp"); |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1061 | free(command); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1062 | |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1063 | /* return controlling TTY back to parent process group before exiting */ |
| 1064 | if (tcsetpgrp(0, parent_pgrp)) |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 1065 | perror("tcsetpgrp"); |
| 1066 | |
| 1067 | /* return exit status if called with "-c" */ |
| 1068 | if (input == NULL && WIFEXITED(status)) |
| 1069 | return WEXITSTATUS(status); |
| 1070 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1071 | return 0; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1075 | int shell_main(int argc, char **argv) |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1076 | { |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1077 | FILE *input = stdin; |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1078 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1079 | /* initialize the cwd */ |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 1080 | cwd = (char *) calloc(BUFSIZ, sizeof(char)); |
| 1081 | if (cwd == 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 1082 | fatalError("out of memory\n"); |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 1083 | } |
| 1084 | getcwd(cwd, sizeof(char)*BUFSIZ); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1085 | |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1086 | #ifdef BB_FEATURE_SH_COMMAND_EDITING |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 1087 | cmdedit_init(); |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1088 | signal(SIGWINCH, win_changed); |
| 1089 | win_changed(0); |
| 1090 | #endif |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1091 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1092 | //if (argv[0] && argv[0][0] == '-') { |
Eric Andersen | 34e1941 | 2000-07-10 18:47:24 +0000 | [diff] [blame] | 1093 | // builtin_source("/etc/profile"); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1094 | //} |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1095 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1096 | if (argc < 2) { |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1097 | fprintf(stdout, "\n\nBusyBox v%s (%s) Built-in shell\n", BB_VER, BB_BT); |
| 1098 | fprintf(stdout, "Enter 'help' for a list of built-in commands.\n\n"); |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1099 | } else { |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1100 | if (argv[1][0]=='-' && argv[1][1]=='c') { |
| 1101 | int i; |
| 1102 | local_pending_command = (char *) calloc(BUFSIZ, sizeof(char)); |
| 1103 | if (local_pending_command == 0) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 1104 | fatalError("out of memory\n"); |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1105 | } |
| 1106 | for(i=2; i<argc; i++) |
| 1107 | { |
| 1108 | if (strlen(local_pending_command) + strlen(argv[i]) >= BUFSIZ) { |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 1109 | local_pending_command = realloc(local_pending_command, |
| 1110 | strlen(local_pending_command) + strlen(argv[i])); |
| 1111 | if (local_pending_command==NULL) |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 1112 | fatalError("commands for -c option too long\n"); |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1113 | } |
| 1114 | strcat(local_pending_command, argv[i]); |
Eric Andersen | b54833c | 2000-07-03 23:56:26 +0000 | [diff] [blame] | 1115 | if ( (i + 1) < argc) |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1116 | strcat(local_pending_command, " "); |
| 1117 | } |
| 1118 | input = NULL; |
| 1119 | |
Erik Andersen | e5b6c7d | 2000-04-17 16:16:10 +0000 | [diff] [blame] | 1120 | } |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1121 | else if (argv[1][0]=='-') { |
| 1122 | usage(shell_usage); |
| 1123 | } |
| 1124 | else { |
| 1125 | input = fopen(argv[1], "r"); |
| 1126 | if (!input) { |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 1127 | fatalError("Couldn't open file '%s': %s\n", argv[1], |
Eric Andersen | 1c314ad | 2000-06-28 16:56:25 +0000 | [diff] [blame] | 1128 | strerror(errno)); |
| 1129 | } |
Erik Andersen | f0657d3 | 2000-04-12 17:49:52 +0000 | [diff] [blame] | 1130 | } |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1131 | } |
Erik Andersen | d75af99 | 2000-03-16 08:09:09 +0000 | [diff] [blame] | 1132 | |
Erik Andersen | 161220c | 2000-03-16 08:12:48 +0000 | [diff] [blame] | 1133 | return (busy_loop(input)); |
Erik Andersen | 3522eb1 | 2000-03-12 23:49:18 +0000 | [diff] [blame] | 1134 | } |