src/server/util/object.c

Tue, 12 Sep 2023 18:08:11 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 12 Sep 2023 18:08:11 +0200
changeset 511
a5a142fea2ae
parent 490
d218607f5a7e
permissions
-rw-r--r--

update uwproj

3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 261
diff changeset
29 #include <cx/string.h>
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
30 #include <cx/linked_list.h>
437
545010bc5e71 replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 435
diff changeset
31 #include <cx/array_list.h>
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
32 #include <cx/compare.h>
92
382bff43c6eb fixed some includes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
33
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
34 #include "../public/nsapi.h"
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "object.h"
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
37 #include "util.h"
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
38 #include "pool.h"
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
39 #include "../daemon/func.h"
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
61
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
43 httpd_object* object_new(pool_handle_t *pool, char *name) {
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
44 httpd_object *obj = pool_malloc(pool, sizeof(httpd_object));
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
45 obj->pool = pool;
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 obj->name = name;
5
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
47 obj->path = NULL;
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 // create directive table
61
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
50 obj->dt = pool_calloc(pool, NUM_NSAPI_TYPES - 1, sizeof(struct dtable));
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 obj->nd = NUM_NSAPI_TYPES - 1;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 return obj;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 }
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 void object_free(httpd_object *obj) {
61
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
57 //free(obj->name);
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
58 //free(obj->dt);
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 }
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
421
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
62 int object_add_directive(httpd_object *obj, directive *dir, int dt) {
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 dtable *l = object_get_dtable(obj, dt);
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 // allocate space for the new directive
421
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
65 if(l->ndir >= l->alloc) {
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
66 l->alloc += 8;
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
67 directive **drs = pool_realloc(obj->pool, l->dirs, (l->alloc)*sizeof(void*));
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
68 if(!drs) {
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
69 l->alloc -= 8;
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
70 return -1;
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
71 }
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
72 l->dirs = drs;
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
73 }
421
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
74
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 // add directive
421
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
76 l->dirs[l->ndir++] = dir;
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
77 return 0;
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 }
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
81 /* objset functions */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
82 httpd_objset* objset_create(pool_handle_t *pool) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
83 httpd_objset *os = pool_malloc(pool, sizeof(httpd_objset));
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
84
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
85 os->obj = pool_calloc(pool, 2, sizeof(void*));
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
86 os->pos = 0;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
87
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
88 return os;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
89 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
90
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
91 void objset_add_object(pool_handle_t *p, httpd_objset *os, httpd_object *obj) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
92 if(os->pos != 0 && os->pos % 2 == 0) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
93 os->obj = pool_realloc(p, os->obj, (os->pos + 2) * sizeof(void*));
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
94 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
95 os->obj[os->pos] = obj;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
96 os->pos++;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
97 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
98
5
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
99 void httpobjconf_add_object(HTTPObjectConfig *conf, httpd_object *obj) {
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
100 conf->nobj++;
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
101 conf->objects = realloc(conf->objects, conf->nobj * sizeof(void*));
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
102 conf->objects[conf->nobj - 1] = obj;
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
103 }
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
104
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
105
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
106 void nsapi_context_next_stage(NSAPIContext *context) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
107 context->dtable_index = 0;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
108 context->objset_index = -1;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
109 context->last_req_code = REQ_NOACTION;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
110 }
83
28433f06d5ee added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 61
diff changeset
111
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
112
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
113 /* ------------------------------ Expression ------------------------------ */
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
114
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
115 Expression* condition_create(pool_handle_t *pool, CxList *tokens) {
424
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
116 size_t pos = 0;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
117 NSAPIExpression *expression = expr_parse_logical_expr(pool, tokens, &pos);
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
118 if(!expression || pos != tokens->size) {
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
119 return NULL;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
120 }
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
121
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
122 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
123 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
124
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
125
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
126 typedef struct {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
127 pool_handle_t *pool;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
128 CxList *op_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
129 CxList *ex_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
130 CxList *tokens;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
131 size_t *pos;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
132 WSBool expect_value;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
133 WSBool expect_arg;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
134 } ExprParser;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
135
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
136 typedef struct {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
137 NSAPIExpressionOperator operator;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
138 cxstring identifier;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
139 int expect_value;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
140 int open_parenthesis;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
141 } ExprOpStackItem;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
142
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
143 static void expr_free(pool_handle_t *pool, NSAPIExpression *expr) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
144 if(expr->left) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
145 expr_free(pool, expr->left);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
146 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
147 if(expr->right) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
148 expr_free(pool, expr->right);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
149 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
150 if(expr->type == NSAPI_EXPRESSION_STRING && expr->value.str.ptr) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
151 pool_free(pool, (char*)expr->value.str.ptr);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
152 } else if(expr->type == NSAPI_EXPRESSION_VARIABLE && expr->value.var.ptr) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
153 pool_free(pool, (char*)expr->value.var.ptr);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
154 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
155 pool_free(pool, expr);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
156 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
157
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
158 static NSAPIExpressionOperator expr_operator(cxstring token) {
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
159 if(!cx_strcmp(token, cx_str(","))) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
160 return NSAPI_EXPRESSION_ARG;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
161 } else if(!cx_strcmp(token, cx_str("+"))) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
162 return NSAPI_EXPRESSION_ADD;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
163 } else if(!cx_strcmp(token, cx_str("-"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
164 return NSAPI_EXPRESSION_SUB;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
165 } else if(!cx_strcmp(token, cx_str("*"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
166 return NSAPI_EXPRESSION_MUL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
167 } else if(!cx_strcmp(token, cx_str("/"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
168 return NSAPI_EXPRESSION_DIV;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
169 } else if(!cx_strcmp(token, cx_str("%"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
170 return NSAPI_EXPRESSION_MOD;
452
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
171 } else if(!cx_strcmp(token, cx_str("."))) {
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
172 return NSAPI_EXPRESSION_STRCAT;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
173 } else if(!cx_strcmp(token, cx_str("not")) || !cx_strcmp(token, cx_str("!"))) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
174 return NSAPI_EXPRESSION_NOT;
452
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
175 } else if(!cx_strcmp(token, cx_str("and")) || !cx_strcmp(token, cx_str("&&"))) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
176 return NSAPI_EXPRESSION_AND;
452
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
177 } else if(!cx_strcmp(token, cx_str("or")) || !cx_strcmp(token, cx_str("||"))) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
178 return NSAPI_EXPRESSION_OR;
452
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
179 } else if(!cx_strcmp(token, cx_str("xor")) || !cx_strcmp(token, cx_str("^"))) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
180 return NSAPI_EXPRESSION_XOR;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
181 } else if(!cx_strcmp(token, cx_str("=="))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
182 return NSAPI_EXPRESSION_EQ;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
183 } else if(!cx_strcmp(token, cx_str("!="))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
184 return NSAPI_EXPRESSION_NEQ;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
185 } else if(!cx_strcmp(token, cx_str(">"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
186 return NSAPI_EXPRESSION_GT;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
187 } else if(!cx_strcmp(token, cx_str("<"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
188 return NSAPI_EXPRESSION_LT;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
189 } else if(!cx_strcmp(token, cx_str(">="))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
190 return NSAPI_EXPRESSION_GE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
191 } else if(!cx_strcmp(token, cx_str("<="))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
192 return NSAPI_EXPRESSION_LE;
452
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
193 } else if(!cx_strcmp(token, cx_str("="))) {
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
194 return NSAPI_EXPRESSION_WILDCARD_MATCH;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
195 } else if(!cx_strcmp(token, cx_str("=~"))) {
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
196 return NSAPI_EXPRESSION_REGEX_MATCH;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
197 } else if(!cx_strcmp(token, cx_str("!~"))) {
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
198 return NSAPI_EXPRESSION_REGEX_MISMATCH;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
199 } else if(!cx_strcmp(token, cx_str("defined"))) {
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
200 return NSAPI_EXPRESSION_VALUE_DEFINED;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
201 } else if(!cx_strcmp(token, cx_str("-d"))) {
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
202 return NSAPI_EXPRESSION_DIR_EXISTS;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
203 } else if(!cx_strcmp(token, cx_str("-e"))) {
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
204 return NSAPI_EXPRESSION_FILE_DIR_EXISTS;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
205 } else if(!cx_strcmp(token, cx_str("-f"))) {
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
206 return NSAPI_EXPRESSION_FILE_EXISTS;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
207 } else if(!cx_strcmp(token, cx_str("-l"))) {
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
208 return NSAPI_EXPRESSION_SYMLINK_EXISTS;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
209 } else if(!cx_strcmp(token, cx_str("-r"))) {
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
210 return NSAPI_EXPRESSION_FILE_READABLE;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
211 } else if(!cx_strcmp(token, cx_str("-s"))) {
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
212 return NSAPI_EXPRESSION_FILE_SIZE;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
213 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
214 return NSAPI_EXPRESSION_NOOP;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
215 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
216
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
217 static int token_is_int(cxstring token) {
424
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
218 if(token.length == 0) {
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
219 return 0;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
220 }
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
221
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
222 size_t start = 0;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
223 if(token.ptr[0] == '-' || token.ptr[0] == '+') {
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
224 if(token.length < 2) {
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
225 return 0;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
226 }
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
227 start++;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
228 }
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
229
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
230 for(size_t i=start;i<token.length;i++) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
231 if(!isdigit(token.ptr[i])) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
232 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
233 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
234 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
235 return 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
236 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
237
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
238 static int expr_set_value(pool_handle_t *pool, NSAPIExpression *expr, cxstring token) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
239 if(token.length >= 2 && token.ptr[0] == '\"' && token.ptr[token.length-1] == '\"') {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
240 expr->value.str = cx_strcast(cx_strdup_a(pool_allocator(pool), cx_strsubsl(token, 1, token.length-2)));
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
241 expr->type = NSAPI_EXPRESSION_STRING;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
242 } else if(token.length >= 2 && token.ptr[0] == '$' && isalpha(token.ptr[1])) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
243 expr->value.var = cx_strcast(cx_strdup_a(pool_allocator(pool), cx_strsubs(token, 1)));
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
244 expr->type = NSAPI_EXPRESSION_VARIABLE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
245 } else if(token_is_int(token)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
246 if(!util_strtoint(token.ptr, &expr->value.i)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
247 return 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
248 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
249 expr->type = NSAPI_EXPRESSION_INT;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
250 } else if(!strcmp(token.ptr, "true")) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
251 expr->type = NSAPI_EXPRESSION_BOOL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
252 expr->value.b = 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
253 } else if(!strcmp(token.ptr, "false")) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
254 expr->type = NSAPI_EXPRESSION_BOOL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
255 expr->value.b = 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
256 } else {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
257 return 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
258 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
259 // TODO: double
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
260 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
261 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
262
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
263 cxstring expr_next_token(CxList *tokens, size_t *pos) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
264 cxstring *token = cxListAt(tokens, *pos);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
265 if(token) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
266 (*pos)++;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
267 return *token;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
268 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
269 return (cxstring){NULL, 0};
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
270 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
271
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
272 NSAPIExpression* expr_parser_pop(ExprParser *parser) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
273 CxList *stack = parser->ex_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
274 if(stack->size == 0) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
275 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
276 }
437
545010bc5e71 replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 435
diff changeset
277 NSAPIExpression *ret = *((NSAPIExpression**)cxListAt(stack, stack->size-1));
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
278 cxListRemove(stack, stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
279 return ret;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
280 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
281
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
282 // takes items from ex_stack and adds a new operator expression to ex_stack
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
283 static int expr_add_operator(ExprParser *parser, ExprOpStackItem *op) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
284 NSAPIExpression *exp = pool_malloc(parser->pool, sizeof(NSAPIExpression));
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
285 exp->operator = op->operator;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
286 // op->expect_value == TRUE means, that the operator was found in a case
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
287 // a value was expected.
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
288 // For example: 1 + - 2
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
289 // After + a value is expected, but there is the - operator
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
290 // in that case, the - operator is a unary expression
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
291 if(op->expect_value) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
292 // We expected a value but got an operator? This must be an unary operator
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
293
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
294 if(op->operator == NSAPI_EXPRESSION_ARG && !parser->expect_arg) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
295 // simplify unary arg
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
296 pool_free(parser->pool, exp);
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
297 parser->expect_value = TRUE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
298 parser->expect_arg = FALSE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
299 return 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
300 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
301
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
302 exp->type = NSAPI_EXPRESSION_UNARY;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
303 exp->left = expr_parser_pop(parser);
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
304 exp->right = NULL;
452
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
305 } else if(exp->operator == NSAPI_EXPRESSION_CALL) {
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
306 // identifiers are added as call, but if they land here, it is
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
307 // actually just an identifier
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
308 exp->operator = NSAPI_EXPRESSION_NOOP;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
309 exp->left = NULL;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
310 exp->right = NULL;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
311 exp->type = NSAPI_EXPRESSION_IDENTIFIER;
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
312 exp->value.identifier = op->identifier;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
313 } else {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
314 // binary operator
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
315 exp->type = NSAPI_EXPRESSION_BINARY;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
316 exp->right = expr_parser_pop(parser);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
317 exp->left = expr_parser_pop(parser);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
318 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
319
452
ce359a2b51fe implement most nsapi expression operators
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 437
diff changeset
320 if(!exp->left && !exp->right && exp->operator != NSAPI_EXPRESSION_NOOP) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
321 return 1; // error
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
322 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
323
437
545010bc5e71 replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 435
diff changeset
324 cxListAdd(parser->ex_stack, &exp);
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
325
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
326 parser->expect_value = TRUE;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
327 parser->expect_arg = FALSE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
328 return 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
329 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
330
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
331 static int expr_add_func(ExprParser *parser, ExprOpStackItem *func) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
332 NSAPIExpression *exp = pool_malloc(parser->pool, sizeof(NSAPIExpression));
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
333 if(!exp) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
334 return 1;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
335 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
336 exp->type = NSAPI_EXPRESSION_IDENTIFIER;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
337 exp->operator = NSAPI_EXPRESSION_CALL;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
338 exp->left = NULL;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
339 exp->right = NULL;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
340 exp->value.str = func->identifier;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
341
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
342 if(parser->ex_stack->size > 0) {
437
545010bc5e71 replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 435
diff changeset
343 NSAPIExpression *top = *((NSAPIExpression**)cxListAt(parser->ex_stack, parser->ex_stack->size - 1));
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
344 if(top && top->operator == NSAPI_EXPRESSION_ARG) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
345 exp->left = top;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
346 cxListRemove(parser->ex_stack, parser->ex_stack->size - 1);
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
347 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
348 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
349
437
545010bc5e71 replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 435
diff changeset
350 if(cxListAdd(parser->ex_stack, &exp)) {
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
351 return 1;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
352 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
353 parser->expect_value = FALSE;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
354 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
355 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
356
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
357 // converts the token to a value expression (int, bool, str, ...)
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
358 // and adds it to parser->ex_stack
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
359 // sets parser->expect_value to false
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
360 static int expr_add_value(ExprParser *parser, cxstring token) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
361 NSAPIExpression *exp = pool_malloc(parser->pool, sizeof(NSAPIExpression));
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
362 ZERO(exp, sizeof(NSAPIExpression));
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
363 if(expr_set_value(parser->pool, exp, token)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
364 return 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
365 }
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
366
437
545010bc5e71 replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 435
diff changeset
367 cxListAdd(parser->ex_stack, &exp);
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
368 if(parser->expect_arg) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
369 ExprOpStackItem argList;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
370 argList.expect_value = TRUE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
371 argList.identifier = (cxstring){NULL, 0};
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
372 argList.open_parenthesis = FALSE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
373 argList.operator = NSAPI_EXPRESSION_ARG;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
374 if(expr_add_operator(parser, &argList)) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
375 return 1;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
376 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
377 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
378
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
379 parser->expect_value = FALSE;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
380 parser->expect_arg = FALSE;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
381 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
382 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
383
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
384 static int token_is_identifier(cxstring token) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
385 if(token.length == 0) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
386 return 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
387 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
388
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
389 if(!isalpha(token.ptr[0])) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
390 return 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
391 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
392
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
393 for(int i=1;i<token.length;i++) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
394 if(!isalnum(token.ptr[i])) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
395 return 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
396 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
397 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
398
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
399 if(!cx_strcmp(token, cx_str("true")) || !cx_strcmp(token, cx_str("false"))) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
400 return 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
401 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
402
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
403 return 1;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
404 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
405
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
406
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
407 static NSAPIExpression* expr_parse_expr(ExprParser *parser) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
408 CxList *op_stack = parser->op_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
409 CxList *ex_stack = parser->ex_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
410
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
411 // Shunting yard algorithm
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
412 cxstring token = expr_next_token(parser->tokens, parser->pos);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
413 for(;token.ptr;token=expr_next_token(parser->tokens, parser->pos)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
414 NSAPIExpressionOperator op = expr_operator(token);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
415 if(op != NSAPI_EXPRESSION_NOOP) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
416 ExprOpStackItem new_op;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
417 new_op.operator = op;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
418 new_op.identifier = (cxstring){NULL,0};
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
419 new_op.expect_value = parser->expect_value;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
420 new_op.open_parenthesis = FALSE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
421 while(op_stack->size > 0) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
422 ExprOpStackItem *stack_item = cxListAt(op_stack, op_stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
423 if(stack_item->operator == NSAPI_EXPRESSION_NOOP) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
424 break;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
425 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
426 // check presedence
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
427 if(op >= stack_item->operator) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
428 break;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
429 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
430 if(expr_add_operator(parser, stack_item)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
431 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
432 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
433 cxListRemove(op_stack, op_stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
434 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
435 cxListAdd(op_stack, &new_op);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
436 parser->expect_value = TRUE;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
437 parser->expect_arg = FALSE;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
438 } else if(token.length == 1 && token.ptr[0] == '(') {
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
439 ExprOpStackItem *prev_op = NULL;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
440 if(op_stack->size > 0) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
441 prev_op = cxListAt(op_stack, op_stack->size - 1);
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
442 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
443
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
444 if(prev_op && prev_op->operator == NSAPI_EXPRESSION_CALL) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
445 // function call open parenthesis
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
446 // we don't need to add a new op stack item, just mark function
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
447 // call as open parenthesis
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
448 prev_op->open_parenthesis = TRUE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
449 parser->expect_arg = TRUE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
450 } else {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
451 ExprOpStackItem new_op;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
452 new_op.operator = NSAPI_EXPRESSION_NOOP;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
453 new_op.identifier = (cxstring){NULL,0};
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
454 new_op.expect_value = 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
455 new_op.open_parenthesis = TRUE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
456 cxListAdd(op_stack, &new_op);
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
457 }
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
458 parser->expect_value = TRUE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
459 } else if(token.length == 1 && token.ptr[0] == ')') {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
460 int found_open_bracket = FALSE;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
461 ExprOpStackItem stack_item;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
462 while(op_stack->size > 0) {
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
463 ExprOpStackItem *stack_item_ptr = cxListAt(op_stack, op_stack->size-1);
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
464 stack_item = *stack_item_ptr;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
465 cxListRemove(op_stack, op_stack->size-1);
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
466 if(stack_item.open_parenthesis) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
467 found_open_bracket = TRUE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
468 break;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
469 } else {
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
470 if(expr_add_operator(parser, &stack_item)) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
471 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
472 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
473 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
474 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
475 if(!found_open_bracket) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
476 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
477 }
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
478 if(stack_item.operator == NSAPI_EXPRESSION_CALL) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
479 if(expr_add_func(parser, &stack_item)) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
480 return NULL;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
481 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
482 }
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
483 parser->expect_value = FALSE;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
484 parser->expect_arg = FALSE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
485 } else if(token_is_identifier(token)) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
486 ExprOpStackItem new_op;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
487 new_op.operator = NSAPI_EXPRESSION_CALL;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
488 new_op.identifier = token;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
489 new_op.expect_value = 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
490 new_op.open_parenthesis = FALSE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
491 cxListAdd(op_stack, &new_op);
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
492 parser->expect_value = FALSE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
493 parser->expect_arg = FALSE;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
494 } else {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
495 if(expr_add_value(parser, token)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
496 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
497 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
498 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
499 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
500
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
501 while(op_stack->size > 0) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
502 ExprOpStackItem *stack_item = cxListAt(op_stack, op_stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
503 if(stack_item->open_parenthesis) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
504 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
505 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
506 if(expr_add_operator(parser, stack_item)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
507 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
508 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
509 cxListRemove(op_stack, op_stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
510 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
511
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
512 if(ex_stack->size != 1) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
513 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
514 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
515
437
545010bc5e71 replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 435
diff changeset
516 NSAPIExpression **ret = cxListAt(ex_stack, 0);
545010bc5e71 replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 435
diff changeset
517 return *ret;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
518 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
519
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
520 NSAPIExpression* expr_parse_logical_expr(pool_handle_t *pool, CxList *tokens, size_t *pos) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 452
diff changeset
521 CxList *op_stack = cxArrayListCreate(pool_allocator(pool), NULL, sizeof(ExprOpStackItem), 32);
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 452
diff changeset
522 CxList *ex_stack = cxArrayListCreate(pool_allocator(pool), NULL, sizeof(NSAPIExpression*), 32);
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
523
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
524 ExprParser parser;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
525 parser.pool = pool;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
526 parser.op_stack = op_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
527 parser.ex_stack = ex_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
528 parser.tokens = tokens;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
529 parser.pos = pos;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
530 parser.expect_value = TRUE;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
531 parser.expect_arg = FALSE;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
532
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
533 NSAPIExpression *ret = expr_parse_expr(&parser);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
534 cxListDestroy(op_stack);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
535 cxListDestroy(ex_stack);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
536
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
537 return ret;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
538 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
539
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
540
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
541
83
28433f06d5ee added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 61
diff changeset
542 int condition_evaluate(Condition *condition, Session *sn, Request *rq) {
107
7e81699d1f77 removed old webdav and expression code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 92
diff changeset
543 return 1;
83
28433f06d5ee added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 61
diff changeset
544 }

mercurial