src/server/util/object.c

Sun, 13 Nov 2022 10:57:38 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 13 Nov 2022 10:57:38 +0100
changeset 432
7c9137f9e7f9
parent 424
3df9258cd3cc
child 435
713ec3da79ec
permissions
-rw-r--r--

add http_send_response function that is usable for non-blocking IO

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;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
131 int expect_value;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
132 } ExprParser;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
133
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
134 typedef struct {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
135 NSAPIExpressionOperator operator;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
136 int expect_value;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
137 int open_parenthesis;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
138 } ExprOpStackItem;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
139
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
140 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
141 if(expr->left) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
142 expr_free(pool, expr->left);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
143 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
144 if(expr->right) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
145 expr_free(pool, expr->right);
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->type == NSAPI_EXPRESSION_STRING && expr->value.str.ptr) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
148 pool_free(pool, (char*)expr->value.str.ptr);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
149 } 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
150 pool_free(pool, (char*)expr->value.var.ptr);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
151 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
152 pool_free(pool, expr);
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
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
155 static NSAPIExpressionOperator expr_operator(cxstring token) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
156 if(!cx_strcmp(token, cx_str("+"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
157 return NSAPI_EXPRESSION_ADD;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
158 } else if(!cx_strcmp(token, cx_str("-"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
159 return NSAPI_EXPRESSION_SUB;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
160 } else if(!cx_strcmp(token, cx_str("*"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
161 return NSAPI_EXPRESSION_MUL;
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_DIV;
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_MOD;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
166 } else if(!cx_strcmp(token, cx_str("not"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
167 return NSAPI_EXPRESSION_NOT;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
168 } else if(!cx_strcmp(token, cx_str("and"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
169 return NSAPI_EXPRESSION_AND;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
170 } else if(!cx_strcmp(token, cx_str("or"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
171 return NSAPI_EXPRESSION_OR;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
172 } else if(!cx_strcmp(token, cx_str("xor"))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
173 return NSAPI_EXPRESSION_XOR;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
174 } else if(!cx_strcmp(token, cx_str("=="))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
175 return NSAPI_EXPRESSION_EQ;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
176 } else if(!cx_strcmp(token, cx_str("!="))) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
177 return NSAPI_EXPRESSION_NEQ;
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_GT;
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_LT;
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_GE;
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_LE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
186 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
187 return NSAPI_EXPRESSION_NOOP;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
188 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
189
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
190 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
191 if(token.length == 0) {
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
192 return 0;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
193 }
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
194
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
195 size_t start = 0;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
196 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
197 if(token.length < 2) {
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
198 return 0;
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 start++;
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
201 }
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
202
3df9258cd3cc allow '-' in tokens, add support for negative integers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 423
diff changeset
203 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
204 if(!isdigit(token.ptr[i])) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
205 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
206 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
207 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
208 return 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
209 }
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 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
212 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
213 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
214 expr->type = NSAPI_EXPRESSION_STRING;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
215 } 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
216 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
217 expr->type = NSAPI_EXPRESSION_VARIABLE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
218 } else if(token_is_int(token)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
219 if(!util_strtoint(token.ptr, &expr->value.i)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
220 return 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
221 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
222 expr->type = NSAPI_EXPRESSION_INT;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
223 } else if(!strcmp(token.ptr, "true")) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
224 expr->type = NSAPI_EXPRESSION_BOOL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
225 expr->value.b = 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
226 } else if(!strcmp(token.ptr, "false")) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
227 expr->type = NSAPI_EXPRESSION_BOOL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
228 expr->value.b = 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
229 } else {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
230 return 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
231 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
232 // TODO: double
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
233 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
234 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
235
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
236 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
237 cxstring *token = cxListAt(tokens, *pos);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
238 if(token) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
239 (*pos)++;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
240 return *token;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
241 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
242 return (cxstring){NULL, 0};
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
243 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
244
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
245 NSAPIExpression* expr_parser_pop(ExprParser *parser) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
246 CxList *stack = parser->ex_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
247 if(stack->size == 0) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
248 return NULL;
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 *ret = cxListAt(stack, stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
251 cxListRemove(stack, stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
252 return ret;
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
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
255 // 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
256 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
257 NSAPIExpression *exp = pool_malloc(parser->pool, sizeof(NSAPIExpression));
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
258 exp->operator = op->operator;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
259 // 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
260 // a value was expected.
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
261 // For example: 1 + - 2
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
262 // 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
263 // 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
264 if(op->expect_value) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
265 // We expected a value but got an operator? This must be an unary operator
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
266 exp->type = NSAPI_EXPRESSION_UNARY;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
267 exp->left = expr_parser_pop(parser);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
268 exp->right = NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
269 } else {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
270 // binary operator
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
271 exp->type = NSAPI_EXPRESSION_BINARY;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
272 exp->right = expr_parser_pop(parser);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
273 exp->left = expr_parser_pop(parser);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
274 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
275
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
276 if(!exp->left && !exp->right) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
277 return 1; // error
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
278 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
279
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
280 cxListAdd(parser->ex_stack, exp);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
281
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
282 parser->expect_value = TRUE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
283 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
284 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
285
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
286 // 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
287 // and adds it to parser->ex_stack
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
288 // sets parser->expect_value to false
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
289 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
290 NSAPIExpression *exp = pool_malloc(parser->pool, sizeof(NSAPIExpression));
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
291 ZERO(exp, sizeof(NSAPIExpression));
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
292 if(expr_set_value(parser->pool, exp, token)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
293 return 1;
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 cxListAdd(parser->ex_stack, exp);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
296 parser->expect_value = FALSE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
297 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
298 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
299
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
300
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
301 static NSAPIExpression* expr_parse_expr(ExprParser *parser) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
302 CxList *op_stack = parser->op_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
303 CxList *ex_stack = parser->ex_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
304
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
305 // Shunting yard algorithm
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
306 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
307 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
308 NSAPIExpressionOperator op = expr_operator(token);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
309 if(op != NSAPI_EXPRESSION_NOOP) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
310 ExprOpStackItem new_op;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
311 new_op.operator = op;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
312 new_op.expect_value = parser->expect_value;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
313 new_op.open_parenthesis = FALSE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
314 while(op_stack->size > 0) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
315 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
316 if(stack_item->operator == NSAPI_EXPRESSION_NOOP) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
317 break;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
318 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
319 // check presedence
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
320 if(op >= stack_item->operator) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
321 break;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
322 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
323 if(expr_add_operator(parser, stack_item)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
324 return NULL;
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 cxListRemove(op_stack, op_stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
327 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
328 cxListAdd(op_stack, &new_op);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
329 parser->expect_value = TRUE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
330 } else if(token.length == 1 && token.ptr[0] == '(') {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
331 ExprOpStackItem new_op;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
332 new_op.operator = NSAPI_EXPRESSION_NOOP;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
333 new_op.expect_value = 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
334 new_op.open_parenthesis = 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
335 cxListAdd(op_stack, &new_op);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
336 parser->expect_value = TRUE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
337 } else if(token.length == 1 && token.ptr[0] == ')') {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
338 int found_open_bracket = FALSE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
339 while(op_stack->size > 0) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
340 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
341 cxListRemove(op_stack, op_stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
342 if(stack_item->open_parenthesis) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
343 found_open_bracket = TRUE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
344 break;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
345 } else {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
346 if(expr_add_operator(parser, stack_item)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
347 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
348 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
349 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
350 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
351 if(!found_open_bracket) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
352 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
353 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
354 parser->expect_value = FALSE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
355 } else {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
356 if(expr_add_value(parser, token)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
357 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
358 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
359 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
360 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
361
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
362 while(op_stack->size > 0) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
363 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
364 if(stack_item->open_parenthesis) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
365 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
366 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
367 if(expr_add_operator(parser, stack_item)) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
368 return NULL;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
369 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
370 cxListRemove(op_stack, op_stack->size-1);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
371 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
372
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
373 if(ex_stack->size != 1) {
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
374 return NULL;
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
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
377 return cxListAt(ex_stack, 0);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
378 }
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 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
381 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
382 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
383
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
384 ExprParser parser;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
385 parser.pool = pool;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
386 parser.op_stack = op_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
387 parser.ex_stack = ex_stack;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
388 parser.tokens = tokens;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
389 parser.pos = pos;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
390 parser.expect_value = TRUE;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
391
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
392 NSAPIExpression *ret = expr_parse_expr(&parser);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
393 cxListDestroy(op_stack);
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
394 cxListDestroy(ex_stack);
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 return ret;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
397 }
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
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 421
diff changeset
400
83
28433f06d5ee added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 61
diff changeset
401 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
402 return 1;
83
28433f06d5ee added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 61
diff changeset
403 }

mercurial