aboutsummaryrefslogtreecommitdiff
path: root/smash/variable.c
blob: d3855c678c2c3b3df0fd72074ef77ccf70c61adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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];
}