Parse commandline strings, move away from old env vars
This commit is contained in:
44
ce/ce.c
44
ce/ce.c
@@ -3,6 +3,7 @@
|
||||
#include "interp.h"
|
||||
#include "strbuf.h"
|
||||
#include <arena.h>
|
||||
#include <cmdline_parser.h>
|
||||
#include <debugconsole.h>
|
||||
#include <filereader.h>
|
||||
#include <in_gb.h>
|
||||
@@ -22,7 +23,16 @@
|
||||
|
||||
#define PROMPT "$ "
|
||||
|
||||
bool print_commands = false;
|
||||
static bool cmdline_printcmds = false;
|
||||
static char cmdline_script[CMDLINE_OPT_VALUE_MAX];
|
||||
static char cmdline_posvars[CMDLINE_OPT_VALUE_MAX];
|
||||
|
||||
static struct cmdline_opt cmdline_opts[] = {
|
||||
CMDLINE_OPT("p", "printcmds", CMDLINE_OPT_VALUE_BOOL, false, &cmdline_printcmds),
|
||||
CMDLINE_OPT("s", "script", CMDLINE_OPT_VALUE_STRING, false, (char*)cmdline_script),
|
||||
CMDLINE_OPT("a", "args", CMDLINE_OPT_VALUE_STRING, false, (char*)cmdline_posvars),
|
||||
CMDLINE_END(),
|
||||
};
|
||||
|
||||
void* wmalloc(void* ctx, size_t size) {
|
||||
(void)ctx;
|
||||
@@ -59,7 +69,7 @@ struct edit_line {
|
||||
static void exec_line(const char* line) {
|
||||
struct list_node_link* tokens = NULL;
|
||||
|
||||
if (print_commands)
|
||||
if (cmdline_printcmds)
|
||||
mprintf("+%s\n", line);
|
||||
|
||||
tokenize(&tokens, line);
|
||||
@@ -98,35 +108,23 @@ static bool split_args_cb(void* ctx, const char* start, size_t len) {
|
||||
}
|
||||
|
||||
void app_main(void) {
|
||||
char scriptpathbuf[PATH_MAX];
|
||||
char posvarsbuf[2048];
|
||||
char printcmdsbuf[4];
|
||||
char line[1024];
|
||||
|
||||
const char* cmdline = get_cmdline();
|
||||
|
||||
debug_printf("cmdline=\"%s\"\n", cmdline);
|
||||
|
||||
if (env_get(process_get_pgid(), "printcmds", (void*)printcmdsbuf, sizeof(printcmdsbuf)) ==
|
||||
ST_OK &&
|
||||
strcmp(printcmdsbuf, "yes") == 0) {
|
||||
print_commands = true;
|
||||
if (cmdline_parse(get_cmdline(), cmdline_opts) < 0) {
|
||||
mprintf("Failed to parse commandline arguments\n");
|
||||
return;
|
||||
}
|
||||
|
||||
memset(scriptpathbuf, 0, sizeof(scriptpathbuf));
|
||||
int has_script = env_get(process_get_pgid(), "s", (void*)scriptpathbuf, sizeof(scriptpathbuf));
|
||||
char line[1024];
|
||||
|
||||
if (has_script == ST_OK) {
|
||||
if (env_get(process_get_pgid(), "args", (void*)posvarsbuf, sizeof(posvarsbuf)) == ST_OK) {
|
||||
strtokenize(posvarsbuf, ' ', NULL, &split_args_cb);
|
||||
}
|
||||
if (strlen(cmdline_script) > 0) {
|
||||
if (strlen(cmdline_posvars) > 0)
|
||||
strtokenize(cmdline_posvars, ' ', NULL, &split_args_cb);
|
||||
|
||||
char volume[VOLUME_MAX];
|
||||
const char* path;
|
||||
int ret;
|
||||
|
||||
if (!path_parse(scriptpathbuf, volume, &path)) {
|
||||
mprintf("ERROR bad path '%s'\n", scriptpathbuf);
|
||||
if (!path_parse(cmdline_script, volume, &path)) {
|
||||
mprintf("ERROR bad path '%s'\n", cmdline_script);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
36
ce/interp.c
36
ce/interp.c
@@ -27,11 +27,11 @@
|
||||
#include <tscreen.h>
|
||||
#include <write_file.h>
|
||||
|
||||
static bool run = true;
|
||||
|
||||
struct posvar posvars[POSVAR_MAX];
|
||||
int posvar_count = 0;
|
||||
|
||||
static bool run = true;
|
||||
|
||||
bool interp_is_running(void) { return run; }
|
||||
|
||||
void interp_shutdown(void) { run = false; }
|
||||
@@ -532,7 +532,17 @@ static void execute_cmd(struct ast_cmd* cmd, struct context* context, bool run_b
|
||||
return;
|
||||
}
|
||||
|
||||
int pid = exec_partial(volume, path);
|
||||
struct strbuf tmpstrbuf;
|
||||
memset(&tmpstrbuf, 0, sizeof(tmpstrbuf));
|
||||
|
||||
for (int i = 0; i < cmd->arg_count; i++) {
|
||||
strbuf_append_str(&tmpstrbuf, cmd->args[i]);
|
||||
strbuf_append(&tmpstrbuf, ' ');
|
||||
}
|
||||
|
||||
strbuf_append(&tmpstrbuf, '\0');
|
||||
|
||||
int pid = exec_partial(volume, path, tmpstrbuf.items);
|
||||
|
||||
if (pid < 0) {
|
||||
cprintf(context, "ERROR could not run '%s': %s\n", cmd->name, str_status[-pid]);
|
||||
@@ -541,26 +551,6 @@ static void execute_cmd(struct ast_cmd* cmd, struct context* context, bool run_b
|
||||
|
||||
int pgid = get_procgroup(pid);
|
||||
|
||||
int i = 0;
|
||||
while (i < cmd->arg_count) {
|
||||
char* arg = cmd->args[i];
|
||||
char *key, *value;
|
||||
|
||||
if (arg[0] == '-') {
|
||||
key = &arg[1];
|
||||
|
||||
if (i < cmd->arg_count - 1) {
|
||||
value = cmd->args[++i];
|
||||
} else {
|
||||
value = "yes";
|
||||
}
|
||||
|
||||
env_set(pgid, key, value, strlen(value) + 1);
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if (run_bg) {
|
||||
exec_partial_fini(pid);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user