1 /* $NetBSD: sel.c,v 1.1.1.2 2014/04/24 12:45:42 pettai Exp $ */
4 * Copyright (c) 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 _hx509_make_expr(enum hx_expr_op op
, void *arg1
, void *arg2
)
43 expr
= malloc(sizeof(*expr
));
54 eval_word(hx509_context context
, hx509_env env
, struct hx_expr
*word
)
60 if (word
->arg2
== NULL
)
61 return hx509_env_find(context
, env
, word
->arg1
);
63 env
= hx509_env_find_binding(context
, env
, word
->arg1
);
67 return eval_word(context
, env
, word
->arg2
);
74 find_variable(hx509_context context
, hx509_env env
, struct hx_expr
*word
)
76 assert(word
->op
== expr_VAR
);
78 if (word
->arg2
== NULL
)
79 return hx509_env_find_binding(context
, env
, word
->arg1
);
81 env
= hx509_env_find_binding(context
, env
, word
->arg1
);
84 return find_variable(context
, env
, word
->arg2
);
88 eval_comp(hx509_context context
, hx509_env env
, struct hx_expr
*expr
)
97 s1
= eval_word(context
, env
, expr
->arg1
);
98 s2
= eval_word(context
, env
, expr
->arg2
);
100 if (s1
== NULL
|| s2
== NULL
)
103 if (expr
->op
== comp_TAILEQ
) {
104 size_t len1
= strlen(s1
);
105 size_t len2
= strlen(s2
);
109 ret
= strcmp(s1
+ (len1
- len2
), s2
) == 0;
111 ret
= strcmp(s1
, s2
) == 0;
112 if (expr
->op
== comp_NE
)
118 struct hx_expr
*subexpr
;
121 w
= eval_word(context
, env
, expr
->arg1
);
123 subexpr
= expr
->arg2
;
125 if (subexpr
->op
== expr_WORDS
) {
127 s1
= eval_word(context
, env
, subexpr
->arg1
);
128 if (strcmp(w
, s1
) == 0)
130 subexpr
= subexpr
->arg2
;
132 } else if (subexpr
->op
== expr_VAR
) {
135 subenv
= find_variable(context
, env
, subexpr
);
140 if (subenv
->type
!= env_string
)
142 if (strcmp(w
, subenv
->name
) == 0)
144 if (strcmp(w
, subenv
->u
.string
) == 0)
146 subenv
= subenv
->next
;
150 _hx509_abort("hx509 eval IN unknown op: %d", (int)subexpr
->op
);
155 _hx509_abort("hx509 eval expr with unknown op: %d", (int)expr
->op
);
161 _hx509_expr_eval(hx509_context context
, hx509_env env
, struct hx_expr
*expr
)
169 return ! _hx509_expr_eval(context
, env
, expr
->arg1
);
171 return _hx509_expr_eval(context
, env
, expr
->arg1
) &&
172 _hx509_expr_eval(context
, env
, expr
->arg2
);
174 return _hx509_expr_eval(context
, env
, expr
->arg1
) ||
175 _hx509_expr_eval(context
, env
, expr
->arg2
);
177 return eval_comp(context
, env
, expr
->arg1
);
179 _hx509_abort("hx509 eval expr with unknown op: %d", (int)expr
->op
);
180 UNREACHABLE(return 0);
185 _hx509_expr_free(struct hx_expr
*expr
)
197 _hx509_expr_free(expr
->arg2
);
201 _hx509_expr_free(expr
->arg1
);
203 _hx509_expr_free(expr
->arg2
);
210 _hx509_expr_parse(const char *buf
)
212 _hx509_expr_input
.buf
= buf
;
213 _hx509_expr_input
.length
= strlen(buf
);
214 _hx509_expr_input
.offset
= 0;
215 _hx509_expr_input
.expr
= NULL
;
217 if (_hx509_expr_input
.error
) {
218 free(_hx509_expr_input
.error
);
219 _hx509_expr_input
.error
= NULL
;
224 return _hx509_expr_input
.expr
;
228 _hx509_sel_yyerror (const char *s
)
230 if (_hx509_expr_input
.error
)
231 free(_hx509_expr_input
.error
);
233 _hx509_expr_input
.error
= strdup(s
);