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