From b8c6ae54395f63eb7e95770c085894b48bbe5f41 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Tue, 17 Jul 2018 16:52:33 +0900 Subject: [PATCH] Shotty support for > and >> --- apps/sh.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/apps/sh.c b/apps/sh.c index 9af886af..b143cc7c 100644 --- a/apps/sh.c +++ b/apps/sh.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -39,6 +40,8 @@ #define PIPE_TOKEN "\xFF\xFFPIPE\xFF\xFF" #define STAR_TOKEN "\xFF\xFFSTAR\xFF\xFF" +#define WRITE_TOKEN "\xFF\xFFWRITE\xFF\xFF" +#define APPEND_TOKEN "\xFF\xFF""APPEND\xFF" /* A shell command is like a C program */ typedef uint32_t(*shell_command_t) (int argc, char ** argv); @@ -529,6 +532,13 @@ int shell_exec(char * buffer) { collected = sprintf(buffer_, "%s", PIPE_TOKEN); goto _new_arg; } + goto _just_add; + case '>': + if (!quoted && !backtick && !collected) { + collected = sprintf(buffer_, "%s", WRITE_TOKEN); + goto _new_arg; + } + goto _just_add; default: if (backtick) { buffer_[collected] = '\\'; @@ -580,12 +590,31 @@ _done: int cmdi = 0; char ** arg_starts[100] = { &argv[0], NULL }; + char * output_files[100] = { NULL }; + int file_args[100] = {0}; int argcs[100] = {0}; + int next_is_file = 0; int i = 0; foreach(node, args) { char * c = node->value; + if (next_is_file) { + if (next_is_file == 1 && !strcmp(c, WRITE_TOKEN)) { + next_is_file = 2; + file_args[cmdi] |= O_APPEND; + continue; + } + output_files[cmdi] = c; + continue; + } + + if (!strcmp(c, WRITE_TOKEN)) { + next_is_file = 1; + file_args[cmdi] = O_WRONLY | O_CREAT; + continue; + } + if (!strcmp(c, PIPE_TOKEN)) { argv[i] = 0; i++; @@ -712,6 +741,9 @@ _nope: } if (!fork()) { + if (output_files[cmdi]) { + dup2(open(output_files[cmdi], file_args[cmdi], 0666), STDOUT_FILENO); + } dup2(last_output[0], STDIN_FILENO); close(last_output[1]); run_cmd(arg_starts[cmdi]); @@ -727,6 +759,9 @@ _nope: } else { child_pid = fork(); if (!child_pid) { + if (output_files[cmdi]) { + dup2(open(output_files[cmdi], file_args[cmdi], 0666), STDOUT_FILENO); + } run_cmd(arg_starts[0]); } }