2006-09-05 03:22:19 +00:00
|
|
|
/* vi: set ts=4 :
|
2007-01-11 17:20:00 +00:00
|
|
|
*
|
2006-09-05 03:22:19 +00:00
|
|
|
* bbsh - busybox shell
|
|
|
|
*
|
|
|
|
* Copyright 2006 Rob Landley <rob@landley.net>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
2006-09-08 16:59:08 +00:00
|
|
|
// A section of code that gets repeatedly or conditionally executed is stored
|
|
|
|
// as a string and parsed each time it's run.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wheee, debugging.
|
|
|
|
|
|
|
|
// Terminal control
|
|
|
|
#define ENABLE_BBSH_TTY 0
|
|
|
|
|
|
|
|
// &, fg, bg, jobs. (ctrl-z with tty.)
|
|
|
|
#define ENABLE_BBSH_JOBCTL 0
|
|
|
|
|
|
|
|
// Flow control (if, while, for, functions { })
|
|
|
|
#define ENABLE_BBSH_FLOWCTL 0
|
|
|
|
|
|
|
|
#define ENABLE_BBSH_ENVVARS 0 // Environment variable support
|
|
|
|
|
|
|
|
// Local and synthetic variables, fancy prompts, set, $?, etc.
|
|
|
|
#define ENABLE_BBSH_LOCALVARS 0
|
|
|
|
|
|
|
|
// Pipes and redirects: | > < >> << && || & () ;
|
|
|
|
#define ENABLE_BBSH_PIPES 0
|
|
|
|
|
|
|
|
/* Fun:
|
|
|
|
|
|
|
|
echo `echo hello#comment " woot` and more
|
|
|
|
*/
|
2006-09-05 03:22:19 +00:00
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2006-09-05 03:22:19 +00:00
|
|
|
|
2006-09-08 16:59:08 +00:00
|
|
|
// A single executable, its arguments, and other information we know about it.
|
|
|
|
#define BBSH_FLAG_EXIT 1
|
|
|
|
#define BBSH_FLAG_SUSPEND 2
|
|
|
|
#define BBSH_FLAG_PIPE 4
|
|
|
|
#define BBSH_FLAG_AND 8
|
|
|
|
#define BBSH_FLAG_OR 16
|
|
|
|
#define BBSH_FLAG_AMP 32
|
|
|
|
#define BBSH_FLAG_SEMI 64
|
|
|
|
#define BBSH_FLAG_PAREN 128
|
|
|
|
|
|
|
|
// What we know about a single process.
|
|
|
|
struct command {
|
|
|
|
struct command *next;
|
2007-01-11 17:20:00 +00:00
|
|
|
int flags; // exit, suspend, && ||
|
2006-09-08 16:59:08 +00:00
|
|
|
int pid; // pid (or exit code)
|
|
|
|
int argc;
|
2009-09-23 21:15:43 +00:00
|
|
|
char *argv[];
|
2006-09-08 16:59:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// A collection of processes piped into/waiting on each other.
|
|
|
|
struct pipeline {
|
|
|
|
struct pipeline *next;
|
|
|
|
int job_id;
|
|
|
|
struct command *cmd;
|
|
|
|
char *cmdline;
|
|
|
|
int cmdlinelen;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void free_list(void *list, void (*freeit)(void *data))
|
|
|
|
{
|
2007-04-12 00:32:05 +00:00
|
|
|
while (list) {
|
2006-09-08 16:59:08 +00:00
|
|
|
void **next = (void **)list;
|
|
|
|
void *list_next = *next;
|
|
|
|
freeit(list);
|
|
|
|
free(list);
|
|
|
|
list = list_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse one word from the command line, appending one or more argv[] entries
|
|
|
|
// to struct command. Handles environment variable substitution and
|
|
|
|
// substrings. Returns pointer to next used byte, or NULL if it
|
|
|
|
// hit an ending token.
|
|
|
|
static char *parse_word(char *start, struct command **cmd)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
// Detect end of line (and truncate line at comment)
|
|
|
|
if (ENABLE_BBSH_PIPES && strchr("><&|(;", *start)) return 0;
|
|
|
|
|
|
|
|
// Grab next word. (Add dequote and envvar logic here)
|
|
|
|
end = start;
|
2007-04-10 09:38:35 +00:00
|
|
|
end = skip_non_whitespace(end);
|
2006-09-08 16:59:08 +00:00
|
|
|
(*cmd)->argv[(*cmd)->argc++] = xstrndup(start, end-start);
|
|
|
|
|
|
|
|
// Allocate more space if there's no room for NULL terminator.
|
|
|
|
|
|
|
|
if (!((*cmd)->argc & 7))
|
|
|
|
*cmd = xrealloc(*cmd,
|
|
|
|
sizeof(struct command) + ((*cmd)->argc+8)*sizeof(char *));
|
|
|
|
(*cmd)->argv[(*cmd)->argc] = 0;
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a line of text into a pipeline.
|
|
|
|
// Returns a pointer to the next line.
|
|
|
|
|
2006-09-08 17:21:19 +00:00
|
|
|
static char *parse_pipeline(char *cmdline, struct pipeline *line)
|
2006-09-05 03:22:19 +00:00
|
|
|
{
|
2006-09-08 17:21:19 +00:00
|
|
|
struct command **cmd = &(line->cmd);
|
|
|
|
char *start = line->cmdline = cmdline;
|
2006-09-08 16:59:08 +00:00
|
|
|
|
|
|
|
if (!cmdline) return 0;
|
|
|
|
|
2006-09-08 17:21:19 +00:00
|
|
|
if (ENABLE_BBSH_JOBCTL) line->cmdline = cmdline;
|
2006-09-05 03:22:19 +00:00
|
|
|
|
|
|
|
// Parse command into argv[]
|
|
|
|
for (;;) {
|
|
|
|
char *end;
|
|
|
|
|
2006-09-08 16:59:08 +00:00
|
|
|
// Skip leading whitespace and detect end of line.
|
2006-10-25 12:46:03 +00:00
|
|
|
start = skip_whitespace(start);
|
2006-09-08 16:59:08 +00:00
|
|
|
if (!*start || *start=='#') {
|
2006-09-08 17:21:19 +00:00
|
|
|
if (ENABLE_BBSH_JOBCTL) line->cmdlinelen = start-cmdline;
|
2006-09-08 16:59:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-09-05 03:22:19 +00:00
|
|
|
|
2007-01-11 17:20:00 +00:00
|
|
|
// Allocate next command structure if necessary
|
2006-09-08 16:59:08 +00:00
|
|
|
if (!*cmd) *cmd = xzalloc(sizeof(struct command)+8*sizeof(char *));
|
2007-01-11 17:20:00 +00:00
|
|
|
|
2006-09-08 16:59:08 +00:00
|
|
|
// Parse next argument and add the results to argv[]
|
|
|
|
end = parse_word(start, cmd);
|
|
|
|
|
|
|
|
// If we hit the end of this command, how did it end?
|
|
|
|
if (!end) {
|
|
|
|
if (ENABLE_BBSH_PIPES && *start) {
|
|
|
|
if (*start==';') {
|
|
|
|
start++;
|
|
|
|
break;
|
|
|
|
}
|
2007-01-11 17:20:00 +00:00
|
|
|
// handle | & < > >> << || &&
|
2006-09-08 16:59:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
start = end;
|
2006-09-05 03:22:19 +00:00
|
|
|
}
|
|
|
|
|
2006-09-08 17:21:19 +00:00
|
|
|
if (ENABLE_BBSH_JOBCTL) line->cmdlinelen = start-cmdline;
|
2006-09-08 16:59:08 +00:00
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the commands in a pipeline
|
2006-09-08 17:21:19 +00:00
|
|
|
static int run_pipeline(struct pipeline *line)
|
2006-09-08 16:59:08 +00:00
|
|
|
{
|
2006-09-08 17:21:19 +00:00
|
|
|
struct command *cmd = line->cmd;
|
2006-09-08 16:59:08 +00:00
|
|
|
if (!cmd || !cmd->argc) return 0;
|
|
|
|
|
|
|
|
// Handle local commands. This is totally fake and plastic.
|
|
|
|
if (cmd->argc==2 && !strcmp(cmd->argv[0],"cd"))
|
|
|
|
chdir(cmd->argv[1]);
|
2007-04-12 00:32:05 +00:00
|
|
|
else if (!strcmp(cmd->argv[0],"exit"))
|
2007-01-11 17:20:00 +00:00
|
|
|
exit(cmd->argc>1 ? atoi(cmd->argv[1]) : 0);
|
2006-09-05 03:22:19 +00:00
|
|
|
else {
|
|
|
|
int status;
|
|
|
|
pid_t pid=fork();
|
2007-04-12 00:32:05 +00:00
|
|
|
if (!pid) {
|
2007-04-11 17:03:19 +00:00
|
|
|
run_applet_and_exit(cmd->argv[0],cmd->argc,cmd->argv);
|
2006-09-08 16:59:08 +00:00
|
|
|
execvp(cmd->argv[0],cmd->argv);
|
2008-06-26 14:32:57 +00:00
|
|
|
printf("No %s", cmd->argv[0]);
|
2008-05-19 09:29:47 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2006-09-05 03:22:19 +00:00
|
|
|
} else waitpid(pid, &status, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-08 16:59:08 +00:00
|
|
|
static void free_cmd(void *data)
|
|
|
|
{
|
|
|
|
struct command *cmd=(struct command *)data;
|
|
|
|
|
2007-04-12 00:32:05 +00:00
|
|
|
while (cmd->argc) free(cmd->argv[--cmd->argc]);
|
2006-09-08 16:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void handle(char *command)
|
|
|
|
{
|
2006-09-08 17:21:19 +00:00
|
|
|
struct pipeline line;
|
2006-09-08 16:59:08 +00:00
|
|
|
char *start = command;
|
|
|
|
|
|
|
|
for (;;) {
|
2006-09-08 17:21:19 +00:00
|
|
|
memset(&line,0,sizeof(struct pipeline));
|
|
|
|
start = parse_pipeline(start, &line);
|
|
|
|
if (!line.cmd) break;
|
2006-09-08 16:59:08 +00:00
|
|
|
|
2006-09-08 17:21:19 +00:00
|
|
|
run_pipeline(&line);
|
|
|
|
free_list(line.cmd, free_cmd);
|
2006-09-08 16:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int bbsh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2010-01-04 13:15:38 +00:00
|
|
|
int bbsh_main(int argc UNUSED_PARAM, char **argv)
|
2006-09-05 03:22:19 +00:00
|
|
|
{
|
|
|
|
char *command=NULL;
|
|
|
|
FILE *f;
|
|
|
|
|
2007-08-18 15:32:12 +00:00
|
|
|
getopt32(argv, "c:", &command);
|
2006-09-05 03:22:19 +00:00
|
|
|
|
2008-07-21 23:05:26 +00:00
|
|
|
f = argv[optind] ? xfopen_for_read(argv[optind]) : NULL;
|
2006-09-05 03:22:19 +00:00
|
|
|
if (command) handle(command);
|
|
|
|
else {
|
|
|
|
unsigned cmdlen=0;
|
|
|
|
for (;;) {
|
2007-04-12 00:32:05 +00:00
|
|
|
if (!f) putchar('$');
|
2009-09-06 00:36:23 +00:00
|
|
|
if (1 > getline(&command, &cmdlen, f ? f : stdin)) break;
|
2006-09-08 16:59:08 +00:00
|
|
|
|
2006-09-05 03:22:19 +00:00
|
|
|
handle(command);
|
|
|
|
}
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) free(command);
|
|
|
|
}
|
2007-01-11 17:20:00 +00:00
|
|
|
|
2006-09-05 03:22:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|