diff options
Diffstat (limited to 'smash/variable.c')
| -rw-r--r-- | smash/variable.c | 75 |
1 files changed, 75 insertions, 0 deletions
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]; +} |