2 * eval.c - gawk parse tree interpreter
6 * Copyright (C) 1986, 1988, 1989, 1991-2000 the Free Software Foundation, Inc.
8 * This file is part of GAWK, the GNU implementation of the
9 * AWK Programming Language.
11 * GAWK is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * GAWK is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
25 * $FreeBSD: src/contrib/awk/eval.c,v 1.4.2.1 2001/01/23 22:08:31 asmodai Exp $
26 * $DragonFly: src/contrib/awk/Attic/eval.c,v 1.2 2003/06/17 04:23:58 dillon Exp $
31 extern double pow P((double x, double y));
32 extern double modf P((double x, double *yp));
33 extern double fmod P((double x, double y));
35 static int eval_condition P((NODE *tree));
36 static NODE *op_assign P((NODE *tree));
37 static NODE *func_call P((NODE *name, NODE *arg_list));
38 static NODE *match_op P((NODE *tree));
39 static void push_args P((int count, NODE *arglist, NODE **oldstack, char *func_name));
40 static void pop_fcall_stack P((void));
41 static void pop_fcall P((void));
42 static int in_function P((void));
43 char *nodetype2str P((NODETYPE type));
44 char *flags2str P((int flagval));
47 NODE *_t; /* used as a temporary in macros */
50 double _msc51bug; /* to get around a bug in MSC 5.1 */
58 /* Macros and variables to save and restore function and loop bindings */
60 * the val variable allows return/continue/break-out-of-context to be
61 * caught and diagnosed
63 #define PUSH_BINDING(stack, x, val) (memcpy((char *)(stack), (char *)(x), sizeof(jmp_buf)), val++)
64 #define RESTORE_BINDING(stack, x, val) (memcpy((char *)(x), (char *)(stack), sizeof(jmp_buf)), val--)
66 static jmp_buf loop_tag; /* always the current binding */
67 static int loop_tag_valid = FALSE; /* nonzero when loop_tag valid */
68 static int func_tag_valid = FALSE;
69 static jmp_buf func_tag;
70 extern int exiting, exit_val;
72 /* This rather ugly macro is for VMS C */
76 #define C(c) ((char)c)
78 * This table is used by the regexp routines to do case independant
79 * matching. Basically, every ascii character maps to itself, except
80 * uppercase letters map to lower case ones. This table has 256
81 * entries, for ISO 8859-1. Note also that if the system this
82 * is compiled on doesn't use 7-bit ascii, casetable[] should not be
83 * defined to the linker, so gawk should not load.
85 * Do NOT make this array static, it is used in several spots, not
88 #if 'a' == 97 /* it's ascii */
90 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
91 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
92 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
93 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
94 /* ' ' '!' '"' '#' '$' '%' '&' ''' */
95 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
96 /* '(' ')' '*' '+' ',' '-' '.' '/' */
97 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
98 /* '0' '1' '2' '3' '4' '5' '6' '7' */
99 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
100 /* '8' '9' ':' ';' '<' '=' '>' '?' */
101 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
102 /* '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' */
103 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
104 /* 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' */
105 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
106 /* 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' */
107 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
108 /* 'X' 'Y' 'Z' '[' '\' ']' '^' '_' */
109 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
110 /* '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' */
111 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
112 /* 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' */
113 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
114 /* 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' */
115 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
116 /* 'x' 'y' 'z' '{' '|' '}' '~' */
117 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
118 #ifndef USE_PURE_ASCII
119 C('\200'), C('\201'), C('\202'), C('\203'), C('\204'), C('\205'), C('\206'), C('\207'),
120 C('\210'), C('\211'), C('\212'), C('\213'), C('\214'), C('\215'), C('\216'), C('\217'),
121 C('\220'), C('\221'), C('\222'), C('\223'), C('\224'), C('\225'), C('\226'), C('\227'),
122 C('\230'), C('\231'), C('\232'), C('\233'), C('\234'), C('\235'), C('\236'), C('\237'),
123 C('\240'), C('\241'), C('\242'), C('\243'), C('\244'), C('\245'), C('\246'), C('\247'),
124 C('\250'), C('\251'), C('\252'), C('\253'), C('\254'), C('\255'), C('\256'), C('\257'),
125 C('\260'), C('\261'), C('\262'), C('\263'), C('\264'), C('\265'), C('\266'), C('\267'),
126 C('\270'), C('\271'), C('\272'), C('\273'), C('\274'), C('\275'), C('\276'), C('\277'),
127 C('\340'), C('\341'), C('\342'), C('\343'), C('\344'), C('\345'), C('\346'), C('\347'),
128 C('\350'), C('\351'), C('\352'), C('\353'), C('\354'), C('\355'), C('\356'), C('\357'),
129 C('\360'), C('\361'), C('\362'), C('\363'), C('\364'), C('\365'), C('\366'), C('\327'),
130 C('\370'), C('\371'), C('\372'), C('\373'), C('\374'), C('\375'), C('\376'), C('\337'),
131 C('\340'), C('\341'), C('\342'), C('\343'), C('\344'), C('\345'), C('\346'), C('\347'),
132 C('\350'), C('\351'), C('\352'), C('\353'), C('\354'), C('\355'), C('\356'), C('\357'),
133 C('\360'), C('\361'), C('\362'), C('\363'), C('\364'), C('\365'), C('\366'), C('\367'),
134 C('\370'), C('\371'), C('\372'), C('\373'), C('\374'), C('\375'), C('\376'), C('\377'),
136 C('\200'), C('\201'), C('\202'), C('\203'), C('\204'), C('\205'), C('\206'), C('\207'),
137 C('\210'), C('\211'), C('\212'), C('\213'), C('\214'), C('\215'), C('\216'), C('\217'),
138 C('\220'), C('\221'), C('\222'), C('\223'), C('\224'), C('\225'), C('\226'), C('\227'),
139 C('\230'), C('\231'), C('\232'), C('\233'), C('\234'), C('\235'), C('\236'), C('\237'),
140 C('\240'), C('\241'), C('\242'), C('\243'), C('\244'), C('\245'), C('\246'), C('\247'),
141 C('\250'), C('\251'), C('\252'), C('\253'), C('\254'), C('\255'), C('\256'), C('\257'),
142 C('\260'), C('\261'), C('\262'), C('\263'), C('\264'), C('\265'), C('\266'), C('\267'),
143 C('\270'), C('\271'), C('\272'), C('\273'), C('\274'), C('\275'), C('\276'), C('\277'),
144 C('\300'), C('\301'), C('\302'), C('\303'), C('\304'), C('\305'), C('\306'), C('\307'),
145 C('\310'), C('\311'), C('\312'), C('\313'), C('\314'), C('\315'), C('\316'), C('\317'),
146 C('\320'), C('\321'), C('\322'), C('\323'), C('\324'), C('\325'), C('\326'), C('\327'),
147 C('\330'), C('\331'), C('\332'), C('\333'), C('\334'), C('\335'), C('\336'), C('\337'),
148 C('\340'), C('\341'), C('\342'), C('\343'), C('\344'), C('\345'), C('\346'), C('\347'),
149 C('\350'), C('\351'), C('\352'), C('\353'), C('\354'), C('\355'), C('\356'), C('\357'),
150 C('\360'), C('\361'), C('\362'), C('\363'), C('\364'), C('\365'), C('\366'), C('\367'),
151 C('\370'), C('\371'), C('\372'), C('\373'), C('\374'), C('\375'), C('\376'), C('\377'),
155 #include "You lose. You will need a translation table for your character set."
161 * This table maps node types to strings for debugging.
162 * KEEP IN SYNC WITH awk.h!!!!
164 static char *nodetypes[] = {
177 "Node_postincrement",
178 "Node_postdecrement",
183 "Node_assign_quotient",
201 "Node_statement_list",
203 "Node_expression_list",
218 "Node_K_delete_loop",
222 "Node_redirect_output",
223 "Node_redirect_append",
224 "Node_redirect_pipe",
225 "Node_redirect_pipein",
226 "Node_redirect_input",
261 if (type >= Node_illegal && type <= Node_final)
262 return nodetypes[(int) type];
264 sprintf(buf, "unknown nodetype %d", (int) type);
268 /* flags2str --- make a flags value readable */
274 static char buffer[BUFSIZ];
279 if (flagval & MALLOC) {
280 strcpy(sp, "MALLOC");
283 if (flagval & TEMP) {
289 if (flagval & PERM) {
295 if (flagval & STRING) {
298 strcpy(sp, "STRING");
313 if (flagval & NUMBER) {
316 strcpy(sp, "NUMBER");
319 if (flagval & MAYBE_NUM) {
322 strcpy(sp, "MAYBE_NUM");
325 if (flagval & ARRAYMAXED) {
328 strcpy(sp, "ARRAYMAXED");
331 if (flagval & SCALAR) {
334 strcpy(sp, "SCALAR");
337 if (flagval & FUNC) {
343 if (flagval & FIELD) {
355 * Tree is a bunch of rules to run. Returns zero if it hit an exit()
360 register NODE *volatile tree;
362 jmp_buf volatile loop_tag_stack; /* shallow binding stack for loop_tag */
363 static jmp_buf rule_tag; /* tag the rule currently being run, for NEXT
364 * and EXIT statements. It is static because
365 * there are no nested rules */
366 register NODE *volatile t = NULL; /* temporary */
367 NODE **volatile lhs; /* lhs == Left Hand Side for assigns, etc */
368 NODE *volatile stable_tree;
369 int volatile traverse = TRUE; /* True => loop thru tree (Node_rule_list) */
371 /* avoid false source indications */
377 sourceline = tree->source_line;
378 source = tree->source_file;
379 switch (tree->type) {
381 traverse = FALSE; /* False => one for-loop iteration only */
384 for (t = tree; t != NULL; t = t->rnode) {
387 sourceline = tree->source_line;
388 source = tree->source_file;
389 switch (setjmp(rule_tag)) {
390 case 0: /* normal non-jump */
391 /* test pattern, if any */
392 if (tree->lnode == NULL ||
393 eval_condition(tree->lnode))
394 (void) interpret(tree->rnode);
396 case TAG_CONTINUE: /* NEXT statement */
403 if (! traverse) /* case Node_rule_node */
404 break; /* don't loop */
408 case Node_statement_list:
409 for (t = tree; t != NULL; t = t->rnode)
410 (void) interpret(t->lnode);
414 if (eval_condition(tree->lnode))
415 (void) interpret(tree->rnode->lnode);
417 (void) interpret(tree->rnode->rnode);
421 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
424 while (eval_condition(stable_tree->lnode)) {
425 switch (setjmp(loop_tag)) {
426 case 0: /* normal non-jump */
427 (void) interpret(stable_tree->rnode);
429 case TAG_CONTINUE: /* continue statement */
431 case TAG_BREAK: /* break statement */
432 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
438 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
442 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
445 switch (setjmp(loop_tag)) {
446 case 0: /* normal non-jump */
447 (void) interpret(stable_tree->rnode);
449 case TAG_CONTINUE: /* continue statement */
451 case TAG_BREAK: /* break statement */
452 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
457 } while (eval_condition(stable_tree->lnode));
458 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
462 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
463 (void) interpret(tree->forloop->init);
465 while (eval_condition(stable_tree->forloop->cond)) {
466 switch (setjmp(loop_tag)) {
467 case 0: /* normal non-jump */
468 (void) interpret(stable_tree->lnode);
470 case TAG_CONTINUE: /* continue statement */
471 (void) interpret(stable_tree->forloop->incr);
473 case TAG_BREAK: /* break statement */
474 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
480 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
483 case Node_K_arrayfor:
485 volatile struct search l; /* For array_for */
486 Func_ptr after_assign = NULL;
488 #define hakvar forloop->init
489 #define arrvar forloop->incr
490 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
491 lhs = get_lhs(tree->hakvar, &after_assign);
493 if (t->type == Node_param_list)
494 t = stack_ptr[t->param_cnt];
495 if (t->type == Node_array_ref)
498 if ((t->flags & SCALAR) != 0)
499 fatal("attempt to use scalar as array");
500 for (assoc_scan(t, (struct search *)&l);
502 assoc_next((struct search *)&l)) {
503 unref(*((NODE **) lhs));
504 *lhs = dupnode(l.retval);
507 switch (setjmp(loop_tag)) {
509 (void) interpret(stable_tree->lnode);
514 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
520 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
525 if (! loop_tag_valid) {
527 * Old AT&T nawk treats break outside of loops like
528 * next. New ones catch it at parse time. Allow it if
529 * do_traditional is on, and complain if lint.
531 static int warned = FALSE;
533 if (do_lint && ! warned) {
534 warning("use of `break' outside a loop is not portable");
537 if (! do_traditional || do_posix)
538 fatal("use of `break' outside a loop is not allowed");
541 longjmp(rule_tag, TAG_CONTINUE);
543 longjmp(loop_tag, TAG_BREAK);
546 case Node_K_continue:
547 if (! loop_tag_valid) {
549 * Old AT&T nawk treats continue outside of loops like
550 * next. New ones catch it at parse time. Allow it if
551 * do_traditional is on, and complain if lint.
553 static int warned = FALSE;
555 if (do_lint && ! warned) {
556 warning("use of `continue' outside a loop is not portable");
559 if (! do_traditional || do_posix)
560 fatal("use of `continue' outside a loop is not allowed");
563 longjmp(rule_tag, TAG_CONTINUE);
565 longjmp(loop_tag, TAG_CONTINUE);
577 do_delete(tree->lnode, tree->rnode);
580 case Node_K_delete_loop:
581 do_delete_loop(tree->lnode, tree->rnode);
586 fatal("`next' cannot be called from a BEGIN rule");
587 else if (in_end_rule)
588 fatal("`next' cannot be called from an END rule");
593 longjmp(rule_tag, TAG_CONTINUE);
596 case Node_K_nextfile:
598 fatal("`nextfile' cannot be called from a BEGIN rule");
599 else if (in_end_rule)
600 fatal("`nextfile' cannot be called from an END rule");
610 * In A,K,&W, p. 49, it says that an exit statement "...
611 * causes the program to behave as if the end of input had
612 * occurred; no more input is read, and the END actions, if
613 * any are executed." This implies that the rest of the rules
614 * are not done. So we immediately break out of the main loop.
617 if (tree->lnode != NULL) {
618 t = tree_eval(tree->lnode);
619 exit_val = (int) force_number(t);
622 longjmp(rule_tag, TAG_BREAK);
626 t = tree_eval(tree->lnode);
627 ret_node = dupnode(t);
629 longjmp(func_tag, TAG_RETURN);
634 * Appears to be an expression statement. Throw away the
637 if (do_lint && tree->type == Node_var)
638 warning("statement has no effect");
646 /* r_tree_eval --- evaluate a subtree */
649 r_tree_eval(tree, iscond)
653 register NODE *r, *t1, *t2; /* return value & temporary subtrees */
666 else if (tree->type == Node_val) {
667 if (tree->stref <= 0)
670 } else if (tree->type == Node_var) {
671 if (tree->var_value->stref <= 0)
673 return tree->var_value;
677 if (tree->type == Node_param_list) {
678 int paramnum = tree->param_cnt + 1;
680 if ((tree->flags & FUNC) != 0)
681 fatal("can't use function name `%s' as variable or array",
684 tree = stack_ptr[tree->param_cnt];
687 sprintf(namebuf, "parameter #%d", paramnum);
688 tree->vname = namebuf;
690 if (tree->type == Node_array_ref)
691 tree = tree->orig_array;
693 switch (tree->type) {
695 return tree->var_value;
698 return tmp_number((AWKNUM) (eval_condition(tree->lnode)
699 && eval_condition(tree->rnode)));
702 return tmp_number((AWKNUM) (eval_condition(tree->lnode)
703 || eval_condition(tree->rnode)));
706 return tmp_number((AWKNUM) ! eval_condition(tree->lnode));
710 return (*tree->proc)(tree->subnode);
713 return (do_getline(tree));
716 return tmp_number((AWKNUM) in_array(tree->lnode, tree->rnode));
719 return func_call(tree->rnode, tree->lnode);
721 /* unary operations */
725 case Node_FIELDWIDTHS:
728 case Node_field_spec:
730 case Node_IGNORECASE:
735 lhs = get_lhs(tree, (Func_ptr *) NULL);
739 fatal("attempt to use array `%s' in a scalar context",
742 case Node_unary_minus:
743 t1 = tree_eval(tree->subnode);
744 x = -force_number(t1);
746 return tmp_number(x);
749 if (eval_condition(tree->lnode))
750 return tree_eval(tree->rnode->lnode);
751 return tree_eval(tree->rnode->rnode);
756 return match_op(tree);
759 fatal("function `%s' called with space between name and (,\n%s",
761 "or used in other expression context");
766 Func_ptr after_assign = NULL;
768 if (iscond && do_lint)
769 warning("assignment used in conditional context");
770 r = tree_eval(tree->rnode);
771 lhs = get_lhs(tree->lnode, &after_assign);
780 tree->lnode->flags |= SCALAR;
791 register NODE **treep;
792 register NODE **strp;
796 int alloc_count, str_count;
800 * This is an efficiency hack for multiple adjacent string
801 * concatenations, to avoid recursion and string copies.
803 * Node_concat trees grow downward to the left, so
804 * descend to lowest (first) node, accumulating nodes
805 * to evaluate to strings as we go.
809 * But first, no arbitrary limits. Count the number of
810 * nodes and malloc the treelist and strlist arrays.
811 * There will be alloc_count + 1 items to concatenate. We
812 * also leave room for an extra pointer at the end to
813 * use as a sentinel. Thus, start alloc_count at 2.
816 for (alloc_count = 2; tree && tree->type == Node_concat; tree = tree->lnode)
819 emalloc(treelist, NODE **, sizeof(NODE *) * alloc_count, "tree_eval");
820 emalloc(strlist, NODE **, sizeof(NODE *) * alloc_count, "tree_eval");
822 /* Now, here we go. */
824 while (tree && tree->type == Node_concat) {
825 *treep++ = tree->rnode;
830 * Now, evaluate to strings in LIFO order, accumulating
831 * the string length, so we can do a single malloc at the
834 * Evaluate the expressions first, then get their
835 * lengthes, in case one of the expressions has a
836 * side effect that changes one of the others.
837 * See test/nasty.awk.
841 while (treep >= treelist) {
842 *strp = force_string(tree_eval(*treep--));
847 str_count = strp - strlist;
849 for (i = 0; i < str_count; i++) {
850 len += (*strp)->stlen;
853 emalloc(str, char *, len+2, "tree_eval");
854 str[len] = str[len+1] = '\0'; /* for good measure */
858 memcpy(dest, (*strp)->stptr, (*strp)->stlen);
859 dest += (*strp)->stlen;
863 r = make_str_node(str, len, ALREADY_MALLOCED);
871 /* other assignment types are easier because they are numeric */
872 case Node_preincrement:
873 case Node_predecrement:
874 case Node_postincrement:
875 case Node_postdecrement:
876 case Node_assign_exp:
877 case Node_assign_times:
878 case Node_assign_quotient:
879 case Node_assign_mod:
880 case Node_assign_plus:
881 case Node_assign_minus:
882 return op_assign(tree);
884 break; /* handled below */
887 /* evaluate subtrees in order to do binary operation, then keep going */
888 t1 = tree_eval(tree->lnode);
889 t2 = tree_eval(tree->rnode);
891 switch (tree->type) {
898 di = cmp_nodes(t1, t2);
901 switch (tree->type) {
903 return tmp_number((AWKNUM) (di == 0));
905 return tmp_number((AWKNUM) (di != 0));
907 return tmp_number((AWKNUM) (di < 0));
909 return tmp_number((AWKNUM) (di > 0));
911 return tmp_number((AWKNUM) (di <= 0));
913 return tmp_number((AWKNUM) (di >= 0));
919 break; /* handled below */
922 x1 = force_number(t1);
924 x2 = force_number(t2);
926 switch (tree->type) {
928 if ((lx = x2) == x2 && lx >= 0) { /* integer exponent */
934 /* doing it this way should be more precise */
939 x = pow((double) x1, (double) x2);
940 return tmp_number(x);
943 return tmp_number(x1 * x2);
947 fatal("division by zero attempted");
949 /* special case for integer division, put in for Cray */
952 return tmp_number(x1 / x2);
953 lx = (long) x1 / lx2;
955 return tmp_number((AWKNUM) lx);
958 return tmp_number(x1 / x2);
962 fatal("division by zero attempted in mod");
964 return tmp_number(fmod(x1, x2));
965 #else /* ! HAVE_FMOD */
966 (void) modf(x1 / x2, &x);
967 return tmp_number(x1 - x * x2);
968 #endif /* ! HAVE_FMOD */
971 return tmp_number(x1 + x2);
974 return tmp_number(x1 - x2);
977 fatal("attempt to use array `%s' in a scalar context",
981 fatal("illegal type (%s) in tree_eval", nodetype2str(tree->type));
986 /* eval_condition --- is TREE true or false? Returns 0==false, non-zero==true */
995 if (tree == NULL) /* Null trees are the easiest kinds */
997 if (tree->type == Node_line_range) {
999 * Node_line_range is kind of like Node_match, EXCEPT: the
1000 * lnode field (more properly, the condpair field) is a node
1001 * of a Node_cond_pair; whether we evaluate the lnode of that
1002 * node or the rnode depends on the triggered word. More
1003 * precisely: if we are not yet triggered, we tree_eval the
1004 * lnode; if that returns true, we set the triggered word.
1005 * If we are triggered (not ELSE IF, note), we tree_eval the
1006 * rnode, clear triggered if it succeeds, and perform our
1007 * action (regardless of success or failure). We want to be
1008 * able to begin and end on a single input record, so this
1009 * isn't an ELSE IF, as noted above.
1011 if (! tree->triggered) {
1012 if (! eval_condition(tree->condpair->lnode))
1015 tree->triggered = TRUE;
1017 /* Else we are triggered */
1018 if (eval_condition(tree->condpair->rnode))
1019 tree->triggered = FALSE;
1024 * Could just be J.random expression. in which case, null and 0 are
1025 * false, anything else is true
1028 t1 = m_tree_eval(tree, TRUE);
1029 if (t1->flags & MAYBE_NUM)
1030 (void) force_number(t1);
1031 if (t1->flags & NUMBER)
1032 ret = (t1->numbr != 0.0);
1034 ret = (t1->stlen != 0);
1039 /* cmp_nodes --- compare two nodes, returning negative, 0, positive */
1043 register NODE *t1, *t2;
1046 register size_t len1, len2;
1052 if (t1->flags & MAYBE_NUM)
1053 (void) force_number(t1);
1054 if (t2->flags & MAYBE_NUM)
1055 (void) force_number(t2);
1056 if ((t1->flags & NUMBER) && (t2->flags & NUMBER)) {
1057 if (t1->numbr == t2->numbr)
1059 /* don't subtract, in case one or both are infinite */
1060 else if (t1->numbr < t2->numbr)
1065 (void) force_string(t1);
1066 (void) force_string(t2);
1069 ldiff = len1 - len2;
1070 if (len1 == 0 || len2 == 0)
1072 l = (ldiff <= 0 ? len1 : len2);
1074 register unsigned char *cp1 = (unsigned char *) t1->stptr;
1075 register unsigned char *cp2 = (unsigned char *) t2->stptr;
1077 for (ret = 0; l-- > 0 && ret == 0; cp1++, cp2++)
1078 ret = casetable[*cp1] - casetable[*cp2];
1080 ret = memcmp(t1->stptr, t2->stptr, l);
1081 return (ret == 0 ? ldiff : ret);
1084 /* op_assign --- do +=, -=, etc. */
1088 register NODE *tree;
1095 Func_ptr after_assign = NULL;
1098 * For ++ and --, get the lhs when doing the op and then
1099 * return. For += etc, do the rhs first, since it can
1100 * rearrange things, and *then* get the lhs.
1103 switch(tree->type) {
1104 case Node_preincrement:
1105 case Node_predecrement:
1106 lhs = get_lhs(tree->lnode, &after_assign);
1107 lval = force_number(*lhs);
1109 *lhs = make_number(lval +
1110 (tree->type == Node_preincrement ? 1.0 : -1.0));
1111 tree->lnode->flags |= SCALAR;
1116 case Node_postincrement:
1117 case Node_postdecrement:
1118 lhs = get_lhs(tree->lnode, &after_assign);
1119 lval = force_number(*lhs);
1121 *lhs = make_number(lval +
1122 (tree->type == Node_postincrement ? 1.0 : -1.0));
1123 tree->lnode->flags |= SCALAR;
1126 return tmp_number(lval);
1128 break; /* handled below */
1132 * It's a += kind of thing. Do the rhs, then the lhs.
1135 tmp = tree_eval(tree->rnode);
1136 rval = force_number(tmp);
1139 lhs = get_lhs(tree->lnode, &after_assign);
1140 lval = force_number(*lhs);
1143 switch(tree->type) {
1144 case Node_assign_exp:
1145 if ((ltemp = rval) == rval) { /* integer exponent */
1147 *lhs = make_number((AWKNUM) 1);
1148 else if (ltemp == 1)
1149 *lhs = make_number(lval);
1151 /* doing it this way should be more precise */
1152 for (t1 = t2 = lval; --ltemp; )
1154 *lhs = make_number(t1);
1157 *lhs = make_number((AWKNUM) pow((double) lval, (double) rval));
1160 case Node_assign_times:
1161 *lhs = make_number(lval * rval);
1164 case Node_assign_quotient:
1165 if (rval == (AWKNUM) 0)
1166 fatal("division by zero attempted in /=");
1168 /* special case for integer division, put in for Cray */
1171 *lhs = make_number(lval / rval);
1174 ltemp = (long) lval / ltemp;
1175 if (ltemp * lval == rval)
1176 *lhs = make_number((AWKNUM) ltemp);
1179 *lhs = make_number(lval / rval);
1182 case Node_assign_mod:
1183 if (rval == (AWKNUM) 0)
1184 fatal("division by zero attempted in %%=");
1186 *lhs = make_number(fmod(lval, rval));
1187 #else /* ! HAVE_FMOD */
1188 (void) modf(lval / rval, &t1);
1189 t2 = lval - rval * t1;
1190 *lhs = make_number(t2);
1191 #endif /* ! HAVE_FMOD */
1194 case Node_assign_plus:
1195 *lhs = make_number(lval + rval);
1198 case Node_assign_minus:
1199 *lhs = make_number(lval - rval);
1204 tree->lnode->flags |= SCALAR;
1210 static struct fcall {
1212 unsigned long count;
1216 } *fcall_list = NULL;
1218 static long fcall_list_size = 0;
1219 static long curfcall = -1;
1221 /* in_function --- return true/false if we need to unwind awk functions */
1226 return (curfcall >= 0);
1229 /* pop_fcall --- pop off a single function call */
1234 NODE *n, **sp, *arg, *argp;
1238 assert(curfcall >= 0);
1239 f = & fcall_list[curfcall];
1240 stack_ptr = f->prevstack;
1243 * here, we pop each parameter and check whether
1244 * it was an array. If so, and if the arg. passed in was
1245 * a simple variable, then the value should be copied back.
1246 * This achieves "call-by-reference" for arrays.
1251 for (argp = f->arglist; count > 0 && argp != NULL; argp = argp->rnode) {
1253 if (arg->type == Node_param_list)
1254 arg = stack_ptr[arg->param_cnt];
1256 if ((arg->type == Node_var /* || arg->type == Node_var_array */)
1257 && n->type == Node_var_array) {
1258 /* should we free arg->var_value ? */
1259 arg->var_array = n->var_array;
1260 arg->type = Node_var_array;
1261 arg->array_size = n->array_size;
1262 arg->table_size = n->table_size;
1263 arg->flags = n->flags;
1265 /* n->lnode overlays the array size, don't unref it if array */
1266 if (n->type != Node_var_array && n->type != Node_array_ref)
1271 while (count-- > 0) {
1273 /* if n is a local array, all the elements should be freed */
1274 if (n->type == Node_var_array)
1276 /* n->lnode overlays the array size, don't unref it if array */
1277 if (n->type != Node_var_array && n->type != Node_array_ref)
1282 free((char *) f->stack);
1283 memset(f, '\0', sizeof(struct fcall));
1287 /* pop_fcall_stack --- pop off all function args, don't leak memory */
1292 while (curfcall >= 0)
1296 /* push_args --- push function arguments onto the stack */
1299 push_args(count, arglist, oldstack, func_name)
1306 NODE *arg, *argp, *r, **sp, *n;
1308 if (fcall_list_size == 0) { /* first time */
1309 emalloc(fcall_list, struct fcall *, 10 * sizeof(struct fcall),
1311 fcall_list_size = 10;
1314 if (++curfcall >= fcall_list_size) {
1315 fcall_list_size *= 2;
1316 erealloc(fcall_list, struct fcall *,
1317 fcall_list_size * sizeof(struct fcall), "push_args");
1319 f = & fcall_list[curfcall];
1320 memset(f, '\0', sizeof(struct fcall));
1323 emalloc(f->stack, NODE **, count*sizeof(NODE *), "func_call");
1325 f->fname = func_name; /* not used, for debugging, just in case */
1326 f->arglist = arglist;
1327 f->prevstack = oldstack;
1331 /* for each calling arg. add NODE * on stack */
1332 for (argp = arglist; count > 0 && argp != NULL; argp = argp->rnode) {
1337 /* call by reference for arrays; see below also */
1338 if (arg->type == Node_param_list) {
1339 /* we must also reassign f here; see below */
1340 f = & fcall_list[curfcall];
1341 arg = f->prevstack[arg->param_cnt];
1343 if (arg->type == Node_var_array) {
1344 r->type = Node_array_ref;
1345 r->flags &= ~SCALAR;
1346 r->orig_array = arg;
1347 r->vname = arg->vname;
1348 } else if (arg->type == Node_array_ref) {
1352 r->lnode = dupnode(n);
1353 r->rnode = (NODE *) NULL;
1354 if ((n->flags & SCALAR) != 0)
1361 if (argp != NULL) /* left over calling args. */
1363 "function `%s' called with more arguments than declared",
1366 /* add remaining params. on stack with null value */
1367 while (count-- > 0) {
1370 r->lnode = Nnull_string;
1371 r->flags &= ~SCALAR;
1372 r->rnode = (NODE *) NULL;
1377 * We have to reassign f. Why, you may ask? It is possible that
1378 * other functions were called during the course of tree_eval()-ing
1379 * the arguments to this function. As a result of that, fcall_list
1380 * may have been realloc()'ed, with the result that f is now
1381 * pointing into free()'d space. This was a nasty one to track down.
1383 f = & fcall_list[curfcall];
1385 stack_ptr = f->stack;
1388 /* func_call --- call a function, call by reference for arrays */
1393 func_call(name, arg_list)
1394 NODE *name; /* name is a Node_val giving function name */
1395 NODE *arg_list; /* Node_expression_list of calling args. */
1399 jmp_buf volatile func_tag_stack;
1400 jmp_buf volatile loop_tag_stack;
1401 int volatile save_loop_tag_valid = FALSE;
1402 NODE *save_ret_node;
1403 extern NODE *ret_node;
1405 /* retrieve function definition node */
1406 f = lookup(name->stptr);
1407 if (f == NULL || f->type != Node_func)
1408 fatal("function `%s' not defined", name->stptr);
1410 fprintf(stderr, "function %s called\n", name->stptr);
1412 push_args(f->lnode->param_cnt, arg_list, stack_ptr, name->stptr);
1415 * Execute function body, saving context, as a return statement
1416 * will longjmp back here.
1418 * Have to save and restore the loop_tag stuff so that a return
1419 * inside a loop in a function body doesn't scrog any loops going
1420 * on in the main program. We save the necessary info in variables
1421 * local to this function so that function nesting works OK.
1422 * We also only bother to save the loop stuff if we're in a loop
1423 * when the function is called.
1425 if (loop_tag_valid) {
1428 save_loop_tag_valid = (volatile int) loop_tag_valid;
1429 PUSH_BINDING(loop_tag_stack, loop_tag, junk);
1430 loop_tag_valid = FALSE;
1432 PUSH_BINDING(func_tag_stack, func_tag, func_tag_valid);
1433 save_ret_node = ret_node;
1434 ret_node = Nnull_string; /* default return value */
1435 if (setjmp(func_tag) == 0)
1436 (void) interpret(f->rnode);
1439 ret_node = (NODE *) save_ret_node;
1440 RESTORE_BINDING(func_tag_stack, func_tag, func_tag_valid);
1443 /* Restore the loop_tag stuff if necessary. */
1444 if (save_loop_tag_valid) {
1447 loop_tag_valid = (int) save_loop_tag_valid;
1448 RESTORE_BINDING(loop_tag_stack, loop_tag, junk);
1451 if ((r->flags & PERM) == 0)
1458 * This returns a POINTER to a node pointer. get_lhs(ptr) is the current
1459 * value of the var, or where to store the var's new value
1461 * For the special variables, don't unref their current value if it's
1462 * the same as the internal copy; perhaps the current one is used in
1463 * a concatenation or some other expression somewhere higher up in the
1468 r_get_lhs(ptr, assign)
1472 register NODE **aptr = NULL;
1476 *assign = NULL; /* for safety */
1477 if (ptr->type == Node_param_list) {
1478 if ((ptr->flags & FUNC) != 0)
1479 fatal("can't use function name `%s' as variable or array", ptr->vname);
1480 ptr = stack_ptr[ptr->param_cnt];
1483 switch (ptr->type) {
1484 case Node_array_ref:
1485 case Node_var_array:
1486 fatal("attempt to use array `%s' in a scalar context",
1490 aptr = &(ptr->var_value);
1492 if (ptr->var_value->stref <= 0)
1497 case Node_FIELDWIDTHS:
1498 aptr = &(FIELDWIDTHS_node->var_value);
1500 *assign = set_FIELDWIDTHS;
1504 aptr = &(RS_node->var_value);
1510 aptr = &(FS_node->var_value);
1516 if (FNR_node->var_value->numbr != FNR) {
1517 unref(FNR_node->var_value);
1518 FNR_node->var_value = make_number((AWKNUM) FNR);
1520 aptr = &(FNR_node->var_value);
1526 if (NR_node->var_value->numbr != NR) {
1527 unref(NR_node->var_value);
1528 NR_node->var_value = make_number((AWKNUM) NR);
1530 aptr = &(NR_node->var_value);
1536 if (NF == -1 || NF_node->var_value->numbr != NF) {
1538 (void) get_field(HUGE-1, assign); /* parse record */
1539 unref(NF_node->var_value);
1540 NF_node->var_value = make_number((AWKNUM) NF);
1542 aptr = &(NF_node->var_value);
1547 case Node_IGNORECASE:
1548 aptr = &(IGNORECASE_node->var_value);
1550 *assign = set_IGNORECASE;
1554 aptr = &(OFMT_node->var_value);
1560 aptr = &(CONVFMT_node->var_value);
1562 *assign = set_CONVFMT;
1566 aptr = &(ORS_node->var_value);
1572 aptr = &(OFS_node->var_value);
1577 case Node_param_list:
1578 aptr = &(stack_ptr[ptr->param_cnt]->var_value);
1581 case Node_field_spec:
1585 n = tree_eval(ptr->lnode);
1586 field_num = (int) force_number(n);
1589 fatal("attempt to access field %d", field_num);
1590 if (field_num == 0 && field0_valid) { /* short circuit */
1591 aptr = &fields_arr[0];
1593 *assign = reset_record;
1596 aptr = get_field(field_num, assign);
1599 case Node_subscript:
1601 if (n->type == Node_param_list) {
1602 int i = n->param_cnt + 1;
1604 n = stack_ptr[n->param_cnt];
1605 if ((n->flags & SCALAR) != 0)
1606 fatal("attempt to use scalar parameter %d as an array", i);
1608 if (n->type == Node_array_ref) {
1610 assert(n->type == Node_var_array || n->type == Node_var);
1612 if (n->type == Node_func) {
1613 fatal("attempt to use function `%s' as array",
1616 aptr = assoc_lookup(n, concat_exp(ptr->rnode));
1620 fatal("`%s' is a function, assignment is not allowed",
1624 fatal("assignment is not allowed to result of builtin function");
1626 fprintf(stderr, "type = %s\n", nodetype2str(ptr->type));
1633 /* match_op --- do ~ and !~ */
1637 register NODE *tree;
1640 register Regexp *rp;
1643 int kludge_need_start = FALSE; /* FIXME: --- see below */
1645 if (tree->type == Node_nomatch)
1647 if (tree->type == Node_regex)
1648 t1 = *get_field(0, (Func_ptr *) 0);
1650 t1 = force_string(tree_eval(tree->lnode));
1653 rp = re_update(tree);
1657 * Any place where research() is called with a last parameter of
1658 * FALSE, we need to use the avoid_dfa test. This is the only place
1661 * A new or improved dfa that distinguishes beginning/end of
1662 * string from beginning/end of line will allow us to get rid of
1663 * this temporary hack.
1665 * The avoid_dfa() function is in re.c; it is not very smart.
1667 if (avoid_dfa(tree, t1->stptr, t1->stlen))
1668 kludge_need_start = TRUE;
1669 i = research(rp, t1->stptr, 0, t1->stlen, kludge_need_start);
1670 i = (i == -1) ^ (match == TRUE);
1672 return tmp_number((AWKNUM) i);
1675 /* set_IGNORECASE --- update IGNORECASE as appropriate */
1680 static int warned = FALSE;
1682 if ((do_lint || do_traditional) && ! warned) {
1684 warning("IGNORECASE not supported in compatibility mode");
1688 else if ((IGNORECASE_node->var_value->flags & (STRING|STR)) != 0) {
1689 if ((IGNORECASE_node->var_value->flags & MAYBE_NUM) == 0)
1690 IGNORECASE = (force_string(IGNORECASE_node->var_value)->stlen > 0);
1692 IGNORECASE = (force_number(IGNORECASE_node->var_value) != 0.0);
1693 } else if ((IGNORECASE_node->var_value->flags & (NUM|NUMBER)) != 0)
1694 IGNORECASE = (force_number(IGNORECASE_node->var_value) != 0.0);
1696 IGNORECASE = FALSE; /* shouldn't happen */
1697 set_FS_if_not_FIELDWIDTHS();
1700 /* set_OFS --- update OFS related variables when OFS assigned to */
1705 OFS = force_string(OFS_node->var_value)->stptr;
1706 OFSlen = OFS_node->var_value->stlen;
1710 /* set_ORS --- update ORS related variables when ORS assigned to */
1715 ORS = force_string(ORS_node->var_value)->stptr;
1716 ORSlen = ORS_node->var_value->stlen;
1720 /* fmt_ok --- is the conversion format a valid one? */
1722 NODE **fmt_list = NULL;
1723 static int fmt_ok P((NODE *n));
1724 static int fmt_index P((NODE *n));
1730 NODE *tmp = force_string(n);
1731 char *p = tmp->stptr;
1735 while (*p && strchr(" +-#", *p) != NULL) /* flags */
1737 while (*p && isdigit(*p)) /* width - %*.*g is NOT allowed */
1739 if (*p == '\0' || (*p != '.' && ! isdigit(*p)))
1743 while (*p && isdigit(*p)) /* precision */
1745 if (*p == '\0' || strchr("efgEG", *p) == NULL)
1752 /* fmt_index --- track values of OFMT and CONVFMT to keep semantics correct */
1758 register int ix = 0;
1759 static int fmt_num = 4;
1760 static int fmt_hiwater = 0;
1762 if (fmt_list == NULL)
1763 emalloc(fmt_list, NODE **, fmt_num*sizeof(*fmt_list), "fmt_index");
1764 (void) force_string(n);
1765 while (ix < fmt_hiwater) {
1766 if (cmp_nodes(fmt_list[ix], n) == 0)
1771 n->stptr[n->stlen] = '\0';
1772 if (do_lint && ! fmt_ok(n))
1773 warning("bad %sFMT specification",
1774 n == CONVFMT_node->var_value ? "CONV"
1775 : n == OFMT_node->var_value ? "O"
1778 if (fmt_hiwater >= fmt_num) {
1780 emalloc(fmt_list, NODE **, fmt_num, "fmt_index");
1782 fmt_list[fmt_hiwater] = dupnode(n);
1783 return fmt_hiwater++;
1786 /* set_OFMT --- track OFMT correctly */
1791 OFMTidx = fmt_index(OFMT_node->var_value);
1792 OFMT = fmt_list[OFMTidx]->stptr;
1795 /* set_CONVFMT --- track CONVFMT correctly */
1800 CONVFMTidx = fmt_index(CONVFMT_node->var_value);
1801 CONVFMT = fmt_list[CONVFMTidx]->stptr;