aboutsummaryrefslogtreecommitdiff
path: root/toys/toysh.c
diff options
context:
space:
mode:
authorlandley <landley@driftwood>2006-10-18 18:38:16 -0400
committerlandley <landley@driftwood>2006-10-18 18:38:16 -0400
commitcd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4 (patch)
treea4e4932ecc15140ed48761ca8d32d5a0edc53bf7 /toys/toysh.c
parent4f344e356d2c36c4b1df46917eaef25f82ca79a9 (diff)
downloadtoybox-cd9dfc3b7b73715840b63180e2e4bfdb6e7ca9a4.tar.gz
Next drop of toysh, plus more infratructure.
Diffstat (limited to 'toys/toysh.c')
-rw-r--r--toys/toysh.c180
1 files changed, 151 insertions, 29 deletions
diff --git a/toys/toysh.c b/toys/toysh.c
index 77db82db..1d792f1f 100644
--- a/toys/toysh.c
+++ b/toys/toysh.c
@@ -14,47 +14,169 @@
#include "toys.h"
-static int handle(char *command)
+// A single executable, its arguments, and other information we know about it.
+#define TOYSH_FLAG_EXIT 1
+#define TOYSH_FLAG_SUSPEND 2
+#define TOYSH_FLAG_PIPE 4
+#define TOYSH_FLAG_AND 8
+#define TOYSH_FLAG_OR 16
+#define TOYSH_FLAG_AMP 32
+#define TOYSH_FLAG_SEMI 64
+#define TOYSH_FLAG_PAREN 128
+
+// What we know about a single process.
+struct command {
+ struct command *next;
+ int flags; // exit, suspend, && ||
+ int pid; // pid (or exit code)
+ int argc;
+ char *argv[0];
+};
+
+// A collection of processes piped into/waiting on each other.
+struct pipeline {
+ struct pipeline *next;
+ int job_id;
+ struct command *cmd;
+ char *cmdline; // Unparsed line for display purposes
+ int cmdlinelen; // How long is cmdline?
+};
+
+// 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)
{
- int argc = 0, status;
- char *argv[10], *start = command;
- pid_t pid;
- struct toy_list *tl;
+ char *end;
+
+ // Detect end of line (and truncate line at comment)
+ if (CFG_TOYSH_PIPES && strchr("><&|(;", *start)) return 0;
+
+ // Grab next word. (Add dequote and envvar logic here)
+ end = start;
+ while (*end && !isspace(*end)) end++;
+ (*cmd)->argv[(*cmd)->argc++] = xstrndup(start, end-start);
+
+ // Allocate more space if there's no room for NULL terminator.
+
+ if (!((*cmd)->argc & 7))
+ xrealloc((void **)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.
+
+static char *parse_pipeline(char *cmdline, struct pipeline *line)
+{
+ struct command **cmd = &(line->cmd);
+ char *start = line->cmdline = cmdline;
+ if (!cmdline) return 0;
+
+ if (CFG_TOYSH_JOBCTL) line->cmdline = cmdline;
+
// Parse command into argv[]
for (;;) {
char *end;
+
+ // Skip leading whitespace and detect end of line.
+ while (isspace(*start)) start++;
+ if (!*start || *start=='#') {
+ if (CFG_TOYSH_JOBCTL) line->cmdlinelen = start-cmdline;
+ return 0;
+ }
+
+ // Allocate next command structure if necessary
+ if (!*cmd) *cmd = xzalloc(sizeof(struct command)+8*sizeof(char *));
- // Skip leading whitespace and detect EOL.
- while(isspace(*start)) start++;
- if (!*start || *start=='#') break;
+ // Parse next argument and add the results to argv[]
+ end = parse_word(start, cmd);
- // Grab next word. (Add dequote and envvar logic here)
- end = start;
- while (*end && !isspace(*end)) end++;
- argv[argc++] = xstrndup(start, end-start);
- start=end;
+ // If we hit the end of this command, how did it end?
+ if (!end) {
+ if (CFG_TOYSH_PIPES && *start) {
+ if (*start==';') {
+ start++;
+ break;
+ }
+ // handle | & < > >> << || &&
+ }
+ break;
+ }
+ start = end;
}
- argv[argc]=0;
- if (!argc) return 0;
+ if (CFG_TOYSH_JOBCTL) line->cmdlinelen = start-cmdline;
- tl = toy_find(argv[0]);
- // This is a bit awkward, next design cycle should clean it up.
- // Should vfork(), move to tryspawn()?
- pid = 0;
- if (tl && (tl->flags & TOYFLAG_NOFORK))
- status = tl->toy_main();
- else {
- pid=fork();
- if(!pid) {
- toy_exec(argv);
- xexec(argv);
- } else waitpid(pid, &status, 0);
+ return start;
+}
+
+// Execute the commands in a pipeline
+static void run_pipeline(struct pipeline *line)
+{
+ struct toy_list *tl;
+ struct command *cmd = line->cmd;
+ if (!cmd || !cmd->argc) return;
+
+ tl = toy_find(cmd->argv[0]);
+ // Is this command a builtin that should run in this process?
+ if (tl && (tl->flags & TOYFLAG_NOFORK)) {
+ struct toy_list *which = toys.which;
+ char **argv = toys.argv;
+
+ toy_init(tl, cmd->argv);
+ cmd->pid = tl->toy_main();
+ toy_init(which, argv);
+ } else {
+ int status;
+
+ cmd->pid = vfork();
+ if (!cmd->pid) xexec(cmd->argv);
+ else waitpid(cmd->pid, &status, 0);
+
+ if (CFG_TOYSH_FLOWCTL || CFG_TOYSH_PIPES) {
+ if (WIFEXITED(status)) cmd->pid = WEXITSTATUS(status);
+ if (WIFSIGNALED(status)) cmd->pid = WTERMSIG(status);
+ }
}
- while(argc) free(argv[--argc]);
- return 0;
+ return;
+}
+
+// Free the contents of a command structure
+static void free_cmd(void *data)
+{
+ struct command *cmd=(struct command *)data;
+
+ while(cmd->argc) free(cmd->argv[--cmd->argc]);
+}
+
+
+// Parse a command line and do what it says to do.
+static void handle(char *command)
+{
+ struct pipeline line;
+ char *start = command;
+
+ // Loop through commands in this line
+
+ for (;;) {
+
+ // Parse a group of connected commands
+
+ memset(&line,0,sizeof(struct pipeline));
+ start = parse_pipeline(start, &line);
+ if (!line.cmd) break;
+
+ // Run those commands
+
+ run_pipeline(&line);
+ llist_free(line.cmd, free_cmd);
+ }
}
int cd_main(void)