X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=shell%2Fbbsh.c;h=897c0227cce785504a9dc6076eabab41d3356fad;hb=33f85eeac5a7babc996cacce4485326d46b6e54d;hp=57a103bf8871a53455cc70db6a40a254e9bf87fd;hpb=3476ad651dc023a35abe87a49478d806dee22f97;p=oweals%2Fbusybox.git diff --git a/shell/bbsh.c b/shell/bbsh.c index 57a103bf8..897c0227c 100644 --- a/shell/bbsh.c +++ b/shell/bbsh.c @@ -1,5 +1,5 @@ /* vi: set ts=4 : - * + * * bbsh - busybox shell * * Copyright 2006 Rob Landley @@ -36,7 +36,7 @@ echo `echo hello#comment " woot` and more */ -#include +#include "libbb.h" // A single executable, its arguments, and other information we know about it. #define BBSH_FLAG_EXIT 1 @@ -51,7 +51,7 @@ // What we know about a single process. struct command { struct command *next; - int flags; // exit, suspend, && || + int flags; // exit, suspend, && || int pid; // pid (or exit code) int argc; char *argv[0]; @@ -68,7 +68,7 @@ struct pipeline { static void free_list(void *list, void (*freeit)(void *data)) { - while(list) { + while (list) { void **next = (void **)list; void *list_next = *next; freeit(list); @@ -90,7 +90,7 @@ static char *parse_word(char *start, struct command **cmd) // Grab next word. (Add dequote and envvar logic here) end = start; - while (*end && !isspace(*end)) end++; + end = skip_non_whitespace(end); (*cmd)->argv[(*cmd)->argc++] = xstrndup(start, end-start); // Allocate more space if there's no room for NULL terminator. @@ -105,29 +105,29 @@ static char *parse_word(char *start, struct command **cmd) // Parse a line of text into a pipeline. // Returns a pointer to the next line. -static char *parse_pipeline(char *cmdline, struct pipeline *pipe) +static char *parse_pipeline(char *cmdline, struct pipeline *line) { - struct command **cmd = &(pipe->cmd); - char *start = pipe->cmdline = cmdline; + struct command **cmd = &(line->cmd); + char *start = line->cmdline = cmdline; if (!cmdline) return 0; - if (ENABLE_BBSH_JOBCTL) pipe->cmdline = cmdline; + if (ENABLE_BBSH_JOBCTL) line->cmdline = cmdline; // Parse command into argv[] for (;;) { char *end; // Skip leading whitespace and detect end of line. - while (isspace(*start)) start++; + start = skip_whitespace(start); if (!*start || *start=='#') { - if (ENABLE_BBSH_JOBCTL) pipe->cmdlinelen = start-cmdline; + if (ENABLE_BBSH_JOBCTL) line->cmdlinelen = start-cmdline; return 0; } - // Allocate next command structure if necessary + // Allocate next command structure if necessary if (!*cmd) *cmd = xzalloc(sizeof(struct command)+8*sizeof(char *)); - + // Parse next argument and add the results to argv[] end = parse_word(start, cmd); @@ -138,37 +138,37 @@ static char *parse_pipeline(char *cmdline, struct pipeline *pipe) start++; break; } - // handle | & < > >> << || && + // handle | & < > >> << || && } break; } start = end; } - if (ENABLE_BBSH_JOBCTL) pipe->cmdlinelen = start-cmdline; + if (ENABLE_BBSH_JOBCTL) line->cmdlinelen = start-cmdline; return start; } // Execute the commands in a pipeline -static int run_pipeline(struct pipeline *pipe) +static int run_pipeline(struct pipeline *line) { - struct command *cmd = pipe->cmd; + struct command *cmd = line->cmd; 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]); - else if(!strcmp(cmd->argv[0],"exit")) - exit(cmd->argc>1 ? atoi(cmd->argv[1]) : 0); + else if (!strcmp(cmd->argv[0],"exit")) + exit(cmd->argc>1 ? atoi(cmd->argv[1]) : 0); else { int status; pid_t pid=fork(); - if(!pid) { - run_applet_by_name(cmd->argv[0],cmd->argc,cmd->argv); + if (!pid) { + run_applet_and_exit(cmd->argv[0],cmd->argc,cmd->argv); execvp(cmd->argv[0],cmd->argv); - printf("No %s",cmd->argv[0]); - exit(1); + printf("No %s", cmd->argv[0]); + exit(EXIT_FAILURE); } else waitpid(pid, &status, 0); } @@ -179,46 +179,45 @@ static void free_cmd(void *data) { struct command *cmd=(struct command *)data; - while(cmd->argc) free(cmd->argv[--cmd->argc]); + while (cmd->argc) free(cmd->argv[--cmd->argc]); } static void handle(char *command) { - struct pipeline pipe; + struct pipeline line; char *start = command; for (;;) { - memset(&pipe,0,sizeof(struct pipeline)); - start = parse_pipeline(start, &pipe); - if (!pipe.cmd) break; + memset(&line,0,sizeof(struct pipeline)); + start = parse_pipeline(start, &line); + if (!line.cmd) break; - run_pipeline(&pipe); - free_list(pipe.cmd, free_cmd); + run_pipeline(&line); + free_list(line.cmd, free_cmd); } } -int bbsh_main(int argc, char *argv[]) +int bbsh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int bbsh_main(int argc, char **argv) { char *command=NULL; FILE *f; - bb_getopt_ulflags(argc, argv, "c:", &command); + getopt32(argv, "c:", &command); - f = argv[optind] ? xfopen(argv[optind],"r") : NULL; + f = argv[optind] ? xfopen_for_read(argv[optind]) : NULL; if (command) handle(command); else { unsigned cmdlen=0; for (;;) { - struct pipeline pipe; - - if(!f) putchar('$'); - if(1 > getline(&command, &cmdlen,f ? : stdin)) break; + if (!f) putchar('$'); + if (1 > getline(&command, &cmdlen,f ? : stdin)) break; handle(command); } if (ENABLE_FEATURE_CLEAN_UP) free(command); } - + return 1; }