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 $
30 extern double pow P((double x, double y));
31 extern double modf P((double x, double *yp));
32 extern double fmod P((double x, double y));
34 static int eval_condition P((NODE *tree));
35 static NODE *op_assign P((NODE *tree));
36 static NODE *func_call P((NODE *name, NODE *arg_list));
37 static NODE *match_op P((NODE *tree));
38 static void push_args P((int count, NODE *arglist, NODE **oldstack, char *func_name));
39 static void pop_fcall_stack P((void));
40 static void pop_fcall P((void));
41 static int in_function P((void));
42 char *nodetype2str P((NODETYPE type));
43 char *flags2str P((int flagval));
46 NODE *_t; /* used as a temporary in macros */
49 double _msc51bug; /* to get around a bug in MSC 5.1 */
57 /* Macros and variables to save and restore function and loop bindings */
59 * the val variable allows return/continue/break-out-of-context to be
60 * caught and diagnosed
62 #define PUSH_BINDING(stack, x, val) (memcpy((char *)(stack), (char *)(x), sizeof(jmp_buf)), val++)
63 #define RESTORE_BINDING(stack, x, val) (memcpy((char *)(x), (char *)(stack), sizeof(jmp_buf)), val--)
65 static jmp_buf loop_tag; /* always the current binding */
66 static int loop_tag_valid = FALSE; /* nonzero when loop_tag valid */
67 static int func_tag_valid = FALSE;
68 static jmp_buf func_tag;
69 extern int exiting, exit_val;
71 /* This rather ugly macro is for VMS C */
75 #define C(c) ((char)c)
77 * This table is used by the regexp routines to do case independant
78 * matching. Basically, every ascii character maps to itself, except
79 * uppercase letters map to lower case ones. This table has 256
80 * entries, for ISO 8859-1. Note also that if the system this
81 * is compiled on doesn't use 7-bit ascii, casetable[] should not be
82 * defined to the linker, so gawk should not load.
84 * Do NOT make this array static, it is used in several spots, not
87 #if 'a' == 97 /* it's ascii */
89 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
90 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
91 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
92 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
93 /* ' ' '!' '"' '#' '$' '%' '&' ''' */
94 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
95 /* '(' ')' '*' '+' ',' '-' '.' '/' */
96 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
97 /* '0' '1' '2' '3' '4' '5' '6' '7' */
98 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
99 /* '8' '9' ':' ';' '<' '=' '>' '?' */
100 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
101 /* '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' */
102 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
103 /* 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' */
104 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
105 /* 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' */
106 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
107 /* 'X' 'Y' 'Z' '[' '\' ']' '^' '_' */
108 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
109 /* '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' */
110 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
111 /* 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' */
112 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
113 /* 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' */
114 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
115 /* 'x' 'y' 'z' '{' '|' '}' '~' */
116 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
117 #ifndef USE_PURE_ASCII
118 C('\200'), C('\201'), C('\202'), C('\203'), C('\204'), C('\205'), C('\206'), C('\207'),
119 C('\210'), C('\211'), C('\212'), C('\213'), C('\214'), C('\215'), C('\216'), C('\217'),
120 C('\220'), C('\221'), C('\222'), C('\223'), C('\224'), C('\225'), C('\226'), C('\227'),
121 C('\230'), C('\231'), C('\232'), C('\233'), C('\234'), C('\235'), C('\236'), C('\237'),
122 C('\240'), C('\241'), C('\242'), C('\243'), C('\244'), C('\245'), C('\246'), C('\247'),
123 C('\250'), C('\251'), C('\252'), C('\253'), C('\254'), C('\255'), C('\256'), C('\257'),
124 C('\260'), C('\261'), C('\262'), C('\263'), C('\264'), C('\265'), C('\266'), C('\267'),
125 C('\270'), C('\271'), C('\272'), C('\273'), C('\274'), C('\275'), C('\276'), C('\277'),
126 C('\340'), C('\341'), C('\342'), C('\343'), C('\344'), C('\345'), C('\346'), C('\347'),
127 C('\350'), C('\351'), C('\352'), C('\353'), C('\354'), C('\355'), C('\356'), C('\357'),
128 C('\360'), C('\361'), C('\362'), C('\363'), C('\364'), C('\365'), C('\366'), C('\327'),
129 C('\370'), C('\371'), C('\372'), C('\373'), C('\374'), C('\375'), C('\376'), C('\337'),
130 C('\340'), C('\341'), C('\342'), C('\343'), C('\344'), C('\345'), C('\346'), C('\347'),
131 C('\350'), C('\351'), C('\352'), C('\353'), C('\354'), C('\355'), C('\356'), C('\357'),
132 C('\360'), C('\361'), C('\362'), C('\363'), C('\364'), C('\365'), C('\366'), C('\367'),
133 C('\370'), C('\371'), C('\372'), C('\373'), C('\374'), C('\375'), C('\376'), C('\377'),
135 C('\200'), C('\201'), C('\202'), C('\203'), C('\204'), C('\205'), C('\206'), C('\207'),
136 C('\210'), C('\211'), C('\212'), C('\213'), C('\214'), C('\215'), C('\216'), C('\217'),
137 C('\220'), C('\221'), C('\222'), C('\223'), C('\224'), C('\225'), C('\226'), C('\227'),
138 C('\230'), C('\231'), C('\232'), C('\233'), C('\234'), C('\235'), C('\236'), C('\237'),
139 C('\240'), C('\241'), C('\242'), C('\243'), C('\244'), C('\245'), C('\246'), C('\247'),
140 C('\250'), C('\251'), C('\252'), C('\253'), C('\254'), C('\255'), C('\256'), C('\257'),
141 C('\260'), C('\261'), C('\262'), C('\263'), C('\264'), C('\265'), C('\266'), C('\267'),
142 C('\270'), C('\271'), C('\272'), C('\273'), C('\274'), C('\275'), C('\276'), C('\277'),
143 C('\300'), C('\301'), C('\302'), C('\303'), C('\304'), C('\305'), C('\306'), C('\307'),
144 C('\310'), C('\311'), C('\312'), C('\313'), C('\314'), C('\315'), C('\316'), C('\317'),
145 C('\320'), C('\321'), C('\322'), C('\323'), C('\324'), C('\325'), C('\326'), C('\327'),
146 C('\330'), C('\331'), C('\332'), C('\333'), C('\334'), C('\335'), C('\336'), C('\337'),
147 C('\340'), C('\341'), C('\342'), C('\343'), C('\344'), C('\345'), C('\346'), C('\347'),
148 C('\350'), C('\351'), C('\352'), C('\353'), C('\354'), C('\355'), C('\356'), C('\357'),
149 C('\360'), C('\361'), C('\362'), C('\363'), C('\364'), C('\365'), C('\366'), C('\367'),
150 C('\370'), C('\371'), C('\372'), C('\373'), C('\374'), C('\375'), C('\376'), C('\377'),
154 #include "You lose. You will need a translation table for your character set."
160 * This table maps node types to strings for debugging.
161 * KEEP IN SYNC WITH awk.h!!!!
163 static char *nodetypes[] = {
176 "Node_postincrement",
177 "Node_postdecrement",
182 "Node_assign_quotient",
200 "Node_statement_list",
202 "Node_expression_list",
217 "Node_K_delete_loop",
221 "Node_redirect_output",
222 "Node_redirect_append",
223 "Node_redirect_pipe",
224 "Node_redirect_pipein",
225 "Node_redirect_input",
260 if (type >= Node_illegal && type <= Node_final)
261 return nodetypes[(int) type];
263 sprintf(buf, "unknown nodetype %d", (int) type);
267 /* flags2str --- make a flags value readable */
273 static char buffer[BUFSIZ];
278 if (flagval & MALLOC) {
279 strcpy(sp, "MALLOC");
282 if (flagval & TEMP) {
288 if (flagval & PERM) {
294 if (flagval & STRING) {
297 strcpy(sp, "STRING");
312 if (flagval & NUMBER) {
315 strcpy(sp, "NUMBER");
318 if (flagval & MAYBE_NUM) {
321 strcpy(sp, "MAYBE_NUM");
324 if (flagval & ARRAYMAXED) {
327 strcpy(sp, "ARRAYMAXED");
330 if (flagval & SCALAR) {
333 strcpy(sp, "SCALAR");
336 if (flagval & FUNC) {
342 if (flagval & FIELD) {
354 * Tree is a bunch of rules to run. Returns zero if it hit an exit()
359 register NODE *volatile tree;
361 jmp_buf volatile loop_tag_stack; /* shallow binding stack for loop_tag */
362 static jmp_buf rule_tag; /* tag the rule currently being run, for NEXT
363 * and EXIT statements. It is static because
364 * there are no nested rules */
365 register NODE *volatile t = NULL; /* temporary */
366 NODE **volatile lhs; /* lhs == Left Hand Side for assigns, etc */
367 NODE *volatile stable_tree;
368 int volatile traverse = TRUE; /* True => loop thru tree (Node_rule_list) */
370 /* avoid false source indications */
376 sourceline = tree->source_line;
377 source = tree->source_file;
378 switch (tree->type) {
380 traverse = FALSE; /* False => one for-loop iteration only */
383 for (t = tree; t != NULL; t = t->rnode) {
386 sourceline = tree->source_line;
387 source = tree->source_file;
388 switch (setjmp(rule_tag)) {
389 case 0: /* normal non-jump */
390 /* test pattern, if any */
391 if (tree->lnode == NULL ||
392 eval_condition(tree->lnode))
393 (void) interpret(tree->rnode);
395 case TAG_CONTINUE: /* NEXT statement */
402 if (! traverse) /* case Node_rule_node */
403 break; /* don't loop */
407 case Node_statement_list:
408 for (t = tree; t != NULL; t = t->rnode)
409 (void) interpret(t->lnode);
413 if (eval_condition(tree->lnode))
414 (void) interpret(tree->rnode->lnode);
416 (void) interpret(tree->rnode->rnode);
420 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
423 while (eval_condition(stable_tree->lnode)) {
424 switch (setjmp(loop_tag)) {
425 case 0: /* normal non-jump */
426 (void) interpret(stable_tree->rnode);
428 case TAG_CONTINUE: /* continue statement */
430 case TAG_BREAK: /* break statement */
431 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
437 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
441 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
444 switch (setjmp(loop_tag)) {
445 case 0: /* normal non-jump */
446 (void) interpret(stable_tree->rnode);
448 case TAG_CONTINUE: /* continue statement */
450 case TAG_BREAK: /* break statement */
451 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
456 } while (eval_condition(stable_tree->lnode));
457 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
461 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
462 (void) interpret(tree->forloop->init);
464 while (eval_condition(stable_tree->forloop->cond)) {
465 switch (setjmp(loop_tag)) {
466 case 0: /* normal non-jump */
467 (void) interpret(stable_tree->lnode);
469 case TAG_CONTINUE: /* continue statement */
470 (void) interpret(stable_tree->forloop->incr);
472 case TAG_BREAK: /* break statement */
473 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
479 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
482 case Node_K_arrayfor:
484 volatile struct search l; /* For array_for */
485 Func_ptr after_assign = NULL;
487 #define hakvar forloop->init
488 #define arrvar forloop->incr
489 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
490 lhs = get_lhs(tree->hakvar, &after_assign);
492 if (t->type == Node_param_list)
493 t = stack_ptr[t->param_cnt];
494 if (t->type == Node_array_ref)
497 if ((t->flags & SCALAR) != 0)
498 fatal("attempt to use scalar as array");
499 for (assoc_scan(t, (struct search *)&l);
501 assoc_next((struct search *)&l)) {
502 unref(*((NODE **) lhs));
503 *lhs = dupnode(l.retval);
506 switch (setjmp(loop_tag)) {
508 (void) interpret(stable_tree->lnode);
513 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
519 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
524 if (! loop_tag_valid) {
526 * Old AT&T nawk treats break outside of loops like
527 * next. New ones catch it at parse time. Allow it if
528 * do_traditional is on, and complain if lint.
530 static int warned = FALSE;
532 if (do_lint && ! warned) {
533 warning("use of `break' outside a loop is not portable");
536 if (! do_traditional || do_posix)
537 fatal("use of `break' outside a loop is not allowed");
540 longjmp(rule_tag, TAG_CONTINUE);
542 longjmp(loop_tag, TAG_BREAK);
545 case Node_K_continue:
546 if (! loop_tag_valid) {
548 * Old AT&T nawk treats continue outside of loops like
549 * next. New ones catch it at parse time. Allow it if
550 * do_traditional is on, and complain if lint.
552 static int warned = FALSE;
554 if (do_lint && ! warned) {
555 warning("use of `continue' outside a loop is not portable");
558 if (! do_traditional || do_posix)
559 fatal("use of `continue' outside a loop is not allowed");
562 longjmp(rule_tag, TAG_CONTINUE);
564 longjmp(loop_tag, TAG_CONTINUE);
576 do_delete(tree->lnode, tree->rnode);
579 case Node_K_delete_loop:
580 do_delete_loop(tree->lnode, tree->rnode);
585 fatal("`next' cannot be called from a BEGIN rule");
586 else if (in_end_rule)
587 fatal("`next' cannot be called from an END rule");
592 longjmp(rule_tag, TAG_CONTINUE);
595 case Node_K_nextfile:
597 fatal("`nextfile' cannot be called from a BEGIN rule");
598 else if (in_end_rule)
599 fatal("`nextfile' cannot be called from an END rule");
609 * In A,K,&W, p. 49, it says that an exit statement "...
610 * causes the program to behave as if the end of input had
611 * occurred; no more input is read, and the END actions, if
612 * any are executed." This implies that the rest of the rules
613 * are not done. So we immediately break out of the main loop.
616 if (tree->lnode != NULL) {
617 t = tree_eval(tree->lnode);
618 exit_val = (int) force_number(t);
621 longjmp(rule_tag, TAG_BREAK);
625 t = tree_eval(tree->lnode);
626 ret_node = dupnode(t);
628 longjmp(func_tag, TAG_RETURN);
633 * Appears to be an expression statement. Throw away the
636 if (do_lint && tree->type == Node_var)
637 warning("statement has no effect");
645 /* r_tree_eval --- evaluate a subtree */
648 r_tree_eval(tree, iscond)
652 register NODE *r, *t1, *t2; /* return value & temporary subtrees */
665 else if (tree->type == Node_val) {
666 if (tree->stref <= 0)
669 } else if (tree->type == Node_var) {
670 if (tree->var_value->stref <= 0)
672 return tree->var_value;
676 if (tree->type == Node_param_list) {
677 int paramnum = tree->param_cnt + 1;
679 if ((tree->flags & FUNC) != 0)
680 fatal("can't use function name `%s' as variable or array",
683 tree = stack_ptr[tree->param_cnt];
686 sprintf(namebuf, "parameter #%d", paramnum);
687 tree->vname = namebuf;
689 if (tree->type == Node_array_ref)
690 tree = tree->orig_array;
692 switch (tree->type) {
694 return tree->var_value;
697 return tmp_number((AWKNUM) (eval_condition(tree->lnode)
698 && eval_condition(tree->rnode)));
701 return tmp_number((AWKNUM) (eval_condition(tree->lnode)
702 || eval_condition(tree->rnode)));
705 return tmp_number((AWKNUM) ! eval_condition(tree->lnode));
709 return (*tree->proc)(tree->subnode);
712 return (do_getline(tree));
715 return tmp_number((AWKNUM) in_array(tree->lnode, tree->rnode));
718 return func_call(tree->rnode, tree->lnode);
720 /* unary operations */
724 case Node_FIELDWIDTHS:
727 case Node_field_spec:
729 case Node_IGNORECASE:
734 lhs = get_lhs(tree, (Func_ptr *) NULL);
738 fatal("attempt to use array `%s' in a scalar context",
741 case Node_unary_minus:
742 t1 = tree_eval(tree->subnode);
743 x = -force_number(t1);
745 return tmp_number(x);
748 if (eval_condition(tree->lnode))
749 return tree_eval(tree->rnode->lnode);
750 return tree_eval(tree->rnode->rnode);
755 return match_op(tree);
758 fatal("function `%s' called with space between name and (,\n%s",
760 "or used in other expression context");
765 Func_ptr after_assign = NULL;
767 if (iscond && do_lint)
768 warning("assignment used in conditional context");
769 r = tree_eval(tree->rnode);
770 lhs = get_lhs(tree->lnode, &after_assign);
779 tree->lnode->flags |= SCALAR;
790 register NODE **treep;
791 register NODE **strp;
795 int alloc_count, str_count;
799 * This is an efficiency hack for multiple adjacent string
800 * concatenations, to avoid recursion and string copies.
802 * Node_concat trees grow downward to the left, so
803 * descend to lowest (first) node, accumulating nodes
804 * to evaluate to strings as we go.
808 * But first, no arbitrary limits. Count the number of
809 * nodes and malloc the treelist and strlist arrays.
810 * There will be alloc_count + 1 items to concatenate. We
811 * also leave room for an extra pointer at the end to
812 * use as a sentinel. Thus, start alloc_count at 2.
815 for (alloc_count = 2; tree && tree->type == Node_concat; tree = tree->lnode)
818 emalloc(treelist, NODE **, sizeof(NODE *) * alloc_count, "tree_eval");
819 emalloc(strlist, NODE **, sizeof(NODE *) * alloc_count, "tree_eval");
821 /* Now, here we go. */
823 while (tree && tree->type == Node_concat) {
824 *treep++ = tree->rnode;
829 * Now, evaluate to strings in LIFO order, accumulating
830 * the string length, so we can do a single malloc at the
833 * Evaluate the expressions first, then get their
834 * lengthes, in case one of the expressions has a
835 * side effect that changes one of the others.
836 * See test/nasty.awk.
840 while (treep >= treelist) {
841 *strp = force_string(tree_eval(*treep--));
846 str_count = strp - strlist;
848 for (i = 0; i < str_count; i++) {
849 len += (*strp)->stlen;
852 emalloc(str, char *, len+2, "tree_eval");
853 str[len] = str[len+1] = '\0'; /* for good measure */
857 memcpy(dest, (*strp)->stptr, (*strp)->stlen);
858 dest += (*strp)->stlen;
862 r = make_str_node(str, len, ALREADY_MALLOCED);
870 /* other assignment types are easier because they are numeric */
871 case Node_preincrement:
872 case Node_predecrement:
873 case Node_postincrement:
874 case Node_postdecrement:
875 case Node_assign_exp:
876 case Node_assign_times:
877 case Node_assign_quotient:
878 case Node_assign_mod:
879 case Node_assign_plus:
880 case Node_assign_minus:
881 return op_assign(tree);
883 break; /* handled below */
886 /* evaluate subtrees in order to do binary operation, then keep going */
887 t1 = tree_eval(tree->lnode);
888 t2 = tree_eval(tree->rnode);
890 switch (tree->type) {
897 di = cmp_nodes(t1, t2);
900 switch (tree->type) {
902 return tmp_number((AWKNUM) (di == 0));
904 return tmp_number((AWKNUM) (di != 0));
906 return tmp_number((AWKNUM) (di < 0));
908 return tmp_number((AWKNUM) (di > 0));
910 return tmp_number((AWKNUM) (di <= 0));
912 return tmp_number((AWKNUM) (di >= 0));
918 break; /* handled below */
921 x1 = force_number(t1);
923 x2 = force_number(t2);
925 switch (tree->type) {
927 if ((lx = x2) == x2 && lx >= 0) { /* integer exponent */
933 /* doing it this way should be more precise */
938 x = pow((double) x1, (double) x2);
939 return tmp_number(x);
942 return tmp_number(x1 * x2);
946 fatal("division by zero attempted");
948 /* special case for integer division, put in for Cray */
951 return tmp_number(x1 / x2);
952 lx = (long) x1 / lx2;
954 return tmp_number((AWKNUM) lx);
957 return tmp_number(x1 / x2);
961 fatal("division by zero attempted in mod");
963 return tmp_number(fmod(x1, x2));
964 #else /* ! HAVE_FMOD */
965 (void) modf(x1 / x2, &x);
966 return tmp_number(x1 - x * x2);
967 #endif /* ! HAVE_FMOD */
970 return tmp_number(x1 + x2);
973 return tmp_number(x1 - x2);
976 fatal("attempt to use array `%s' in a scalar context",
980 fatal("illegal type (%s) in tree_eval", nodetype2str(tree->type));
985 /* eval_condition --- is TREE true or false? Returns 0==false, non-zero==true */
994 if (tree == NULL) /* Null trees are the easiest kinds */
996 if (tree->type == Node_line_range) {
998 * Node_line_range is kind of like Node_match, EXCEPT: the
999 * lnode field (more properly, the condpair field) is a node
1000 * of a Node_cond_pair; whether we evaluate the lnode of that
1001 * node or the rnode depends on the triggered word. More
1002 * precisely: if we are not yet triggered, we tree_eval the
1003 * lnode; if that returns true, we set the triggered word.
1004 * If we are triggered (not ELSE IF, note), we tree_eval the
1005 * rnode, clear triggered if it succeeds, and perform our
1006 * action (regardless of success or failure). We want to be
1007 * able to begin and end on a single input record, so this
1008 * isn't an ELSE IF, as noted above.
1010 if (! tree->triggered) {
1011 if (! eval_condition(tree->condpair->lnode))
1014 tree->triggered = TRUE;
1016 /* Else we are triggered */
1017 if (eval_condition(tree->condpair->rnode))
1018 tree->triggered = FALSE;
1023 * Could just be J.random expression. in which case, null and 0 are
1024 * false, anything else is true
1027 t1 = m_tree_eval(tree, TRUE);
1028 if (t1->flags & MAYBE_NUM)
1029 (void) force_number(t1);
1030 if (t1->flags & NUMBER)
1031 ret = (t1->numbr != 0.0);
1033 ret = (t1->stlen != 0);
1038 /* cmp_nodes --- compare two nodes, returning negative, 0, positive */
1042 register NODE *t1, *t2;
1045 register size_t len1, len2;
1051 if (t1->flags & MAYBE_NUM)
1052 (void) force_number(t1);
1053 if (t2->flags & MAYBE_NUM)
1054 (void) force_number(t2);
1055 if ((t1->flags & NUMBER) && (t2->flags & NUMBER)) {
1056 if (t1->numbr == t2->numbr)
1058 /* don't subtract, in case one or both are infinite */
1059 else if (t1->numbr < t2->numbr)
1064 (void) force_string(t1);
1065 (void) force_string(t2);
1068 ldiff = len1 - len2;
1069 if (len1 == 0 || len2 == 0)
1071 l = (ldiff <= 0 ? len1 : len2);
1073 register unsigned char *cp1 = (unsigned char *) t1->stptr;
1074 register unsigned char *cp2 = (unsigned char *) t2->stptr;
1076 for (ret = 0; l-- > 0 && ret == 0; cp1++, cp2++)
1077 ret = casetable[*cp1] - casetable[*cp2];
1079 ret = memcmp(t1->stptr, t2->stptr, l);
1080 return (ret == 0 ? ldiff : ret);
1083 /* op_assign --- do +=, -=, etc. */
1087 register NODE *tree;
1094 Func_ptr after_assign = NULL;
1097 * For ++ and --, get the lhs when doing the op and then
1098 * return. For += etc, do the rhs first, since it can
1099 * rearrange things, and *then* get the lhs.
1102 switch(tree->type) {
1103 case Node_preincrement:
1104 case Node_predecrement:
1105 lhs = get_lhs(tree->lnode, &after_assign);
1106 lval = force_number(*lhs);
1108 *lhs = make_number(lval +
1109 (tree->type == Node_preincrement ? 1.0 : -1.0));
1110 tree->lnode->flags |= SCALAR;
1115 case Node_postincrement:
1116 case Node_postdecrement:
1117 lhs = get_lhs(tree->lnode, &after_assign);
1118 lval = force_number(*lhs);
1120 *lhs = make_number(lval +
1121 (tree->type == Node_postincrement ? 1.0 : -1.0));
1122 tree->lnode->flags |= SCALAR;
1125 return tmp_number(lval);
1127 break; /* handled below */
1131 * It's a += kind of thing. Do the rhs, then the lhs.
1134 tmp = tree_eval(tree->rnode);
1135 rval = force_number(tmp);
1138 lhs = get_lhs(tree->lnode, &after_assign);
1139 lval = force_number(*lhs);
1142 switch(tree->type) {
1143 case Node_assign_exp:
1144 if ((ltemp = rval) == rval) { /* integer exponent */
1146 *lhs = make_number((AWKNUM) 1);
1147 else if (ltemp == 1)
1148 *lhs = make_number(lval);
1150 /* doing it this way should be more precise */
1151 for (t1 = t2 = lval; --ltemp; )
1153 *lhs = make_number(t1);
1156 *lhs = make_number((AWKNUM) pow((double) lval, (double) rval));
1159 case Node_assign_times:
1160 *lhs = make_number(lval * rval);
1163 case Node_assign_quotient:
1164 if (rval == (AWKNUM) 0)
1165 fatal("division by zero attempted in /=");
1167 /* special case for integer division, put in for Cray */
1170 *lhs = make_number(lval / rval);
1173 ltemp = (long) lval / ltemp;
1174 if (ltemp * lval == rval)
1175 *lhs = make_number((AWKNUM) ltemp);
1178 *lhs = make_number(lval / rval);
1181 case Node_assign_mod:
1182 if (rval == (AWKNUM) 0)
1183 fatal("division by zero attempted in %%=");
1185 *lhs = make_number(fmod(lval, rval));
1186 #else /* ! HAVE_FMOD */
1187 (void) modf(lval / rval, &t1);
1188 t2 = lval - rval * t1;
1189 *lhs = make_number(t2);
1190 #endif /* ! HAVE_FMOD */
1193 case Node_assign_plus:
1194 *lhs = make_number(lval + rval);
1197 case Node_assign_minus:
1198 *lhs = make_number(lval - rval);
1203 tree->lnode->flags |= SCALAR;
1209 static struct fcall {
1211 unsigned long count;
1215 } *fcall_list = NULL;
1217 static long fcall_list_size = 0;
1218 static long curfcall = -1;
1220 /* in_function --- return true/false if we need to unwind awk functions */
1225 return (curfcall >= 0);
1228 /* pop_fcall --- pop off a single function call */
1233 NODE *n, **sp, *arg, *argp;
1237 assert(curfcall >= 0);
1238 f = & fcall_list[curfcall];
1239 stack_ptr = f->prevstack;
1242 * here, we pop each parameter and check whether
1243 * it was an array. If so, and if the arg. passed in was
1244 * a simple variable, then the value should be copied back.
1245 * This achieves "call-by-reference" for arrays.
1250 for (argp = f->arglist; count > 0 && argp != NULL; argp = argp->rnode) {
1252 if (arg->type == Node_param_list)
1253 arg = stack_ptr[arg->param_cnt];
1255 if ((arg->type == Node_var /* || arg->type == Node_var_array */)
1256 && n->type == Node_var_array) {
1257 /* should we free arg->var_value ? */
1258 arg->var_array = n->var_array;
1259 arg->type = Node_var_array;
1260 arg->array_size = n->array_size;
1261 arg->table_size = n->table_size;
1262 arg->flags = n->flags;
1264 /* n->lnode overlays the array size, don't unref it if array */
1265 if (n->type != Node_var_array && n->type != Node_array_ref)
1270 while (count-- > 0) {
1272 /* if n is a local array, all the elements should be freed */
1273 if (n->type == Node_var_array)
1275 /* n->lnode overlays the array size, don't unref it if array */
1276 if (n->type != Node_var_array && n->type != Node_array_ref)
1281 free((char *) f->stack);
1282 memset(f, '\0', sizeof(struct fcall));
1286 /* pop_fcall_stack --- pop off all function args, don't leak memory */
1291 while (curfcall >= 0)
1295 /* push_args --- push function arguments onto the stack */
1298 push_args(count, arglist, oldstack, func_name)
1305 NODE *arg, *argp, *r, **sp, *n;
1307 if (fcall_list_size == 0) { /* first time */
1308 emalloc(fcall_list, struct fcall *, 10 * sizeof(struct fcall),
1310 fcall_list_size = 10;
1313 if (++curfcall >= fcall_list_size) {
1314 fcall_list_size *= 2;
1315 erealloc(fcall_list, struct fcall *,
1316 fcall_list_size * sizeof(struct fcall), "push_args");
1318 f = & fcall_list[curfcall];
1319 memset(f, '\0', sizeof(struct fcall));
1322 emalloc(f->stack, NODE **, count*sizeof(NODE *), "func_call");
1324 f->fname = func_name; /* not used, for debugging, just in case */
1325 f->arglist = arglist;
1326 f->prevstack = oldstack;
1330 /* for each calling arg. add NODE * on stack */
1331 for (argp = arglist; count > 0 && argp != NULL; argp = argp->rnode) {
1336 /* call by reference for arrays; see below also */
1337 if (arg->type == Node_param_list) {
1338 /* we must also reassign f here; see below */
1339 f = & fcall_list[curfcall];
1340 arg = f->prevstack[arg->param_cnt];
1342 if (arg->type == Node_var_array) {
1343 r->type = Node_array_ref;
1344 r->flags &= ~SCALAR;
1345 r->orig_array = arg;
1346 r->vname = arg->vname;
1347 } else if (arg->type == Node_array_ref) {
1351 r->lnode = dupnode(n);
1352 r->rnode = (NODE *) NULL;
1353 if ((n->flags & SCALAR) != 0)
1360 if (argp != NULL) /* left over calling args. */
1362 "function `%s' called with more arguments than declared",
1365 /* add remaining params. on stack with null value */
1366 while (count-- > 0) {
1369 r->lnode = Nnull_string;
1370 r->flags &= ~SCALAR;
1371 r->rnode = (NODE *) NULL;
1376 * We have to reassign f. Why, you may ask? It is possible that
1377 * other functions were called during the course of tree_eval()-ing
1378 * the arguments to this function. As a result of that, fcall_list
1379 * may have been realloc()'ed, with the result that f is now
1380 * pointing into free()'d space. This was a nasty one to track down.
1382 f = & fcall_list[curfcall];
1384 stack_ptr = f->stack;
1387 /* func_call --- call a function, call by reference for arrays */
1392 func_call(name, arg_list)
1393 NODE *name; /* name is a Node_val giving function name */
1394 NODE *arg_list; /* Node_expression_list of calling args. */
1398 jmp_buf volatile func_tag_stack;
1399 jmp_buf volatile loop_tag_stack;
1400 int volatile save_loop_tag_valid = FALSE;
1401 NODE *save_ret_node;
1402 extern NODE *ret_node;
1404 /* retrieve function definition node */
1405 f = lookup(name->stptr);
1406 if (f == NULL || f->type != Node_func)
1407 fatal("function `%s' not defined", name->stptr);
1409 fprintf(stderr, "function %s called\n", name->stptr);
1411 push_args(f->lnode->param_cnt, arg_list, stack_ptr, name->stptr);
1414 * Execute function body, saving context, as a return statement
1415 * will longjmp back here.
1417 * Have to save and restore the loop_tag stuff so that a return
1418 * inside a loop in a function body doesn't scrog any loops going
1419 * on in the main program. We save the necessary info in variables
1420 * local to this function so that function nesting works OK.
1421 * We also only bother to save the loop stuff if we're in a loop
1422 * when the function is called.
1424 if (loop_tag_valid) {
1427 save_loop_tag_valid = (volatile int) loop_tag_valid;
1428 PUSH_BINDING(loop_tag_stack, loop_tag, junk);
1429 loop_tag_valid = FALSE;
1431 PUSH_BINDING(func_tag_stack, func_tag, func_tag_valid);
1432 save_ret_node = ret_node;
1433 ret_node = Nnull_string; /* default return value */
1434 if (setjmp(func_tag) == 0)
1435 (void) interpret(f->rnode);
1438 ret_node = (NODE *) save_ret_node;
1439 RESTORE_BINDING(func_tag_stack, func_tag, func_tag_valid);
1442 /* Restore the loop_tag stuff if necessary. */
1443 if (save_loop_tag_valid) {
1446 loop_tag_valid = (int) save_loop_tag_valid;
1447 RESTORE_BINDING(loop_tag_stack, loop_tag, junk);
1450 if ((r->flags & PERM) == 0)
1457 * This returns a POINTER to a node pointer. get_lhs(ptr) is the current
1458 * value of the var, or where to store the var's new value
1460 * For the special variables, don't unref their current value if it's
1461 * the same as the internal copy; perhaps the current one is used in
1462 * a concatenation or some other expression somewhere higher up in the
1467 r_get_lhs(ptr, assign)
1471 register NODE **aptr = NULL;
1475 *assign = NULL; /* for safety */
1476 if (ptr->type == Node_param_list) {
1477 if ((ptr->flags & FUNC) != 0)
1478 fatal("can't use function name `%s' as variable or array", ptr->vname);
1479 ptr = stack_ptr[ptr->param_cnt];
1482 switch (ptr->type) {
1483 case Node_array_ref:
1484 case Node_var_array:
1485 fatal("attempt to use array `%s' in a scalar context",
1489 aptr = &(ptr->var_value);
1491 if (ptr->var_value->stref <= 0)
1496 case Node_FIELDWIDTHS:
1497 aptr = &(FIELDWIDTHS_node->var_value);
1499 *assign = set_FIELDWIDTHS;
1503 aptr = &(RS_node->var_value);
1509 aptr = &(FS_node->var_value);
1515 if (FNR_node->var_value->numbr != FNR) {
1516 unref(FNR_node->var_value);
1517 FNR_node->var_value = make_number((AWKNUM) FNR);
1519 aptr = &(FNR_node->var_value);
1525 if (NR_node->var_value->numbr != NR) {
1526 unref(NR_node->var_value);
1527 NR_node->var_value = make_number((AWKNUM) NR);
1529 aptr = &(NR_node->var_value);
1535 if (NF == -1 || NF_node->var_value->numbr != NF) {
1537 (void) get_field(HUGE-1, assign); /* parse record */
1538 unref(NF_node->var_value);
1539 NF_node->var_value = make_number((AWKNUM) NF);
1541 aptr = &(NF_node->var_value);
1546 case Node_IGNORECASE:
1547 aptr = &(IGNORECASE_node->var_value);
1549 *assign = set_IGNORECASE;
1553 aptr = &(OFMT_node->var_value);
1559 aptr = &(CONVFMT_node->var_value);
1561 *assign = set_CONVFMT;
1565 aptr = &(ORS_node->var_value);
1571 aptr = &(OFS_node->var_value);
1576 case Node_param_list:
1577 aptr = &(stack_ptr[ptr->param_cnt]->var_value);
1580 case Node_field_spec:
1584 n = tree_eval(ptr->lnode);
1585 field_num = (int) force_number(n);
1588 fatal("attempt to access field %d", field_num);
1589 if (field_num == 0 && field0_valid) { /* short circuit */
1590 aptr = &fields_arr[0];
1592 *assign = reset_record;
1595 aptr = get_field(field_num, assign);
1598 case Node_subscript:
1600 if (n->type == Node_param_list) {
1601 int i = n->param_cnt + 1;
1603 n = stack_ptr[n->param_cnt];
1604 if ((n->flags & SCALAR) != 0)
1605 fatal("attempt to use scalar parameter %d as an array", i);
1607 if (n->type == Node_array_ref) {
1609 assert(n->type == Node_var_array || n->type == Node_var);
1611 if (n->type == Node_func) {
1612 fatal("attempt to use function `%s' as array",
1615 aptr = assoc_lookup(n, concat_exp(ptr->rnode));
1619 fatal("`%s' is a function, assignment is not allowed",
1623 fatal("assignment is not allowed to result of builtin function");
1625 fprintf(stderr, "type = %s\n", nodetype2str(ptr->type));
1632 /* match_op --- do ~ and !~ */
1636 register NODE *tree;
1639 register Regexp *rp;
1642 int kludge_need_start = FALSE; /* FIXME: --- see below */
1644 if (tree->type == Node_nomatch)
1646 if (tree->type == Node_regex)
1647 t1 = *get_field(0, (Func_ptr *) 0);
1649 t1 = force_string(tree_eval(tree->lnode));
1652 rp = re_update(tree);
1656 * Any place where research() is called with a last parameter of
1657 * FALSE, we need to use the avoid_dfa test. This is the only place
1660 * A new or improved dfa that distinguishes beginning/end of
1661 * string from beginning/end of line will allow us to get rid of
1662 * this temporary hack.
1664 * The avoid_dfa() function is in re.c; it is not very smart.
1666 if (avoid_dfa(tree, t1->stptr, t1->stlen))
1667 kludge_need_start = TRUE;
1668 i = research(rp, t1->stptr, 0, t1->stlen, kludge_need_start);
1669 i = (i == -1) ^ (match == TRUE);
1671 return tmp_number((AWKNUM) i);
1674 /* set_IGNORECASE --- update IGNORECASE as appropriate */
1679 static int warned = FALSE;
1681 if ((do_lint || do_traditional) && ! warned) {
1683 warning("IGNORECASE not supported in compatibility mode");
1687 else if ((IGNORECASE_node->var_value->flags & (STRING|STR)) != 0) {
1688 if ((IGNORECASE_node->var_value->flags & MAYBE_NUM) == 0)
1689 IGNORECASE = (force_string(IGNORECASE_node->var_value)->stlen > 0);
1691 IGNORECASE = (force_number(IGNORECASE_node->var_value) != 0.0);
1692 } else if ((IGNORECASE_node->var_value->flags & (NUM|NUMBER)) != 0)
1693 IGNORECASE = (force_number(IGNORECASE_node->var_value) != 0.0);
1695 IGNORECASE = FALSE; /* shouldn't happen */
1696 set_FS_if_not_FIELDWIDTHS();
1699 /* set_OFS --- update OFS related variables when OFS assigned to */
1704 OFS = force_string(OFS_node->var_value)->stptr;
1705 OFSlen = OFS_node->var_value->stlen;
1709 /* set_ORS --- update ORS related variables when ORS assigned to */
1714 ORS = force_string(ORS_node->var_value)->stptr;
1715 ORSlen = ORS_node->var_value->stlen;
1719 /* fmt_ok --- is the conversion format a valid one? */
1721 NODE **fmt_list = NULL;
1722 static int fmt_ok P((NODE *n));
1723 static int fmt_index P((NODE *n));
1729 NODE *tmp = force_string(n);
1730 char *p = tmp->stptr;
1734 while (*p && strchr(" +-#", *p) != NULL) /* flags */
1736 while (*p && isdigit(*p)) /* width - %*.*g is NOT allowed */
1738 if (*p == '\0' || (*p != '.' && ! isdigit(*p)))
1742 while (*p && isdigit(*p)) /* precision */
1744 if (*p == '\0' || strchr("efgEG", *p) == NULL)
1751 /* fmt_index --- track values of OFMT and CONVFMT to keep semantics correct */
1757 register int ix = 0;
1758 static int fmt_num = 4;
1759 static int fmt_hiwater = 0;
1761 if (fmt_list == NULL)
1762 emalloc(fmt_list, NODE **, fmt_num*sizeof(*fmt_list), "fmt_index");
1763 (void) force_string(n);
1764 while (ix < fmt_hiwater) {
1765 if (cmp_nodes(fmt_list[ix], n) == 0)
1770 n->stptr[n->stlen] = '\0';
1771 if (do_lint && ! fmt_ok(n))
1772 warning("bad %sFMT specification",
1773 n == CONVFMT_node->var_value ? "CONV"
1774 : n == OFMT_node->var_value ? "O"
1777 if (fmt_hiwater >= fmt_num) {
1779 emalloc(fmt_list, NODE **, fmt_num, "fmt_index");
1781 fmt_list[fmt_hiwater] = dupnode(n);
1782 return fmt_hiwater++;
1785 /* set_OFMT --- track OFMT correctly */
1790 OFMTidx = fmt_index(OFMT_node->var_value);
1791 OFMT = fmt_list[OFMTidx]->stptr;
1794 /* set_CONVFMT --- track CONVFMT correctly */
1799 CONVFMTidx = fmt_index(CONVFMT_node->var_value);
1800 CONVFMT = fmt_list[CONVFMTidx]->stptr;