2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 _hx509_make_expr(enum hx_expr_op op
, void *arg1
, void *arg2
)
41 expr
= malloc(sizeof(*expr
));
52 eval_word(hx509_context context
, hx509_env env
, struct hx_expr
*word
)
58 if (word
->arg2
== NULL
)
59 return hx509_env_find(context
, env
, word
->arg1
);
61 env
= hx509_env_find_binding(context
, env
, word
->arg1
);
65 return eval_word(context
, env
, word
->arg2
);
72 find_variable(hx509_context context
, hx509_env env
, struct hx_expr
*word
)
74 assert(word
->op
== expr_VAR
);
76 if (word
->arg2
== NULL
)
77 return hx509_env_find_binding(context
, env
, word
->arg1
);
79 env
= hx509_env_find_binding(context
, env
, word
->arg1
);
82 return find_variable(context
, env
, word
->arg2
);
86 eval_comp(hx509_context context
, hx509_env env
, struct hx_expr
*expr
)
95 s1
= eval_word(context
, env
, expr
->arg1
);
96 s2
= eval_word(context
, env
, expr
->arg2
);
98 if (s1
== NULL
|| s2
== NULL
)
101 if (expr
->op
== comp_TAILEQ
) {
102 size_t len1
= strlen(s1
);
103 size_t len2
= strlen(s2
);
107 ret
= strcmp(s1
+ (len1
- len2
), s2
) == 0;
109 ret
= strcmp(s1
, s2
) == 0;
110 if (expr
->op
== comp_NE
)
116 struct hx_expr
*subexpr
;
119 w
= eval_word(context
, env
, expr
->arg1
);
121 subexpr
= expr
->arg2
;
123 if (subexpr
->op
== expr_WORDS
) {
125 s1
= eval_word(context
, env
, subexpr
->arg1
);
126 if (strcmp(w
, s1
) == 0)
128 subexpr
= subexpr
->arg2
;
130 } else if (subexpr
->op
== expr_VAR
) {
133 subenv
= find_variable(context
, env
, subexpr
);
138 if (subenv
->type
!= env_string
)
140 if (strcmp(w
, subenv
->name
) == 0)
142 if (strcmp(w
, subenv
->u
.string
) == 0)
144 subenv
= subenv
->next
;
148 _hx509_abort("hx509 eval IN unknown op: %d", (int)subexpr
->op
);
153 _hx509_abort("hx509 eval expr with unknown op: %d", (int)expr
->op
);
159 _hx509_expr_eval(hx509_context context
, hx509_env env
, struct hx_expr
*expr
)
167 return ! _hx509_expr_eval(context
, env
, expr
->arg1
);
169 return _hx509_expr_eval(context
, env
, expr
->arg1
) &&
170 _hx509_expr_eval(context
, env
, expr
->arg2
);
172 return _hx509_expr_eval(context
, env
, expr
->arg1
) ||
173 _hx509_expr_eval(context
, env
, expr
->arg2
);
175 return eval_comp(context
, env
, expr
->arg1
);
177 _hx509_abort("hx509 eval expr with unknown op: %d", (int)expr
->op
);
178 UNREACHABLE(return 0);
183 _hx509_expr_free(struct hx_expr
*expr
)
195 _hx509_expr_free(expr
->arg2
);
199 _hx509_expr_free(expr
->arg1
);
201 _hx509_expr_free(expr
->arg2
);
208 _hx509_expr_parse(const char *buf
)
210 _hx509_expr_input
.buf
= buf
;
211 _hx509_expr_input
.length
= strlen(buf
);
212 _hx509_expr_input
.offset
= 0;
213 _hx509_expr_input
.expr
= NULL
;
215 if (_hx509_expr_input
.error
) {
216 free(_hx509_expr_input
.error
);
217 _hx509_expr_input
.error
= NULL
;
222 return _hx509_expr_input
.expr
;
226 _hx509_sel_yyerror (char *s
)
228 if (_hx509_expr_input
.error
)
229 free(_hx509_expr_input
.error
);
231 _hx509_expr_input
.error
= strdup(s
);