Sat, 03 Dec 2022 12:27:00 +0100
improve acceptor error handling
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; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
171 | } else if(!cx_strcmp(token, cx_str("not"))) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
172 | return NSAPI_EXPRESSION_NOT; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
173 | } else if(!cx_strcmp(token, cx_str("and"))) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
174 | return NSAPI_EXPRESSION_AND; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
175 | } else if(!cx_strcmp(token, cx_str("or"))) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
176 | return NSAPI_EXPRESSION_OR; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
177 | } else if(!cx_strcmp(token, cx_str("xor"))) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
178 | return NSAPI_EXPRESSION_XOR; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
179 | } else if(!cx_strcmp(token, cx_str("=="))) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
180 | return NSAPI_EXPRESSION_EQ; |
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_NEQ; |
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_GT; |
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_LT; |
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_GE; |
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_LE; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
191 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
192 | return NSAPI_EXPRESSION_NOOP; |
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 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
195 | 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
|
196 | if(token.length == 0) { |
3df9258cd3cc
allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
423
diff
changeset
|
197 | return 0; |
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 | |
3df9258cd3cc
allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
423
diff
changeset
|
200 | size_t start = 0; |
3df9258cd3cc
allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
423
diff
changeset
|
201 | 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
|
202 | if(token.length < 2) { |
3df9258cd3cc
allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
423
diff
changeset
|
203 | return 0; |
3df9258cd3cc
allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
423
diff
changeset
|
204 | } |
3df9258cd3cc
allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
423
diff
changeset
|
205 | start++; |
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 | |
3df9258cd3cc
allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
423
diff
changeset
|
208 | 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
|
209 | if(!isdigit(token.ptr[i])) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
210 | return 0; |
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 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
213 | return 1; |
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 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
216 | 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
|
217 | 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
|
218 | 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
|
219 | expr->type = NSAPI_EXPRESSION_STRING; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
220 | } 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
|
221 | 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
|
222 | expr->type = NSAPI_EXPRESSION_VARIABLE; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
223 | } else if(token_is_int(token)) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
224 | if(!util_strtoint(token.ptr, &expr->value.i)) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
225 | return 1; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
226 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
227 | expr->type = NSAPI_EXPRESSION_INT; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
228 | } else if(!strcmp(token.ptr, "true")) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
229 | expr->type = NSAPI_EXPRESSION_BOOL; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
230 | expr->value.b = 1; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
231 | } else if(!strcmp(token.ptr, "false")) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
232 | expr->type = NSAPI_EXPRESSION_BOOL; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
233 | expr->value.b = 0; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
234 | } else { |
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 | // TODO: double |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
238 | return 0; |
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 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
241 | 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
|
242 | cxstring *token = cxListAt(tokens, *pos); |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
243 | if(token) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
244 | (*pos)++; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
245 | return *token; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
246 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
247 | return (cxstring){NULL, 0}; |
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 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
250 | NSAPIExpression* expr_parser_pop(ExprParser *parser) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
251 | CxList *stack = parser->ex_stack; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
252 | if(stack->size == 0) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
253 | return NULL; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
254 | } |
437
545010bc5e71
replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
435
diff
changeset
|
255 | 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
|
256 | cxListRemove(stack, stack->size-1); |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
257 | return ret; |
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 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
260 | // 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
|
261 | 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
|
262 | NSAPIExpression *exp = pool_malloc(parser->pool, sizeof(NSAPIExpression)); |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
263 | exp->operator = op->operator; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
264 | // 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
|
265 | // a value was expected. |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
266 | // For example: 1 + - 2 |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
267 | // 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
|
268 | // 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
|
269 | if(op->expect_value) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
270 | // 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
|
271 | |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
272 | 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
|
273 | // simplify unary arg |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
274 | pool_free(parser->pool, exp); |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
275 | parser->expect_value = TRUE; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
276 | parser->expect_arg = FALSE; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
277 | return 0; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
278 | } |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
279 | |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
280 | exp->type = NSAPI_EXPRESSION_UNARY; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
281 | exp->left = expr_parser_pop(parser); |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
282 | exp->right = NULL; |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
283 | } else { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
284 | // binary operator |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
285 | exp->type = NSAPI_EXPRESSION_BINARY; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
286 | exp->right = expr_parser_pop(parser); |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
287 | exp->left = expr_parser_pop(parser); |
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 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
290 | if(!exp->left && !exp->right) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
291 | return 1; // error |
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 | |
437
545010bc5e71
replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
435
diff
changeset
|
294 | cxListAdd(parser->ex_stack, &exp); |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
295 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
296 | parser->expect_value = TRUE; |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
297 | parser->expect_arg = FALSE; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
298 | return 0; |
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 | |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
301 | 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
|
302 | 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
|
303 | if(!exp) { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
304 | return 1; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
305 | } |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
306 | exp->type = NSAPI_EXPRESSION_IDENTIFIER; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
307 | exp->operator = NSAPI_EXPRESSION_CALL; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
308 | exp->left = NULL; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
309 | exp->right = NULL; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
310 | exp->value.str = func->identifier; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
311 | |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
312 | 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
|
313 | 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
|
314 | if(top && top->operator == NSAPI_EXPRESSION_ARG) { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
315 | exp->left = top; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
316 | 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
|
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 | |
437
545010bc5e71
replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
435
diff
changeset
|
320 | if(cxListAdd(parser->ex_stack, &exp)) { |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
321 | return 1; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
322 | } |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
323 | parser->expect_value = FALSE; |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
324 | return 0; |
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 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
327 | // 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
|
328 | // and adds it to parser->ex_stack |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
329 | // sets parser->expect_value to false |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
330 | 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
|
331 | NSAPIExpression *exp = pool_malloc(parser->pool, sizeof(NSAPIExpression)); |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
332 | ZERO(exp, sizeof(NSAPIExpression)); |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
333 | if(expr_set_value(parser->pool, exp, token)) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
334 | return 1; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
335 | } |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
336 | |
437
545010bc5e71
replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
435
diff
changeset
|
337 | cxListAdd(parser->ex_stack, &exp); |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
338 | if(parser->expect_arg) { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
339 | ExprOpStackItem argList; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
340 | argList.expect_value = TRUE; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
341 | argList.identifier = (cxstring){NULL, 0}; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
342 | argList.open_parenthesis = FALSE; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
343 | argList.operator = NSAPI_EXPRESSION_ARG; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
344 | if(expr_add_operator(parser, &argList)) { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
345 | return 1; |
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 | } |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
348 | |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
349 | parser->expect_value = FALSE; |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
350 | parser->expect_arg = FALSE; |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
351 | return 0; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
352 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
353 | |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
354 | static int token_is_identifier(cxstring token) { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
355 | if(token.length == 0) { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
356 | return 0; |
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 | |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
359 | if(!isalpha(token.ptr[0])) { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
360 | return 0; |
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 | |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
363 | 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
|
364 | if(!isalnum(token.ptr[i])) { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
365 | return 0; |
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 | |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
369 | 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
|
370 | return 0; |
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 | |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
373 | return 1; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
374 | } |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
375 | |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
376 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
377 | static NSAPIExpression* expr_parse_expr(ExprParser *parser) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
378 | CxList *op_stack = parser->op_stack; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
379 | CxList *ex_stack = parser->ex_stack; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
380 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
381 | // Shunting yard algorithm |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
382 | 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
|
383 | 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
|
384 | NSAPIExpressionOperator op = expr_operator(token); |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
385 | if(op != NSAPI_EXPRESSION_NOOP) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
386 | ExprOpStackItem new_op; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
387 | new_op.operator = op; |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
388 | new_op.identifier = (cxstring){NULL,0}; |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
389 | new_op.expect_value = parser->expect_value; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
390 | new_op.open_parenthesis = FALSE; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
391 | while(op_stack->size > 0) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
392 | 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
|
393 | if(stack_item->operator == NSAPI_EXPRESSION_NOOP) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
394 | break; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
395 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
396 | // check presedence |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
397 | if(op >= stack_item->operator) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
398 | break; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
399 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
400 | if(expr_add_operator(parser, stack_item)) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
401 | return NULL; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
402 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
403 | cxListRemove(op_stack, op_stack->size-1); |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
404 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
405 | cxListAdd(op_stack, &new_op); |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
406 | parser->expect_value = TRUE; |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
407 | parser->expect_arg = FALSE; |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
408 | } 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
|
409 | ExprOpStackItem *prev_op = NULL; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
410 | if(op_stack->size > 0) { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
411 | 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
|
412 | } |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
413 | |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
414 | 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
|
415 | // function call open parenthesis |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
416 | // 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
|
417 | // call as open parenthesis |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
418 | prev_op->open_parenthesis = TRUE; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
419 | parser->expect_arg = TRUE; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
420 | } else { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
421 | ExprOpStackItem new_op; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
422 | new_op.operator = NSAPI_EXPRESSION_NOOP; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
423 | new_op.identifier = (cxstring){NULL,0}; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
424 | new_op.expect_value = 0; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
425 | new_op.open_parenthesis = TRUE; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
426 | cxListAdd(op_stack, &new_op); |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
427 | } |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
428 | parser->expect_value = TRUE; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
429 | } else if(token.length == 1 && token.ptr[0] == ')') { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
430 | int found_open_bracket = FALSE; |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
431 | ExprOpStackItem stack_item; |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
432 | while(op_stack->size > 0) { |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
433 | 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
|
434 | stack_item = *stack_item_ptr; |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
435 | 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
|
436 | if(stack_item.open_parenthesis) { |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
437 | found_open_bracket = TRUE; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
438 | break; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
439 | } else { |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
440 | if(expr_add_operator(parser, &stack_item)) { |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
441 | return NULL; |
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 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
445 | if(!found_open_bracket) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
446 | return NULL; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
447 | } |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
448 | if(stack_item.operator == NSAPI_EXPRESSION_CALL) { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
449 | if(expr_add_func(parser, &stack_item)) { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
450 | return NULL; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
451 | } |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
452 | } |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
453 | parser->expect_value = FALSE; |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
454 | parser->expect_arg = FALSE; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
455 | } else if(token_is_identifier(token)) { |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
456 | ExprOpStackItem new_op; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
457 | new_op.operator = NSAPI_EXPRESSION_CALL; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
458 | new_op.identifier = token; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
459 | new_op.expect_value = 0; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
460 | new_op.open_parenthesis = FALSE; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
461 | cxListAdd(op_stack, &new_op); |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
462 | parser->expect_value = FALSE; |
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
463 | parser->expect_arg = FALSE; |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
464 | } else { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
465 | if(expr_add_value(parser, token)) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
466 | return NULL; |
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 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
471 | while(op_stack->size > 0) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
472 | 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
|
473 | if(stack_item->open_parenthesis) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
474 | return NULL; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
475 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
476 | if(expr_add_operator(parser, stack_item)) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
477 | return NULL; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
478 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
479 | cxListRemove(op_stack, op_stack->size-1); |
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 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
482 | if(ex_stack->size != 1) { |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
483 | return NULL; |
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 | |
437
545010bc5e71
replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
435
diff
changeset
|
486 | 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
|
487 | return *ret; |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
488 | } |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
489 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
490 | NSAPIExpression* expr_parse_logical_expr(pool_handle_t *pool, CxList *tokens, size_t *pos) { |
437
545010bc5e71
replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
435
diff
changeset
|
491 | CxList *op_stack = cxArrayListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(ExprOpStackItem), 32); |
545010bc5e71
replace linkedlist with arraylist in the expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
435
diff
changeset
|
492 | CxList *ex_stack = cxArrayListCreate(pool_allocator(pool), cx_cmp_ptr, sizeof(NSAPIExpression*), 32); |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
493 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
494 | ExprParser parser; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
495 | parser.pool = pool; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
496 | parser.op_stack = op_stack; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
497 | parser.ex_stack = ex_stack; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
498 | parser.tokens = tokens; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
499 | parser.pos = pos; |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
500 | parser.expect_value = TRUE; |
435
713ec3da79ec
expression parser: add support for functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
424
diff
changeset
|
501 | parser.expect_arg = FALSE; |
423
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
502 | |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
503 | NSAPIExpression *ret = expr_parse_expr(&parser); |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
504 | cxListDestroy(op_stack); |
bb7cff720dd0
add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
421
diff
changeset
|
505 | cxListDestroy(ex_stack); |
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 | return ret; |
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 | |
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 | |
83
28433f06d5ee
added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
61
diff
changeset
|
512 | 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
|
513 | return 1; |
83
28433f06d5ee
added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
61
diff
changeset
|
514 | } |