From dffe48ee9dec21e3dbcb374f938d747da8d66f12 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Tue, 14 Feb 2023 09:33:29 +0100 Subject: add set env variables --- smash/variable.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 smash/variable.c (limited to 'smash/variable.c') diff --git a/smash/variable.c b/smash/variable.c new file mode 100644 index 0000000..d3855c6 --- /dev/null +++ b/smash/variable.c @@ -0,0 +1,75 @@ +#include "variable.h" +#include "../lib/cstr/cstr.h" +#include "../lib/sys/pipe.h" +#include "../lib/sys/dup2.h" +#include "../lib/io/io.h" +#include "../lib/sys/types.h" +#include "../lib/env/env.h" + + +char current_write_variable[128] = { 0 }; +int fd[2]; +char readbuf[BUFSIZ]; + +void cleanbuf(char *buf, int n) { + for (int i = 0; i < n; ++i) buf[i] = 0; +} + +char *setup_variable_line_context(char *line) +{ + char* start = line; + fd[PIPE_IN] = STDOUT_FD; + cleanbuf(current_write_variable, 128); + + if (line[0] != '[') + return line; + + int i = 0; + ++line; + + while (*line != ']') current_write_variable[i++] = *(line++); + + if (*(++line) != '=') { + return start; + } + + ++line; + current_write_variable[i] = 0; + + pipe(fd); + + return line; +} + + +void cleanup_variable_line_context() +{ + u64 size = 0; + u64 string_size = 0; + + if (current_write_variable[0] == 0) + return; + + cleanbuf(readbuf, BUFSIZ); + + close(fd[PIPE_IN]); + + while ((size = read(fd[PIPE_OUT], readbuf + string_size, BUFSIZ))) { + string_size += size; + } + + readbuf[string_size] = 0; + + if (readbuf[string_size - 1] == '\n') + readbuf[string_size - 1] = 0; + + setenv(current_write_variable, readbuf); + + close(fd[PIPE_OUT]); +} + + +int get_end_fd() +{ + return fd[PIPE_IN]; +} -- cgit v1.2.3-70-g09d2