//Allow the shell to invoke all the compiled in BusyBox commands as if they
//were shell builtins. Nice for staticly linking an emergency rescue shell
//amoung other thing.
-#define BB_FEATURE_STANDALONE_SHELL
+#define BB_FEATURE_SH_STANDALONE_SHELL
//
// Enable tab completion in the shell (not yet
// working very well -- so don't turn this on)
#include "cmdedit.h"
#endif
-
+#define MAX_READ 128 /* size of input buffer for `read' builtin */
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
static int shell_export(struct job *cmd, struct jobSet *junk);
static int shell_source(struct job *cmd, struct jobSet *jobList);
static int shell_unset(struct job *cmd, struct jobSet *junk);
+static int shell_read(struct job *cmd, struct jobSet *junk);
static void checkJobs(struct jobSet *jobList);
static int getCommand(FILE * source, char *command);
{"jobs", "Lists the active jobs", "jobs", shell_jobs},
{"export", "Set environment variable", "export [VAR=value]", shell_export},
{"unset", "Unset environment variable", "unset VAR", shell_unset},
+ {"read", "Input environment variable", "read [VAR]", shell_read},
{NULL, NULL, NULL, NULL}
};
#endif
;
-static char cwd[1024];
static char *prompt = "# ";
+static char *cwd = NULL;
static char *local_pending_command = NULL;
#ifdef BB_FEATURE_SH_COMMAND_EDITING
return (res);
}
+/* built-in 'read VAR' handler */
+static int shell_read(struct job *cmd, struct jobSet *junk)
+{
+ int res = 0, len, newlen;
+ char *s;
+ char string[MAX_READ];
+
+ if (cmd->progs[0].argv[1]) {
+ /* argument (VAR) given: put "VAR=" into buffer */
+ strcpy(string, cmd->progs[0].argv[1]);
+ len = strlen(string);
+ string[len++] = '=';
+ string[len] = '\0';
+ fgets(&string[len], sizeof(string) - len, stdin); /* read string */
+ newlen = strlen(string);
+ if(newlen > len)
+ string[--newlen] = '\0'; /* chomp trailing newline */
+ /*
+ ** string should now contain "VAR=<value>"
+ ** copy it (putenv() won't do that, so we must make sure
+ ** the string resides in a static buffer!)
+ */
+ res = -1;
+ if((s = strdup(string)))
+ res = putenv(s);
+ if (res)
+ fprintf(stdout, "read: %s\n", strerror(errno));
+ }
+ else
+ fgets(string, sizeof(string), stdin);
+
+ return (res);
+}
+
/* Built-in '.' handler (read-in and execute commands from file) */
static int shell_source(struct job *cmd, struct jobSet *junk)
{
int rc;
int flags;
int i;
- char *src, *dst;
+ char *src, *dst, *var;
if (argc > 1) { /* cmd->globResult is already initialized */
flags = GLOB_APPEND;
flags = 0;
i = 0;
}
+ /* do shell variable substitution */
+ if(*prog->argv[argc - 1] == '$' && (var = getenv(prog->argv[argc - 1] + 1)))
+ prog->argv[argc - 1] = var;
rc = glob(prog->argv[argc - 1], flags, NULL, &prog->globResult);
if (rc == GLOB_NOSPACE) {
job->progs = malloc(sizeof(*job->progs));
/* We set the argv elements to point inside of this string. The
- memory is freed by freeJob().
+ memory is freed by freeJob(). Allocate twice the original
+ length in case we need to quote every single character.
Getting clean memory relieves us of the task of NULL
terminating things and makes the rest of this look a bit
cleaner (though it is, admittedly, a tad less efficient) */
- job->cmdBuf = command = calloc(1, strlen(*commandPtr) + 1);
+ job->cmdBuf = command = calloc(1, 2*strlen(*commandPtr) + 1);
job->text = NULL;
prog = job->progs;
sizeof(*prog->argv) *
argvAlloced);
}
- prog->argv[argc] = buf;
-
globLastArgument(prog, &argc, &argvAlloced);
+ prog->argv[argc] = buf;
}
} else
switch (*src) {
if (*chptr && *prog->argv[argc]) {
buf++, argc++;
globLastArgument(prog, &argc, &argvAlloced);
+ prog->argv[argc] = buf;
}
}
int nextin, nextout;
int pipefds[2]; /* pipefd[0] is for reading */
struct builtInCommand *x;
-#ifdef BB_FEATURE_STANDALONE_SHELL
+#ifdef BB_FEATURE_SH_STANDALONE_SHELL
const struct BB_applet *a = applets;
#endif
exit (x->function(&newJob, jobList));
}
}
-#ifdef BB_FEATURE_STANDALONE_SHELL
+#ifdef BB_FEATURE_SH_STANDALONE_SHELL
/* Handle busybox internals here */
while (a->name != 0) {
if (strcmp(newJob.progs[i].argv[0], a->name) == 0) {
if (tcsetpgrp(0, parent_pgrp))
perror("tcsetpgrp");
+ /* return controlling TTY back to parent process group before exiting */
+ if (tcsetpgrp(0, parent_pgrp))
+ perror("tcsetpgrp");
+
+ /* return exit status if called with "-c" */
+ if (input == NULL && WIFEXITED(status))
+ return WEXITSTATUS(status);
+
return 0;
}
FILE *input = stdin;
/* initialize the cwd */
- getcwd(cwd, sizeof(cwd));
+ cwd = (char *) calloc(BUFSIZ, sizeof(char));
+ if (cwd == 0) {
+ fatalError("sh: out of memory\n");
+ }
+ getcwd(cwd, sizeof(char)*BUFSIZ);
#ifdef BB_FEATURE_SH_COMMAND_EDITING
cmdedit_init();
for(i=2; i<argc; i++)
{
if (strlen(local_pending_command) + strlen(argv[i]) >= BUFSIZ) {
- fatalError("sh: commands for -c option too long\n");
+ local_pending_command = realloc(local_pending_command,
+ strlen(local_pending_command) + strlen(argv[i]));
+ if (local_pending_command==NULL)
+ fatalError("sh: commands for -c option too long\n");
}
strcat(local_pending_command, argv[i]);
- if (i + 1 < argc)
+ if ( (i + 1) < argc)
strcat(local_pending_command, " ");
}
input = NULL;
#include "cmdedit.h"
#endif
-
+#define MAX_READ 128 /* size of input buffer for `read' builtin */
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
static int shell_export(struct job *cmd, struct jobSet *junk);
static int shell_source(struct job *cmd, struct jobSet *jobList);
static int shell_unset(struct job *cmd, struct jobSet *junk);
+static int shell_read(struct job *cmd, struct jobSet *junk);
static void checkJobs(struct jobSet *jobList);
static int getCommand(FILE * source, char *command);
{"jobs", "Lists the active jobs", "jobs", shell_jobs},
{"export", "Set environment variable", "export [VAR=value]", shell_export},
{"unset", "Unset environment variable", "unset VAR", shell_unset},
+ {"read", "Input environment variable", "read [VAR]", shell_read},
{NULL, NULL, NULL, NULL}
};
#endif
;
-static char cwd[1024];
static char *prompt = "# ";
+static char *cwd = NULL;
static char *local_pending_command = NULL;
#ifdef BB_FEATURE_SH_COMMAND_EDITING
return (res);
}
+/* built-in 'read VAR' handler */
+static int shell_read(struct job *cmd, struct jobSet *junk)
+{
+ int res = 0, len, newlen;
+ char *s;
+ char string[MAX_READ];
+
+ if (cmd->progs[0].argv[1]) {
+ /* argument (VAR) given: put "VAR=" into buffer */
+ strcpy(string, cmd->progs[0].argv[1]);
+ len = strlen(string);
+ string[len++] = '=';
+ string[len] = '\0';
+ fgets(&string[len], sizeof(string) - len, stdin); /* read string */
+ newlen = strlen(string);
+ if(newlen > len)
+ string[--newlen] = '\0'; /* chomp trailing newline */
+ /*
+ ** string should now contain "VAR=<value>"
+ ** copy it (putenv() won't do that, so we must make sure
+ ** the string resides in a static buffer!)
+ */
+ res = -1;
+ if((s = strdup(string)))
+ res = putenv(s);
+ if (res)
+ fprintf(stdout, "read: %s\n", strerror(errno));
+ }
+ else
+ fgets(string, sizeof(string), stdin);
+
+ return (res);
+}
+
/* Built-in '.' handler (read-in and execute commands from file) */
static int shell_source(struct job *cmd, struct jobSet *junk)
{
int rc;
int flags;
int i;
- char *src, *dst;
+ char *src, *dst, *var;
if (argc > 1) { /* cmd->globResult is already initialized */
flags = GLOB_APPEND;
flags = 0;
i = 0;
}
+ /* do shell variable substitution */
+ if(*prog->argv[argc - 1] == '$' && (var = getenv(prog->argv[argc - 1] + 1)))
+ prog->argv[argc - 1] = var;
rc = glob(prog->argv[argc - 1], flags, NULL, &prog->globResult);
if (rc == GLOB_NOSPACE) {
job->progs = malloc(sizeof(*job->progs));
/* We set the argv elements to point inside of this string. The
- memory is freed by freeJob().
+ memory is freed by freeJob(). Allocate twice the original
+ length in case we need to quote every single character.
Getting clean memory relieves us of the task of NULL
terminating things and makes the rest of this look a bit
cleaner (though it is, admittedly, a tad less efficient) */
- job->cmdBuf = command = calloc(1, strlen(*commandPtr) + 1);
+ job->cmdBuf = command = calloc(1, 2*strlen(*commandPtr) + 1);
job->text = NULL;
prog = job->progs;
sizeof(*prog->argv) *
argvAlloced);
}
- prog->argv[argc] = buf;
-
globLastArgument(prog, &argc, &argvAlloced);
+ prog->argv[argc] = buf;
}
} else
switch (*src) {
if (*chptr && *prog->argv[argc]) {
buf++, argc++;
globLastArgument(prog, &argc, &argvAlloced);
+ prog->argv[argc] = buf;
}
}
int nextin, nextout;
int pipefds[2]; /* pipefd[0] is for reading */
struct builtInCommand *x;
-#ifdef BB_FEATURE_STANDALONE_SHELL
+#ifdef BB_FEATURE_SH_STANDALONE_SHELL
const struct BB_applet *a = applets;
#endif
exit (x->function(&newJob, jobList));
}
}
-#ifdef BB_FEATURE_STANDALONE_SHELL
+#ifdef BB_FEATURE_SH_STANDALONE_SHELL
/* Handle busybox internals here */
while (a->name != 0) {
if (strcmp(newJob.progs[i].argv[0], a->name) == 0) {
if (tcsetpgrp(0, parent_pgrp))
perror("tcsetpgrp");
+ /* return controlling TTY back to parent process group before exiting */
+ if (tcsetpgrp(0, parent_pgrp))
+ perror("tcsetpgrp");
+
+ /* return exit status if called with "-c" */
+ if (input == NULL && WIFEXITED(status))
+ return WEXITSTATUS(status);
+
return 0;
}
FILE *input = stdin;
/* initialize the cwd */
- getcwd(cwd, sizeof(cwd));
+ cwd = (char *) calloc(BUFSIZ, sizeof(char));
+ if (cwd == 0) {
+ fatalError("sh: out of memory\n");
+ }
+ getcwd(cwd, sizeof(char)*BUFSIZ);
#ifdef BB_FEATURE_SH_COMMAND_EDITING
cmdedit_init();
for(i=2; i<argc; i++)
{
if (strlen(local_pending_command) + strlen(argv[i]) >= BUFSIZ) {
- fatalError("sh: commands for -c option too long\n");
+ local_pending_command = realloc(local_pending_command,
+ strlen(local_pending_command) + strlen(argv[i]));
+ if (local_pending_command==NULL)
+ fatalError("sh: commands for -c option too long\n");
}
strcat(local_pending_command, argv[i]);
- if (i + 1 < argc)
+ if ( (i + 1) < argc)
strcat(local_pending_command, " ");
}
input = NULL;
#include "cmdedit.h"
#endif
-
+#define MAX_READ 128 /* size of input buffer for `read' builtin */
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
static int shell_export(struct job *cmd, struct jobSet *junk);
static int shell_source(struct job *cmd, struct jobSet *jobList);
static int shell_unset(struct job *cmd, struct jobSet *junk);
+static int shell_read(struct job *cmd, struct jobSet *junk);
static void checkJobs(struct jobSet *jobList);
static int getCommand(FILE * source, char *command);
{"jobs", "Lists the active jobs", "jobs", shell_jobs},
{"export", "Set environment variable", "export [VAR=value]", shell_export},
{"unset", "Unset environment variable", "unset VAR", shell_unset},
+ {"read", "Input environment variable", "read [VAR]", shell_read},
{NULL, NULL, NULL, NULL}
};
#endif
;
-static char cwd[1024];
static char *prompt = "# ";
+static char *cwd = NULL;
static char *local_pending_command = NULL;
#ifdef BB_FEATURE_SH_COMMAND_EDITING
return (res);
}
+/* built-in 'read VAR' handler */
+static int shell_read(struct job *cmd, struct jobSet *junk)
+{
+ int res = 0, len, newlen;
+ char *s;
+ char string[MAX_READ];
+
+ if (cmd->progs[0].argv[1]) {
+ /* argument (VAR) given: put "VAR=" into buffer */
+ strcpy(string, cmd->progs[0].argv[1]);
+ len = strlen(string);
+ string[len++] = '=';
+ string[len] = '\0';
+ fgets(&string[len], sizeof(string) - len, stdin); /* read string */
+ newlen = strlen(string);
+ if(newlen > len)
+ string[--newlen] = '\0'; /* chomp trailing newline */
+ /*
+ ** string should now contain "VAR=<value>"
+ ** copy it (putenv() won't do that, so we must make sure
+ ** the string resides in a static buffer!)
+ */
+ res = -1;
+ if((s = strdup(string)))
+ res = putenv(s);
+ if (res)
+ fprintf(stdout, "read: %s\n", strerror(errno));
+ }
+ else
+ fgets(string, sizeof(string), stdin);
+
+ return (res);
+}
+
/* Built-in '.' handler (read-in and execute commands from file) */
static int shell_source(struct job *cmd, struct jobSet *junk)
{
int rc;
int flags;
int i;
- char *src, *dst;
+ char *src, *dst, *var;
if (argc > 1) { /* cmd->globResult is already initialized */
flags = GLOB_APPEND;
flags = 0;
i = 0;
}
+ /* do shell variable substitution */
+ if(*prog->argv[argc - 1] == '$' && (var = getenv(prog->argv[argc - 1] + 1)))
+ prog->argv[argc - 1] = var;
rc = glob(prog->argv[argc - 1], flags, NULL, &prog->globResult);
if (rc == GLOB_NOSPACE) {
job->progs = malloc(sizeof(*job->progs));
/* We set the argv elements to point inside of this string. The
- memory is freed by freeJob().
+ memory is freed by freeJob(). Allocate twice the original
+ length in case we need to quote every single character.
Getting clean memory relieves us of the task of NULL
terminating things and makes the rest of this look a bit
cleaner (though it is, admittedly, a tad less efficient) */
- job->cmdBuf = command = calloc(1, strlen(*commandPtr) + 1);
+ job->cmdBuf = command = calloc(1, 2*strlen(*commandPtr) + 1);
job->text = NULL;
prog = job->progs;
sizeof(*prog->argv) *
argvAlloced);
}
- prog->argv[argc] = buf;
-
globLastArgument(prog, &argc, &argvAlloced);
+ prog->argv[argc] = buf;
}
} else
switch (*src) {
if (*chptr && *prog->argv[argc]) {
buf++, argc++;
globLastArgument(prog, &argc, &argvAlloced);
+ prog->argv[argc] = buf;
}
}
int nextin, nextout;
int pipefds[2]; /* pipefd[0] is for reading */
struct builtInCommand *x;
-#ifdef BB_FEATURE_STANDALONE_SHELL
+#ifdef BB_FEATURE_SH_STANDALONE_SHELL
const struct BB_applet *a = applets;
#endif
exit (x->function(&newJob, jobList));
}
}
-#ifdef BB_FEATURE_STANDALONE_SHELL
+#ifdef BB_FEATURE_SH_STANDALONE_SHELL
/* Handle busybox internals here */
while (a->name != 0) {
if (strcmp(newJob.progs[i].argv[0], a->name) == 0) {
if (tcsetpgrp(0, parent_pgrp))
perror("tcsetpgrp");
+ /* return controlling TTY back to parent process group before exiting */
+ if (tcsetpgrp(0, parent_pgrp))
+ perror("tcsetpgrp");
+
+ /* return exit status if called with "-c" */
+ if (input == NULL && WIFEXITED(status))
+ return WEXITSTATUS(status);
+
return 0;
}
FILE *input = stdin;
/* initialize the cwd */
- getcwd(cwd, sizeof(cwd));
+ cwd = (char *) calloc(BUFSIZ, sizeof(char));
+ if (cwd == 0) {
+ fatalError("sh: out of memory\n");
+ }
+ getcwd(cwd, sizeof(char)*BUFSIZ);
#ifdef BB_FEATURE_SH_COMMAND_EDITING
cmdedit_init();
for(i=2; i<argc; i++)
{
if (strlen(local_pending_command) + strlen(argv[i]) >= BUFSIZ) {
- fatalError("sh: commands for -c option too long\n");
+ local_pending_command = realloc(local_pending_command,
+ strlen(local_pending_command) + strlen(argv[i]));
+ if (local_pending_command==NULL)
+ fatalError("sh: commands for -c option too long\n");
}
strcat(local_pending_command, argv[i]);
- if (i + 1 < argc)
+ if ( (i + 1) < argc)
strcat(local_pending_command, " ");
}
input = NULL;