6 #include "mod_ssi_expr.h"
7 #include "mod_ssi_exprparser.h"
25 ssi_val_t
*ssi_val_init(void) {
28 s
= calloc(1, sizeof(*s
));
33 void ssi_val_free(ssi_val_t
*s
) {
34 if (s
->str
) buffer_free(s
->str
);
39 int ssi_val_tobool(ssi_val_t
*B
) {
40 if (B
->type
== SSI_TYPE_STRING
) {
41 return !buffer_string_is_empty(B
->str
);
47 static int ssi_expr_tokenizer(server
*srv
, connection
*con
, handler_ctx
*p
,
48 ssi_tokenizer_t
*t
, int *token_id
, buffer
*token
) {
54 for (tid
= 0; tid
== 0 && t
->offset
< t
->size
&& t
->input
[t
->offset
] ; ) {
55 char c
= t
->input
[t
->offset
];
65 buffer_copy_string_len(token
, CONST_STR_LEN("(=)"));
69 if (t
->input
[t
->offset
+ 1] == '=') {
75 buffer_copy_string_len(token
, CONST_STR_LEN("(>=)"));
82 buffer_copy_string_len(token
, CONST_STR_LEN("(>)"));
87 if (t
->input
[t
->offset
+ 1] == '=') {
93 buffer_copy_string_len(token
, CONST_STR_LEN("(<=)"));
100 buffer_copy_string_len(token
, CONST_STR_LEN("(<)"));
106 if (t
->input
[t
->offset
+ 1] == '=') {
112 buffer_copy_string_len(token
, CONST_STR_LEN("(!=)"));
119 buffer_copy_string_len(token
, CONST_STR_LEN("(!)"));
124 if (t
->input
[t
->offset
+ 1] == '&') {
130 buffer_copy_string_len(token
, CONST_STR_LEN("(&&)"));
132 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
140 if (t
->input
[t
->offset
+ 1] == '|') {
146 buffer_copy_string_len(token
, CONST_STR_LEN("(||)"));
148 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
162 /* search for the terminating " */
163 for (i
= 1; t
->input
[t
->offset
+ i
] && t
->input
[t
->offset
+ i
] != '\''; i
++);
165 if (t
->input
[t
->offset
+ i
]) {
168 buffer_copy_string_len(token
, t
->input
+ t
->offset
+ 1, i
-1);
171 t
->line_pos
+= i
+ 1;
175 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
177 "missing closing quote");
189 buffer_copy_string_len(token
, CONST_STR_LEN("("));
197 buffer_copy_string_len(token
, CONST_STR_LEN(")"));
200 if (t
->input
[t
->offset
+ 1] == '{') {
201 for (i
= 2; t
->input
[t
->offset
+ i
] && t
->input
[t
->offset
+ i
] != '}'; i
++);
203 if (t
->input
[t
->offset
+ i
] != '}') {
204 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
206 "missing closing quote");
211 buffer_copy_string_len(token
, t
->input
+ t
->offset
+ 2, i
-3);
213 for (i
= 1; isalpha(t
->input
[t
->offset
+ i
]) ||
214 t
->input
[t
->offset
+ i
] == '_' ||
215 ((i
> 1) && isdigit(t
->input
[t
->offset
+ i
])); i
++);
217 buffer_copy_string_len(token
, t
->input
+ t
->offset
+ 1, i
-1);
222 if (NULL
!= (ds
= (data_string
*)array_get_element_klen(p
->ssi_cgi_env
, CONST_BUF_LEN(token
)))) {
223 buffer_copy_buffer(token
, ds
->value
);
224 } else if (NULL
!= (ds
= (data_string
*)array_get_element_klen(p
->ssi_vars
, CONST_BUF_LEN(token
)))) {
225 buffer_copy_buffer(token
, ds
->value
);
227 buffer_copy_string_len(token
, CONST_STR_LEN(""));
235 for (i
= 0; isgraph(t
->input
[t
->offset
+ i
]); i
++) {
236 char d
= t
->input
[t
->offset
+ i
];
255 buffer_copy_string_len(token
, t
->input
+ t
->offset
, i
);
268 } else if (t
->offset
< t
->size
) {
269 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
276 int ssi_eval_expr(server
*srv
, connection
*con
, handler_ctx
*p
, const char *expr
) {
286 t
.size
= strlen(expr
);
296 /* default context */
298 pParser
= ssiexprparserAlloc( malloc
);
299 force_assert(pParser
);
300 token
= buffer_init();
301 while((1 == (ret
= ssi_expr_tokenizer(srv
, con
, p
, &t
, &token_id
, token
))) && context
.ok
) {
302 ssiexprparser(pParser
, token_id
, token
, &context
);
304 token
= buffer_init();
306 ssiexprparser(pParser
, 0, token
, &context
);
307 ssiexprparserFree(pParser
, free
);
312 log_error_write(srv
, __FILE__
, __LINE__
, "s",
313 "expr parser failed");
317 if (context
.ok
== 0) {
318 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
320 "parser failed somehow near here");
324 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
329 return context
.val
.bo
;