2 * eval.c - gawk parse tree interpreter
6 * Copyright (C) 1986, 1988, 1989, 1991-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28 extern double pow P((double x, double y));
29 extern double modf P((double x, double *yp));
30 extern double fmod P((double x, double y));
32 static inline void make_scalar P((NODE *tree));
33 static int eval_condition P((NODE *tree));
34 static NODE *op_assign P((NODE *tree));
35 static NODE *func_call P((NODE *tree));
36 static NODE *match_op P((NODE *tree));
37 static void pop_forloop P((void));
38 static inline void pop_all_forloops P((void));
39 static void push_forloop P((const char *varname, NODE **elems, size_t nelems));
40 static void push_args P((int count, NODE *arglist, NODE **oldstack,
41 const char *func_name, char **varnames));
42 static inline void pop_fcall_stack P((void));
43 static void pop_fcall P((void));
44 static int comp_func P((const void *p1, const void *p2));
47 NODE *_t; /* used as a temporary in macros */
50 double _msc51bug; /* to get around a bug in MSC 5.1 */
60 #define INCREMENT(n) n++
62 #define INCREMENT(n) /* nothing */
65 /* Macros and variables to save and restore function and loop bindings */
67 * the val variable allows return/continue/break-out-of-context to be
68 * caught and diagnosed
70 #define PUSH_BINDING(stack, x, val) (memcpy((char *)(stack), (const char *)(x), sizeof(jmp_buf)), val++)
71 #define RESTORE_BINDING(stack, x, val) (memcpy((char *)(x), (const char *)(stack), sizeof(jmp_buf)), val--)
73 static jmp_buf loop_tag; /* always the current binding */
74 static int loop_tag_valid = FALSE; /* nonzero when loop_tag valid */
75 static int func_tag_valid = FALSE;
76 static jmp_buf func_tag;
77 extern int exiting, exit_val;
79 /* This rather ugly macro is for VMS C */
83 #define C(c) ((char)c)
85 * This table is used by the regexp routines to do case independant
86 * matching. Basically, every ascii character maps to itself, except
87 * uppercase letters map to lower case ones. This table has 256
88 * entries, for ISO 8859-1. Note also that if the system this
89 * is compiled on doesn't use 7-bit ascii, casetable[] should not be
90 * defined to the linker, so gawk should not load.
92 * Do NOT make this array static, it is used in several spots, not
96 * This table is also used for IGNORECASE for == and !=, and index().
97 * Although with GLIBC, we could use tolower() everywhere and RE_ICASE
98 * for the regex matcher, precomputing this table once gives us a
99 * performance improvement. I also think it's better for portability
100 * to non-GLIBC systems. All the world is not (yet :-) GNU/Linux.
102 #if 'a' == 97 /* it's ascii */
104 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
105 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
106 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
107 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
108 /* ' ' '!' '"' '#' '$' '%' '&' ''' */
109 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
110 /* '(' ')' '*' '+' ',' '-' '.' '/' */
111 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
112 /* '0' '1' '2' '3' '4' '5' '6' '7' */
113 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
114 /* '8' '9' ':' ';' '<' '=' '>' '?' */
115 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
116 /* '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' */
117 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
118 /* 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' */
119 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
120 /* 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' */
121 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
122 /* 'X' 'Y' 'Z' '[' '\' ']' '^' '_' */
123 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
124 /* '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' */
125 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
126 /* 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' */
127 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
128 /* 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' */
129 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
130 /* 'x' 'y' 'z' '{' '|' '}' '~' */
131 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
134 C('\200'), C('\201'), C('\202'), C('\203'), C('\204'), C('\205'), C('\206'), C('\207'),
135 C('\210'), C('\211'), C('\212'), C('\213'), C('\214'), C('\215'), C('\216'), C('\217'),
136 C('\220'), C('\221'), C('\222'), C('\223'), C('\224'), C('\225'), C('\226'), C('\227'),
137 C('\230'), C('\231'), C('\232'), C('\233'), C('\234'), C('\235'), C('\236'), C('\237'),
138 C('\240'), C('\241'), C('\242'), C('\243'), C('\244'), C('\245'), C('\246'), C('\247'),
139 C('\250'), C('\251'), C('\252'), C('\253'), C('\254'), C('\255'), C('\256'), C('\257'),
140 C('\260'), C('\261'), C('\262'), C('\263'), C('\264'), C('\265'), C('\266'), C('\267'),
141 C('\270'), C('\271'), C('\272'), C('\273'), C('\274'), C('\275'), C('\276'), C('\277'),
142 C('\340'), C('\341'), C('\342'), C('\343'), C('\344'), C('\345'), C('\346'), C('\347'),
143 C('\350'), C('\351'), C('\352'), C('\353'), C('\354'), C('\355'), C('\356'), C('\357'),
144 C('\360'), C('\361'), C('\362'), C('\363'), C('\364'), C('\365'), C('\366'), C('\327'),
145 C('\370'), C('\371'), C('\372'), C('\373'), C('\374'), C('\375'), C('\376'), C('\337'),
146 C('\340'), C('\341'), C('\342'), C('\343'), C('\344'), C('\345'), C('\346'), C('\347'),
147 C('\350'), C('\351'), C('\352'), C('\353'), C('\354'), C('\355'), C('\356'), C('\357'),
148 C('\360'), C('\361'), C('\362'), C('\363'), C('\364'), C('\365'), C('\366'), C('\367'),
149 C('\370'), C('\371'), C('\372'), C('\373'), C('\374'), C('\375'), C('\376'), C('\377'),
152 #include "You lose. You will need a translation table for your character set."
157 /* load_casetable --- for a non-ASCII locale, redo the table */
162 #if defined(LC_CTYPE)
165 static int loaded = FALSE;
167 if (loaded || do_traditional)
171 cp = setlocale(LC_CTYPE, NULL);
173 /* this is not per standard, but it's pretty safe */
174 if (cp == NULL || strcmp(cp, "C") == 0 || strcmp(cp, "POSIX") == 0)
177 for (i = 0200; i <= 0377; i++) {
178 if (isalpha(i) && islower(i) && i != toupper(i))
179 casetable[i] = toupper(i);
185 * This table maps node types to strings for debugging.
186 * KEEP IN SYNC WITH awk.h!!!!
188 static const char *const nodetypes[] = {
201 "Node_postincrement",
202 "Node_postdecrement",
207 "Node_assign_quotient",
212 "Node_assign_concat",
226 "Node_statement_list",
230 "Node_expression_list",
249 "Node_K_delete_loop",
253 "Node_redirect_output",
254 "Node_redirect_append",
255 "Node_redirect_pipe",
256 "Node_redirect_pipein",
257 "Node_redirect_input",
258 "Node_redirect_twoway",
289 "Node_final --- this should never appear",
293 /* nodetype2str --- convert a node type into a printable value */
296 nodetype2str(NODETYPE type)
300 if (type >= Node_illegal && type <= Node_final)
301 return nodetypes[(int) type];
303 sprintf(buf, _("unknown nodetype %d"), (int) type);
307 /* flags2str --- make a flags value readable */
310 flags2str(int flagval)
312 static const struct flagtab values[] = {
313 { MALLOC, "MALLOC" },
316 { STRING, "STRING" },
317 { STRCUR, "STRCUR" },
318 { NUMCUR, "NUMCUR" },
319 { NUMBER, "NUMBER" },
320 { MAYBE_NUM, "MAYBE_NUM" },
321 { ARRAYMAXED, "ARRAYMAXED" },
324 { INTLSTR, "INTLSTR" },
326 { WSTRCUR, "WSTRCUR" },
331 return genflags2str(flagval, values);
334 /* genflags2str --- general routine to convert a flag value to a string */
337 genflags2str(int flagval, const struct flagtab *tab)
339 static char buffer[BUFSIZ];
341 int i, space_left, space_needed;
345 for (i = 0; tab[i].name != NULL; i++) {
346 if ((flagval & tab[i].val) != 0) {
348 * note the trick, we want 1 or 0 for whether we need
351 space_needed = (strlen(tab[i].name) + (sp != buffer));
352 if (space_left < space_needed)
353 fatal(_("buffer overflow in genflags2str"));
359 strcpy(sp, tab[i].name);
361 space_left -= strlen(sp);
370 * make_scalar --- make sure that tree is a scalar.
372 * tree is in a scalar context. If it is a variable, accomplish
373 * what's needed; otherwise, do nothing.
375 * Notice that nodes of type Node_var_new have undefined value in var_value
376 * (a.k.a. lnode)---even though awkgram.y:variable() initializes it,
377 * push_args() doesn't. Thus we have to initialize it.
381 make_scalar(NODE *tree)
383 switch (tree->type) {
385 fatal(_("attempt to use array `%s' in a scalar context"),
389 switch (tree->orig_array->type) {
391 fatal(_("attempt to use array `%s' in a scalar context"),
394 tree->orig_array->type = Node_var;
395 tree->orig_array->var_value = Nnull_string;
404 tree->type = Node_var;
405 tree->var_value = Nnull_string;
414 * Tree is a bunch of rules to run. Returns zero if it hit an exit()
418 interpret(register NODE *volatile tree)
420 jmp_buf volatile loop_tag_stack; /* shallow binding stack for loop_tag */
421 static jmp_buf rule_tag; /* tag the rule currently being run, for NEXT
422 * and EXIT statements. It is static because
423 * there are no nested rules */
424 register NODE *volatile t = NULL; /* temporary */
425 NODE **volatile lhs; /* lhs == Left Hand Side for assigns, etc */
426 NODE *volatile stable_tree;
427 int volatile traverse = TRUE; /* True => loop thru tree (Node_rule_list) */
429 /* avoid false source indications */
435 sourceline = tree->source_line;
436 source = tree->source_file;
437 switch (tree->type) {
439 traverse = FALSE; /* False => one for-loop iteration only */
442 for (t = tree; t != NULL; t = t->rnode) {
445 sourceline = tree->source_line;
446 source = tree->source_file;
447 INCREMENT(tree->exec_count);
448 switch (setjmp(rule_tag)) {
449 case 0: /* normal non-jump */
450 /* test pattern, if any */
451 if (tree->lnode == NULL ||
452 eval_condition(tree->lnode)) {
453 /* using the lnode exec_count is kludgey */
454 if (tree->lnode != NULL)
455 INCREMENT(tree->lnode->exec_count);
456 (void) interpret(tree->rnode);
459 case TAG_CONTINUE: /* NEXT statement */
463 case TAG_BREAK: /* EXIT statement */
470 if (! traverse) /* case Node_rule_node */
471 break; /* don't loop */
475 case Node_statement_list:
476 for (t = tree; t != NULL; t = t->rnode)
477 (void) interpret(t->lnode);
481 INCREMENT(tree->exec_count);
482 if (eval_condition(tree->lnode)) {
483 INCREMENT(tree->rnode->exec_count);
484 (void) interpret(tree->rnode->lnode);
486 (void) interpret(tree->rnode->rnode);
498 int match_found = FALSE;
500 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
501 INCREMENT(tree->exec_count);
504 switch_value = tree_eval(stable_tree->lnode);
505 switch_body = stable_tree->rnode;
506 case_list = switch_body->lnode;
507 default_list = switch_body->rnode;
509 for (; case_list != NULL; case_list = case_list->rnode) {
510 case_stmt = case_list->lnode;
513 * Once a match is found, all cases will be processed as they fall through,
514 * so continue to execute statements until a break is reached.
517 if (case_stmt->type == Node_K_default)
519 else if (case_stmt->lnode->type == Node_regex) {
522 /* see comments in match_op() code about this. */
523 int kludge_need_start = 0;
525 t1 = force_string(switch_value);
526 rp = re_update(case_stmt->lnode);
528 if (avoid_dfa(tree, t1->stptr, t1->stlen))
529 kludge_need_start = RE_NEED_START;
530 match_found = (research(rp, t1->stptr, 0, t1->stlen, kludge_need_start) >= 0);
531 if (t1 != switch_value)
534 match_found = (cmp_nodes(switch_value, case_stmt->lnode) == 0);
537 /* If a match was found, execute the statements associated with the case. */
539 INCREMENT(case_stmt->exec_count);
540 switch (setjmp(loop_tag)) {
541 case 0: /* Normal non-jump */
542 (void) interpret(case_stmt->rnode);
544 case TAG_CONTINUE: /* continue statement */
545 free_temp(switch_value);
546 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
547 longjmp(loop_tag, TAG_CONTINUE);
549 case TAG_BREAK: /* break statement */
550 free_temp(switch_value);
551 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
560 free_temp(switch_value);
563 * If a default section was found, execute the statements associated with it
564 * and execute any trailing case statements if the default falls through.
566 if (! match_found && default_list != NULL) {
567 for (case_list = default_list;
568 case_list != NULL; case_list = case_list->rnode) {
569 case_stmt = case_list->lnode;
571 INCREMENT(case_stmt->exec_count);
572 switch (setjmp(loop_tag)) {
573 case 0: /* Normal non-jump */
574 (void) interpret(case_stmt->rnode);
576 case TAG_CONTINUE: /* continue statement */
577 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
578 longjmp(loop_tag, TAG_CONTINUE);
580 case TAG_BREAK: /* break statement */
581 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
589 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
594 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
597 while (eval_condition(stable_tree->lnode)) {
598 INCREMENT(stable_tree->exec_count);
599 switch (setjmp(loop_tag)) {
600 case 0: /* normal non-jump */
601 (void) interpret(stable_tree->rnode);
603 case TAG_CONTINUE: /* continue statement */
605 case TAG_BREAK: /* break statement */
606 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
612 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
616 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
619 INCREMENT(stable_tree->exec_count);
620 switch (setjmp(loop_tag)) {
621 case 0: /* normal non-jump */
622 (void) interpret(stable_tree->rnode);
624 case TAG_CONTINUE: /* continue statement */
626 case TAG_BREAK: /* break statement */
627 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
632 } while (eval_condition(stable_tree->lnode));
633 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
637 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
638 (void) interpret(tree->forloop->init);
640 while (eval_condition(stable_tree->forloop->cond)) {
641 INCREMENT(stable_tree->exec_count);
642 switch (setjmp(loop_tag)) {
643 case 0: /* normal non-jump */
644 (void) interpret(stable_tree->lnode);
646 case TAG_CONTINUE: /* continue statement */
647 (void) interpret(stable_tree->forloop->incr);
649 case TAG_BREAK: /* break statement */
650 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
656 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
659 case Node_K_arrayfor:
661 Func_ptr after_assign = NULL;
663 NODE *volatile array;
664 NODE *volatile save_array;
665 volatile size_t i, num_elems;
667 volatile int retval = 0;
668 int sort_indices = whiny_users;
670 #define hakvar forloop->init
671 #define arrvar forloop->incr
673 save_array = tree->arrvar;
674 array = get_array(save_array);
676 /* sanity: do nothing if empty */
677 if (array->var_array == NULL || array->table_size == 0)
678 break; /* from switch */
680 /* allocate space for array */
681 num_elems = array->table_size;
682 emalloc(list, NODE **, num_elems * sizeof(NODE *), "for_loop");
685 for (i = j = 0; i < array->array_size; i++) {
686 NODE *t = array->var_array[i];
691 for (; t != NULL; t = t->ahnext) {
692 list[j++] = dupnode(t);
693 assert(list[j-1] == t);
699 qsort(list, num_elems, sizeof(NODE *), comp_func); /* shazzam! */
701 /* now we can run the loop */
702 push_forloop(array->vname, list, num_elems);
703 PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
705 lhs = get_lhs(tree->hakvar, &after_assign, FALSE);
707 for (i = 0; i < num_elems; i++) {
708 INCREMENT(stable_tree->exec_count);
709 unref(*((NODE **) lhs));
710 *lhs = make_string(list[i]->ahname_str, list[i]->ahname_len);
713 switch (setjmp(loop_tag)) {
715 (void) interpret(stable_tree->lnode);
729 RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
732 if (do_lint && num_elems != array->table_size)
733 lintwarn(_("for loop: array `%s' changed size from %ld to %ld during loop execution"),
734 array_vname(save_array), (long) num_elems, (long) array->table_size);
744 INCREMENT(tree->exec_count);
745 if (! loop_tag_valid) {
747 * Old AT&T nawk treats break outside of loops like
748 * next. New ones catch it at parse time. Allow it if
749 * do_traditional is on, and complain if lint.
751 static int warned = FALSE;
753 if (do_lint && ! warned) {
754 lintwarn(_("`break' outside a loop is not portable"));
757 if (! do_traditional || do_posix)
758 fatal(_("`break' outside a loop is not allowed"));
759 longjmp(rule_tag, TAG_CONTINUE);
761 longjmp(loop_tag, TAG_BREAK);
764 case Node_K_continue:
765 INCREMENT(tree->exec_count);
766 if (! loop_tag_valid) {
768 * Old AT&T nawk treats continue outside of loops like
769 * next. New ones catch it at parse time. Allow it if
770 * do_traditional is on, and complain if lint.
772 static int warned = FALSE;
774 if (do_lint && ! warned) {
775 lintwarn(_("`continue' outside a loop is not portable"));
778 if (! do_traditional || do_posix)
779 fatal(_("`continue' outside a loop is not allowed"));
780 longjmp(rule_tag, TAG_CONTINUE);
782 longjmp(loop_tag, TAG_CONTINUE);
786 INCREMENT(tree->exec_count);
790 case Node_K_print_rec:
791 INCREMENT(tree->exec_count);
796 INCREMENT(tree->exec_count);
801 INCREMENT(tree->exec_count);
802 do_delete(tree->lnode, tree->rnode);
805 case Node_K_delete_loop:
806 INCREMENT(tree->exec_count);
807 do_delete_loop(tree->lnode, tree->rnode);
811 INCREMENT(tree->exec_count);
813 fatal(_("`next' cannot be called from a BEGIN rule"));
814 else if (in_end_rule)
815 fatal(_("`next' cannot be called from an END rule"));
817 /* could add a lint check here for in a loop or function */
818 longjmp(rule_tag, TAG_CONTINUE);
821 case Node_K_nextfile:
822 INCREMENT(tree->exec_count);
824 fatal(_("`nextfile' cannot be called from a BEGIN rule"));
825 else if (in_end_rule)
826 fatal(_("`nextfile' cannot be called from an END rule"));
828 /* could add a lint check here for in a loop or function */
830 * Have to do this cleanup here, since we don't longjump
831 * back to the main awk rule loop (rule_tag).
840 INCREMENT(tree->exec_count);
842 * In A,K,&W, p. 49, it says that an exit statement "...
843 * causes the program to behave as if the end of input had
844 * occurred; no more input is read, and the END actions, if
845 * any are executed." This implies that the rest of the rules
846 * are not done. So we immediately break out of the main loop.
849 if (tree->lnode != NULL) {
850 t = tree_eval(tree->lnode);
851 exit_val = (int) force_number(t);
854 longjmp(rule_tag, TAG_BREAK);
858 INCREMENT(tree->exec_count);
859 t = tree_eval(tree->lnode);
860 if ((t->flags & (PERM|TEMP)) != 0)
863 ret_node = copynode(t); /* don't do a dupnode here */
864 ret_node->flags |= TEMP;
866 longjmp(func_tag, TAG_RETURN);
871 * Appears to be an expression statement. Throw away the
874 if (do_lint && (tree->type == Node_var || tree->type == Node_var_new))
875 lintwarn(_("statement has no effect"));
876 INCREMENT(tree->exec_count);
878 if (t) /* stopme() returns NULL */
886 * calc_exp_posint --- calculate x^n for positive integral n,
887 * using exponentiation by squaring without recursion.
891 calc_exp_posint(AWKNUM x, long n)
904 /* calc_exp --- calculate x1^x2 */
907 calc_exp(AWKNUM x1, AWKNUM x2)
911 if ((lx = x2) == x2) { /* integer exponent */
914 return (lx > 0) ? calc_exp_posint(x1, lx)
915 : 1.0 / calc_exp_posint(x1, -lx);
917 return (AWKNUM) pow((double) x1, (double) x2);
920 /* r_tree_eval --- evaluate a subtree */
923 r_tree_eval(register NODE *tree, int iscond)
925 register NODE *r, *t1, *t2; /* return value & temporary subtrees */
933 #ifndef TREE_EVAL_MACRO
936 if (tree->type == Node_val) {
937 if (tree->stref <= 0)
939 return ((tree->flags & INTLSTR) != 0
940 ? r_force_string(tree)
942 } else if (tree->type == Node_var) {
943 if (tree->var_value->stref <= 0)
945 if (! var_uninitialized(tree))
946 return tree->var_value;
950 if (tree->type == Node_param_list) {
951 if ((tree->flags & FUNC) != 0)
952 fatal(_("can't use function name `%s' as variable or array"),
955 tree = stack_ptr[tree->param_cnt];
959 lintwarn(_("reference to uninitialized argument `%s'"),
964 if (do_lint && var_uninitialized(tree))
965 lintwarn(_("reference to uninitialized argument `%s'"),
971 switch (tree->type) {
973 if (do_lint && var_uninitialized(tree))
974 lintwarn(_("reference to uninitialized variable `%s'"),
976 return tree->var_value;
979 return tmp_number((AWKNUM) (eval_condition(tree->lnode)
980 && eval_condition(tree->rnode)));
983 return tmp_number((AWKNUM) (eval_condition(tree->lnode)
984 || eval_condition(tree->rnode)));
987 return tmp_number((AWKNUM) ! eval_condition(tree->lnode));
991 return (*tree->builtin)(tree->subnode);
994 return do_getline(tree);
997 return tmp_number((AWKNUM) (in_array(tree->lnode, tree->rnode) != NULL));
1000 return func_call(tree);
1002 /* unary operations */
1006 case Node_FIELDWIDTHS:
1009 case Node_field_spec:
1010 case Node_subscript:
1011 case Node_IGNORECASE:
1019 case Node_TEXTDOMAIN:
1020 lhs = get_lhs(tree, (Func_ptr *) NULL, TRUE);
1023 case Node_unary_minus:
1024 t1 = tree_eval(tree->subnode);
1025 x = -force_number(t1);
1027 return tmp_number(x);
1030 if (eval_condition(tree->lnode))
1031 return tree_eval(tree->rnode->lnode);
1032 return tree_eval(tree->rnode->rnode);
1038 return match_op(tree);
1045 register NODE **treep;
1046 register NODE **strp;
1047 register size_t len;
1048 register size_t supposed_len;
1050 register char *dest;
1051 int alloc_count, str_count;
1055 * This is an efficiency hack for multiple adjacent string
1056 * concatenations, to avoid recursion and string copies.
1058 * Node_concat trees grow downward to the left, so
1059 * descend to lowest (first) node, accumulating nodes
1060 * to evaluate to strings as we go.
1064 * But first, no arbitrary limits. Count the number of
1065 * nodes and malloc the treelist and strlist arrays.
1066 * There will be alloc_count + 1 items to concatenate. We
1067 * also leave room for an extra pointer at the end to
1068 * use as a sentinel. Thus, start alloc_count at 2.
1071 for (alloc_count = 2; tree != NULL && tree->type == Node_concat;
1075 emalloc(treelist, NODE **, sizeof(NODE *) * alloc_count, "tree_eval");
1076 emalloc(strlist, NODE **, sizeof(NODE *) * alloc_count, "tree_eval");
1078 /* Now, here we go. */
1080 while (tree != NULL && tree->type == Node_concat) {
1081 *treep++ = tree->rnode;
1086 * Now, evaluate to strings in LIFO order, accumulating
1087 * the string length, so we can do a single malloc at the
1090 * Evaluate the expressions first, then get their
1091 * lengthes, in case one of the expressions has a
1092 * side effect that changes one of the others.
1093 * See test/nasty.awk.
1095 * dupnode the results a la do_print, to give us
1096 * more predicable behavior; compare gawk 3.0.6 to
1097 * nawk/mawk on test/nasty.awk.
1100 supposed_len = len = 0;
1101 while (treep >= treelist) {
1104 /* Here lies the wumpus's brother. R.I.P. */
1105 n = force_string(tree_eval(*treep--));
1108 supposed_len += (*strp)->stlen;
1113 str_count = strp - strlist;
1115 for (i = 0; i < str_count; i++) {
1116 len += (*strp)->stlen;
1119 if (do_lint && supposed_len != len)
1120 lintwarn(_("concatenation: side effects in one expression have changed the length of another!"));
1121 emalloc(str, char *, len+2, "tree_eval");
1122 str[len] = str[len+1] = '\0'; /* for good measure */
1125 while (*strp != NULL) {
1126 memcpy(dest, (*strp)->stptr, (*strp)->stlen);
1127 dest += (*strp)->stlen;
1131 r = make_str_node(str, len, ALREADY_MALLOCED);
1140 case Node_assign_concat:
1142 Func_ptr after_assign = NULL;
1146 * Note that something lovely like this:
1148 * BEGIN { a = "a"; a = a (a = "b"); print a }
1150 * is not defined. It could print `ab' or `bb'.
1151 * Gawk 3.1.3 prints `ab', so we do that too, simply
1152 * by evaluating the LHS first. Ugh.
1154 * Thanks to mary1john@earthlink.net for pointing
1157 lhs = get_lhs(tree->lnode, &after_assign, FALSE);
1158 *lhs = force_string(*lhs);
1160 r = force_string(tree_eval(tree->rnode));
1163 * Don't clobber string constants!
1165 * Also check stref; see test/strcat1.awk,
1166 * the test for l->stref == 1 can't be an
1169 * Thanks again to mary1john@earthlink.net for pointing
1172 if (l != r && (l->flags & PERM) == 0 && l->stref == 1) {
1173 size_t nlen = l->stlen + r->stlen + 2;
1175 erealloc(l->stptr, char *, nlen, "interpret");
1176 memcpy(l->stptr + l->stlen, r->stptr, r->stlen);
1177 l->stlen += r->stlen;
1178 l->stptr[l->stlen] = '\0';
1181 size_t nlen = l->stlen + r->stlen + 2;
1183 emalloc(nval, char *, nlen, "interpret");
1184 memcpy(nval, l->stptr, l->stlen);
1185 memcpy(nval + l->stlen, r->stptr, r->stlen);
1187 *lhs = make_str_node(nval, l->stlen + r->stlen, ALREADY_MALLOCED);
1197 Func_ptr after_assign = NULL;
1199 if (do_lint && iscond)
1200 lintwarn(_("assignment used in conditional context"));
1201 r = tree_eval(tree->rnode);
1202 lhs = get_lhs(tree->lnode, &after_assign, FALSE);
1210 /* other assignment types are easier because they are numeric */
1211 case Node_preincrement:
1212 case Node_predecrement:
1213 case Node_postincrement:
1214 case Node_postdecrement:
1215 case Node_assign_exp:
1216 case Node_assign_times:
1217 case Node_assign_quotient:
1218 case Node_assign_mod:
1219 case Node_assign_plus:
1220 case Node_assign_minus:
1221 return op_assign(tree);
1223 break; /* handled below */
1227 * Evaluate subtrees in order to do binary operation, then keep going.
1228 * Use dupnode to make sure that these values don't disappear out
1229 * from under us during recursive subexpression evaluation.
1231 t1 = dupnode(tree_eval(tree->lnode));
1232 t2 = dupnode(tree_eval(tree->rnode));
1234 switch (tree->type) {
1241 di = cmp_nodes(t1, t2);
1244 switch (tree->type) {
1246 return tmp_number((AWKNUM) (di == 0));
1248 return tmp_number((AWKNUM) (di != 0));
1250 return tmp_number((AWKNUM) (di < 0));
1252 return tmp_number((AWKNUM) (di > 0));
1254 return tmp_number((AWKNUM) (di <= 0));
1256 return tmp_number((AWKNUM) (di >= 0));
1262 break; /* handled below */
1265 x1 = force_number(t1);
1266 x2 = force_number(t2);
1269 switch (tree->type) {
1271 return tmp_number(calc_exp(x1, x2));
1274 return tmp_number(x1 * x2);
1278 fatal(_("division by zero attempted"));
1280 /* special case for integer division, put in for Cray */
1283 return tmp_number(x1 / x2);
1284 lx = (long) x1 / lx2;
1286 return tmp_number((AWKNUM) lx);
1289 return tmp_number(x1 / x2);
1293 fatal(_("division by zero attempted in `%%'"));
1295 return tmp_number(fmod(x1, x2));
1296 #else /* ! HAVE_FMOD */
1297 (void) modf(x1 / x2, &x);
1298 return tmp_number(x1 - x * x2);
1299 #endif /* ! HAVE_FMOD */
1302 return tmp_number(x1 + x2);
1305 return tmp_number(x1 - x2);
1308 fatal(_("illegal type (%s) in tree_eval"), nodetype2str(tree->type));
1313 /* eval_condition --- is TREE true or false? Returns 0==false, non-zero==true */
1316 eval_condition(register NODE *tree)
1321 if (tree == NULL) /* Null trees are the easiest kinds */
1323 if (tree->type == Node_line_range) {
1325 * Node_line_range is kind of like Node_match, EXCEPT: the
1326 * lnode field (more properly, the condpair field) is a node
1327 * of a Node_cond_pair; whether we evaluate the lnode of that
1328 * node or the rnode depends on the triggered word. More
1329 * precisely: if we are not yet triggered, we tree_eval the
1330 * lnode; if that returns true, we set the triggered word.
1331 * If we are triggered (not ELSE IF, note), we tree_eval the
1332 * rnode, clear triggered if it succeeds, and perform our
1333 * action (regardless of success or failure). We want to be
1334 * able to begin and end on a single input record, so this
1335 * isn't an ELSE IF, as noted above.
1337 if (! tree->triggered) {
1338 if (! eval_condition(tree->condpair->lnode))
1341 tree->triggered = TRUE;
1343 /* Else we are triggered */
1344 if (eval_condition(tree->condpair->rnode))
1345 tree->triggered = FALSE;
1350 * Could just be J.random expression. in which case, null and 0 are
1351 * false, anything else is true
1354 t1 = m_tree_eval(tree, TRUE);
1355 if (t1->flags & MAYBE_NUM)
1356 (void) force_number(t1);
1357 if (t1->flags & NUMBER)
1358 ret = (t1->numbr != 0.0);
1360 ret = (t1->stlen != 0);
1365 /* cmp_nodes --- compare two nodes, returning negative, 0, positive */
1368 cmp_nodes(register NODE *t1, register NODE *t2)
1371 register size_t len1, len2;
1377 if (t1->flags & MAYBE_NUM)
1378 (void) force_number(t1);
1379 if (t2->flags & MAYBE_NUM)
1380 (void) force_number(t2);
1381 if ((t1->flags & NUMBER) && (t2->flags & NUMBER)) {
1382 if (t1->numbr == t2->numbr)
1384 /* don't subtract, in case one or both are infinite */
1385 else if (t1->numbr < t2->numbr)
1390 (void) force_string(t1);
1391 (void) force_string(t2);
1394 ldiff = len1 - len2;
1395 if (len1 == 0 || len2 == 0)
1397 l = (ldiff <= 0 ? len1 : len2);
1399 const unsigned char *cp1 = (const unsigned char *) t1->stptr;
1400 const unsigned char *cp2 = (const unsigned char *) t2->stptr;
1403 if (gawk_mb_cur_max > 1) {
1405 memset(&mbs, 0, sizeof(mbstate_t));
1406 ret = strncasecmpmbs((const char *) cp1, mbs,
1407 (const char *) cp2, mbs, l);
1410 /* Could use tolower() here; see discussion above. */
1411 for (ret = 0; l-- > 0 && ret == 0; cp1++, cp2++)
1412 ret = casetable[*cp1] - casetable[*cp2];
1414 ret = memcmp(t1->stptr, t2->stptr, l);
1415 return (ret == 0 ? ldiff : ret);
1418 /* op_assign --- do +=, -=, etc. */
1421 op_assign(register NODE *tree)
1426 Func_ptr after_assign = NULL;
1430 * For += etc, do the rhs first, since it can rearrange things,
1431 * and *then* get the lhs.
1433 if (tree->rnode != NULL) {
1434 tmp = tree_eval(tree->rnode);
1435 rval = force_number(tmp);
1438 rval = (AWKNUM) 1.0;
1440 lhs = get_lhs(tree->lnode, &after_assign, TRUE);
1441 lval = force_number(*lhs);
1444 switch(tree->type) {
1445 case Node_postincrement:
1448 case Node_preincrement:
1449 case Node_assign_plus:
1450 *lhs = make_number(lval + rval);
1453 case Node_postdecrement:
1456 case Node_predecrement:
1457 case Node_assign_minus:
1458 *lhs = make_number(lval - rval);
1461 case Node_assign_exp:
1462 *lhs = make_number(calc_exp(lval, rval));
1465 case Node_assign_times:
1466 *lhs = make_number(lval * rval);
1469 case Node_assign_quotient:
1470 if (rval == (AWKNUM) 0)
1471 fatal(_("division by zero attempted in `/='"));
1476 /* special case for integer division, put in for Cray */
1479 *lhs = make_number(lval / rval);
1482 ltemp = (long) lval / ltemp;
1483 if (ltemp * lval == rval)
1484 *lhs = make_number((AWKNUM) ltemp);
1487 *lhs = make_number(lval / rval);
1491 case Node_assign_mod:
1492 if (rval == (AWKNUM) 0)
1493 fatal(_("division by zero attempted in `%%='"));
1495 *lhs = make_number(fmod(lval, rval));
1496 #else /* ! HAVE_FMOD */
1500 (void) modf(lval / rval, &t1);
1501 t2 = lval - rval * t1;
1502 *lhs = make_number(t2);
1504 #endif /* ! HAVE_FMOD */
1514 /* for postincrement or postdecrement, return the old value */
1515 return (post ? tmp_number(lval) : *lhs);
1519 * Avoiding memory leaks is difficult. In paticular, any of `next',
1520 * `nextfile', `break' or `continue' (when not in a loop), can longjmp
1521 * out to the outermost level. This leaks memory if it happens in a
1522 * called function. It also leaks memory if it happens in a
1523 * `for (iggy in foo)' loop, since such loops malloc an array of the
1524 * current array indices to loop over, which provides stability.
1526 * The following code takes care of these problems. First comes the
1527 * array-loop management code. This can be a stack of arrays being looped
1528 * on at any one time. This stack serves for both mainline code and
1529 * function body code. As each loop starts and finishes, it pushes its
1530 * info onto this stack and off of it; whether the loop is in a function
1531 * body or not isn't relevant.
1533 * Since the list of indices is created using dupnode(), when popping
1534 * this stack it should be safe to unref() things, and then memory
1535 * will get finally released when the function call stack is popped.
1536 * This means that the loop_stack should be popped first upon a `next'.
1539 static struct loop_info {
1540 const char *varname; /* variable name, for debugging */
1541 NODE **elems; /* list of indices */
1542 size_t nelems; /* how many there are */
1543 } *loop_stack = NULL;
1544 size_t nloops = 0; /* how many slots there are in the stack */
1545 size_t nloops_active = 0; /* how many loops are actively stacked */
1547 /* pop_forloop --- pop one for loop off the stack */
1553 struct loop_info *loop;
1555 assert(nloops_active > 0);
1557 curloop = --nloops_active; /* 0-based indexing */
1558 loop = & loop_stack[curloop];
1560 for (i = 0; i < loop->nelems; i++)
1561 unref(loop->elems[i]);
1566 loop->varname = NULL;
1570 /* pop_forloops --- pop the for loops stack all the way */
1575 while (nloops_active > 0)
1576 pop_forloop(); /* decrements nloops_active for us */
1579 /* push_forloop --- add a single for loop to the stack */
1582 push_forloop(const char *varname, NODE **elems, size_t nelems)
1584 #define NLOOPS 4 /* seems like a good guess */
1585 if (loop_stack == NULL) {
1586 /* allocate stack, set vars */
1588 emalloc(loop_stack, struct loop_info *, nloops * sizeof(struct loop_info),
1590 } else if (nloops_active == nloops) {
1591 /* grow stack, set vars */
1593 erealloc(loop_stack, struct loop_info *, nloops * sizeof(struct loop_info),
1597 loop_stack[nloops_active].varname = varname;
1598 loop_stack[nloops_active].elems = elems;
1599 loop_stack[nloops_active].nelems = nelems;
1605 * N.B. The code that uses fcalls[] *always* uses indexing.
1606 * This avoids severe problems in case fcalls gets realloc()'ed
1607 * during recursive tree_eval()'s or whatever, so that we don't
1608 * have to carefully reassign pointers into the array. The
1609 * minor speed gain from using a pointer was offset too much
1610 * by the hassles to get the code right and commented.
1612 * Thanks and a tip of the hatlo to Brian Kernighan.
1615 static struct fcall {
1616 const char *fname; /* function name */
1617 size_t count; /* how many args */
1618 NODE *arglist; /* list thereof */
1619 NODE **prevstack; /* function stack frame of previous function */
1620 NODE **stack; /* function stack frame of current function */
1623 static long fcall_list_size = 0;
1624 static long curfcall = -1;
1627 * get_curfunc_arg_count --- return number actual parameters
1629 * This is for use by dynamically loaded C extension functions.
1632 get_curfunc_arg_count(void)
1637 assert(curfcall >= 0);
1639 /* count the # of expressions in argument expression list */
1640 for (argc = 0, argp = fcalls[curfcall].arglist;
1641 argp != NULL; argp = argp->rnode)
1647 /* pop_fcall --- pop off a single function call */
1655 assert(curfcall >= 0);
1656 stack_ptr = fcalls[curfcall].prevstack;
1658 sp = fcalls[curfcall].stack;
1660 for (count = fcalls[curfcall].count; count > 0; count--) {
1662 if (n->type == Node_var) /* local variable */
1663 unref(n->var_value);
1664 else if (n->type == Node_var_array) /* local array */
1668 if (fcalls[curfcall].stack) {
1669 free((char *) fcalls[curfcall].stack);
1670 fcalls[curfcall].stack = NULL;
1675 /* pop_fcall_stack --- pop off all function args, don't leak memory */
1680 while (curfcall >= 0)
1684 /* push_args --- push function arguments onto the stack */
1687 push_args(int count,
1690 const char *func_name,
1693 NODE *arg, *r, **sp;
1696 if (fcall_list_size == 0) { /* first time */
1697 emalloc(fcalls, struct fcall *, 10 * sizeof(struct fcall),
1699 fcall_list_size = 10;
1702 if (++curfcall >= fcall_list_size) {
1703 fcall_list_size *= 2;
1704 erealloc(fcalls, struct fcall *,
1705 fcall_list_size * sizeof(struct fcall), "push_args");
1709 emalloc(fcalls[curfcall].stack, NODE **, count*sizeof(NODE *), "push_args");
1711 fcalls[curfcall].stack = NULL;
1712 fcalls[curfcall].count = count;
1713 fcalls[curfcall].fname = func_name; /* not used, for debugging, just in case */
1714 fcalls[curfcall].arglist = argp;
1715 fcalls[curfcall].prevstack = oldstack;
1717 sp = fcalls[curfcall].stack;
1719 /* for each calling arg. add NODE * on stack */
1720 for (i = 0; i < count; i++) {
1724 /* local variable */
1725 r->type = Node_var_new;
1726 r->var_value = Nnull_string;
1727 r->vname = varnames[i];
1732 /* call by reference for arrays; see below also */
1733 if (arg->type == Node_param_list)
1734 arg = fcalls[curfcall].prevstack[arg->param_cnt];
1736 if (arg->type == Node_var_array || arg->type == Node_var_new) {
1737 r->type = Node_array_ref;
1738 r->orig_array = arg;
1739 r->prev_array = arg;
1740 } else if (arg->type == Node_array_ref) {
1742 r->prev_array = arg;
1744 NODE *n = tree_eval(arg);
1747 r->lnode = dupnode(n);
1748 r->rnode = (NODE *) NULL;
1751 r->vname = varnames[i];
1756 /* Left over calling args. */
1758 _("function `%s' called with more arguments than declared"),
1760 /* Evaluate them, they may have side effects: */
1763 if (arg->type == Node_param_list)
1764 arg = fcalls[curfcall].prevstack[arg->param_cnt];
1765 if (arg->type != Node_var_array &&
1766 arg->type != Node_array_ref &&
1767 arg->type != Node_var_new)
1768 free_temp(tree_eval(arg));
1769 } while ((argp = argp->rnode) != NULL);
1772 stack_ptr = fcalls[curfcall].stack;
1775 /* func_call --- call a function, call by reference for arrays */
1780 func_call(NODE *tree)
1783 NODE *name, *arg_list;
1785 jmp_buf volatile func_tag_stack;
1786 jmp_buf volatile loop_tag_stack;
1787 int volatile save_loop_tag_valid = FALSE;
1788 NODE *save_ret_node;
1789 extern NODE *ret_node;
1791 /* tree->rnode is a Node_val giving function name */
1792 /* tree->lnode is Node_expression_list of calling args. */
1794 arg_list = tree->lnode;
1796 /* retrieve function definition node */
1797 if (tree->funcbody != NULL)
1800 f = lookup(name->stptr);
1801 if (f == NULL || f->type != Node_func)
1802 fatal(_("function `%s' not defined"), name->stptr);
1804 tree->funcbody = f; /* save for next call */
1808 fprintf(stderr, "function `%s' called\n", name->stptr);
1810 push_args(f->lnode->param_cnt, arg_list, stack_ptr, name->stptr,
1814 * Execute function body, saving context, as a return statement
1815 * will longjmp back here.
1817 * Have to save and restore the loop_tag stuff so that a return
1818 * inside a loop in a function body doesn't scrog any loops going
1819 * on in the main program. We save the necessary info in variables
1820 * local to this function so that function nesting works OK.
1821 * We also only bother to save the loop stuff if we're in a loop
1822 * when the function is called.
1824 if (loop_tag_valid) {
1827 save_loop_tag_valid = (volatile int) loop_tag_valid;
1828 PUSH_BINDING(loop_tag_stack, loop_tag, junk);
1829 loop_tag_valid = FALSE;
1831 PUSH_BINDING(func_tag_stack, func_tag, func_tag_valid);
1832 save_ret_node = ret_node;
1833 ret_node = Nnull_string; /* default return value */
1834 INCREMENT(f->exec_count); /* count function calls */
1835 if (setjmp(func_tag) == 0)
1836 (void) interpret(f->rnode);
1839 ret_node = (NODE *) save_ret_node;
1840 RESTORE_BINDING(func_tag_stack, func_tag, func_tag_valid);
1843 /* Restore the loop_tag stuff if necessary. */
1844 if (save_loop_tag_valid) {
1847 loop_tag_valid = (int) save_loop_tag_valid;
1848 RESTORE_BINDING(loop_tag_stack, loop_tag, junk);
1855 /* dump_fcall_stack --- print a backtrace of the awk function calls */
1858 dump_fcall_stack(FILE *fp)
1865 fprintf(fp, _("\n\t# Function Call Stack:\n\n"));
1866 for (i = curfcall; i >= 0; i--)
1867 fprintf(fp, "\t# %3d. %s\n", i+1, fcalls[i].fname);
1868 fprintf(fp, _("\t# -- main --\n"));
1870 #endif /* PROFILING */
1874 * This returns a POINTER to a node pointer. get_lhs(ptr) is the current
1875 * value of the var, or where to store the var's new value
1877 * For the special variables, don't unref their current value if it's
1878 * the same as the internal copy; perhaps the current one is used in
1879 * a concatenation or some other expression somewhere higher up in the
1884 r_get_lhs(register NODE *ptr, Func_ptr *assign, int reference)
1886 register NODE **aptr = NULL;
1890 *assign = NULL; /* for safety */
1891 if (ptr->type == Node_param_list) {
1892 if ((ptr->flags & FUNC) != 0)
1893 fatal(_("can't use function name `%s' as variable or array"), ptr->vname);
1894 ptr = stack_ptr[ptr->param_cnt];
1899 switch (ptr->type) {
1901 if (do_lint && reference && var_uninitialized(ptr))
1902 lintwarn(_("reference to uninitialized variable `%s'"),
1905 aptr = &(ptr->var_value);
1907 if (ptr->var_value->stref <= 0)
1912 case Node_FIELDWIDTHS:
1913 aptr = &(FIELDWIDTHS_node->var_value);
1915 *assign = set_FIELDWIDTHS;
1919 aptr = &(RS_node->var_value);
1925 aptr = &(FS_node->var_value);
1931 if (FNR_node->var_value->numbr != FNR) {
1932 unref(FNR_node->var_value);
1933 FNR_node->var_value = make_number((AWKNUM) FNR);
1935 aptr = &(FNR_node->var_value);
1941 if (NR_node->var_value->numbr != NR) {
1942 unref(NR_node->var_value);
1943 NR_node->var_value = make_number((AWKNUM) NR);
1945 aptr = &(NR_node->var_value);
1951 if (NF == -1 || NF_node->var_value->numbr != NF) {
1953 (void) get_field(UNLIMITED-1, assign); /* parse record */
1954 unref(NF_node->var_value);
1955 NF_node->var_value = make_number((AWKNUM) NF);
1957 aptr = &(NF_node->var_value);
1962 case Node_IGNORECASE:
1963 aptr = &(IGNORECASE_node->var_value);
1965 *assign = set_IGNORECASE;
1969 aptr = &(BINMODE_node->var_value);
1971 *assign = set_BINMODE;
1975 aptr = &(LINT_node->var_value);
1981 aptr = &(OFMT_node->var_value);
1987 aptr = &(CONVFMT_node->var_value);
1989 *assign = set_CONVFMT;
1993 aptr = &(ORS_node->var_value);
1999 aptr = &(OFS_node->var_value);
2005 aptr = &(SUBSEP_node->var_value);
2007 *assign = set_SUBSEP;
2010 case Node_TEXTDOMAIN:
2011 aptr = &(TEXTDOMAIN_node->var_value);
2013 *assign = set_TEXTDOMAIN;
2016 case Node_field_spec:
2020 n = tree_eval(ptr->lnode);
2022 if ((n->flags & NUMBER) == 0) {
2023 lintwarn(_("attempt to field reference from non-numeric value"));
2025 lintwarn(_("attempt to reference from null string"));
2028 field_num = (int) force_number(n);
2031 fatal(_("attempt to access field %d"), field_num);
2032 if (field_num == 0 && field0_valid) { /* short circuit */
2033 aptr = &fields_arr[0];
2035 *assign = reset_record;
2037 aptr = get_field(field_num, assign);
2038 if (do_lint && reference && (*aptr == Null_field || *aptr == Nnull_string))
2039 lintwarn(_("reference to uninitialized field `$%d'"),
2044 case Node_subscript:
2045 n = get_array(ptr->lnode);
2046 aptr = assoc_lookup(n, concat_exp(ptr->rnode), reference);
2051 /* in gawk for a while */
2052 fatal(_("assignment is not allowed to result of builtin function"));
2055 * This is how Christos at Deshaw did it.
2056 * Does this buy us anything?
2058 if (ptr->builtin == NULL)
2059 fatal(_("assignment is not allowed to result of builtin function"));
2060 ptr->callresult = (*ptr->builtin)(ptr->subnode);
2061 aptr = &ptr->callresult;
2066 fprintf(stderr, "type = %s\n", nodetype2str(ptr->type));
2073 /* match_op --- do ~ and !~ */
2076 match_op(register NODE *tree)
2079 register Regexp *rp;
2082 int kludge_need_start = 0; /* FIXME: --- see below */
2084 if (tree->type == Node_nomatch)
2086 if (tree->type == Node_regex)
2087 t1 = *get_field(0, (Func_ptr *) 0);
2089 t1 = force_string(tree_eval(tree->lnode));
2092 rp = re_update(tree);
2096 * Any place where research() is called with a last parameter of
2097 * zero, we need to use the avoid_dfa test. This appears here and
2098 * in the code for Node_K_switch.
2100 * A new or improved dfa that distinguishes beginning/end of
2101 * string from beginning/end of line will allow us to get rid of
2102 * this temporary hack.
2104 * The avoid_dfa() function is in re.c; it is not very smart.
2106 if (avoid_dfa(tree, t1->stptr, t1->stlen))
2107 kludge_need_start = RE_NEED_START;
2108 i = research(rp, t1->stptr, 0, t1->stlen, kludge_need_start);
2109 i = (i == -1) ^ (match == TRUE);
2111 return tmp_number((AWKNUM) i);
2114 /* set_IGNORECASE --- update IGNORECASE as appropriate */
2119 static int warned = FALSE;
2121 if ((do_lint || do_traditional) && ! warned) {
2123 lintwarn(_("`IGNORECASE' is a gawk extension"));
2128 else if ((IGNORECASE_node->var_value->flags & (STRING|STRCUR)) != 0) {
2129 if ((IGNORECASE_node->var_value->flags & MAYBE_NUM) == 0)
2130 IGNORECASE = (force_string(IGNORECASE_node->var_value)->stlen > 0);
2132 IGNORECASE = (force_number(IGNORECASE_node->var_value) != 0.0);
2133 } else if ((IGNORECASE_node->var_value->flags & (NUMCUR|NUMBER)) != 0)
2134 IGNORECASE = (force_number(IGNORECASE_node->var_value) != 0.0);
2136 IGNORECASE = FALSE; /* shouldn't happen */
2138 set_RS(); /* set_RS() calls set_FS() if need be, for us */
2141 /* set_BINMODE --- set translation mode (OS/2, DOS, others) */
2146 static int warned = FALSE;
2151 if ((do_lint || do_traditional) && ! warned) {
2153 lintwarn(_("`BINMODE' is a gawk extension"));
2157 else if ((BINMODE_node->var_value->flags & STRING) != 0) {
2158 v = BINMODE_node->var_value;
2163 for (cp = p; *cp != '\0'; cp++) {
2170 if (! digits || (BINMODE_node->var_value->flags & MAYBE_NUM) == 0) {
2172 if (strcmp(p, "r") == 0)
2174 else if (strcmp(p, "w") == 0)
2176 else if (strcmp(p, "rw") == 0 || strcmp(p, "wr") == 0)
2179 if (BINMODE == 0 && v->stlen != 0) {
2180 /* arbitrary string, assume both */
2182 warning("BINMODE: arbitrary string value treated as \"rw\"");
2185 BINMODE = (int) force_number(BINMODE_node->var_value);
2188 } else if ((BINMODE_node->var_value->flags & NUMBER) != 0)
2189 BINMODE = (int) force_number(BINMODE_node->var_value);
2191 BINMODE = 0; /* shouldn't happen */
2194 /* set_OFS --- update OFS related variables when OFS assigned to */
2199 OFS = force_string(OFS_node->var_value)->stptr;
2200 OFSlen = OFS_node->var_value->stlen;
2204 /* set_ORS --- update ORS related variables when ORS assigned to */
2209 ORS = force_string(ORS_node->var_value)->stptr;
2210 ORSlen = ORS_node->var_value->stlen;
2214 /* fmt_ok --- is the conversion format a valid one? */
2216 NODE **fmt_list = NULL;
2217 static int fmt_ok P((NODE *n));
2218 static int fmt_index P((NODE *n));
2223 NODE *tmp = force_string(n);
2224 const char *p = tmp->stptr;
2225 #if ! defined(PRINTF_HAS_F_FORMAT) || PRINTF_HAS_F_FORMAT != 1
2226 static const char float_formats[] = "efgEG";
2228 static const char float_formats[] = "efgEFG";
2230 #if defined(HAVE_LOCALE_H)
2231 static const char flags[] = " +-#'";
2233 static const char flags[] = " +-#";
2238 while (*p && strchr(flags, *p) != NULL) /* flags */
2240 while (*p && ISDIGIT(*p)) /* width - %*.*g is NOT allowed */
2242 if (*p == '\0' || (*p != '.' && ! ISDIGIT(*p)))
2246 while (*p && ISDIGIT(*p)) /* precision */
2248 if (*p == '\0' || strchr(float_formats, *p) == NULL)
2255 /* fmt_index --- track values of OFMT and CONVFMT to keep semantics correct */
2260 register int ix = 0;
2261 static int fmt_num = 4;
2262 static int fmt_hiwater = 0;
2264 if (fmt_list == NULL)
2265 emalloc(fmt_list, NODE **, fmt_num*sizeof(*fmt_list), "fmt_index");
2266 (void) force_string(n);
2267 while (ix < fmt_hiwater) {
2268 if (cmp_nodes(fmt_list[ix], n) == 0)
2273 n->stptr[n->stlen] = '\0';
2274 if (do_lint && ! fmt_ok(n))
2275 lintwarn(_("bad `%sFMT' specification `%s'"),
2276 n == CONVFMT_node->var_value ? "CONV"
2277 : n == OFMT_node->var_value ? "O"
2280 if (fmt_hiwater >= fmt_num) {
2282 erealloc(fmt_list, NODE **, fmt_num * sizeof(*fmt_list), "fmt_index");
2284 fmt_list[fmt_hiwater] = dupnode(n);
2285 return fmt_hiwater++;
2288 /* set_OFMT --- track OFMT correctly */
2293 OFMTidx = fmt_index(OFMT_node->var_value);
2294 OFMT = fmt_list[OFMTidx]->stptr;
2297 /* set_CONVFMT --- track CONVFMT correctly */
2302 CONVFMTidx = fmt_index(CONVFMT_node->var_value);
2303 CONVFMT = fmt_list[CONVFMTidx]->stptr;
2306 /* set_LINT --- update LINT as appropriate */
2312 int old_lint = do_lint;
2314 if ((LINT_node->var_value->flags & (STRING|STRCUR)) != 0) {
2315 if ((LINT_node->var_value->flags & MAYBE_NUM) == 0) {
2316 const char *lintval;
2319 do_lint = (force_string(LINT_node->var_value)->stlen > 0);
2320 lintval = LINT_node->var_value->stptr;
2321 lintlen = LINT_node->var_value->stlen;
2324 if (lintlen == 5 && strncmp(lintval, "fatal", 5) == 0)
2326 else if (lintlen == 7 && strncmp(lintval, "invalid", 7) == 0)
2327 do_lint = LINT_INVALID;
2333 if (force_number(LINT_node->var_value) != 0.0)
2339 } else if ((LINT_node->var_value->flags & (NUMCUR|NUMBER)) != 0) {
2340 if (force_number(LINT_node->var_value) != 0.0)
2346 do_lint = FALSE; /* shouldn't happen */
2351 /* explicitly use warning() here, in case lintfunc == r_fatal */
2352 if (old_lint != do_lint && old_lint && do_lint == FALSE)
2353 warning(_("turning off `--lint' due to assignment to `LINT'"));
2354 #endif /* ! NO_LINT */
2357 /* set_TEXTDOMAIN --- update TEXTDOMAIN variable when TEXTDOMAIN assigned to */
2364 TEXTDOMAIN = force_string(TEXTDOMAIN_node->var_value)->stptr;
2365 len = TEXTDOMAIN_node->var_value->stlen;
2366 TEXTDOMAIN[len] = '\0';
2368 * Note: don't call textdomain(); this value is for
2369 * the awk program, not for gawk itself.
2374 * assign_val --- do mechanics of assignment, for calling from multiple
2379 assign_val(NODE **lhs_p, NODE *rhs)
2381 if (rhs != *lhs_p) {
2383 * Since we know that the nodes are different,
2384 * we can do the unref() before the dupnode().
2387 *lhs_p = dupnode(rhs);
2392 /* update_ERRNO_saved --- update the value of ERRNO based on argument */
2395 update_ERRNO_saved(int errcode)
2399 cp = strerror(errcode);
2401 unref(ERRNO_node->var_value);
2402 ERRNO_node->var_value = make_string(cp, strlen(cp));
2405 /* update_ERRNO --- update the value of ERRNO based on errno */
2410 update_ERRNO_saved(errno);
2413 /* comp_func --- array index comparison function for qsort */
2416 comp_func(const void *p1, const void *p2)
2419 const char *str1, *str2;
2420 const NODE *t1, *t2;
2423 t1 = *((const NODE *const *) p1);
2424 t2 = *((const NODE *const *) p2);
2427 t1 = force_string(t1);
2428 t2 = force_string(t2);
2430 len1 = t1->ahname_len;
2431 str1 = t1->ahname_str;
2433 len2 = t2->ahname_len;
2434 str2 = t2->ahname_str;
2436 /* Array indexes are strings, compare as such, always! */
2437 cmp1 = memcmp(str1, str2, len1 < len2 ? len1 : len2);
2438 /* if prefixes are equal, size matters */
2439 return (cmp1 != 0 ? cmp1 :
2440 len1 < len2 ? -1 : (len1 > len2));