aboutsummaryrefslogtreecommitdiff
path: root/smash/variable.c
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-02-14 09:33:29 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2023-02-14 09:33:29 +0100
commitdffe48ee9dec21e3dbcb374f938d747da8d66f12 (patch)
treef4a71f3ade01e2ece4dae580081a9834672911d5 /smash/variable.c
parentcf625ea48ac09b8b484f90e5971e2d8001aa76de (diff)
add set env variables
Diffstat (limited to 'smash/variable.c')
-rw-r--r--smash/variable.c75
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];
+}