summaryrefslogtreecommitdiff
path: root/other/burneye/src/conf/tmp/script.c
blob: 3fc76343daeee4698f2e93b7b3964f63c833767d (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* fornax - distributed network
 *
 * by team teso
 *
 * scripting capabilities routines
 */

#include <stdio.h>
#include "branch.h"
#include "call.h"
#include "compiler.h"
#include "element.h"
#include "functions.h"
#include "script.h"
#include "symbol.h"

sym_elem **	gl_ns = NULL;
extern function	gl_fnc[];


/* static functions
 */

void	scr_elem_exec (element **el);
static void	scr_call (call *c);
static void	scr_branch (branch * br);


/* scr_elem_exec
 *
 * execute the element list pointed to by `el'
 *
 * return in any case
 */

void
scr_elem_exec (element **el)
{
	int		el_ptr;
	element *	ec;

	if (el == NULL || el[0] == NULL)
		return;

	for (el_ptr = 0 ; el[el_ptr] != NULL ; ++el_ptr) {
		ec = el[el_ptr];

		switch (ec->type) {
		case (ELEM_TYPE_CALL):
			scr_call ((call *) ec->data);
			break;

		case (ELEM_TYPE_BRANCH):
			scr_branch ((branch *) ec->data);
			break;

		/* in case it is a set element, just add the symbol element
		 * to the global namespace
		 */
		case (ELEM_TYPE_SET):
			gl_ns = scr_ns_add (gl_ns, (sym_elem *) ec->data);
			break;

		/* should never happen
		 */
		default:
			break;
		}
	}

	return;
}


/* scr_call
 *
 * make a call, just like et did when he phoned home. well, not really, but
 * quite similar
 *
 * return in any case
 */

static void
scr_call (call *c)
{
	fnc_handler	f_h;

	/* nothing to execute today, baby
	 */
	if (c == NULL || c->name == NULL)
		return;

	f_h = fnc_find (gl_fnc, c->name);

	/* no function found or nop -> return
	 */
	if (f_h == NULL)
		return;

	(void) f_h (c->parameters);

	return;
}


static void
scr_branch (branch * br)
{
	if (cond_verify (br->cond) == 1) {
		scr_elem_exec (br->b_true);
	} else {
		scr_elem_exec (br->b_false);
	}

	return;
}


sym_elem **
scr_ns_add (sym_elem **ns, sym_elem *elem)
{
	return (sym_elem_add (ns, elem));
}


int
scr_exec (char *script)
{
	element **	scr;

	scr = cp_compile (script, strlen (script));
	if (scr == NULL)
		return (0);

	scr_elem_exec (scr);

	elem_list_free (scr);

	return (1);
}


element **
scr_compile (char *script)
{
	element **	scr = NULL;

	if (script != NULL)
		scr = cp_compile (script, strlen (script));

	return (scr);
}