6 #include "mod_ssi_expr.h"
7 #include "mod_ssi_exprparser.h"
24 ssi_val_t
*ssi_val_init(void) {
27 s
= calloc(1, sizeof(*s
));
32 void ssi_val_free(ssi_val_t
*s
) {
33 if (s
->str
) buffer_free(s
->str
);
38 int ssi_val_tobool(ssi_val_t
*B
) {
39 if (B
->type
== SSI_TYPE_STRING
) {
40 return !buffer_string_is_empty(B
->str
);
46 static int ssi_expr_tokenizer(server
*srv
, connection
*con
, handler_ctx
*p
,
47 ssi_tokenizer_t
*t
, int *token_id
, buffer
*token
) {
53 for (tid
= 0; tid
== 0 && t
->offset
< t
->size
&& t
->input
[t
->offset
] ; ) {
54 char c
= t
->input
[t
->offset
];
64 buffer_copy_string_len(token
, CONST_STR_LEN("(=)"));
68 if (t
->input
[t
->offset
+ 1] == '=') {
74 buffer_copy_string_len(token
, CONST_STR_LEN("(>=)"));
81 buffer_copy_string_len(token
, CONST_STR_LEN("(>)"));
86 if (t
->input
[t
->offset
+ 1] == '=') {
92 buffer_copy_string_len(token
, CONST_STR_LEN("(<=)"));
99 buffer_copy_string_len(token
, CONST_STR_LEN("(<)"));
105 if (t
->input
[t
->offset
+ 1] == '=') {
111 buffer_copy_string_len(token
, CONST_STR_LEN("(!=)"));
118 buffer_copy_string_len(token
, CONST_STR_LEN("(!)"));
123 if (t
->input
[t
->offset
+ 1] == '&') {
129 buffer_copy_string_len(token
, CONST_STR_LEN("(&&)"));
131 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
139 if (t
->input
[t
->offset
+ 1] == '|') {
145 buffer_copy_string_len(token
, CONST_STR_LEN("(||)"));
147 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
161 /* search for the terminating " */
162 for (i
= 1; t
->input
[t
->offset
+ i
] && t
->input
[t
->offset
+ i
] != '\''; i
++);
164 if (t
->input
[t
->offset
+ i
]) {
167 buffer_copy_string_len(token
, t
->input
+ t
->offset
+ 1, i
-1);
170 t
->line_pos
+= i
+ 1;
174 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
176 "missing closing quote");
188 buffer_copy_string_len(token
, CONST_STR_LEN("("));
196 buffer_copy_string_len(token
, CONST_STR_LEN(")"));
199 if (t
->input
[t
->offset
+ 1] == '{') {
200 for (i
= 2; t
->input
[t
->offset
+ i
] && t
->input
[t
->offset
+ i
] != '}'; i
++);
202 if (t
->input
[t
->offset
+ i
] != '}') {
203 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
205 "missing closing quote");
210 buffer_copy_string_len(token
, t
->input
+ t
->offset
+ 2, i
-3);
212 for (i
= 1; isalpha(t
->input
[t
->offset
+ i
]) ||
213 t
->input
[t
->offset
+ i
] == '_' ||
214 ((i
> 1) && isdigit(t
->input
[t
->offset
+ i
])); i
++);
216 buffer_copy_string_len(token
, t
->input
+ t
->offset
+ 1, i
-1);
221 if (NULL
!= (ds
= (data_string
*)array_get_element(p
->ssi_cgi_env
, token
->ptr
))) {
222 buffer_copy_buffer(token
, ds
->value
);
223 } else if (NULL
!= (ds
= (data_string
*)array_get_element(p
->ssi_vars
, token
->ptr
))) {
224 buffer_copy_buffer(token
, ds
->value
);
226 buffer_copy_string_len(token
, CONST_STR_LEN(""));
234 for (i
= 0; isgraph(t
->input
[t
->offset
+ i
]); i
++) {
235 char d
= t
->input
[t
->offset
+ i
];
254 buffer_copy_string_len(token
, t
->input
+ t
->offset
, i
);
267 } else if (t
->offset
< t
->size
) {
268 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
275 int ssi_eval_expr(server
*srv
, connection
*con
, handler_ctx
*p
, const char *expr
) {
285 t
.size
= strlen(expr
);
295 /* default context */
297 pParser
= ssiexprparserAlloc( malloc
);
298 force_assert(pParser
);
299 token
= buffer_init();
300 while((1 == (ret
= ssi_expr_tokenizer(srv
, con
, p
, &t
, &token_id
, token
))) && context
.ok
) {
301 ssiexprparser(pParser
, token_id
, token
, &context
);
303 token
= buffer_init();
305 ssiexprparser(pParser
, 0, token
, &context
);
306 ssiexprparserFree(pParser
, free
);
311 log_error_write(srv
, __FILE__
, __LINE__
, "s",
312 "expr parser failed");
316 if (context
.ok
== 0) {
317 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
319 "parser failed somehow near here");
323 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
328 return context
.val
.bo
;