src/server/util/object.c

Sun, 20 Nov 2022 12:19:09 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 20 Nov 2022 12:19:09 +0100
changeset 436
1260fad21be7
parent 435
713ec3da79ec
child 437
545010bc5e71
permissions
-rw-r--r--

update ucx

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>
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
31 #include <cx/compare.h>
92
382bff43c6eb fixed some includes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
32
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
33 #include "../public/nsapi.h"
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "object.h"
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
36 #include "util.h"
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
37 #include "pool.h"
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 13
diff changeset
38 #include "../daemon/func.h"
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
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
61
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
42 httpd_object* object_new(pool_handle_t *pool, char *name) {
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
43 httpd_object *obj = pool_malloc(pool, sizeof(httpd_object));
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
44 obj->pool = pool;
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 obj->name = name;
5
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
46 obj->path = NULL;
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 // create directive table
61
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
49 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
50 obj->nd = NUM_NSAPI_TYPES - 1;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 return obj;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 }
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 void object_free(httpd_object *obj) {
61
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
56 //free(obj->name);
c858850f3d3a improved configuration reloading
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
57 //free(obj->dt);
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 }
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
421
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
61 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
62 dtable *l = object_get_dtable(obj, dt);
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 // 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
64 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
65 l->alloc += 8;
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
66 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
67 if(!drs) {
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
68 l->alloc -= 8;
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
69 return -1;
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
70 }
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
71 l->dirs = drs;
12
34aa8001ea53 Added directory index
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
72 }
421
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
73
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 // add directive
421
437562f5681d use realloc instead of malloc in object_add_directive
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 420
diff changeset
75 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
76 return 0;
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 }
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
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
80 /* objset functions */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
81 httpd_objset* objset_create(pool_handle_t *pool) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
82 httpd_objset *os = pool_malloc(pool, sizeof(httpd_objset));
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
83
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
84 os->obj = pool_calloc(pool, 2, sizeof(void*));
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
85 os->pos = 0;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
86
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
87 return os;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
88 }
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 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
91 if(os->pos != 0 && os->pos % 2 == 0) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
92 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
93 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
94 os->obj[os->pos] = obj;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
95 os->pos++;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
96 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
97
5
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
98 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
99 conf->nobj++;
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
100 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
101 conf->objects[conf->nobj - 1] = obj;
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
102 }
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
103
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 void nsapi_context_next_stage(NSAPIContext *context) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
106 context->dtable_index = 0;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
107 context->objset_index = -1;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
108 context->last_req_code = REQ_NOACTION;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
109 }
83
28433f06d5ee added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 61
diff changeset
110
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
111
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
112 /* ------------------------------ Expression ------------------------------ */
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
113
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
114 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
115 size_t pos = 0;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
116 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
117 if(!expression || pos != tokens->size) {
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
118 return NULL;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
119 }
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
120
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
121 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
122 }
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 typedef struct {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
126 pool_handle_t *pool;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
127 CxList *op_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
128 CxList *ex_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
129 CxList *tokens;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
130 size_t *pos;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
131 WSBool expect_value;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
132 WSBool expect_arg;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
133 } ExprParser;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
134
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
135 typedef struct {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
136 NSAPIExpressionOperator operator;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
137 cxstring identifier;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
138 int expect_value;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
139 int open_parenthesis;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
140 } ExprOpStackItem;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
141
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
142 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
143 if(expr->left) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
144 expr_free(pool, expr->left);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
145 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
146 if(expr->right) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
147 expr_free(pool, expr->right);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
148 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
149 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
150 pool_free(pool, (char*)expr->value.str.ptr);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
151 } 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
152 pool_free(pool, (char*)expr->value.var.ptr);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
153 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
154 pool_free(pool, expr);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
155 }
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 static NSAPIExpressionOperator expr_operator(cxstring token) {
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
158 if(!cx_strcmp(token, cx_str(","))) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
159 return NSAPI_EXPRESSION_ARG;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
160 } else if(!cx_strcmp(token, cx_str("+"))) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
161 return NSAPI_EXPRESSION_ADD;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
162 } else if(!cx_strcmp(token, cx_str("-"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
163 return NSAPI_EXPRESSION_SUB;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
164 } else if(!cx_strcmp(token, cx_str("*"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
165 return NSAPI_EXPRESSION_MUL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
166 } else if(!cx_strcmp(token, cx_str("/"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
167 return NSAPI_EXPRESSION_DIV;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
168 } else if(!cx_strcmp(token, cx_str("%"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
169 return NSAPI_EXPRESSION_MOD;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
170 } else if(!cx_strcmp(token, cx_str("not"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
171 return NSAPI_EXPRESSION_NOT;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
172 } else if(!cx_strcmp(token, cx_str("and"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
173 return NSAPI_EXPRESSION_AND;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
174 } else if(!cx_strcmp(token, cx_str("or"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
175 return NSAPI_EXPRESSION_OR;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
176 } else if(!cx_strcmp(token, cx_str("xor"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
177 return NSAPI_EXPRESSION_XOR;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
178 } else if(!cx_strcmp(token, cx_str("=="))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
179 return NSAPI_EXPRESSION_EQ;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
180 } else if(!cx_strcmp(token, cx_str("!="))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
181 return NSAPI_EXPRESSION_NEQ;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
182 } else if(!cx_strcmp(token, cx_str(">"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
183 return NSAPI_EXPRESSION_GT;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
184 } else if(!cx_strcmp(token, cx_str("<"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
185 return NSAPI_EXPRESSION_LT;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
186 } else if(!cx_strcmp(token, cx_str(">="))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
187 return NSAPI_EXPRESSION_GE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
188 } else if(!cx_strcmp(token, cx_str("<="))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
189 return NSAPI_EXPRESSION_LE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
190 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
191 return NSAPI_EXPRESSION_NOOP;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
192 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
193
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
194 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
195 if(token.length == 0) {
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
196 return 0;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
197 }
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
198
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
199 size_t start = 0;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
200 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
201 if(token.length < 2) {
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
202 return 0;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
203 }
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
204 start++;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
205 }
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
206
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
207 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
208 if(!isdigit(token.ptr[i])) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
209 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
210 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
211 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
212 return 1;
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
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
215 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
216 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
217 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
218 expr->type = NSAPI_EXPRESSION_STRING;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
219 } 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
220 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
221 expr->type = NSAPI_EXPRESSION_VARIABLE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
222 } else if(token_is_int(token)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
223 if(!util_strtoint(token.ptr, &expr->value.i)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
224 return 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
225 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
226 expr->type = NSAPI_EXPRESSION_INT;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
227 } else if(!strcmp(token.ptr, "true")) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
228 expr->type = NSAPI_EXPRESSION_BOOL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
229 expr->value.b = 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
230 } else if(!strcmp(token.ptr, "false")) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
231 expr->type = NSAPI_EXPRESSION_BOOL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
232 expr->value.b = 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
233 } else {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
234 return 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
235 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
236 // TODO: double
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
237 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
238 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
239
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
240 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
241 cxstring *token = cxListAt(tokens, *pos);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
242 if(token) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
243 (*pos)++;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
244 return *token;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
245 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
246 return (cxstring){NULL, 0};
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
247 }
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 NSAPIExpression* expr_parser_pop(ExprParser *parser) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
250 CxList *stack = parser->ex_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
251 if(stack->size == 0) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
252 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
253 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
254 NSAPIExpression *ret = cxListAt(stack, stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
255 cxListRemove(stack, stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
256 return ret;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
257 }
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 // 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
260 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
261 NSAPIExpression *exp = pool_malloc(parser->pool, sizeof(NSAPIExpression));
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
262 exp->operator = op->operator;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
263 // 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
264 // a value was expected.
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
265 // For example: 1 + - 2
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
266 // 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
267 // 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
268 if(op->expect_value) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
269 // 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
270
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
271 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
272 // simplify unary arg
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
273 pool_free(parser->pool, exp);
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
274 parser->expect_value = TRUE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
275 parser->expect_arg = FALSE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
276 return 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
277 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
278
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
279 exp->type = NSAPI_EXPRESSION_UNARY;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
280 exp->left = expr_parser_pop(parser);
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
281 exp->right = NULL;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
282 } else {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
283 // binary operator
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
284 exp->type = NSAPI_EXPRESSION_BINARY;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
285 exp->right = expr_parser_pop(parser);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
286 exp->left = expr_parser_pop(parser);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
287 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
288
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
289 if(!exp->left && !exp->right) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
290 return 1; // error
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
291 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
292
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
293 cxListAdd(parser->ex_stack, exp);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
294
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
295 parser->expect_value = TRUE;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
296 parser->expect_arg = FALSE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
297 return 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
298 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
299
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
300 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
301 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
302 if(!exp) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
303 return 1;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
304 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
305 exp->type = NSAPI_EXPRESSION_IDENTIFIER;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
306 exp->operator = NSAPI_EXPRESSION_CALL;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
307 exp->left = NULL;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
308 exp->right = NULL;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
309 exp->value.str = func->identifier;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
310
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
311 if(parser->ex_stack->size > 0) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
312 NSAPIExpression *top = cxListAt(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
313 if(top && top->operator == NSAPI_EXPRESSION_ARG) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
314 exp->left = top;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
315 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
316 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
317 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
318
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
319 if(cxListAdd(parser->ex_stack, exp)) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
320 return 1;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
321 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
322 parser->expect_value = FALSE;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
323 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
324 }
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 // 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
327 // and adds it to parser->ex_stack
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
328 // sets parser->expect_value to false
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
329 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
330 NSAPIExpression *exp = pool_malloc(parser->pool, sizeof(NSAPIExpression));
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
331 ZERO(exp, sizeof(NSAPIExpression));
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
332 if(expr_set_value(parser->pool, exp, token)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
333 return 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
334 }
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
335
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
336 cxListAdd(parser->ex_stack, exp);
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
337 if(parser->expect_arg) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
338 ExprOpStackItem argList;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
339 argList.expect_value = TRUE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
340 argList.identifier = (cxstring){NULL, 0};
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
341 argList.open_parenthesis = FALSE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
342 argList.operator = NSAPI_EXPRESSION_ARG;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
343 if(expr_add_operator(parser, &argList)) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
344 return 1;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
345 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
346 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
347
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
348 parser->expect_value = FALSE;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
349 parser->expect_arg = FALSE;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
350 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
351 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
352
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
353 static int token_is_identifier(cxstring token) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
354 if(token.length == 0) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
355 return 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
356 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
357
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
358 if(!isalpha(token.ptr[0])) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
359 return 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
360 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
361
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
362 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
363 if(!isalnum(token.ptr[i])) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
364 return 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
365 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
366 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
367
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
368 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
369 return 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
370 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
371
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
372 return 1;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
373 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
374
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
375
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
376 static NSAPIExpression* expr_parse_expr(ExprParser *parser) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
377 CxList *op_stack = parser->op_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
378 CxList *ex_stack = parser->ex_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
379
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
380 // Shunting yard algorithm
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
381 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
382 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
383 NSAPIExpressionOperator op = expr_operator(token);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
384 if(op != NSAPI_EXPRESSION_NOOP) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
385 ExprOpStackItem new_op;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
386 new_op.operator = op;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
387 new_op.identifier = (cxstring){NULL,0};
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
388 new_op.expect_value = parser->expect_value;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
389 new_op.open_parenthesis = FALSE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
390 while(op_stack->size > 0) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
391 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
392 if(stack_item->operator == NSAPI_EXPRESSION_NOOP) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
393 break;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
394 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
395 // check presedence
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
396 if(op >= stack_item->operator) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
397 break;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
398 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
399 if(expr_add_operator(parser, stack_item)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
400 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
401 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
402 cxListRemove(op_stack, op_stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
403 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
404 cxListAdd(op_stack, &new_op);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
405 parser->expect_value = TRUE;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
406 parser->expect_arg = FALSE;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
407 } 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
408 ExprOpStackItem *prev_op = NULL;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
409 if(op_stack->size > 0) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
410 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
411 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
412
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
413 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
414 // function call open parenthesis
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
415 // 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
416 // call as open parenthesis
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
417 prev_op->open_parenthesis = TRUE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
418 parser->expect_arg = TRUE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
419 } else {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
420 ExprOpStackItem new_op;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
421 new_op.operator = NSAPI_EXPRESSION_NOOP;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
422 new_op.identifier = (cxstring){NULL,0};
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
423 new_op.expect_value = 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
424 new_op.open_parenthesis = TRUE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
425 cxListAdd(op_stack, &new_op);
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
426 }
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
427 parser->expect_value = TRUE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
428 } else if(token.length == 1 && token.ptr[0] == ')') {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
429 int found_open_bracket = FALSE;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
430 ExprOpStackItem stack_item;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
431 while(op_stack->size > 0) {
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
432 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
433 stack_item = *stack_item_ptr;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
434 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
435 if(stack_item.open_parenthesis) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
436 found_open_bracket = TRUE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
437 break;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
438 } else {
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
439 if(expr_add_operator(parser, &stack_item)) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
440 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
441 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
442 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
443 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
444 if(!found_open_bracket) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
445 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
446 }
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
447 if(stack_item.operator == NSAPI_EXPRESSION_CALL) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
448 if(expr_add_func(parser, &stack_item)) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
449 return NULL;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
450 }
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
451 }
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
452 parser->expect_value = FALSE;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
453 parser->expect_arg = FALSE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
454 } else if(token_is_identifier(token)) {
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
455 ExprOpStackItem new_op;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
456 new_op.operator = NSAPI_EXPRESSION_CALL;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
457 new_op.identifier = token;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
458 new_op.expect_value = 0;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
459 new_op.open_parenthesis = FALSE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
460 cxListAdd(op_stack, &new_op);
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
461 parser->expect_value = FALSE;
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
462 parser->expect_arg = FALSE;
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
463 } else {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
464 if(expr_add_value(parser, token)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
465 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
466 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
467 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
468 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
469
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
470 while(op_stack->size > 0) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
471 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
472 if(stack_item->open_parenthesis) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
473 return NULL;
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(expr_add_operator(parser, stack_item)) {
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 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
478 cxListRemove(op_stack, op_stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
479 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
480
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
481 if(ex_stack->size != 1) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
482 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
483 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
484
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
485 return cxListAt(ex_stack, 0);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
486 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
487
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
488 NSAPIExpression* expr_parse_logical_expr(pool_handle_t *pool, CxList *tokens, size_t *pos) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
489 CxList *op_stack = cxLinkedListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(ExprOpStackItem));
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
490 CxList *ex_stack = cxPointerLinkedListCreate(pool_allocator(pool), cx_cmp_ptr);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
491
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
492 ExprParser parser;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
493 parser.pool = pool;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
494 parser.op_stack = op_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
495 parser.ex_stack = ex_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
496 parser.tokens = tokens;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
497 parser.pos = pos;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
498 parser.expect_value = TRUE;
435
713ec3da79ec expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
499 parser.expect_arg = FALSE;
423
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 NSAPIExpression *ret = expr_parse_expr(&parser);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
502 cxListDestroy(op_stack);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
503 cxListDestroy(ex_stack);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
504
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
505 return ret;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
506 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
507
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
83
28433f06d5ee added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 61
diff changeset
510 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
511 return 1;
83
28433f06d5ee added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 61
diff changeset
512 }

mercurial