aboutsummaryrefslogtreecommitdiff
path: root/smash/parser.c
blob: 55a6fd2852b4ce9ef95b48f01094ef17b1c516bc (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
76
77
78
79
80
81
82
#include "parser.h"

#include "../lib/malloc/malloc.h"
#include "../lib/cstr/cstr.h"

#include "../lib/sys/write.h"

char **__new_argv(char *exp);
void __free_argv(char **argv);

expression_list_t *new_expression_from_line(char *line)
{

	u64 n = cstr_split(line, '|');
	char *current = line;
	char *next    = 0;
	expression_list_t *head = 0;
	expression_list_t *expression = 0;

	for (; n > 0; --n) {
		next = (char*)next_split(current);

		strip_cstr(current, ' ');

		if (!expression) {
			expression = malloc(sizeof(expression_list_t));
			head = expression;
		} else {
			expression->next = malloc(sizeof(expression_list_t));
			expression = expression->next;
		}

		expression->call = current;
		expression->next = 0;

		current = next;
	}

	return head;
}


void free_expression(expression_list_t *expression)
{
	expression_list_t *next;

	while (expression) {
		next = expression->next;
		free(expression);
		expression = next;
	}
}


char **new_argv(char *exp)
{
	u64 n = cstr_split(exp, ' ');
	char **argv = malloc(sizeof(char*) * (n + 1));
	u64 i = 0;
	char *current = exp;
	char *next;

	argv[n] = 0;

	for (; n > 0; --n) {
		next = (char*)next_split(current);
		strip_cstr(current, ' ');

		if (cstr_length(current))
			argv[i++] = current;

		current = next;
	}

	return argv;
}


void free_argv(char **argv)
{
	free(argv);
}