2 * Copyright (c) 2017 by Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * Internet Systems Consortium, Inc.
18 * Redwood City, CA 94063
20 * https://www.isc.org/
26 #include <sys/errno.h>
27 #include <sys/types.h>
28 #include <arpa/inet.h>
36 static void debug(const char* fmt
, ...);
39 print_expression(struct element
*expr
, isc_boolean_t
*lose
)
41 if (expr
->type
== ELEMENT_BOOLEAN
)
42 return print_boolean_expression(expr
, lose
);
43 if (expr
->type
== ELEMENT_INTEGER
)
44 return print_numeric_expression(expr
, lose
);
45 if (expr
->type
== ELEMENT_STRING
)
46 return print_data_expression(expr
, lose
);
48 if (is_boolean_expression(expr
))
49 return print_boolean_expression(expr
, lose
);
50 if (is_numeric_expression(expr
))
51 return print_numeric_expression(expr
, lose
);
52 if (is_data_expression(expr
))
53 return print_data_expression(expr
, lose
);
59 print_boolean_expression(struct element
*expr
, isc_boolean_t
*lose
)
61 struct string
*result
;
63 if (expr
->type
== ELEMENT_BOOLEAN
) {
71 * From is_boolean_expression
73 if (expr
->type
!= ELEMENT_MAP
) {
77 result
= allocString();
80 if (mapContains(expr
, "check")) {
83 appendString(result
, "check ");
84 name
= mapGet(expr
, "check");
85 if ((name
== NULL
) || (name
->type
!= ELEMENT_STRING
)) {
87 appendString(result
, "???");
89 concatString(result
, stringValue(name
));
90 return result
->content
;
94 if (mapContains(expr
, "exists")) {
96 struct element
*universe
;
99 appendString(result
, "exists ");
100 arg
= mapGet(expr
, "exists");
101 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
103 appendString(result
, "???");
104 return result
->content
;
106 universe
= mapGet(arg
, "universe");
107 if ((universe
== NULL
) || (universe
->type
!= ELEMENT_STRING
)) {
109 appendString(result
, "???");
110 return result
->content
;
112 concatString(result
, stringValue(universe
));
113 appendString(result
, ".");
114 name
= mapGet(arg
, "name");
115 if ((name
== NULL
) || (name
->type
!= ELEMENT_STRING
)) {
117 appendString(result
, "???");
118 return result
->content
;
120 concatString(result
, stringValue(name
));
121 return result
->content
;
124 /* variable-exists */
125 if (mapContains(expr
, "variable-exists")) {
126 struct element
*name
;
128 appendString(result
, "variable-exists ");
129 name
= mapGet(expr
, "variable-exists");
130 if ((name
== NULL
) || (name
->type
!= ELEMENT_STRING
)) {
132 appendString(result
, "???");
134 concatString(result
, stringValue(name
));
135 return result
->content
;
139 if (mapContains(expr
, "equal")) {
141 struct element
*left
;
142 struct element
*right
;
143 isc_boolean_t add_parenthesis
;
145 appendString(result
, "equal ");
146 arg
= mapGet(expr
, "equal");
147 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
149 appendString(result
, "???");
150 return result
->content
;
152 left
= mapGet(arg
, "left");
155 appendString(result
, "???");
156 return result
->content
;
158 result
= allocString();
159 add_parenthesis
= ISC_TF(expr_precedence(expr_equal
,
162 appendString(result
, "(");
163 appendString(result
, print_expression(left
, lose
));
165 appendString(result
, ")");
166 appendString(result
, " = ");
167 right
= mapGet(arg
, "right");
170 appendString(result
, "???");
171 return result
->content
;
173 add_parenthesis
= ISC_TF(expr_precedence(expr_equal
,
176 appendString(result
, "(");
177 appendString(result
, print_expression(right
, lose
));
179 appendString(result
, ")");
180 return result
->content
;
184 if (mapContains(expr
, "not-equal")) {
186 struct element
*left
;
187 struct element
*right
;
188 isc_boolean_t add_parenthesis
;
190 appendString(result
, "not-equal ");
191 arg
= mapGet(expr
, "not-equal");
192 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
194 appendString(result
, "???");
195 return result
->content
;
197 left
= mapGet(arg
, "left");
200 appendString(result
, "???");
201 return result
->content
;
203 result
= allocString();
204 add_parenthesis
= ISC_TF(expr_precedence(expr_not_equal
,
207 appendString(result
, "(");
208 appendString(result
, print_expression(left
, lose
));
210 appendString(result
, ")");
211 appendString(result
, " != ");
212 right
= mapGet(arg
, "right");
215 appendString(result
, "???");
216 return result
->content
;
218 add_parenthesis
= ISC_TF(expr_precedence(expr_not_equal
,
221 appendString(result
, "(");
222 appendString(result
, print_expression(right
, lose
));
224 appendString(result
, ")");
225 return result
->content
;
229 if (mapContains(expr
, "regex-match")) {
231 struct element
*left
;
232 struct element
*right
;
233 isc_boolean_t add_parenthesis
;
235 appendString(result
, "regex-match ");
236 arg
= mapGet(expr
, "regex-match");
237 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
239 appendString(result
, "???");
240 return result
->content
;
242 left
= mapGet(arg
, "left");
245 appendString(result
, "???");
246 return result
->content
;
248 result
= allocString();
249 add_parenthesis
= ISC_TF(expr_precedence(expr_regex_match
,
252 appendString(result
, "(");
253 appendString(result
, print_expression(left
, lose
));
255 appendString(result
, ")");
256 appendString(result
, " ~= ");
257 right
= mapGet(arg
, "right");
260 appendString(result
, "???");
261 return result
->content
;
263 appendString(result
, print_expression(right
, lose
));
264 return result
->content
;
268 if (mapContains(expr
, "iregex-match")) {
270 struct element
*left
;
271 struct element
*right
;
272 isc_boolean_t add_parenthesis
;
274 appendString(result
, "iregex-match ");
275 arg
= mapGet(expr
, "iregex-match");
276 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
278 appendString(result
, "???");
279 return result
->content
;
281 left
= mapGet(arg
, "left");
284 appendString(result
, "???");
285 return result
->content
;
287 result
= allocString();
288 add_parenthesis
= ISC_TF(expr_precedence(expr_iregex_match
,
291 appendString(result
, "(");
292 appendString(result
, print_expression(left
, lose
));
294 appendString(result
, ")");
295 appendString(result
, " ~~ ");
296 right
= mapGet(arg
, "right");
299 appendString(result
, "???");
300 return result
->content
;
302 appendString(result
, print_expression(right
, lose
));
303 return result
->content
;
307 if (mapContains(expr
, "and")) {
309 struct element
*left
;
310 struct element
*right
;
311 isc_boolean_t add_parenthesis
;
313 appendString(result
, "and ");
314 arg
= mapGet(expr
, "and");
315 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
317 appendString(result
, "???");
318 return result
->content
;
320 left
= mapGet(arg
, "left");
323 appendString(result
, "???");
324 return result
->content
;
326 result
= allocString();
327 add_parenthesis
= ISC_TF(expr_precedence(expr_and
,
330 appendString(result
, "(");
331 appendString(result
, print_expression(left
, lose
));
333 appendString(result
, ")");
334 appendString(result
, " and ");
335 right
= mapGet(arg
, "right");
338 appendString(result
, "???");
339 return result
->content
;
341 add_parenthesis
= ISC_TF(expr_precedence(expr_and
,
344 appendString(result
, "(");
345 appendString(result
, print_expression(right
, lose
));
347 appendString(result
, ")");
348 return result
->content
;
352 if (mapContains(expr
, "or")) {
354 struct element
*left
;
355 struct element
*right
;
356 isc_boolean_t add_parenthesis
;
358 appendString(result
, "or ");
359 arg
= mapGet(expr
, "or");
360 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
362 appendString(result
, "???");
363 return result
->content
;
365 left
= mapGet(arg
, "left");
368 appendString(result
, "???");
369 return result
->content
;
371 result
= allocString();
372 add_parenthesis
= ISC_TF(expr_precedence(expr_or
,
375 appendString(result
, "(");
376 appendString(result
, print_expression(left
, lose
));
378 appendString(result
, ")");
379 appendString(result
, " or ");
380 right
= mapGet(arg
, "right");
383 appendString(result
, "???");
384 return result
->content
;
386 add_parenthesis
= ISC_TF(expr_precedence(expr_or
,
389 appendString(result
, "(");
390 appendString(result
, print_expression(right
, lose
));
392 appendString(result
, ")");
393 return result
->content
;
397 if (mapContains(expr
, "not")) {
399 isc_boolean_t add_parenthesis
;
401 appendString(result
, "not ");
402 arg
= mapGet(expr
, "not");
405 appendString(result
, "???");
406 return result
->content
;
408 add_parenthesis
= ISC_TF(expr_precedence(expr_not
,
411 appendString(result
, "(");
412 appendString(result
, print_expression(arg
, lose
));
414 appendString(result
, ")");
415 return result
->content
;
419 if (mapContains(expr
, "known")) {
424 if (mapContains(expr
, "static")) {
428 /* variable-reference */
429 if (mapContains(expr
, "variable-reference")) {
430 struct element
*name
;
432 appendString(result
, "variable-reference ");
433 name
= mapGet(expr
, "variable-reference");
434 if ((name
== NULL
) || (name
->type
!= ELEMENT_STRING
)) {
436 appendString(result
, "???");
437 return result
->content
;
439 return stringValue(name
)->content
;
443 if (mapContains(expr
, "funcall")) {
445 struct element
*name
;
446 struct element
*args
;
449 appendString(result
, "funcall ");
450 arg
= mapGet(expr
, "funcall");
451 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
453 appendString(result
, "???");
454 return result
->content
;
456 name
= mapGet(arg
, "name");
457 if ((name
== NULL
) || (name
->type
!= ELEMENT_STRING
)) {
459 appendString(result
, "???");
460 return result
->content
;
462 result
= allocString();
463 concatString(result
, stringValue(name
));
464 appendString(result
, "(");
465 args
= mapGet(arg
, "arguments");
466 if ((args
== NULL
) || (args
->type
!= ELEMENT_LIST
)) {
468 appendString(result
, "???" ")");
469 return result
->content
;
471 for (i
= 0; i
< listSize(args
); i
++) {
472 struct element
*item
;
475 appendString(result
, ", ");
476 item
= listGet(args
, i
);
478 debug("funcall null argument %u",
481 appendString(result
, "???");
484 appendString(result
, print_expression(item
, lose
));
486 appendString(result
, ")");
487 return result
->content
;
491 appendString(result
, "???");
492 return result
->content
;
496 print_data_expression(struct element
*expr
, isc_boolean_t
*lose
)
498 struct string
*result
;
500 if (expr
->type
== ELEMENT_STRING
)
501 return quote(stringValue(expr
))->content
;
504 * From is_data_expression
506 if (expr
->type
!= ELEMENT_MAP
) {
510 result
= allocString();
513 if (mapContains(expr
, "substring")) {
515 struct element
*string
;
516 struct element
*offset
;
517 struct element
*length
;
519 appendString(result
, "substring(");
520 arg
= mapGet(expr
, "substring");
521 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
523 appendString(result
, "???" ")");
524 return result
->content
;
526 string
= mapGet(arg
, "expression");
527 if (string
== NULL
) {
529 appendString(result
, "???" ")");
530 return result
->content
;
532 appendString(result
, print_data_expression(string
, lose
));
533 appendString(result
, ", ");
534 offset
= mapGet(arg
, "offset");
535 if (offset
== NULL
) {
537 appendString(result
, "???" ")");
538 return result
->content
;
540 appendString(result
, print_numeric_expression(offset
, lose
));
541 appendString(result
, ", ");
542 length
= mapGet(arg
, "length");
543 if (length
== NULL
) {
545 appendString(result
, "???" ")");
546 return result
->content
;
548 appendString(result
, print_numeric_expression(length
, lose
));
549 appendString(result
, ")");
550 return result
->content
;
554 if (mapContains(expr
, "suffix")) {
556 struct element
*string
;
557 struct element
*length
;
559 appendString(result
, "suffix(");
560 arg
= mapGet(expr
, "suffix");
561 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
563 appendString(result
, "???" ")");
564 return result
->content
;
566 string
= mapGet(arg
, "expression");
567 if (string
== NULL
) {
569 appendString(result
, "???" ")");
570 return result
->content
;
572 appendString(result
, print_data_expression(string
, lose
));
573 appendString(result
, ", ");
574 length
= mapGet(arg
, "length");
575 if (length
== NULL
) {
577 appendString(result
, "???" ")");
578 return result
->content
;
580 appendString(result
, print_numeric_expression(length
, lose
));
581 appendString(result
, ")");
582 return result
->content
;
586 if (mapContains(expr
, "lowercase")) {
589 appendString(result
, "lowercase(");
590 arg
= mapGet(expr
, "lowercase");
593 appendString(result
, "???" ")");
594 return result
->content
;
596 appendString(result
, print_data_expression(arg
, lose
));
597 appendString(result
, ")");
598 return result
->content
;
602 if (mapContains(expr
, "uppercase")) {
605 appendString(result
, "uppercase(");
606 arg
= mapGet(expr
, "uppercase");
609 appendString(result
, "???" ")");
610 return result
->content
;
612 appendString(result
, print_data_expression(arg
, lose
));
613 appendString(result
, ")");
614 return result
->content
;
618 if (mapContains(expr
, "option")) {
620 struct element
*universe
;
621 struct element
*name
;
623 appendString(result
, "option ");
624 arg
= mapGet(expr
, "option");
625 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
627 appendString(result
, "???");
628 return result
->content
;
630 universe
= mapGet(arg
, "universe");
631 if ((universe
== NULL
) || (universe
->type
!= ELEMENT_STRING
)) {
633 appendString(result
, "???");
634 return result
->content
;
636 concatString(result
, stringValue(universe
));
637 appendString(result
, ".");
638 name
= mapGet(arg
, "name");
639 if ((name
== NULL
) || (name
->type
!= ELEMENT_STRING
)) {
641 appendString(result
, "???");
642 return result
->content
;
644 concatString(result
, stringValue(name
));
645 return result
->content
;
649 if (mapContains(expr
, "hardware"))
653 if (mapContains(expr
, "hw-type"))
657 if (mapContains(expr
, "hw-address"))
661 if (mapContains(expr
, "const-data")) {
664 arg
= mapGet(expr
, "const-data");
665 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_STRING
)) {
667 appendString(result
, "???");
668 return result
->content
;
670 concatString(result
, stringValue(arg
));
671 return result
->content
;
675 if (mapContains(expr
, "packet")) {
677 struct element
*offset
;
678 struct element
*length
;
680 appendString(result
, "packet(");
681 arg
= mapGet(expr
, "packet");
682 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
684 appendString(result
, "???" ")");
685 return result
->content
;
687 offset
= mapGet(arg
, "offset");
688 if (offset
== NULL
) {
690 appendString(result
, "???" ")");
691 return result
->content
;
693 appendString(result
, print_numeric_expression(offset
, lose
));
694 appendString(result
, ", ");
695 length
= mapGet(arg
, "length");
696 if (length
== NULL
) {
698 appendString(result
, "???" ")");
699 return result
->content
;
701 appendString(result
, print_numeric_expression(length
, lose
));
702 appendString(result
, ")");
703 return result
->content
;
707 if (mapContains(expr
, "concat")) {
709 struct element
*left
;
710 struct element
*right
;
712 appendString(result
, "concat(");
713 arg
= mapGet(expr
, "concat");
714 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
716 appendString(result
, "???" ")");
717 return result
->content
;
719 left
= mapGet(arg
, "left");
722 appendString(result
, "???" ")");
723 return result
->content
;
725 appendString(result
, print_data_expression(left
, lose
));
726 appendString(result
, ", ");
727 right
= mapGet(arg
, "right");
730 appendString(result
, "???" ")");
731 return result
->content
;
733 appendString(result
, print_data_expression(right
, lose
));
734 appendString(result
, ")");
735 return result
->content
;
739 if (mapContains(expr
, "encapsulate")) {
742 appendString(result
, "encapsulate ");
743 arg
= mapGet(expr
, "encapsulate");
746 appendString(result
, "???");
747 return result
->content
;
749 appendString(result
, print_data_expression(arg
, lose
));
750 return result
->content
;
754 if (mapContains(expr
, "encode-int8")) {
757 appendString(result
, "encode-int(");
758 arg
= mapGet(expr
, "encode-int8");
761 appendString(result
, "???, 8)");
762 return result
->content
;
764 appendString(result
, print_numeric_expression(arg
, lose
));
765 appendString(result
, ", 8)");
766 return result
->content
;
770 if (mapContains(expr
, "encode-int16")) {
773 appendString(result
, "encode-int(");
774 arg
= mapGet(expr
, "encode-int16");
777 appendString(result
, "???, 16)");
778 return result
->content
;
780 appendString(result
, print_numeric_expression(arg
, lose
));
781 appendString(result
, ", 16)");
782 return result
->content
;
786 if (mapContains(expr
, "encode-int32")) {
789 appendString(result
, "encode-int(");
790 arg
= mapGet(expr
, "encode-int32");
793 appendString(result
, "???, 32)");
794 return result
->content
;
796 appendString(result
, print_numeric_expression(arg
, lose
));
797 appendString(result
, ", 32)");
798 return result
->content
;
802 if (mapContains(expr
, "gethostbyname")) {
805 appendString(result
, "gethostbyname(");
806 arg
= mapGet(expr
, "gethostbyname");
809 appendString(result
, "???");
810 return result
->content
;
812 appendString(result
, print_data_expression(arg
, lose
));
813 appendString(result
, ")");
814 return result
->content
;
817 /* binary-to-ascii */
818 if (mapContains(expr
, "binary-to-ascii")) {
820 struct element
*base
;
821 struct element
*width
;
822 struct element
*separator
;
823 struct element
*buffer
;
825 appendString(result
, "binary-to-ascii(");
826 arg
= mapGet(expr
, "binary-to-ascii");
827 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
829 appendString(result
, "???" ")");
830 return result
->content
;
832 base
= mapGet(arg
, "base");
835 appendString(result
, "???" ")");
836 return result
->content
;
838 appendString(result
, print_numeric_expression(base
, lose
));
839 appendString(result
, ", ");
840 width
= mapGet(arg
, "width");
843 appendString(result
, "???" ")");
844 return result
->content
;
846 appendString(result
, print_numeric_expression(width
, lose
));
847 appendString(result
, ", ");
848 separator
= mapGet(arg
, "separator");
849 if (separator
== NULL
) {
851 appendString(result
, "???" ")");
852 return result
->content
;
854 appendString(result
, print_data_expression(separator
, lose
));
855 appendString(result
, ", ");
856 buffer
= mapGet(arg
, "buffer");
857 if (buffer
== NULL
) {
859 appendString(result
, "???" ")");
860 return result
->content
;
862 appendString(result
, print_data_expression(buffer
, lose
));
863 appendString(result
, ")");
864 return result
->content
;
868 if (mapContains(expr
, "filename"))
872 if (mapContains(expr
, "server-name"))
873 return "server-name";
876 if (mapContains(expr
, "reverse")) {
878 struct element
*width
;
879 struct element
*buffer
;
881 appendString(result
, "reverse(");
882 arg
= mapGet(expr
, "reverse");
883 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
885 appendString(result
, "???" ")");
886 return result
->content
;
888 width
= mapGet(arg
, "width");
891 appendString(result
, "???" ")");
892 return result
->content
;
894 appendString(result
, print_numeric_expression(width
, lose
));
895 appendString(result
, ", ");
896 buffer
= mapGet(arg
, "buffer");
897 if (buffer
== NULL
) {
899 appendString(result
, "???" ")");
900 return result
->content
;
902 appendString(result
, print_data_expression(buffer
, lose
));
903 appendString(result
, ")");
904 return result
->content
;
907 /* pick-first-value */
908 if (mapContains(expr
, "pick-first-value")) {
912 appendString(result
, "pick-first-value(");
913 arg
= mapGet(expr
, "pick-first-value");
914 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_LIST
)) {
916 appendString(result
, "???" ")");
917 return result
->content
;
919 for (i
= 0; i
< listSize(arg
); i
++) {
920 struct element
*item
;
923 appendString(result
, ", ");
924 item
= listGet(arg
, i
);
927 appendString(result
, "???");
931 print_data_expression(item
, lose
));
933 appendString(result
, ")");
934 return result
->content
;
938 if (mapContains(expr
, "host-decl-name"))
939 return "host-decl-name";
942 if (mapContains(expr
, "leased-address"))
943 return "leased-address";
946 if (mapContains(expr
, "config-option")) {
948 struct element
*universe
;
949 struct element
*name
;
951 appendString(result
, "config-option ");
952 arg
= mapGet(expr
, "config-option");
953 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
955 appendString(result
, "???");
956 return result
->content
;
958 universe
= mapGet(arg
, "universe");
959 if ((universe
== NULL
) || (universe
->type
!= ELEMENT_STRING
)) {
961 appendString(result
, "???");
962 return result
->content
;
964 concatString(result
, stringValue(universe
));
965 appendString(result
, ".");
966 name
= mapGet(arg
, "name");
967 if ((name
== NULL
) || (name
->type
!= ELEMENT_STRING
)) {
969 appendString(result
, "???");
970 return result
->content
;
972 concatString(result
, stringValue(name
));
973 return result
->content
;
977 if (mapContains(expr
, "null"))
981 if (mapContains(expr
, "gethostname"))
982 return "gethostname";
985 if (mapContains(expr
, "v6relay")) {
987 struct element
*relay
;
988 struct element
*option
;
991 appendString(result
, "v6relay(");
992 arg
= mapGet(expr
, "v6relay");
993 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
995 appendString(result
, "???" ")");
996 return result
->content
;
998 relay
= mapGet(arg
, "relay");
1001 appendString(result
, "???" ")");
1002 return result
->content
;
1004 appendString(result
, print_numeric_expression(relay
, lose
));
1005 appendString(result
, ", ");
1006 option
= mapGet(arg
, "relay-option");
1007 if (option
== NULL
) {
1009 appendString(result
, "???" ")");
1010 return result
->content
;
1012 appendString(result
, print_data_expression(option
, lose
));
1013 appendString(result
, ")");
1014 return result
->content
;
1018 appendString(result
, "???");
1019 return result
->content
;
1023 print_numeric_expression(struct element
*expr
, isc_boolean_t
*lose
)
1025 struct string
*result
;
1027 if (expr
->type
== ELEMENT_INTEGER
) {
1030 snprintf(buf
, sizeof(buf
), "%lld", (long long)intValue(expr
));
1031 result
= makeString(-1, buf
);
1032 return result
->content
;
1036 * From is_numeric_expression
1038 if (expr
->type
!= ELEMENT_MAP
) {
1042 result
= allocString();
1045 if (mapContains(expr
, "extract-int8")) {
1046 struct element
*arg
;
1048 appendString(result
, "extract-int(");
1049 arg
= mapGet(expr
, "extract-int8");
1052 appendString(result
, "???, 8)");
1053 return result
->content
;
1055 appendString(result
, print_data_expression(arg
, lose
));
1056 appendString(result
, ", 8)");
1057 return result
->content
;
1061 if (mapContains(expr
, "extract-int16")) {
1062 struct element
*arg
;
1064 appendString(result
, "extract-int(");
1065 arg
= mapGet(expr
, "extract-int16");
1068 appendString(result
, "???, 16)");
1069 return result
->content
;
1071 appendString(result
, print_data_expression(arg
, lose
));
1072 appendString(result
, ", 16)");
1073 return result
->content
;
1077 if (mapContains(expr
, "extract-int32")) {
1078 struct element
*arg
;
1080 appendString(result
, "extract-int(");
1081 arg
= mapGet(expr
, "extract-int32");
1084 appendString(result
, "???, 32)");
1085 return result
->content
;
1087 appendString(result
, print_data_expression(arg
, lose
));
1088 appendString(result
, ", 32)");
1089 return result
->content
;
1093 if (mapContains(expr
, "const-int")) {
1094 struct element
*arg
;
1097 arg
= mapGet(expr
, "const-int");
1098 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_INTEGER
)) {
1100 appendString(result
, "???");
1101 return result
->content
;
1103 snprintf(buf
, sizeof(buf
), "%lld", (long long)intValue(arg
));
1104 result
= makeString(-1, buf
);
1105 return result
->content
;
1109 if (mapContains(expr
, "lease-time"))
1110 return "lease-time";
1113 if (mapContains(expr
, "add")) {
1114 struct element
*arg
;
1115 struct element
*left
;
1116 struct element
*right
;
1117 isc_boolean_t add_parenthesis
;
1119 appendString(result
, "add ");
1120 arg
= mapGet(expr
, "add");
1121 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
1123 appendString(result
, "???");
1124 return result
->content
;
1126 left
= mapGet(arg
, "left");
1129 appendString(result
, "???");
1130 return result
->content
;
1132 result
= allocString();
1133 add_parenthesis
= ISC_TF(expr_precedence(expr_add
,
1135 if (add_parenthesis
)
1136 appendString(result
, "(");
1137 appendString(result
, print_expression(left
, lose
));
1138 if (add_parenthesis
)
1139 appendString(result
, ")");
1140 appendString(result
, " + ");
1141 right
= mapGet(arg
, "right");
1142 if (right
== NULL
) {
1144 appendString(result
, "???");
1145 return result
->content
;
1147 add_parenthesis
= ISC_TF(expr_precedence(expr_add
,
1149 if (add_parenthesis
)
1150 appendString(result
, "(");
1151 appendString(result
, print_expression(right
, lose
));
1152 if (add_parenthesis
)
1153 appendString(result
, ")");
1154 return result
->content
;
1158 if (mapContains(expr
, "subtract")) {
1159 struct element
*arg
;
1160 struct element
*left
;
1161 struct element
*right
;
1162 isc_boolean_t add_parenthesis
;
1164 appendString(result
, "subtract ");
1165 arg
= mapGet(expr
, "subtract");
1166 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
1168 appendString(result
, "???");
1169 return result
->content
;
1171 left
= mapGet(arg
, "left");
1174 appendString(result
, "???");
1175 return result
->content
;
1177 result
= allocString();
1178 add_parenthesis
= ISC_TF(expr_precedence(expr_subtract
,
1180 if (add_parenthesis
)
1181 appendString(result
, "(");
1182 appendString(result
, print_expression(left
, lose
));
1183 if (add_parenthesis
)
1184 appendString(result
, ")");
1185 appendString(result
, " - ");
1186 right
= mapGet(arg
, "right");
1187 if (right
== NULL
) {
1189 appendString(result
, "???");
1190 return result
->content
;
1192 add_parenthesis
= ISC_TF(expr_precedence(expr_subtract
,
1194 if (add_parenthesis
)
1195 appendString(result
, "(");
1196 appendString(result
, print_expression(right
, lose
));
1197 if (add_parenthesis
)
1198 appendString(result
, ")");
1199 return result
->content
;
1203 if (mapContains(expr
, "multiply")) {
1204 struct element
*arg
;
1205 struct element
*left
;
1206 struct element
*right
;
1207 isc_boolean_t add_parenthesis
;
1209 appendString(result
, "multiply ");
1210 arg
= mapGet(expr
, "multiply");
1211 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
1213 appendString(result
, "???");
1214 return result
->content
;
1216 left
= mapGet(arg
, "left");
1219 appendString(result
, "???");
1220 return result
->content
;
1222 result
= allocString();
1223 add_parenthesis
= ISC_TF(expr_precedence(expr_multiply
,
1225 if (add_parenthesis
)
1226 appendString(result
, "(");
1227 appendString(result
, print_expression(left
, lose
));
1228 if (add_parenthesis
)
1229 appendString(result
, ")");
1230 appendString(result
, " * ");
1231 right
= mapGet(arg
, "right");
1232 if (right
== NULL
) {
1234 appendString(result
, "???");
1235 return result
->content
;
1237 add_parenthesis
= ISC_TF(expr_precedence(expr_multiply
,
1239 if (add_parenthesis
)
1240 appendString(result
, "(");
1241 appendString(result
, print_expression(right
, lose
));
1242 if (add_parenthesis
)
1243 appendString(result
, ")");
1244 return result
->content
;
1248 if (mapContains(expr
, "divide")) {
1249 struct element
*arg
;
1250 struct element
*left
;
1251 struct element
*right
;
1252 isc_boolean_t add_parenthesis
;
1254 appendString(result
, "divide ");
1255 arg
= mapGet(expr
, "divide");
1256 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
1258 appendString(result
, "???");
1259 return result
->content
;
1261 left
= mapGet(arg
, "left");
1264 appendString(result
, "???");
1265 return result
->content
;
1267 result
= allocString();
1268 add_parenthesis
= ISC_TF(expr_precedence(expr_divide
,
1270 if (add_parenthesis
)
1271 appendString(result
, "(");
1272 appendString(result
, print_expression(left
, lose
));
1273 if (add_parenthesis
)
1274 appendString(result
, ")");
1275 appendString(result
, " / ");
1276 right
= mapGet(arg
, "right");
1277 if (right
== NULL
) {
1279 appendString(result
, "???");
1280 return result
->content
;
1282 add_parenthesis
= ISC_TF(expr_precedence(expr_divide
,
1284 if (add_parenthesis
)
1285 appendString(result
, "(");
1286 appendString(result
, print_expression(right
, lose
));
1287 if (add_parenthesis
)
1288 appendString(result
, ")");
1289 return result
->content
;
1293 if (mapContains(expr
, "remainder")) {
1294 struct element
*arg
;
1295 struct element
*left
;
1296 struct element
*right
;
1297 isc_boolean_t add_parenthesis
;
1299 appendString(result
, "remainder ");
1300 arg
= mapGet(expr
, "remainder");
1301 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
1303 appendString(result
, "???");
1304 return result
->content
;
1306 left
= mapGet(arg
, "left");
1309 appendString(result
, "???");
1310 return result
->content
;
1312 result
= allocString();
1313 add_parenthesis
= ISC_TF(expr_precedence(expr_remainder
,
1315 if (add_parenthesis
)
1316 appendString(result
, "(");
1317 appendString(result
, print_expression(left
, lose
));
1318 if (add_parenthesis
)
1319 appendString(result
, ")");
1320 appendString(result
, " % ");
1321 right
= mapGet(arg
, "right");
1322 if (right
== NULL
) {
1324 appendString(result
, "???");
1325 return result
->content
;
1327 add_parenthesis
= ISC_TF(expr_precedence(expr_remainder
,
1329 if (add_parenthesis
)
1330 appendString(result
, "(");
1331 appendString(result
, print_expression(right
, lose
));
1332 if (add_parenthesis
)
1333 appendString(result
, ")");
1334 return result
->content
;
1338 if (mapContains(expr
, "binary-and")) {
1339 struct element
*arg
;
1340 struct element
*left
;
1341 struct element
*right
;
1342 isc_boolean_t add_parenthesis
;
1344 appendString(result
, "binary-and ");
1345 arg
= mapGet(expr
, "binary-and");
1346 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
1348 appendString(result
, "???");
1349 return result
->content
;
1351 left
= mapGet(arg
, "left");
1354 appendString(result
, "???");
1355 return result
->content
;
1357 result
= allocString();
1358 add_parenthesis
= ISC_TF(expr_precedence(expr_binary_and
,
1360 if (add_parenthesis
)
1361 appendString(result
, "(");
1362 appendString(result
, print_expression(left
, lose
));
1363 if (add_parenthesis
)
1364 appendString(result
, ")");
1365 appendString(result
, " & ");
1366 right
= mapGet(arg
, "right");
1367 if (right
== NULL
) {
1369 appendString(result
, "???");
1370 return result
->content
;
1372 add_parenthesis
= ISC_TF(expr_precedence(expr_binary_and
,
1374 if (add_parenthesis
)
1375 appendString(result
, "(");
1376 appendString(result
, print_expression(right
, lose
));
1377 if (add_parenthesis
)
1378 appendString(result
, ")");
1379 return result
->content
;
1383 if (mapContains(expr
, "binary-or")) {
1384 struct element
*arg
;
1385 struct element
*left
;
1386 struct element
*right
;
1387 isc_boolean_t add_parenthesis
;
1389 appendString(result
, "binary-or ");
1390 arg
= mapGet(expr
, "binary-or");
1391 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
1393 appendString(result
, "???");
1394 return result
->content
;
1396 left
= mapGet(arg
, "left");
1399 appendString(result
, "???");
1400 return result
->content
;
1402 result
= allocString();
1403 add_parenthesis
= ISC_TF(expr_precedence(expr_binary_or
,
1405 if (add_parenthesis
)
1406 appendString(result
, "(");
1407 appendString(result
, print_expression(left
, lose
));
1408 if (add_parenthesis
)
1409 appendString(result
, ")");
1410 appendString(result
, " | ");
1411 right
= mapGet(arg
, "right");
1412 if (right
== NULL
) {
1414 appendString(result
, "???");
1415 return result
->content
;
1417 add_parenthesis
= ISC_TF(expr_precedence(expr_binary_or
,
1419 if (add_parenthesis
)
1420 appendString(result
, "(");
1421 appendString(result
, print_expression(right
, lose
));
1422 if (add_parenthesis
)
1423 appendString(result
, ")");
1424 return result
->content
;
1428 if (mapContains(expr
, "binary-xor")) {
1429 struct element
*arg
;
1430 struct element
*left
;
1431 struct element
*right
;
1432 isc_boolean_t add_parenthesis
;
1434 appendString(result
, "binary-xor ");
1435 arg
= mapGet(expr
, "binary-xor");
1436 if ((arg
== NULL
) || (arg
->type
!= ELEMENT_MAP
)) {
1438 appendString(result
, "???");
1439 return result
->content
;
1441 left
= mapGet(arg
, "left");
1444 appendString(result
, "???");
1445 return result
->content
;
1447 result
= allocString();
1448 add_parenthesis
= ISC_TF(expr_precedence(expr_binary_xor
,
1450 if (add_parenthesis
)
1451 appendString(result
, "(");
1452 appendString(result
, print_expression(left
, lose
));
1453 if (add_parenthesis
)
1454 appendString(result
, ")");
1455 appendString(result
, " ^ ");
1456 right
= mapGet(arg
, "right");
1457 if (right
== NULL
) {
1459 appendString(result
, "???");
1460 return result
->content
;
1462 add_parenthesis
= ISC_TF(expr_precedence(expr_binary_xor
,
1464 if (add_parenthesis
)
1465 appendString(result
, "(");
1466 appendString(result
, print_expression(right
, lose
));
1467 if (add_parenthesis
)
1468 appendString(result
, ")");
1469 return result
->content
;
1473 if (mapContains(expr
, "client-state"))
1474 return "client-state";
1477 appendString(result
, "???");
1478 return result
->content
;
1482 debug(const char* fmt
, ...)
1486 va_start(list
, fmt
);
1487 vfprintf(stderr
, fmt
, list
);
1488 fprintf(stderr
, "\n");