2 * awkgram.y --- yacc/bison parser
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
34 #define DONT_FREE FALSE
36 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
37 static void yyerror(const char *m, ...) ATTRIBUTE_PRINTF_1;
39 static void yyerror(); /* va_alist */
41 static char *get_src_buf P((void));
42 static int yylex P((void));
43 static NODE *node_common P((NODETYPE op));
44 static NODE *snode P((NODE *subn, NODETYPE op, int sindex));
45 static NODE *make_for_loop P((NODE *init, NODE *cond, NODE *incr));
46 static NODE *append_right P((NODE *list, NODE *new));
47 static inline NODE *append_pattern P((NODE **list, NODE *patt));
48 static void func_install P((NODE *params, NODE *def));
49 static void pop_var P((NODE *np, int freeit));
50 static void pop_params P((NODE *params));
51 static NODE *make_param P((char *name));
52 static NODE *mk_rexp P((NODE *exp));
53 static int dup_parms P((NODE *func));
54 static void param_sanity P((NODE *arglist));
55 static int parms_shadow P((const char *fname, NODE *func));
56 static int isnoeffect P((NODETYPE t));
57 static int isassignable P((NODE *n));
58 static void dumpintlstr P((const char *str, size_t len));
59 static void dumpintlstr2 P((const char *str1, size_t len1, const char *str2, size_t len2));
60 static void count_args P((NODE *n));
61 static int isarray P((NODE *n));
63 enum defref { FUNC_DEFINE, FUNC_USE };
64 static void func_use P((const char *name, enum defref how));
65 static void check_funcs P((void));
67 static int want_regexp; /* lexical scanning kludge */
68 static int can_return; /* parsing kludge */
69 static int begin_or_end_rule = FALSE; /* parsing kludge */
70 static int parsing_end_rule = FALSE; /* for warnings */
71 static int in_print = FALSE; /* lexical scanning kludge for print */
72 static int in_parens = 0; /* lexical scanning kludge for print */
73 static char *lexptr; /* pointer to next char during parsing */
75 static char *lexptr_begin; /* keep track of where we were for error msgs */
76 static char *lexeme; /* beginning of lexeme for debugging */
77 static char *thisline = NULL;
78 #define YYDEBUG_LEXER_TEXT (lexeme)
79 static int param_counter;
80 static char *tokstart = NULL;
81 static char *tok = NULL;
84 static long func_count; /* total number of functions */
86 #define HASHSIZE 1021 /* this constant only used here */
87 NODE *variables[HASHSIZE];
88 static int var_count; /* total number of global variables */
91 extern int sourceline;
92 extern struct src *srcfiles;
95 extern NODE *begin_block;
96 extern NODE *end_block;
99 * This string cannot occur as a real awk identifier.
100 * Use it as a special token to make function parsing
101 * uniform, but if it's seen, don't install the function.
103 * function split(x) { return x }
104 * function x(a) { return a }
105 * should only produce one error message, and not core dump.
107 static char builtin_func[] = "@builtin";
114 NODETYPE nodetypeval;
116 NODE *(*ptrval) P((void));
119 %type <nodeval> function_prologue pattern action variable param_list
120 %type <nodeval> exp common_exp
121 %type <nodeval> simp_exp non_post_simp_exp
122 %type <nodeval> expression_list opt_expression_list print_expression_list
123 %type <nodeval> statements statement if_statement switch_body case_statements case_statement case_value opt_param_list
124 %type <nodeval> simple_stmt opt_simple_stmt
125 %type <nodeval> opt_exp opt_variable regexp
126 %type <nodeval> input_redir output_redir
127 %type <nodetypeval> print
128 %type <nodetypeval> assign_operator a_relop relop_or_less
129 %type <sval> func_name
130 %type <lval> lex_builtin
132 %token <sval> FUNC_CALL NAME REGEXP
134 %token <nodeval> YNUMBER YSTRING
135 %token <nodetypeval> RELOP IO_OUT IO_IN
136 %token <nodetypeval> ASSIGNOP ASSIGN MATCHOP CONCAT_OP
137 %token <nodetypeval> LEX_BEGIN LEX_END LEX_IF LEX_ELSE LEX_RETURN LEX_DELETE
138 %token <nodetypeval> LEX_SWITCH LEX_CASE LEX_DEFAULT LEX_WHILE LEX_DO LEX_FOR LEX_BREAK LEX_CONTINUE
139 %token <nodetypeval> LEX_PRINT LEX_PRINTF LEX_NEXT LEX_EXIT LEX_FUNCTION
140 %token <nodetypeval> LEX_GETLINE LEX_NEXTFILE
141 %token <nodetypeval> LEX_IN
142 %token <lval> LEX_AND LEX_OR INCREMENT DECREMENT
143 %token <lval> LEX_BUILTIN LEX_LENGTH
146 /* these are just yylval numbers */
148 /* Lowest to highest */
149 %right ASSIGNOP ASSIGN SLASH_BEFORE_EQUAL
155 %left FUNC_CALL LEX_BUILTIN LEX_LENGTH
158 %nonassoc RELOP '<' '>' IO_IN IO_OUT
160 %left YSTRING YNUMBER
165 %left INCREMENT DECREMENT
171 : opt_nls program opt_nls
181 begin_or_end_rule = parsing_end_rule = FALSE;
186 begin_or_end_rule = parsing_end_rule = FALSE;
188 * If errors, give up, don't produce an infinite
189 * stream of syntax error messages.
200 | pattern statement_term
202 if ($1->lnode != NULL) {
203 /* pattern rule with non-empty pattern */
204 $1->rnode = node(NULL, Node_K_print_rec, NULL);
207 if (begin_or_end_rule)
208 msg(_("%s blocks must have an action part"),
209 (parsing_end_rule ? "END" : "BEGIN"));
211 msg(_("each rule must have a pattern or an action part"));
215 | function_prologue action
219 func_install($1, $2);
227 $$ = append_pattern(&expression_value, (NODE *) NULL);
231 $$ = append_pattern(&expression_value, $1);
238 r->type = Node_line_range;
239 r->condpair = node($1, Node_cond_pair, $3);
240 r->triggered = FALSE;
241 $$ = append_pattern(&expression_value, r);
245 begin_or_end_rule = TRUE;
246 $$ = append_pattern(&begin_block, (NODE *) NULL);
250 begin_or_end_rule = parsing_end_rule = TRUE;
251 $$ = append_pattern(&end_block, (NODE *) NULL);
256 : l_brace statements r_brace opt_semi opt_nls
267 yyerror(_("`%s' is a built-in function, it cannot be redefined"),
285 func_name '(' opt_param_list r_paren opt_nls
291 $$ = append_right(t, $5);
293 /* check for duplicate parameter names */
301 * In this rule, want_regexp tells yylex that the next thing
302 * is a regexp so it should read up to the closing slash.
306 REGEXP /* The terminating '/' is consumed by yylex(). */
309 size_t len = strlen($3);
313 lintwarn(_("regexp constant `//' looks like a C++ comment, but is not"));
314 else if (($3)[0] == '*' && ($3)[len-1] == '*')
315 /* possible C comment */
316 lintwarn(_("regexp constant `/%s/' looks like a C comment, but is not"), tokstart);
319 n->type = Node_regex;
320 n->re_exp = make_string($3, len);
321 n->re_reg = make_regexp($3, len, FALSE, TRUE);
337 | statements statement
342 if (do_lint && isnoeffect($2->type))
343 lintwarn(_("statement may have no effect"));
348 ($1->type == Node_statement_list ? $1
349 : node($1, Node_statement_list, (NODE *) NULL)),
350 ($2->type == Node_statement_list ? $2
351 : node($2, Node_statement_list, (NODE *) NULL)));
367 | l_brace statements r_brace
371 | LEX_SWITCH '(' exp r_paren opt_nls l_brace switch_body opt_nls r_brace
372 { $$ = node($3, Node_K_switch, $7); }
373 | LEX_WHILE '(' exp r_paren opt_nls statement
374 { $$ = node($3, Node_K_while, $6); }
375 | LEX_DO opt_nls statement LEX_WHILE '(' exp r_paren opt_nls
376 { $$ = node($6, Node_K_do, $3); }
377 | LEX_FOR '(' NAME LEX_IN NAME r_paren opt_nls statement
380 * Efficiency hack. Recognize the special case of
385 * and treat it as if it were
389 * Check that the body is a `delete a[i]' statement,
390 * and that both the loop var and array names match.
392 if ($8 != NULL && $8->type == Node_K_delete && $8->rnode != NULL) {
395 assert($8->rnode->type == Node_expression_list);
396 arr = $8->lnode; /* array var */
397 sub = $8->rnode->lnode; /* index var */
399 if ( (arr->type == Node_var_new
400 || arr->type == Node_var_array
401 || arr->type == Node_param_list)
402 && (sub->type == Node_var_new
403 || sub->type == Node_var
404 || sub->type == Node_param_list)
405 && strcmp($3, sub->vname) == 0
406 && strcmp($5, arr->vname) == 0) {
407 $8->type = Node_K_delete_loop;
409 free($3); /* thanks to valgrind for pointing these out */
416 $$ = node($8, Node_K_arrayfor,
417 make_for_loop(variable($3, CAN_FREE, Node_var),
418 (NODE *) NULL, variable($5, CAN_FREE, Node_var_array)));
421 | LEX_FOR '(' opt_simple_stmt semi opt_nls exp semi opt_nls opt_simple_stmt r_paren opt_nls statement
423 $$ = node($12, Node_K_for, (NODE *) make_for_loop($3, $6, $9));
425 | LEX_FOR '(' opt_simple_stmt semi opt_nls semi opt_nls opt_simple_stmt r_paren opt_nls statement
427 $$ = node($11, Node_K_for,
428 (NODE *) make_for_loop($3, (NODE *) NULL, $8));
430 | LEX_BREAK statement_term
431 /* for break, maybe we'll have to remember where to break to */
432 { $$ = node((NODE *) NULL, Node_K_break, (NODE *) NULL); }
433 | LEX_CONTINUE statement_term
435 { $$ = node((NODE *) NULL, Node_K_continue, (NODE *) NULL); }
436 | LEX_NEXT statement_term
439 if (begin_or_end_rule)
440 yyerror(_("`%s' used in %s action"), "next",
441 (parsing_end_rule ? "END" : "BEGIN"));
443 $$ = node((NODE *) NULL, type, (NODE *) NULL);
445 | LEX_NEXTFILE statement_term
447 if (do_traditional) {
449 * can't use yyerror, since may have overshot
453 error(_("`nextfile' is a gawk extension"));
456 lintwarn(_("`nextfile' is a gawk extension"));
457 if (begin_or_end_rule) {
460 error(_("`%s' used in %s action"), "nextfile",
461 (parsing_end_rule ? "END" : "BEGIN"));
463 $$ = node((NODE *) NULL, Node_K_nextfile, (NODE *) NULL);
465 | LEX_EXIT opt_exp statement_term
466 { $$ = node($2, Node_K_exit, (NODE *) NULL); }
470 yyerror(_("`return' used outside function context"));
472 opt_exp statement_term
474 $$ = node($3 == NULL ? Nnull_string : $3,
475 Node_K_return, (NODE *) NULL);
477 | simple_stmt statement_term
481 * A simple_stmt exists to satisfy a constraint in the POSIX
482 * grammar allowing them to occur as the 1st and 3rd parts
483 * in a `for (...;...;...)' loop. This is a historical oddity
484 * inherited from Unix awk, not at all documented in the AK&W
485 * awk book. We support it, as this was reported as a bug.
486 * We don't bother to document it though. So there.
489 : print { in_print = TRUE; in_parens = 0; } print_expression_list output_redir
492 * Optimization: plain `print' has no expression list, so $3 is null.
493 * If $3 is an expression list with one element (rnode == null)
494 * and lnode is a field spec for field 0, we have `print $0'.
495 * For both, use Node_K_print_rec, which is faster for these two cases.
497 if ($1 == Node_K_print &&
499 || ($3->type == Node_expression_list
501 && $3->lnode->type == Node_field_spec
502 && $3->lnode->lnode->type == Node_val
503 && $3->lnode->lnode->numbr == 0.0))
505 static int warned = FALSE;
507 $$ = node(NULL, Node_K_print_rec, $4);
509 if (do_lint && $3 == NULL && begin_or_end_rule && ! warned) {
512 _("plain `print' in BEGIN or END rule should probably be `print \"\"'"));
515 $$ = node($3, $1, $4);
516 if ($$->type == Node_K_printf)
520 | LEX_DELETE NAME '[' expression_list ']'
521 { $$ = node(variable($2, CAN_FREE, Node_var_array), Node_K_delete, $4); }
525 lintwarn(_("`delete array' is a gawk extension"));
526 if (do_traditional) {
528 * can't use yyerror, since may have overshot
532 error(_("`delete array' is a gawk extension"));
534 $$ = node(variable($2, CAN_FREE, Node_var_array), Node_K_delete, (NODE *) NULL);
536 | LEX_DELETE '(' NAME ')'
538 /* this is for tawk compatibility. maybe the warnings should always be done. */
540 lintwarn(_("`delete(array)' is a non-portable tawk extension"));
541 if (do_traditional) {
543 * can't use yyerror, since may have overshot
547 error(_("`delete(array)' is a non-portable tawk extension"));
549 $$ = node(variable($3, CAN_FREE, Node_var_array), Node_K_delete, (NODE *) NULL);
572 const char **case_values = NULL;
578 emalloc(case_values, const char **, sizeof(char*) * maxcount, "switch_body");
579 for (curr = $1; curr != NULL; curr = curr->rnode) {
580 /* Assure that case statement values are unique. */
581 if (curr->lnode->type == Node_K_case) {
584 if (curr->lnode->lnode->type == Node_regex)
585 caseval = curr->lnode->lnode->re_exp->stptr;
587 caseval = force_string(tree_eval(curr->lnode->lnode))->stptr;
589 for (i = 0; i < case_count; i++)
590 if (strcmp(caseval, case_values[i]) == 0)
591 yyerror(_("duplicate case values in switch body: %s"), caseval);
593 if (case_count >= maxcount) {
595 erealloc(case_values, const char **, sizeof(char*) * maxcount, "switch_body");
597 case_values[case_count++] = caseval;
599 /* Otherwise save a pointer to the default node. */
601 yyerror(_("Duplicate `default' detected in switch body"));
608 /* Create the switch body. */
609 $$ = node(head, Node_switch_body, dflt);
617 | case_statements case_statement
622 if (do_lint && isnoeffect($2->type))
623 lintwarn(_("statement may have no effect"));
625 $$ = node($2, Node_case_list, (NODE *) NULL);
628 ($1->type == Node_case_list ? $1 : node($1, Node_case_list, (NODE *) NULL)),
629 ($2->type == Node_case_list ? $2 : node($2, Node_case_list, (NODE *) NULL))
634 | case_statements error
639 : LEX_CASE case_value colon opt_nls statements
640 { $$ = node($2, Node_K_case, $5); }
641 | LEX_DEFAULT colon opt_nls statements
642 { $$ = node((NODE *) NULL, Node_K_default, $4); }
648 | '-' YNUMBER %prec UNARY
650 $2->numbr = -(force_number($2));
653 | '+' YNUMBER %prec UNARY
667 * Note: ``print(x)'' is already parsed by the first rule,
668 * so there is no good in covering it by the second one too.
670 print_expression_list
671 : opt_expression_list
672 | '(' exp comma expression_list r_paren
673 { $$ = node($2, Node_expression_list, $4); }
683 | IO_OUT { in_print = FALSE; in_parens = 0; } common_exp
685 $$ = node($3, $1, (NODE *) NULL);
686 if ($1 == Node_redirect_twoway
687 && $3->type == Node_K_getline
689 && $3->rnode->type == Node_redirect_twoway)
690 yyerror(_("multistage two-way pipelines don't work"));
695 : LEX_IF '(' exp r_paren opt_nls statement
697 $$ = node($3, Node_K_if,
698 node($6, Node_if_branches, (NODE *) NULL));
700 | LEX_IF '(' exp r_paren opt_nls statement
701 LEX_ELSE opt_nls statement
702 { $$ = node($3, Node_K_if,
703 node($6, Node_if_branches, $9)); }
720 { $$ = node($2, Node_redirect_input, (NODE *) NULL); }
732 { $$ = make_param($1); }
733 | param_list comma NAME
734 { $$ = append_right($1, make_param($3)); yyerrok; }
739 | param_list comma error
743 /* optional expression, as in for loop */
760 { $$ = node($1, Node_expression_list, (NODE *) NULL); }
761 | expression_list comma exp
763 $$ = append_right($1,
764 node($3, Node_expression_list, (NODE *) NULL));
769 | expression_list error
771 | expression_list error exp
773 | expression_list comma error
777 /* Expressions, not including the comma operator. */
778 exp : variable assign_operator exp %prec ASSIGNOP
780 if (do_lint && $3->type == Node_regex)
781 lintwarn(_("regular expression on right of assignment"));
783 * Optimization of `x = x y'. Can save lots of time
786 if (( $1->type == Node_var
787 || $1->type == Node_var_new
788 || $1->type == Node_param_list)
790 && $3->type == Node_concat
791 && $3->lnode == $1) {
792 $3->type = Node_assign_concat; /* Just change the type */
793 $$ = $3; /* And use it directly */
795 $$ = node($1, $2, $3);
798 { $$ = node($1, Node_and, $3); }
800 { $$ = node($1, Node_or, $3); }
803 if ($1->type == Node_regex)
804 warning(_("regular expression on left of `~' or `!~' operator"));
805 $$ = node($1, $2, mk_rexp($3));
808 { $$ = node(variable($3, CAN_FREE, Node_var_array), Node_in_array, $1); }
809 | exp a_relop exp %prec RELOP
811 if (do_lint && $3->type == Node_regex)
812 lintwarn(_("regular expression on right of comparison"));
813 $$ = node($1, $2, $3);
815 | exp '?' exp ':' exp
816 { $$ = node($1, Node_cond_exp, node($3, Node_if_branches, $5));}
826 | SLASH_BEFORE_EQUAL ASSIGN /* `/=' */
827 { $$ = Node_assign_quotient; }
839 { $$ = Node_greater; }
845 | '!' regexp %prec UNARY
847 $$ = node(node(make_number(0.0),
853 | '(' expression_list r_paren LEX_IN NAME
854 { $$ = node(variable($5, CAN_FREE, Node_var_array), Node_in_array, $2); }
857 | common_exp simp_exp %prec CONCAT_OP
858 { $$ = node($1, Node_concat, $2); }
863 /* Binary operators in order of decreasing precedence. */
864 | simp_exp '^' simp_exp
865 { $$ = node($1, Node_exp, $3); }
866 | simp_exp '*' simp_exp
867 { $$ = node($1, Node_times, $3); }
868 | simp_exp '/' simp_exp
869 { $$ = node($1, Node_quotient, $3); }
870 | simp_exp '%' simp_exp
871 { $$ = node($1, Node_mod, $3); }
872 | simp_exp '+' simp_exp
873 { $$ = node($1, Node_plus, $3); }
874 | simp_exp '-' simp_exp
875 { $$ = node($1, Node_minus, $3); }
876 | LEX_GETLINE opt_variable input_redir
878 if (do_lint && parsing_end_rule && $3 == NULL)
879 lintwarn(_("non-redirected `getline' undefined inside END action"));
880 $$ = node($2, Node_K_getline, $3);
882 | simp_exp IO_IN LEX_GETLINE opt_variable
884 $$ = node($4, Node_K_getline,
885 node($1, $2, (NODE *) NULL));
888 { $$ = node($1, Node_postincrement, (NODE *) NULL); }
890 { $$ = node($1, Node_postdecrement, (NODE *) NULL); }
894 : '!' simp_exp %prec UNARY
895 { $$ = node($2, Node_not, (NODE *) NULL); }
899 '(' opt_expression_list r_paren
900 { $$ = snode($3, Node_builtin, (int) $1); }
901 | LEX_LENGTH '(' opt_expression_list r_paren
902 { $$ = snode($3, Node_builtin, (int) $1); }
906 lintwarn(_("call of `length' without parentheses is not portable"));
907 $$ = snode((NODE *) NULL, Node_builtin, (int) $1);
909 warning(_("call of `length' without parentheses is deprecated by POSIX"));
911 | FUNC_CALL '(' opt_expression_list r_paren
913 $$ = node($3, Node_func_call, make_string($1, strlen($1)));
915 func_use($1, FUNC_USE);
921 { $$ = node($2, Node_preincrement, (NODE *) NULL); }
923 { $$ = node($2, Node_predecrement, (NODE *) NULL); }
929 | '-' simp_exp %prec UNARY
931 if ($2->type == Node_val && ($2->flags & (STRCUR|STRING)) == 0) {
932 $2->numbr = -(force_number($2));
935 $$ = node($2, Node_unary_minus, (NODE *) NULL);
937 | '+' simp_exp %prec UNARY
941 * POSIX semantics: force a conversion to numeric type
943 $$ = node (make_number(0.0), Node_plus, $2);
956 { $$ = variable($1, CAN_FREE, Node_var_new); }
957 | NAME '[' expression_list ']'
961 if ((n = lookup($1)) != NULL && ! isarray(n))
962 yyerror(_("use of non-array as array"));
963 else if ($3 == NULL) {
964 fatal(_("invalid subscript expression"));
965 } else if ($3->rnode == NULL) {
966 $$ = node(variable($1, CAN_FREE, Node_var_array), Node_subscript, $3->lnode);
969 $$ = node(variable($1, CAN_FREE, Node_var_array), Node_subscript, $3);
971 | '$' non_post_simp_exp
972 { $$ = node($2, Node_field_spec, (NODE *) NULL); }
976 { fatal(_("can't use built-in function `%s' as a variable"), tokstart); }
986 : '}' opt_nls { yyerrok; }
1006 comma : ',' opt_nls { yyerrok; }
1012 const char *operator; /* text to match */
1013 NODETYPE value; /* node type */
1014 int class; /* lexical class */
1015 unsigned flags; /* # of args. allowed and compatability */
1016 # define ARGS 0xFF /* 0, 1, 2, 3 args allowed (any combination */
1017 # define A(n) (1<<(n))
1018 # define VERSION_MASK 0xFF00 /* old awk is zero */
1019 # define NOT_OLD 0x0100 /* feature not in old awk */
1020 # define NOT_POSIX 0x0200 /* feature not in POSIX */
1021 # define GAWKX 0x0400 /* gawk extension */
1022 # define RESX 0x0800 /* Bell Labs Research extension */
1023 NODE *(*ptr) P((NODE *)); /* function that implements this keyword */
1026 /* Tokentab is sorted ascii ascending order, so it can be binary searched. */
1027 /* Function pointers come from declarations in awk.h. */
1029 static const struct token tokentab[] = {
1030 {"BEGIN", Node_illegal, LEX_BEGIN, 0, 0},
1031 {"END", Node_illegal, LEX_END, 0, 0},
1033 {"adump", Node_builtin, LEX_BUILTIN, GAWKX|A(1), do_adump},
1035 {"and", Node_builtin, LEX_BUILTIN, GAWKX|A(2), do_and},
1036 {"asort", Node_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2), do_asort},
1037 {"asorti", Node_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2), do_asorti},
1038 {"atan2", Node_builtin, LEX_BUILTIN, NOT_OLD|A(2), do_atan2},
1039 {"bindtextdomain", Node_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2), do_bindtextdomain},
1040 {"break", Node_K_break, LEX_BREAK, 0, 0},
1042 {"case", Node_K_case, LEX_CASE, GAWKX, 0},
1044 {"close", Node_builtin, LEX_BUILTIN, NOT_OLD|A(1)|A(2), do_close},
1045 {"compl", Node_builtin, LEX_BUILTIN, GAWKX|A(1), do_compl},
1046 {"continue", Node_K_continue, LEX_CONTINUE, 0, 0},
1047 {"cos", Node_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_cos},
1048 {"dcgettext", Node_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2)|A(3), do_dcgettext},
1049 {"dcngettext", Node_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2)|A(3)|A(4)|A(5), do_dcngettext},
1051 {"default", Node_K_default, LEX_DEFAULT, GAWKX, 0},
1053 {"delete", Node_K_delete, LEX_DELETE, NOT_OLD, 0},
1054 {"do", Node_K_do, LEX_DO, NOT_OLD, 0},
1055 {"else", Node_illegal, LEX_ELSE, 0, 0},
1056 {"exit", Node_K_exit, LEX_EXIT, 0, 0},
1057 {"exp", Node_builtin, LEX_BUILTIN, A(1), do_exp},
1058 {"extension", Node_builtin, LEX_BUILTIN, GAWKX|A(2), do_ext},
1059 {"fflush", Node_builtin, LEX_BUILTIN, RESX|A(0)|A(1), do_fflush},
1060 {"for", Node_K_for, LEX_FOR, 0, 0},
1061 {"func", Node_K_function, LEX_FUNCTION, NOT_POSIX|NOT_OLD, 0},
1062 {"function", Node_K_function, LEX_FUNCTION, NOT_OLD, 0},
1063 {"gensub", Node_builtin, LEX_BUILTIN, GAWKX|A(3)|A(4), do_gensub},
1064 {"getline", Node_K_getline, LEX_GETLINE, NOT_OLD, 0},
1065 {"gsub", Node_builtin, LEX_BUILTIN, NOT_OLD|A(2)|A(3), do_gsub},
1066 {"if", Node_K_if, LEX_IF, 0, 0},
1067 {"in", Node_illegal, LEX_IN, 0, 0},
1068 {"index", Node_builtin, LEX_BUILTIN, A(2), do_index},
1069 {"int", Node_builtin, LEX_BUILTIN, A(1), do_int},
1070 {"length", Node_builtin, LEX_LENGTH, A(0)|A(1), do_length},
1071 {"log", Node_builtin, LEX_BUILTIN, A(1), do_log},
1072 {"lshift", Node_builtin, LEX_BUILTIN, GAWKX|A(2), do_lshift},
1073 {"match", Node_builtin, LEX_BUILTIN, NOT_OLD|A(2)|A(3), do_match},
1074 {"mktime", Node_builtin, LEX_BUILTIN, GAWKX|A(1), do_mktime},
1075 {"next", Node_K_next, LEX_NEXT, 0, 0},
1076 {"nextfile", Node_K_nextfile, LEX_NEXTFILE, GAWKX, 0},
1077 {"or", Node_builtin, LEX_BUILTIN, GAWKX|A(2), do_or},
1078 {"print", Node_K_print, LEX_PRINT, 0, 0},
1079 {"printf", Node_K_printf, LEX_PRINTF, 0, 0},
1080 {"rand", Node_builtin, LEX_BUILTIN, NOT_OLD|A(0), do_rand},
1081 {"return", Node_K_return, LEX_RETURN, NOT_OLD, 0},
1082 {"rshift", Node_builtin, LEX_BUILTIN, GAWKX|A(2), do_rshift},
1083 {"sin", Node_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_sin},
1084 {"split", Node_builtin, LEX_BUILTIN, A(2)|A(3), do_split},
1085 {"sprintf", Node_builtin, LEX_BUILTIN, 0, do_sprintf},
1086 {"sqrt", Node_builtin, LEX_BUILTIN, A(1), do_sqrt},
1087 {"srand", Node_builtin, LEX_BUILTIN, NOT_OLD|A(0)|A(1), do_srand},
1088 #if defined(GAWKDEBUG) || defined(ARRAYDEBUG) /* || ... */
1089 {"stopme", Node_builtin, LEX_BUILTIN, GAWKX|A(0), stopme},
1091 {"strftime", Node_builtin, LEX_BUILTIN, GAWKX|A(0)|A(1)|A(2), do_strftime},
1092 {"strtonum", Node_builtin, LEX_BUILTIN, GAWKX|A(1), do_strtonum},
1093 {"sub", Node_builtin, LEX_BUILTIN, NOT_OLD|A(2)|A(3), do_sub},
1094 {"substr", Node_builtin, LEX_BUILTIN, A(2)|A(3), do_substr},
1096 {"switch", Node_K_switch, LEX_SWITCH, GAWKX, 0},
1098 {"system", Node_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_system},
1099 {"systime", Node_builtin, LEX_BUILTIN, GAWKX|A(0), do_systime},
1100 {"tolower", Node_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_tolower},
1101 {"toupper", Node_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_toupper},
1102 {"while", Node_K_while, LEX_WHILE, 0, 0},
1103 {"xor", Node_builtin, LEX_BUILTIN, GAWKX|A(2), do_xor},
1107 /* Variable containing the current shift state. */
1108 static mbstate_t cur_mbstate;
1109 /* Ring buffer containing current characters. */
1110 #define MAX_CHAR_IN_RING_BUFFER 8
1111 #define RING_BUFFER_SIZE (MAX_CHAR_IN_RING_BUFFER * MB_LEN_MAX)
1112 static char cur_char_ring[RING_BUFFER_SIZE];
1113 /* Index for ring buffers. */
1114 static int cur_ring_idx;
1115 /* This macro means that last nextc() return a singlebyte character
1116 or 1st byte of a multibyte character. */
1117 #define nextc_is_1stbyte (cur_char_ring[cur_ring_idx] == 1)
1118 #else /* MBS_SUPPORT */
1120 #define nextc_is_1stbyte 1
1121 #endif /* MBS_SUPPORT */
1123 /* getfname --- return name of a builtin function (for pretty printing) */
1126 getfname(register NODE *(*fptr)(NODE *))
1130 j = sizeof(tokentab) / sizeof(tokentab[0]);
1131 /* linear search, no other way to do it */
1132 for (i = 0; i < j; i++)
1133 if (tokentab[i].ptr == fptr)
1134 return tokentab[i].operator;
1139 /* yyerror --- print a syntax error message, show where */
1142 * Function identifier purposely indented to avoid mangling
1143 * by ansi2knr. Sigh.
1147 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
1148 yyerror(const char *m, ...)
1156 const char *mesg = NULL;
1157 register char *bp, *cp;
1161 static char end_of_file_line[] = "(END OF FILE)";
1165 /* Find the current line in the input file */
1166 if (lexptr && lexeme) {
1167 if (thisline == NULL) {
1171 mesg = _("unexpected newline or end of string");
1173 for (; cp != lexptr_begin && *cp != '\n'; --cp)
1179 /* NL isn't guaranteed */
1181 while (bp < lexend && *bp && *bp != '\n')
1184 thisline = end_of_file_line;
1185 bp = thisline + strlen(thisline);
1189 * Saving and restoring *bp keeps valgrind happy,
1190 * since the guts of glibc uses strlen, even though
1191 * we're passing an explict precision. Sigh.
1193 * 8/2003: We may not need this anymore.
1198 msg("%.*s", (int) (bp - thisline), thisline);
1202 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
1209 mesg = va_arg(args, char *);
1211 count = (bp - thisline) + strlen(mesg) + 2 + 1;
1212 emalloc(buf, char *, count, "yyerror");
1216 if (lexptr != NULL) {
1218 while (scan < lexeme)
1219 if (*scan++ == '\t')
1232 /* get_src_buf --- read the next buffer of source program */
1237 static int samefile = FALSE;
1238 static int nextfile = 0;
1239 static char *buf = NULL;
1240 static size_t buflen = 0;
1244 register char *scan;
1253 if (nextfile > numfiles)
1256 if (srcfiles[nextfile].stype == CMDLINE) {
1257 if ((l = strlen(srcfiles[nextfile].val)) == 0) {
1259 * Yet Another Special case:
1260 * gawk '' /path/name
1263 static int warned = FALSE;
1265 if (do_lint && ! warned) {
1267 lintwarn(_("empty program text on command line"));
1272 if (srcfiles[nextfile].val[l-1] == '\n') {
1273 /* has terminating newline, can use it directly */
1275 lexptr = lexptr_begin = srcfiles[nextfile].val;
1276 /* fall through to pointer adjustment and return, below */
1278 /* copy it into static buffer */
1280 /* make sure buffer exists and has room */
1282 emalloc(buf, char *, l+2, "get_src_buf");
1284 } else if (l+2 > buflen) {
1285 erealloc(buf, char *, l+2, "get_src_buf");
1288 buffer has room, just use it */
1291 memcpy(buf, srcfiles[nextfile].val, l);
1295 /* set vars and return */
1296 lexptr = lexptr_begin = buf;
1298 lexend = lexptr + l;
1299 nextfile++; /* for next entry to this routine */
1304 source = srcfiles[nextfile].val;
1305 if (source == NULL) { /* read all the source files, all done */
1311 return lexeme = lexptr = lexptr_begin = NULL;
1313 fd = pathopen(source);
1314 if (fd <= INVALID_HANDLE) {
1317 /* suppress file name and line no. in error mesg */
1320 fatal(_("can't open source file `%s' for reading (%s)"),
1321 in, strerror(errno));
1323 l = optimal_bufsize(fd, & sbuf);
1325 * Make sure that something silly like
1326 * AWKBUFSIZE=8 make check
1329 #define A_DECENT_BUFFER_SIZE 128
1330 if (l < A_DECENT_BUFFER_SIZE)
1331 l = A_DECENT_BUFFER_SIZE;
1332 #undef A_DECENT_BUFFER_SIZE
1336 /* make sure buffer exists and has room */
1338 emalloc(buf, char *, l+2, "get_src_buf");
1340 } else if (l+2 > buflen) {
1341 erealloc(buf, char *, l+2, "get_src_buf");
1344 buffer has room, just use it */
1347 readloc = lexeme = lexptr = lexptr_begin = buf;
1352 * In same file, ran off edge of buffer.
1353 * Shift current line down to front, adjust
1354 * pointers and fill in the rest of the buffer.
1357 int lexeme_offset = lexeme - lexptr_begin;
1358 int lexptr_offset = lexptr - lexptr_begin;
1359 int lexend_offset = lexend - lexptr_begin;
1361 /* find beginning of current line */
1362 for (scan = lexeme; scan >= lexptr_begin; scan--) {
1363 if (*scan == '\n') {
1370 /* have to grow the buffer */
1372 erealloc(buf, char *, buflen, "get_src_buf");
1374 /* shift things down */
1375 memmove(buf, scan, lexend - scan);
1377 * make offsets relative to start of line,
1378 * not start of buffer.
1380 lexend_offset = lexend - scan;
1381 lexeme_offset = lexeme - scan;
1382 lexptr_offset = lexptr - scan;
1385 /* adjust pointers */
1386 lexeme = buf + lexeme_offset;
1387 lexptr = buf + lexptr_offset;
1388 lexend = buf + lexend_offset;
1390 readcount = buflen - (lexend - buf);
1394 /* add more data to buffer */
1395 n = read(fd, readloc, readcount);
1397 fatal(_("can't read sourcefile `%s' (%s)"),
1398 source, strerror(errno));
1401 static int warned = FALSE;
1403 if (do_lint && ! warned) {
1405 lintwarn(_("source file `%s' is empty"), source);
1408 if (fd != fileno(stdin)) /* safety */
1414 lexend = lexptr + n;
1418 /* tokadd --- add a character to the token buffer */
1420 #define tokadd(x) (*tok++ = (x), tok == tokend ? tokexpand() : tok)
1422 /* tokexpand --- grow the token buffer */
1427 static int toksize = 60;
1430 tokoffset = tok - tokstart;
1432 if (tokstart != NULL)
1433 erealloc(tokstart, char *, toksize, "tokexpand");
1435 emalloc(tokstart, char *, toksize, "tokexpand");
1436 tokend = tokstart + toksize;
1437 tok = tokstart + tokoffset;
1441 /* nextc --- get the next input character */
1448 if (gawk_mb_cur_max > 1) {
1449 if (!lexptr || lexptr >= lexend) {
1450 if (! get_src_buf())
1454 /* Update the buffer index. */
1455 cur_ring_idx = (cur_ring_idx == RING_BUFFER_SIZE - 1)? 0 :
1458 /* Did we already check the current character? */
1459 if (cur_char_ring[cur_ring_idx] == 0) {
1460 /* No, we need to check the next character on the buffer. */
1461 int idx, work_ring_idx = cur_ring_idx;
1462 mbstate_t tmp_state;
1465 for (idx = 0 ; lexptr + idx < lexend ; idx++) {
1466 tmp_state = cur_mbstate;
1467 mbclen = mbrlen(lexptr, idx + 1, &tmp_state);
1469 if (mbclen == 1 || mbclen == (size_t)-1 || mbclen == 0) {
1470 /* It is a singlebyte character, non-complete multibyte
1471 character or EOF. We treat it as a singlebyte
1473 cur_char_ring[work_ring_idx] = 1;
1475 } else if (mbclen == (size_t)-2) {
1476 /* It is not a complete multibyte character. */
1477 cur_char_ring[work_ring_idx] = idx + 1;
1480 cur_char_ring[work_ring_idx] = mbclen;
1483 work_ring_idx = (work_ring_idx == RING_BUFFER_SIZE - 1)?
1484 0 : work_ring_idx + 1;
1486 cur_mbstate = tmp_state;
1488 /* Put a mark on the position on which we write next character. */
1489 work_ring_idx = (work_ring_idx == RING_BUFFER_SIZE - 1)?
1490 0 : work_ring_idx + 1;
1491 cur_char_ring[work_ring_idx] = 0;
1494 return (int) (unsigned char) *lexptr++;
1499 if (lexptr && lexptr < lexend)
1500 c = (int) (unsigned char) *lexptr++;
1501 else if (get_src_buf())
1502 c = (int) (unsigned char) *lexptr++;
1510 #else /* MBS_SUPPORT */
1518 if (lexptr && lexptr < lexend)
1519 c = (int) (unsigned char) *lexptr++;
1520 else if (get_src_buf())
1521 c = (int) (unsigned char) *lexptr++;
1528 #define nextc() ((lexptr && lexptr < lexend) ? \
1529 ((int) (unsigned char) *lexptr++) : \
1530 (get_src_buf() ? ((int) (unsigned char) *lexptr++) : EOF) \
1534 #endif /* MBS_SUPPORT */
1536 /* pushback --- push a character back on the input */
1542 if (gawk_mb_cur_max > 1)
1543 cur_ring_idx = (cur_ring_idx == 0)? RING_BUFFER_SIZE - 1 :
1546 (lexptr && lexptr > lexptr_begin ? lexptr-- : lexptr);
1550 /* allow_newline --- allow newline after &&, ||, ? and : */
1562 while ((c = nextc()) != '\n' && c != EOF)
1576 /* yylex --- Read the input and turn it into tokens. */
1582 int seen_e = FALSE; /* These are for numbers */
1583 int seen_point = FALSE;
1584 int esc_seen; /* for literal strings */
1586 static int did_newline = FALSE;
1588 static int lasttok = 0, eof_warned = FALSE;
1590 int intlstr = FALSE;
1592 if (nextc() == EOF) {
1593 if (lasttok != NEWLINE) {
1595 if (do_lint && ! eof_warned) {
1596 lintwarn(_("source file does not end in newline"));
1599 return NEWLINE; /* fake it */
1604 #if defined OS2 || defined __EMX__
1606 * added for OS/2's extproc feature of cmd.exe
1607 * (like #! in BSD sh)
1609 if (strncasecmp(lexptr, "extproc ", 8) == 0) {
1610 while (*lexptr && *lexptr != '\n')
1617 int in_brack = 0; /* count brackets, [[:alnum:]] allowed */
1619 * Counting brackets is non-trivial. [[] is ok,
1620 * and so is [\]], with a point being that /[/]/ as a regexp
1621 * constant has to work.
1623 * Do not count [ or ] if either one is preceded by a \.
1624 * A `[' should be counted if
1625 * a) it is the first one so far (in_brack == 0)
1626 * b) it is the `[' in `[:'
1627 * A ']' should be counted if not preceded by a \, since
1628 * it is either closing `:]' or just a plain list.
1629 * According to POSIX, []] is how you put a ] into a set.
1630 * Try to handle that too.
1632 * The code for \ handles \[ and \].
1635 want_regexp = FALSE;
1640 if (gawk_mb_cur_max == 1 || nextc_is_1stbyte) switch (c) {
1642 /* one day check for `.' and `=' too */
1643 if (nextc() == ':' || in_brack == 0)
1648 if (tokstart[0] == '['
1649 && (tok == tokstart + 1
1650 || (tok == tokstart + 2
1651 && tokstart[1] == '^')))
1657 if ((c = nextc()) == EOF) {
1658 yyerror(_("unterminated regexp ends with `\\' at end of file"));
1659 goto end_regexp; /* kludge */
1660 } else if (c == '\n') {
1669 case '/': /* end of the regexp */
1674 yylval.sval = tokstart;
1679 if (peek == 'i' || peek == 's') {
1682 _("%s: %d: tawk regex modifier `/.../%c' doesn't work in gawk"),
1683 source, sourceline, peek);
1686 _("tawk regex modifier `/.../%c' doesn't work in gawk"),
1690 return lasttok = REGEXP;
1693 yyerror(_("unterminated regexp"));
1694 goto end_regexp; /* kludge */
1696 yyerror(_("unterminated regexp at end of file"));
1697 goto end_regexp; /* kludge */
1704 /* skipping \r is a hack, but windows is just too pervasive. sigh. */
1705 while ((c = nextc()) == ' ' || c == '\t' || c == '\r')
1708 lexeme = lexptr ? lexptr - 1 : lexptr;
1711 yylval.nodetypeval = Node_illegal;
1713 if (gawk_mb_cur_max == 1 || nextc_is_1stbyte) switch (c) {
1715 if (lasttok != NEWLINE) {
1717 if (do_lint && ! eof_warned) {
1718 lintwarn(_("source file does not end in newline"));
1721 return NEWLINE; /* fake it */
1727 return lasttok = NEWLINE;
1729 case '#': /* it's a comment */
1730 while ((c = nextc()) != '\n') {
1732 if (lasttok != NEWLINE) {
1734 if (do_lint && ! eof_warned) {
1736 _("source file does not end in newline"));
1739 return NEWLINE; /* fake it */
1745 return lasttok = NEWLINE;
1748 #ifdef RELAXED_CONTINUATION
1750 * This code puports to allow comments and/or whitespace
1751 * after the `\' at the end of a line used for continuation.
1752 * Use it at your own risk. We think it's a bad idea, which
1753 * is why it's not on by default.
1755 if (! do_traditional) {
1756 /* strip trailing white-space and/or comment */
1757 while ((c = nextc()) == ' ' || c == '\t' || c == '\r')
1762 _("use of `\\ #...' line continuation is not portable"));
1763 while ((c = nextc()) != '\n')
1769 #endif /* RELAXED_CONTINUATION */
1770 if (nextc() == '\n') {
1774 yyerror(_("backslash not last character on line"));
1786 * in_parens is undefined unless we are parsing a print
1787 * statement (in_print), but why bother with a check?
1805 if ((c = nextc()) == '=') {
1806 yylval.nodetypeval = Node_assign_times;
1807 return lasttok = ASSIGNOP;
1808 } else if (do_posix) {
1810 return lasttok = '*';
1811 } else if (c == '*') {
1812 /* make ** and **= aliases for ^ and ^= */
1813 static int did_warn_op = FALSE, did_warn_assgn = FALSE;
1815 if (nextc() == '=') {
1816 if (! did_warn_assgn) {
1817 did_warn_assgn = TRUE;
1819 lintwarn(_("POSIX does not allow operator `**='"));
1821 warning(_("old awk does not support operator `**='"));
1823 yylval.nodetypeval = Node_assign_exp;
1827 if (! did_warn_op) {
1830 lintwarn(_("POSIX does not allow operator `**'"));
1832 warning(_("old awk does not support operator `**'"));
1834 return lasttok = '^';
1838 return lasttok = '*';
1841 if (nextc() == '=') {
1843 return lasttok = SLASH_BEFORE_EQUAL;
1846 return lasttok = '/';
1849 if (nextc() == '=') {
1850 yylval.nodetypeval = Node_assign_mod;
1851 return lasttok = ASSIGNOP;
1854 return lasttok = '%';
1858 static int did_warn_op = FALSE, did_warn_assgn = FALSE;
1860 if (nextc() == '=') {
1861 if (do_lint_old && ! did_warn_assgn) {
1862 did_warn_assgn = TRUE;
1863 warning(_("operator `^=' is not supported in old awk"));
1865 yylval.nodetypeval = Node_assign_exp;
1866 return lasttok = ASSIGNOP;
1869 if (do_lint_old && ! did_warn_op) {
1871 warning(_("operator `^' is not supported in old awk"));
1873 return lasttok = '^';
1877 if ((c = nextc()) == '=') {
1878 yylval.nodetypeval = Node_assign_plus;
1879 return lasttok = ASSIGNOP;
1882 return lasttok = INCREMENT;
1884 return lasttok = '+';
1887 if ((c = nextc()) == '=') {
1888 yylval.nodetypeval = Node_notequal;
1889 return lasttok = RELOP;
1892 yylval.nodetypeval = Node_nomatch;
1893 return lasttok = MATCHOP;
1896 return lasttok = '!';
1899 if (nextc() == '=') {
1900 yylval.nodetypeval = Node_leq;
1901 return lasttok = RELOP;
1903 yylval.nodetypeval = Node_less;
1905 return lasttok = '<';
1908 if (nextc() == '=') {
1909 yylval.nodetypeval = Node_equal;
1910 return lasttok = RELOP;
1912 yylval.nodetypeval = Node_assign;
1914 return lasttok = ASSIGN;
1917 if ((c = nextc()) == '=') {
1918 yylval.nodetypeval = Node_geq;
1919 return lasttok = RELOP;
1920 } else if (c == '>') {
1921 yylval.nodetypeval = Node_redirect_append;
1922 return lasttok = IO_OUT;
1925 if (in_print && in_parens == 0) {
1926 yylval.nodetypeval = Node_redirect_output;
1927 return lasttok = IO_OUT;
1929 yylval.nodetypeval = Node_greater;
1930 return lasttok = '>';
1933 yylval.nodetypeval = Node_match;
1934 return lasttok = MATCHOP;
1938 * Added did newline stuff. Easier than
1939 * hacking the grammar.
1942 did_newline = FALSE;
1946 --lexptr; /* pick up } next time */
1947 return lasttok = NEWLINE;
1952 while ((c = nextc()) != '"') {
1955 yyerror(_("unterminated string"));
1958 if ((gawk_mb_cur_max == 1 || nextc_is_1stbyte) &&
1970 yyerror(_("unterminated string"));
1975 yylval.nodeval = make_str_node(tokstart,
1976 tok - tokstart, esc_seen ? SCAN : 0);
1977 yylval.nodeval->flags |= PERM;
1979 yylval.nodeval->flags |= INTLSTR;
1982 dumpintlstr(yylval.nodeval->stptr,
1983 yylval.nodeval->stlen);
1985 return lasttok = YSTRING;
1988 if ((c = nextc()) == '=') {
1989 yylval.nodetypeval = Node_assign_minus;
1990 return lasttok = ASSIGNOP;
1993 return lasttok = DECREMENT;
1995 return lasttok = '-';
2001 return lasttok = '.';
2017 int gotnumber = FALSE;
2025 if (tok == tokstart + 2) {
2028 if (ISXDIGIT(peek)) {
2030 pushback(); /* following digit */
2032 pushback(); /* x or X */
2038 /* period ends exponent part of floating point number */
2039 if (seen_point || seen_e) {
2054 if ((c = nextc()) == '-' || c == '+') {
2061 pushback(); /* non-digit after + or - */
2062 pushback(); /* + or - */
2063 pushback(); /* e or E */
2065 } else if (! ISDIGIT(c)) {
2066 pushback(); /* character after e or E */
2067 pushback(); /* e or E */
2069 pushback(); /* digit */
2082 if (do_traditional || ! inhex)
2106 else if (do_lint && ! eof_warned) {
2107 lintwarn(_("source file does not end in newline"));
2111 if (! do_traditional && isnondecimal(tokstart, FALSE)) {
2113 if (ISDIGIT(tokstart[1])) /* not an 'x' or 'X' */
2114 lintwarn("numeric constant `%.*s' treated as octal",
2115 (int) strlen(tokstart)-1, tokstart);
2116 else if (tokstart[1] == 'x' || tokstart[1] == 'X')
2117 lintwarn("numeric constant `%.*s' treated as hexadecimal",
2118 (int) strlen(tokstart)-1, tokstart);
2120 yylval.nodeval = make_number(nondec2awknum(tokstart, strlen(tokstart)));
2122 yylval.nodeval = make_number(atof(tokstart));
2123 yylval.nodeval->flags |= PERM;
2124 return lasttok = YNUMBER;
2127 if ((c = nextc()) == '&') {
2128 yylval.nodetypeval = Node_and;
2130 return lasttok = LEX_AND;
2133 return lasttok = '&';
2136 if ((c = nextc()) == '|') {
2137 yylval.nodetypeval = Node_or;
2139 return lasttok = LEX_OR;
2140 } else if (! do_traditional && c == '&') {
2141 yylval.nodetypeval = Node_redirect_twoway;
2142 return lasttok = (in_print && in_parens == 0 ? IO_OUT : IO_IN);
2145 if (in_print && in_parens == 0) {
2146 yylval.nodetypeval = Node_redirect_pipe;
2147 return lasttok = IO_OUT;
2149 yylval.nodetypeval = Node_redirect_pipein;
2150 return lasttok = IO_IN;
2154 if (c != '_' && ! ISALPHA(c)) {
2155 yyerror(_("invalid char '%c' in expression"), c);
2160 * Lots of fog here. Consider:
2162 * print "xyzzy"$_"foo"
2164 * Without the check for ` lasttok != '$' ', this is parsed as
2166 * print "xxyzz" $(_"foo")
2168 * With the check, it is "correctly" parsed as three
2169 * string concatenations. Sigh. This seems to be
2170 * "more correct", but this is definitely one of those
2171 * occasions where the interactions are funny.
2173 if (! do_traditional && c == '_' && lasttok != '$') {
2174 if ((c = nextc()) == '"') {
2182 /* it's some type of name-type-thing. Find its length. */
2184 while (is_identchar(c)) {
2189 emalloc(tokkey, char *, tok - tokstart, "yylex");
2190 memcpy(tokkey, tokstart, tok - tokstart);
2193 else if (do_lint && ! eof_warned) {
2194 lintwarn(_("source file does not end in newline"));
2198 /* See if it is a special token. */
2200 if ((mid = check_special(tokstart)) >= 0) {
2202 if (tokentab[mid].flags & GAWKX)
2203 lintwarn(_("`%s' is a gawk extension"),
2204 tokentab[mid].operator);
2205 if (tokentab[mid].flags & RESX)
2206 lintwarn(_("`%s' is a Bell Labs extension"),
2207 tokentab[mid].operator);
2208 if (tokentab[mid].flags & NOT_POSIX)
2209 lintwarn(_("POSIX does not allow `%s'"),
2210 tokentab[mid].operator);
2212 if (do_lint_old && (tokentab[mid].flags & NOT_OLD))
2213 warning(_("`%s' is not supported in old awk"),
2214 tokentab[mid].operator);
2215 if ((do_traditional && (tokentab[mid].flags & GAWKX))
2216 || (do_posix && (tokentab[mid].flags & NOT_POSIX)))
2219 if (tokentab[mid].class == LEX_BUILTIN
2220 || tokentab[mid].class == LEX_LENGTH)
2223 yylval.nodetypeval = tokentab[mid].value;
2225 return lasttok = tokentab[mid].class;
2229 yylval.sval = tokkey;
2231 return lasttok = FUNC_CALL;
2233 static short goto_warned = FALSE;
2235 #define SMART_ALECK 1
2236 if (SMART_ALECK && do_lint
2237 && ! goto_warned && strcasecmp(tokkey, "goto") == 0) {
2239 lintwarn(_("`goto' considered harmful!\n"));
2241 return lasttok = NAME;
2245 /* node_common --- common code for allocating a new node */
2248 node_common(NODETYPE op)
2255 /* if lookahead is a NL, lineno is 1 too high */
2256 if (lexeme && lexeme >= lexptr_begin && *lexeme == '\n')
2257 r->source_line = sourceline - 1;
2259 r->source_line = sourceline;
2260 r->source_file = source;
2264 /* node --- allocates a node with defined lnode and rnode. */
2267 node(NODE *left, NODETYPE op, NODE *right)
2271 r = node_common(op);
2277 /* snode --- allocate a node with defined subnode and builtin for builtin
2278 functions. Checks for arg. count and supplies defaults where
2282 snode(NODE *subn, NODETYPE op, int idx)
2289 r = node_common(op);
2291 /* traverse expression list to see how many args. given */
2292 for (n = subn; n != NULL; n = n->rnode) {
2298 /* check against how many args. are allowed for this builtin */
2299 args_allowed = tokentab[idx].flags & ARGS;
2300 if (args_allowed && (args_allowed & A(nexp)) == 0)
2301 fatal(_("%d is invalid as number of arguments for %s"),
2302 nexp, tokentab[idx].operator);
2304 r->builtin = tokentab[idx].ptr;
2306 /* special case processing for a few builtins */
2307 if (nexp == 0 && r->builtin == do_length) {
2308 subn = node(node(make_number(0.0), Node_field_spec, (NODE *) NULL),
2309 Node_expression_list,
2311 } else if (r->builtin == do_match) {
2312 static short warned = FALSE;
2314 if (subn->rnode->lnode->type != Node_regex)
2315 subn->rnode->lnode = mk_rexp(subn->rnode->lnode);
2317 if (subn->rnode->rnode != NULL) { /* 3rd argument there */
2318 if (do_lint && ! warned) {
2320 lintwarn(_("match: third argument is a gawk extension"));
2323 fatal(_("match: third argument is a gawk extension"));
2325 } else if (r->builtin == do_sub || r->builtin == do_gsub) {
2326 if (subn->lnode->type != Node_regex)
2327 subn->lnode = mk_rexp(subn->lnode);
2329 append_right(subn, node(node(make_number(0.0),
2332 Node_expression_list,
2334 else if (subn->rnode->rnode->lnode->type == Node_val) {
2336 lintwarn(_("%s: string literal as last arg of substitute has no effect"),
2337 (r->builtin == do_sub) ? "sub" : "gsub");
2338 } else if (! isassignable(subn->rnode->rnode->lnode)) {
2339 yyerror(_("%s third parameter is not a changeable object"),
2340 (r->builtin == do_sub) ? "sub" : "gsub");
2342 } else if (r->builtin == do_gensub) {
2343 if (subn->lnode->type != Node_regex)
2344 subn->lnode = mk_rexp(subn->lnode);
2346 append_right(subn, node(node(make_number(0.0),
2349 Node_expression_list,
2351 } else if (r->builtin == do_split) {
2354 node(FS_node, Node_expression_list, (NODE *) NULL));
2355 n = subn->rnode->rnode->lnode;
2356 if (n->type != Node_regex)
2357 subn->rnode->rnode->lnode = mk_rexp(n);
2359 subn->rnode->rnode->lnode->re_flags |= FS_DFLT;
2360 } else if (r->builtin == do_close) {
2361 static short warned = FALSE;
2364 if (do_lint && nexp == 2 && ! warned) {
2366 lintwarn(_("close: second argument is a gawk extension"));
2369 fatal(_("close: second argument is a gawk extension"));
2371 } else if (do_intl /* --gen-po */
2372 && r->builtin == do_dcgettext /* dcgettext(...) */
2373 && subn->lnode->type == Node_val /* 1st arg is constant */
2374 && (subn->lnode->flags & STRCUR) != 0) { /* it's a string constant */
2375 /* ala xgettext, dcgettext("some string" ...) dumps the string */
2376 NODE *str = subn->lnode;
2378 if ((str->flags & INTLSTR) != 0)
2379 warning(_("use of dcgettext(_\"...\") is incorrect: remove leading underscore"));
2380 /* don't dump it, the lexer already did */
2382 dumpintlstr(str->stptr, str->stlen);
2383 } else if (do_intl /* --gen-po */
2384 && r->builtin == do_dcngettext /* dcngettext(...) */
2385 && subn->lnode->type == Node_val /* 1st arg is constant */
2386 && (subn->lnode->flags & STRCUR) != 0 /* it's a string constant */
2387 && subn->rnode->lnode->type == Node_val /* 2nd arg is constant too */
2388 && (subn->rnode->lnode->flags & STRCUR) != 0) { /* it's a string constant */
2389 /* ala xgettext, dcngettext("some string", "some plural" ...) dumps the string */
2390 NODE *str1 = subn->lnode;
2391 NODE *str2 = subn->rnode->lnode;
2393 if (((str1->flags | str2->flags) & INTLSTR) != 0)
2394 warning(_("use of dcngettext(_\"...\") is incorrect: remove leading underscore"));
2396 dumpintlstr2(str1->stptr, str1->stlen, str2->stptr, str2->stlen);
2400 if (r->builtin == do_sprintf) {
2402 r->lnode->printf_count = r->printf_count; /* hack */
2407 /* make_for_loop --- build a for loop */
2410 make_for_loop(NODE *init, NODE *cond, NODE *incr)
2412 register FOR_LOOP_HEADER *r;
2415 emalloc(r, FOR_LOOP_HEADER *, sizeof(FOR_LOOP_HEADER), "make_for_loop");
2417 n->type = Node_illegal;
2421 n->sub.nodep.r.hd = r;
2425 /* dup_parms --- return TRUE if there are duplicate parameters */
2428 dup_parms(NODE *func)
2431 const char *fname, **names;
2432 int count, i, j, dups;
2435 if (func == NULL) /* error earlier */
2438 fname = func->param;
2439 count = func->param_cnt;
2440 params = func->rnode;
2442 if (count == 0) /* no args, no problem */
2445 if (params == NULL) /* error earlier */
2448 emalloc(names, const char **, count * sizeof(char *), "dup_parms");
2451 for (np = params; np != NULL; np = np->rnode) {
2452 if (np->param == NULL) { /* error earlier, give up, go home */
2456 names[i++] = np->param;
2460 for (i = 1; i < count; i++) {
2461 for (j = 0; j < i; j++) {
2462 if (strcmp(names[i], names[j]) == 0) {
2465 _("function `%s': parameter #%d, `%s', duplicates parameter #%d"),
2466 fname, i+1, names[j], j+1);
2472 return (dups > 0 ? TRUE : FALSE);
2475 /* parms_shadow --- check if parameters shadow globals */
2478 parms_shadow(const char *fname, NODE *func)
2483 if (fname == NULL || func == NULL) /* error earlier */
2486 count = func->lnode->param_cnt;
2488 if (count == 0) /* no args, no problem */
2492 * Use warning() and not lintwarn() so that can warn
2493 * about all shadowed parameters.
2495 for (i = 0; i < count; i++) {
2496 if (lookup(func->parmlist[i]) != NULL) {
2498 _("function `%s': parameter `%s' shadows global variable"),
2499 fname, func->parmlist[i]);
2509 * Install a name in the symbol table, even if it is already there.
2510 * Caller must check against redefinition if that is desired.
2514 install(char *name, NODE *value)
2517 register size_t len;
2518 register int bucket;
2522 bucket = hash(name, len, (unsigned long) HASHSIZE);
2524 hp->type = Node_hashnode;
2525 hp->hnext = variables[bucket];
2526 variables[bucket] = hp;
2530 hp->hvalue->vname = name;
2534 /* lookup --- find the most recent hash node for name installed by install */
2537 lookup(const char *name)
2539 register NODE *bucket;
2540 register size_t len;
2543 for (bucket = variables[hash(name, len, (unsigned long) HASHSIZE)];
2544 bucket != NULL; bucket = bucket->hnext)
2545 if (bucket->hlength == len && STREQN(bucket->hname, name, len))
2546 return bucket->hvalue;
2551 /* var_comp --- compare two variable names */
2554 var_comp(const void *v1, const void *v2)
2556 const NODE *const *npp1, *const *npp2;
2557 const NODE *n1, *n2;
2560 npp1 = (const NODE *const *) v1;
2561 npp2 = (const NODE *const *) v2;
2565 if (n1->hlength > n2->hlength)
2566 minlen = n1->hlength;
2568 minlen = n2->hlength;
2570 return strncmp(n1->hname, n2->hname, minlen);
2573 /* valinfo --- dump var info */
2576 valinfo(NODE *n, FILE *fp)
2578 if (n->flags & STRING) {
2579 fprintf(fp, "string (");
2580 pp_string_fp(fp, n->stptr, n->stlen, '"', FALSE);
2582 } else if (n->flags & NUMBER)
2583 fprintf(fp, "number (%.17g)\n", n->numbr);
2584 else if (n->flags & STRCUR) {
2585 fprintf(fp, "string value (");
2586 pp_string_fp(fp, n->stptr, n->stlen, '"', FALSE);
2588 } else if (n->flags & NUMCUR)
2589 fprintf(fp, "number value (%.17g)\n", n->numbr);
2591 fprintf(fp, "?? flags %s\n", flags2str(n->flags));
2595 /* dump_vars --- dump the symbol table */
2598 dump_vars(const char *fname)
2605 emalloc(table, NODE **, var_count * sizeof(NODE *), "dump_vars");
2609 else if ((fp = fopen(fname, "w")) == NULL) {
2610 warning(_("could not open `%s' for writing (%s)"), fname, strerror(errno));
2611 warning(_("sending profile to standard error"));
2615 for (i = j = 0; i < HASHSIZE; i++)
2616 for (p = variables[i]; p != NULL; p = p->hnext)
2619 assert(j == var_count);
2622 qsort(table, j, sizeof(NODE *), var_comp);
2624 for (i = 0; i < j; i++) {
2626 if (p->hvalue->type == Node_func)
2628 fprintf(fp, "%.*s: ", (int) p->hlength, p->hname);
2629 if (p->hvalue->type == Node_var_array)
2630 fprintf(fp, "array, %ld elements\n", p->hvalue->table_size);
2631 else if (p->hvalue->type == Node_var_new)
2632 fprintf(fp, "unused variable\n");
2633 else if (p->hvalue->type == Node_var)
2634 valinfo(p->hvalue->var_value, fp);
2636 NODE **lhs = get_lhs(p->hvalue, NULL, FALSE);
2642 if (fp != stderr && fclose(fp) != 0)
2643 warning(_("%s: close failed (%s)"), fname, strerror(errno));
2648 /* release_all_vars --- free all variable memory */
2656 for (i = 0; i < HASHSIZE; i++)
2657 for (p = variables[i]; p != NULL; p = next) {
2660 if (p->hvalue->type == Node_func)
2662 else if (p->hvalue->type == Node_var_array)
2663 assoc_clear(p->hvalue);
2664 else if (p->hvalue->type != Node_var_new) {
2665 NODE **lhs = get_lhs(p->hvalue, NULL, FALSE);
2673 /* finfo --- for use in comparison and sorting of function names */
2681 /* fcompare --- comparison function for qsort */
2684 fcompare(const void *p1, const void *p2)
2686 const struct finfo *f1, *f2;
2689 f1 = (const struct finfo *) p1;
2690 f2 = (const struct finfo *) p2;
2692 if (f1->nlen > f2->nlen)
2697 return strncmp(f1->name, f2->name, minlen);
2700 /* dump_funcs --- print all functions */
2707 struct finfo *tab = NULL;
2710 * Walk through symbol table countng functions.
2711 * Could be more than func_count if there are
2712 * extension functions.
2714 for (i = j = 0; i < HASHSIZE; i++) {
2715 for (p = variables[i]; p != NULL; p = p->hnext) {
2716 if (p->hvalue->type == Node_func) {
2725 emalloc(tab, struct finfo *, j * sizeof(struct finfo), "dump_funcs");
2727 /* now walk again, copying info */
2728 for (i = j = 0; i < HASHSIZE; i++) {
2729 for (p = variables[i]; p != NULL; p = p->hnext) {
2730 if (p->hvalue->type == Node_func) {
2731 tab[j].name = p->hname;
2732 tab[j].nlen = p->hlength;
2733 tab[j].func = p->hvalue;
2741 qsort(tab, j, sizeof(struct finfo), fcompare);
2743 for (i = 0; i < j; i++)
2744 pp_func(tab[i].name, tab[i].nlen, tab[i].func);
2749 /* shadow_funcs --- check all functions for parameters that shadow globals */
2757 static int calls = 0;
2760 if (func_count == 0)
2764 fatal(_("shadow_funcs() called twice!"));
2766 emalloc(tab, struct finfo *, func_count * sizeof(struct finfo), "shadow_funcs");
2768 for (i = j = 0; i < HASHSIZE; i++) {
2769 for (p = variables[i]; p != NULL; p = p->hnext) {
2770 if (p->hvalue->type == Node_func) {
2771 tab[j].name = p->hname;
2772 tab[j].nlen = p->hlength;
2773 tab[j].func = p->hvalue;
2779 assert(j == func_count);
2782 qsort(tab, func_count, sizeof(struct finfo), fcompare);
2784 for (i = 0; i < j; i++)
2785 shadow |= parms_shadow(tab[i].name, tab[i].func);
2789 /* End with fatal if the user requested it. */
2790 if (shadow && lintfunc != warning)
2791 lintwarn(_("there were shadowed variables."));
2796 * Add new to the rightmost branch of LIST. This uses n^2 time, so we make
2797 * a simple attempt at optimizing it.
2801 append_right(NODE *list, NODE *new)
2803 register NODE *oldlist;
2804 static NODE *savefront = NULL, *savetail = NULL;
2806 if (list == NULL || new == NULL)
2810 if (savefront == oldlist)
2811 list = savetail; /* Be careful: maybe list->rnode != NULL */
2813 savefront = oldlist;
2815 while (list->rnode != NULL)
2817 savetail = list->rnode = new;
2823 * A wrapper around append_right, used for rule lists.
2825 static inline NODE *
2826 append_pattern(NODE **list, NODE *patt)
2828 NODE *n = node(patt, Node_rule_node, (NODE *) NULL);
2833 NODE *n1 = node(n, Node_rule_list, (NODE *) NULL);
2834 if ((*list)->type != Node_rule_list)
2835 *list = node(*list, Node_rule_list, n1);
2837 (void) append_right(*list, n1);
2844 * check if name is already installed; if so, it had better have Null value,
2845 * in which case def is added as the value. Otherwise, install name with def
2848 * Extra work, build up and save a list of the parameter names in a table
2849 * and hang it off params->parmlist. This is used to set the `vname' field
2850 * of each function parameter during a function call. See eval.c.
2854 func_install(NODE *params, NODE *def)
2856 NODE *r, *n, *thisfunc;
2857 char **pnames, *names, *sp;
2858 size_t pcount = 0, space = 0;
2861 /* check for function foo(foo) { ... }. bleah. */
2862 for (n = params->rnode; n != NULL; n = n->rnode) {
2863 if (strcmp(n->param, params->param) == 0)
2864 fatal(_("function `%s': can't use function name as parameter name"),
2868 thisfunc = NULL; /* turn off warnings */
2870 /* symbol table managment */
2871 pop_var(params, FALSE);
2872 r = lookup(params->param);
2874 fatal(_("function name `%s' previously defined"), params->param);
2875 } else if (params->param == builtin_func) /* not a valid function name */
2878 /* install the function */
2879 thisfunc = node(params, Node_func, def);
2880 (void) install(params->param, thisfunc);
2882 /* figure out amount of space to allocate for variable names */
2883 for (n = params->rnode; n != NULL; n = n->rnode) {
2885 space += strlen(n->param) + 1;
2888 /* allocate it and fill it in */
2890 emalloc(names, char *, space, "func_install");
2891 emalloc(pnames, char **, pcount * sizeof(char *), "func_install");
2893 for (i = 0, n = params->rnode; i < pcount; i++, n = n->rnode) {
2895 strcpy(sp, n->param);
2896 sp += strlen(n->param) + 1;
2898 thisfunc->parmlist = pnames;
2900 thisfunc->parmlist = NULL;
2903 /* update lint table info */
2904 func_use(params->param, FUNC_DEFINE);
2906 func_count++; /* used by profiling / pretty printer */
2909 /* remove params from symbol table */
2910 pop_params(params->rnode);
2913 /* pop_var --- remove a variable from the symbol table */
2916 pop_var(NODE *np, int freeit)
2918 register NODE *bucket, **save;
2919 register size_t len;
2924 save = &(variables[hash(name, len, (unsigned long) HASHSIZE)]);
2925 for (bucket = *save; bucket != NULL; bucket = bucket->hnext) {
2926 if (len == bucket->hlength && STREQN(bucket->hname, name, len)) {
2928 *save = bucket->hnext;
2934 save = &(bucket->hnext);
2938 /* pop_params --- remove list of function parameters from symbol table */
2941 * pop parameters out of the symbol table. do this in reverse order to
2942 * avoid reading freed memory if there were duplicated parameters.
2945 pop_params(NODE *params)
2949 pop_params(params->rnode);
2950 pop_var(params, TRUE);
2953 /* make_param --- make NAME into a function parameter */
2956 make_param(char *name)
2961 r->type = Node_param_list;
2964 r->param_cnt = param_counter++;
2965 return (install(name, r));
2968 static struct fdesc {
2973 } *ftable[HASHSIZE];
2975 /* func_use --- track uses and definitions of functions */
2978 func_use(const char *name, enum defref how)
2985 ind = hash(name, len, HASHSIZE);
2987 for (fp = ftable[ind]; fp != NULL; fp = fp->next) {
2988 if (strcmp(fp->name, name) == 0) {
2989 if (how == FUNC_DEFINE)
2997 /* not in the table, fall through to allocate a new one */
2999 emalloc(fp, struct fdesc *, sizeof(struct fdesc), "func_use");
3000 memset(fp, '\0', sizeof(struct fdesc));
3001 emalloc(fp->name, char *, len + 1, "func_use");
3002 strcpy(fp->name, name);
3003 if (how == FUNC_DEFINE)
3007 fp->next = ftable[ind];
3011 /* check_funcs --- verify functions that are called but not defined */
3016 struct fdesc *fp, *next;
3019 for (i = 0; i < HASHSIZE; i++) {
3020 for (fp = ftable[i]; fp != NULL; fp = fp->next) {
3022 /* making this the default breaks old code. sigh. */
3023 if (fp->defined == 0) {
3025 _("function `%s' called but never defined"), fp->name);
3029 if (do_lint && fp->defined == 0)
3031 _("function `%s' called but never defined"), fp->name);
3033 if (do_lint && fp->used == 0) {
3034 lintwarn(_("function `%s' defined but never called"),
3040 /* now let's free all the memory */
3041 for (i = 0; i < HASHSIZE; i++) {
3042 for (fp = ftable[i]; fp != NULL; fp = next) {
3050 /* param_sanity --- look for parameters that are regexp constants */
3053 param_sanity(NODE *arglist)
3058 for (i = 1, argp = arglist; argp != NULL; argp = argp->rnode, i++) {
3060 if (arg->type == Node_regex)
3061 warning(_("regexp constant for parameter #%d yields boolean value"), i);
3065 /* deferred varibles --- those that are only defined if needed. */
3068 * Is there any reason to use a hash table for deferred variables? At the
3069 * moment, there are only 1 to 3 such variables, so it may not be worth
3070 * the overhead. If more modules start using this facility, it should
3071 * probably be converted into a hash table.
3074 static struct deferred_variable {
3075 NODE *(*load_func)(void);
3076 struct deferred_variable *next;
3077 char name[1]; /* variable-length array */
3078 } *deferred_variables;
3080 /* register_deferred_variable --- add a var name and loading function to the list */
3083 register_deferred_variable(const char *name, NODE *(*load_func)(void))
3085 struct deferred_variable *dv;
3086 size_t sl = strlen(name);
3088 emalloc(dv, struct deferred_variable *, sizeof(*dv)+sl,
3089 "register_deferred_variable");
3090 dv->load_func = load_func;
3091 dv->next = deferred_variables;
3092 memcpy(dv->name, name, sl+1);
3093 deferred_variables = dv;
3096 /* variable --- make sure NAME is in the symbol table */
3099 variable(char *name, int can_free, NODETYPE type)
3103 if ((r = lookup(name)) != NULL) {
3104 if (r->type == Node_func)
3105 fatal(_("function `%s' called with space between name and `(',\nor used as a variable or an array"),
3110 struct deferred_variable *dv;
3112 for (dv = deferred_variables; TRUE; dv = dv->next) {
3115 * This is the only case in which we may not
3120 if (type == Node_var_array)
3121 n = node(NULL, type, NULL);
3123 n = node(Nnull_string, type, NULL);
3125 return install(name, n);
3127 if (STREQ(name, dv->name)) {
3128 r = (*dv->load_func)();
3138 /* mk_rexp --- make a regular expression constant */
3145 if (exp->type == Node_regex)
3149 n->type = Node_dynregex;
3158 /* isnoeffect --- when used as a statement, has no side effects */
3161 * To be completely general, we should recursively walk the parse
3162 * tree, to make sure that all the subexpressions also have no effect.
3163 * Instead, we just weaken the actual warning that's printed, up above
3168 isnoeffect(NODETYPE type)
3176 case Node_subscript:
3179 case Node_unary_minus:
3180 case Node_field_spec:
3199 case Node_FIELDWIDTHS:
3200 case Node_IGNORECASE:
3208 case Node_TEXTDOMAIN:
3211 break; /* keeps gcc -Wall happy */
3217 /* isassignable --- can this node be assigned to? */
3220 isassignable(register NODE *n)
3225 case Node_FIELDWIDTHS:
3231 case Node_IGNORECASE:
3239 case Node_TEXTDOMAIN:
3240 case Node_field_spec:
3241 case Node_subscript:
3243 case Node_param_list:
3244 return ((n->flags & FUNC) == 0); /* ok if not func name */
3246 break; /* keeps gcc -Wall happy */
3251 /* stopme --- for debugging */
3254 stopme(NODE *tree ATTRIBUTE_UNUSED)
3259 /* dumpintlstr --- write out an initial .po file entry for the string */
3262 dumpintlstr(const char *str, size_t len)
3266 /* See the GNU gettext distribution for details on the file format */
3268 if (source != NULL) {
3269 /* ala the gettext sources, remove leading `./'s */
3270 for (cp = source; cp[0] == '.' && cp[1] == '/'; cp += 2)
3272 printf("#: %s:%d\n", cp, sourceline);
3276 pp_string_fp(stdout, str, len, '"', TRUE);
3278 printf("msgstr \"\"\n\n");
3282 /* dumpintlstr2 --- write out an initial .po file entry for the string and its plural */
3285 dumpintlstr2(const char *str1, size_t len1, const char *str2, size_t len2)
3289 /* See the GNU gettext distribution for details on the file format */
3291 if (source != NULL) {
3292 /* ala the gettext sources, remove leading `./'s */
3293 for (cp = source; cp[0] == '.' && cp[1] == '/'; cp += 2)
3295 printf("#: %s:%d\n", cp, sourceline);
3299 pp_string_fp(stdout, str1, len1, '"', TRUE);
3301 printf("msgid_plural ");
3302 pp_string_fp(stdout, str2, len2, '"', TRUE);
3304 printf("msgstr[0] \"\"\nmsgstr[1] \"\"\n\n");
3308 /* count_args --- count the number of printf arguments */
3311 count_args(NODE *tree)
3316 assert(tree->type == Node_K_printf
3317 || (tree->type == Node_builtin && tree->builtin == do_sprintf));
3320 tree = tree->lnode; /* printf format string */
3322 for (count = 0; tree != NULL; tree = tree->rnode)
3325 save_tree->printf_count = count;
3328 /* isarray --- can this type be subscripted? */
3335 case Node_var_array:
3337 case Node_param_list:
3338 return (n->flags & FUNC) == 0;
3339 case Node_array_ref:
3343 break; /* keeps gcc -Wall happy */
3349 /* See if name is a special token. */
3352 check_special(const char *name)
3358 high = (sizeof(tokentab) / sizeof(tokentab[0])) - 1;
3359 while (low <= high) {
3360 mid = (low + high) / 2;
3361 i = *name - tokentab[mid].operator[0];
3363 i = strcmp(name, tokentab[mid].operator);
3365 if (i < 0) /* token < mid */
3367 else if (i > 0) /* token > mid */