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