blob: af103e3cc741c08f708c1df0513f3875b233ff25 [file] [log] [blame]
Glenn L McGrath545106f2002-11-11 06:21:00 +00001/* vi: set sw=4 ts=4: */
2/*
3 * awk implementation for busybox
4 *
5 * Copyright (C) 2002 by Dmitry Zakharov <dmit@crp.bank.gov.ua>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath545106f2002-11-11 06:21:00 +00008 */
Sven-Göran Berghf200f732013-11-12 14:18:25 +01009//config:config AWK
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "awk (22 kb)"
Sven-Göran Berghf200f732013-11-12 14:18:25 +010011//config: default y
12//config: help
Denys Vlasenko68b653b2017-07-27 10:53:09 +020013//config: Awk is used as a pattern scanning and processing language.
Sven-Göran Berghf200f732013-11-12 14:18:25 +010014//config:
15//config:config FEATURE_AWK_LIBM
16//config: bool "Enable math functions (requires libm)"
17//config: default y
18//config: depends on AWK
19//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020020//config: Enable math functions of the Awk programming language.
21//config: NOTE: This requires libm to be present for linking.
Sven-Göran Berghf200f732013-11-12 14:18:25 +010022//config:
23//config:config FEATURE_AWK_GNU_EXTENSIONS
24//config: bool "Enable a few GNU extensions"
25//config: default y
26//config: depends on AWK
27//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020028//config: Enable a few features from gawk:
29//config: * command line option -e AWK_PROGRAM
30//config: * simultaneous use of -f and -e on the command line.
31//config: This enables the use of awk library files.
32//config: Example: awk -f mylib.awk -e '{print myfunction($1);}' ...
Sven-Göran Berghf200f732013-11-12 14:18:25 +010033
34//applet:IF_AWK(APPLET_NOEXEC(awk, awk, BB_DIR_USR_BIN, BB_SUID_DROP, awk))
35
36//kbuild:lib-$(CONFIG_AWK) += awk.o
37
Pere Orga6a3e01d2011-04-01 22:56:30 +020038//usage:#define awk_trivial_usage
39//usage: "[OPTIONS] [AWK_PROGRAM] [FILE]..."
40//usage:#define awk_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020041//usage: " -v VAR=VAL Set variable"
Pere Orga6a3e01d2011-04-01 22:56:30 +020042//usage: "\n -F SEP Use SEP as field separator"
43//usage: "\n -f FILE Read program from FILE"
Sven-Göran Berghf200f732013-11-12 14:18:25 +010044//usage: IF_FEATURE_AWK_GNU_EXTENSIONS(
45//usage: "\n -e AWK_PROGRAM"
46//usage: )
Pere Orga6a3e01d2011-04-01 22:56:30 +020047
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000048#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000049#include "xregex.h"
50#include <math.h>
Glenn L McGrath545106f2002-11-11 06:21:00 +000051
Denis Vlasenko99912ca2007-04-10 15:43:37 +000052/* This is a NOEXEC applet. Be very careful! */
53
Glenn L McGrath545106f2002-11-11 06:21:00 +000054
Denys Vlasenkoda62b092010-03-11 12:13:18 +010055/* If you comment out one of these below, it will be #defined later
56 * to perform debug printfs to stderr: */
57#define debug_printf_walker(...) do {} while (0)
Denys Vlasenkod527e0c2010-10-05 13:22:11 +020058#define debug_printf_eval(...) do {} while (0)
Denys Vlasenko7b46d112011-09-11 00:30:56 +020059#define debug_printf_parse(...) do {} while (0)
Denys Vlasenkoda62b092010-03-11 12:13:18 +010060
61#ifndef debug_printf_walker
62# define debug_printf_walker(...) (fprintf(stderr, __VA_ARGS__))
63#endif
Denys Vlasenkod527e0c2010-10-05 13:22:11 +020064#ifndef debug_printf_eval
65# define debug_printf_eval(...) (fprintf(stderr, __VA_ARGS__))
66#endif
Denys Vlasenko7b46d112011-09-11 00:30:56 +020067#ifndef debug_printf_parse
68# define debug_printf_parse(...) (fprintf(stderr, __VA_ARGS__))
69#endif
Denys Vlasenkoda62b092010-03-11 12:13:18 +010070
71
Denys Vlasenkoa1799db2017-08-16 19:07:53 +020072/* "+": stop on first non-option:
73 * $ awk 'BEGIN { for(i=1; i<ARGC; ++i) { print i ": " ARGV[i] }}' -argz
74 * 1: -argz
75 */
76#define OPTSTR_AWK "+" \
Denys Vlasenko237bedd2016-07-06 21:58:02 +020077 "F:v:*f:*" \
78 IF_FEATURE_AWK_GNU_EXTENSIONS("e:*") \
Sven-Göran Berghf200f732013-11-12 14:18:25 +010079 "W:"
Sven-Göran Berghf200f732013-11-12 14:18:25 +010080enum {
81 OPTBIT_F, /* define field separator */
82 OPTBIT_v, /* define variable */
83 OPTBIT_f, /* pull in awk program from file */
84 IF_FEATURE_AWK_GNU_EXTENSIONS(OPTBIT_e,) /* -e AWK_PROGRAM */
85 OPTBIT_W, /* -W ignored */
86 OPT_F = 1 << OPTBIT_F,
87 OPT_v = 1 << OPTBIT_v,
88 OPT_f = 1 << OPTBIT_f,
89 OPT_e = IF_FEATURE_AWK_GNU_EXTENSIONS((1 << OPTBIT_e)) + 0,
90 OPT_W = 1 << OPTBIT_W
91};
Denys Vlasenkoda62b092010-03-11 12:13:18 +010092
Denis Vlasenko629563b2007-02-24 17:05:52 +000093#define MAXVARFMT 240
94#define MINNVBLOCK 64
Glenn L McGrath545106f2002-11-11 06:21:00 +000095
96/* variable flags */
Denis Vlasenko629563b2007-02-24 17:05:52 +000097#define VF_NUMBER 0x0001 /* 1 = primary type is number */
98#define VF_ARRAY 0x0002 /* 1 = it's an array */
Glenn L McGrath545106f2002-11-11 06:21:00 +000099
Denis Vlasenko629563b2007-02-24 17:05:52 +0000100#define VF_CACHED 0x0100 /* 1 = num/str value has cached str/num eq */
101#define VF_USER 0x0200 /* 1 = user input (may be numeric string) */
102#define VF_SPECIAL 0x0400 /* 1 = requires extra handling when changed */
103#define VF_WALK 0x0800 /* 1 = variable has alloc'd x.walker list */
104#define VF_FSTR 0x1000 /* 1 = var::string points to fstring buffer */
105#define VF_CHILD 0x2000 /* 1 = function arg; x.parent points to source */
106#define VF_DIRTY 0x4000 /* 1 = variable was set explicitly */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000107
108/* these flags are static, don't change them when value is changed */
Denis Vlasenko629563b2007-02-24 17:05:52 +0000109#define VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000110
Denys Vlasenkoda62b092010-03-11 12:13:18 +0100111typedef struct walker_list {
112 char *end;
113 char *cur;
114 struct walker_list *prev;
115 char wbuf[1];
116} walker_list;
117
Glenn L McGrath545106f2002-11-11 06:21:00 +0000118/* Variable */
119typedef struct var_s {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000120 unsigned type; /* flags */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000121 double number;
122 char *string;
123 union {
Denis Vlasenko629563b2007-02-24 17:05:52 +0000124 int aidx; /* func arg idx (for compilation stage) */
125 struct xhash_s *array; /* array ptr */
126 struct var_s *parent; /* for func args, ptr to actual parameter */
Denys Vlasenkoda62b092010-03-11 12:13:18 +0100127 walker_list *walker; /* list of array elements (for..in) */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000128 } x;
129} var;
130
131/* Node chain (pattern-action chain, BEGIN, END, function bodies) */
132typedef struct chain_s {
133 struct node_s *first;
134 struct node_s *last;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000135 const char *programname;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000136} chain;
137
138/* Function */
139typedef struct func_s {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000140 unsigned nargs;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000141 struct chain_s body;
142} func;
143
144/* I/O stream */
145typedef struct rstream_s {
146 FILE *F;
147 char *buffer;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +0000148 int adv;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000149 int size;
150 int pos;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000151 smallint is_pipe;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000152} rstream;
153
154typedef struct hash_item_s {
155 union {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000156 struct var_s v; /* variable/array hash */
157 struct rstream_s rs; /* redirect streams hash */
158 struct func_s f; /* functions hash */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000159 } data;
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000160 struct hash_item_s *next; /* next in chain */
161 char name[1]; /* really it's longer */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000162} hash_item;
163
164typedef struct xhash_s {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000165 unsigned nel; /* num of elements */
166 unsigned csize; /* current hash size */
167 unsigned nprime; /* next hash size in PRIMES[] */
168 unsigned glen; /* summary length of item names */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000169 struct hash_item_s **items;
170} xhash;
171
172/* Tree node */
173typedef struct node_s {
Mike Frysingerf87b3e32005-09-27 04:16:22 +0000174 uint32_t info;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000175 unsigned lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000176 union {
177 struct node_s *n;
178 var *v;
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100179 int aidx;
180 char *new_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000181 regex_t *re;
182 } l;
183 union {
184 struct node_s *n;
185 regex_t *ire;
186 func *f;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000187 } r;
188 union {
189 struct node_s *n;
190 } a;
191} node;
192
193/* Block of temporary variables */
194typedef struct nvblock_s {
195 int size;
196 var *pos;
197 struct nvblock_s *prev;
198 struct nvblock_s *next;
Denys Vlasenkod069e532009-09-09 23:12:10 +0200199 var nv[];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000200} nvblock;
201
202typedef struct tsplitter_s {
203 node n;
204 regex_t re[2];
205} tsplitter;
206
207/* simple token classes */
208/* Order and hex values are very important!!! See next_token() */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200209#define TC_SEQSTART (1 << 0) /* ( */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000210#define TC_SEQTERM (1 << 1) /* ) */
211#define TC_REGEXP (1 << 2) /* /.../ */
212#define TC_OUTRDR (1 << 3) /* | > >> */
213#define TC_UOPPOST (1 << 4) /* unary postfix operator */
214#define TC_UOPPRE1 (1 << 5) /* unary prefix operator */
215#define TC_BINOPX (1 << 6) /* two-opnd operator */
216#define TC_IN (1 << 7)
217#define TC_COMMA (1 << 8)
218#define TC_PIPE (1 << 9) /* input redirection pipe */
219#define TC_UOPPRE2 (1 << 10) /* unary prefix operator */
220#define TC_ARRTERM (1 << 11) /* ] */
221#define TC_GRPSTART (1 << 12) /* { */
222#define TC_GRPTERM (1 << 13) /* } */
223#define TC_SEMICOL (1 << 14)
224#define TC_NEWLINE (1 << 15)
225#define TC_STATX (1 << 16) /* ctl statement (for, next...) */
226#define TC_WHILE (1 << 17)
227#define TC_ELSE (1 << 18)
228#define TC_BUILTIN (1 << 19)
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200229/* This costs ~50 bytes of code.
230 * A separate class to support deprecated "length" form. If we don't need that
231 * (i.e. if we demand that only "length()" with () is valid), then TC_LENGTH
232 * can be merged with TC_BUILTIN:
233 */
234#define TC_LENGTH (1 << 20)
235#define TC_GETLINE (1 << 21)
Denys Vlasenko95f79532017-08-02 14:26:33 +0200236#define TC_FUNCDECL (1 << 22) /* 'function' 'func' */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200237#define TC_BEGIN (1 << 23)
238#define TC_END (1 << 24)
239#define TC_EOF (1 << 25)
240#define TC_VARIABLE (1 << 26)
241#define TC_ARRAY (1 << 27)
242#define TC_FUNCTION (1 << 28)
243#define TC_STRING (1 << 29)
244#define TC_NUMBER (1 << 30)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000245
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000246#define TC_UOPPRE (TC_UOPPRE1 | TC_UOPPRE2)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000247
248/* combined token classes */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000249#define TC_BINOP (TC_BINOPX | TC_COMMA | TC_PIPE | TC_IN)
Denys Vlasenko1390a012013-07-20 21:23:01 +0200250//#define TC_UNARYOP (TC_UOPPRE | TC_UOPPOST)
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000251#define TC_OPERAND (TC_VARIABLE | TC_ARRAY | TC_FUNCTION \
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200252 | TC_BUILTIN | TC_LENGTH | TC_GETLINE \
253 | TC_SEQSTART | TC_STRING | TC_NUMBER)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000254
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000255#define TC_STATEMNT (TC_STATX | TC_WHILE)
256#define TC_OPTERM (TC_SEMICOL | TC_NEWLINE)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000257
258/* word tokens, cannot mean something else if not expected */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200259#define TC_WORD (TC_IN | TC_STATEMNT | TC_ELSE \
260 | TC_BUILTIN | TC_LENGTH | TC_GETLINE \
261 | TC_FUNCDECL | TC_BEGIN | TC_END)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000262
263/* discard newlines after these */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000264#define TC_NOTERM (TC_COMMA | TC_GRPSTART | TC_GRPTERM \
265 | TC_BINOP | TC_OPTERM)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000266
267/* what can expression begin with */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000268#define TC_OPSEQ (TC_OPERAND | TC_UOPPRE | TC_REGEXP)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000269/* what can group begin with */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000270#define TC_GRPSEQ (TC_OPSEQ | TC_OPTERM | TC_STATEMNT | TC_GRPSTART)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000271
272/* if previous token class is CONCAT1 and next is CONCAT2, concatenation */
273/* operator is inserted between them */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000274#define TC_CONCAT1 (TC_VARIABLE | TC_ARRTERM | TC_SEQTERM \
275 | TC_STRING | TC_NUMBER | TC_UOPPOST)
276#define TC_CONCAT2 (TC_OPERAND | TC_UOPPRE)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000277
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000278#define OF_RES1 0x010000
279#define OF_RES2 0x020000
280#define OF_STR1 0x040000
281#define OF_STR2 0x080000
282#define OF_NUM1 0x100000
283#define OF_CHECKED 0x200000
Glenn L McGrath545106f2002-11-11 06:21:00 +0000284
285/* combined operator flags */
286#define xx 0
287#define xV OF_RES2
288#define xS (OF_RES2 | OF_STR2)
289#define Vx OF_RES1
290#define VV (OF_RES1 | OF_RES2)
291#define Nx (OF_RES1 | OF_NUM1)
292#define NV (OF_RES1 | OF_NUM1 | OF_RES2)
293#define Sx (OF_RES1 | OF_STR1)
294#define SV (OF_RES1 | OF_STR1 | OF_RES2)
295#define SS (OF_RES1 | OF_STR1 | OF_RES2 | OF_STR2)
296
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000297#define OPCLSMASK 0xFF00
298#define OPNMASK 0x007F
Glenn L McGrath545106f2002-11-11 06:21:00 +0000299
300/* operator priority is a highest byte (even: r->l, odd: l->r grouping)
301 * For builtins it has different meaning: n n s3 s2 s1 v3 v2 v1,
302 * n - min. number of args, vN - resolve Nth arg to var, sN - resolve to string
303 */
Denys Vlasenko202a1b92011-09-10 04:51:09 +0200304#undef P
305#undef PRIMASK
306#undef PRIMASK2
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000307#define P(x) (x << 24)
308#define PRIMASK 0x7F000000
309#define PRIMASK2 0x7E000000
Glenn L McGrath545106f2002-11-11 06:21:00 +0000310
311/* Operation classes */
312
313#define SHIFT_TIL_THIS 0x0600
314#define RECUR_FROM_THIS 0x1000
315
316enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000317 OC_DELETE = 0x0100, OC_EXEC = 0x0200, OC_NEWSOURCE = 0x0300,
318 OC_PRINT = 0x0400, OC_PRINTF = 0x0500, OC_WALKINIT = 0x0600,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000319
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000320 OC_BR = 0x0700, OC_BREAK = 0x0800, OC_CONTINUE = 0x0900,
321 OC_EXIT = 0x0a00, OC_NEXT = 0x0b00, OC_NEXTFILE = 0x0c00,
322 OC_TEST = 0x0d00, OC_WALKNEXT = 0x0e00,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000323
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000324 OC_BINARY = 0x1000, OC_BUILTIN = 0x1100, OC_COLON = 0x1200,
325 OC_COMMA = 0x1300, OC_COMPARE = 0x1400, OC_CONCAT = 0x1500,
326 OC_FBLTIN = 0x1600, OC_FIELD = 0x1700, OC_FNARG = 0x1800,
327 OC_FUNC = 0x1900, OC_GETLINE = 0x1a00, OC_IN = 0x1b00,
328 OC_LAND = 0x1c00, OC_LOR = 0x1d00, OC_MATCH = 0x1e00,
329 OC_MOVE = 0x1f00, OC_PGETLINE = 0x2000, OC_REGEXP = 0x2100,
330 OC_REPLACE = 0x2200, OC_RETURN = 0x2300, OC_SPRINTF = 0x2400,
331 OC_TERNARY = 0x2500, OC_UNARY = 0x2600, OC_VAR = 0x2700,
332 OC_DONE = 0x2800,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000333
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000334 ST_IF = 0x3000, ST_DO = 0x3100, ST_FOR = 0x3200,
335 ST_WHILE = 0x3300
Glenn L McGrath545106f2002-11-11 06:21:00 +0000336};
337
338/* simple builtins */
339enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000340 F_in, F_rn, F_co, F_ex, F_lg, F_si, F_sq, F_sr,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000341 F_ti, F_le, F_sy, F_ff, F_cl
342};
343
344/* builtins */
345enum {
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +0200346 B_a2, B_ix, B_ma, B_sp, B_ss, B_ti, B_mt, B_lo, B_up,
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000347 B_ge, B_gs, B_su,
348 B_an, B_co, B_ls, B_or, B_rs, B_xo,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000349};
350
351/* tokens and their corresponding info values */
352
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200353#define NTC "\377" /* switch to next token class (tc<<1) */
354#define NTCC '\377'
Glenn L McGrath545106f2002-11-11 06:21:00 +0000355
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000356static const char tokenlist[] ALIGN1 =
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200357 "\1(" NTC /* TC_SEQSTART */
358 "\1)" NTC /* TC_SEQTERM */
359 "\1/" NTC /* TC_REGEXP */
360 "\2>>" "\1>" "\1|" NTC /* TC_OUTRDR */
361 "\2++" "\2--" NTC /* TC_UOPPOST */
362 "\2++" "\2--" "\1$" NTC /* TC_UOPPRE1 */
363 "\2==" "\1=" "\2+=" "\2-=" /* TC_BINOPX */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200364 "\2*=" "\2/=" "\2%=" "\2^="
365 "\1+" "\1-" "\3**=" "\2**"
366 "\1/" "\1%" "\1^" "\1*"
367 "\2!=" "\2>=" "\2<=" "\1>"
368 "\1<" "\2!~" "\1~" "\2&&"
369 "\2||" "\1?" "\1:" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200370 "\2in" NTC /* TC_IN */
371 "\1," NTC /* TC_COMMA */
372 "\1|" NTC /* TC_PIPE */
373 "\1+" "\1-" "\1!" NTC /* TC_UOPPRE2 */
374 "\1]" NTC /* TC_ARRTERM */
375 "\1{" NTC /* TC_GRPSTART */
376 "\1}" NTC /* TC_GRPTERM */
377 "\1;" NTC /* TC_SEMICOL */
378 "\1\n" NTC /* TC_NEWLINE */
379 "\2if" "\2do" "\3for" "\5break" /* TC_STATX */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200380 "\10continue" "\6delete" "\5print"
381 "\6printf" "\4next" "\10nextfile"
382 "\6return" "\4exit" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200383 "\5while" NTC /* TC_WHILE */
384 "\4else" NTC /* TC_ELSE */
385 "\3and" "\5compl" "\6lshift" "\2or" /* TC_BUILTIN */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200386 "\6rshift" "\3xor"
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200387 "\5close" "\6system" "\6fflush" "\5atan2"
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200388 "\3cos" "\3exp" "\3int" "\3log"
389 "\4rand" "\3sin" "\4sqrt" "\5srand"
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200390 "\6gensub" "\4gsub" "\5index" /* "\6length" was here */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200391 "\5match" "\5split" "\7sprintf" "\3sub"
392 "\6substr" "\7systime" "\10strftime" "\6mktime"
393 "\7tolower" "\7toupper" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200394 "\6length" NTC /* TC_LENGTH */
395 "\7getline" NTC /* TC_GETLINE */
396 "\4func" "\10function" NTC /* TC_FUNCDECL */
397 "\5BEGIN" NTC /* TC_BEGIN */
398 "\3END" /* TC_END */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200399 /* compiler adds trailing "\0" */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000400 ;
401
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200402#define OC_B OC_BUILTIN
403
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000404static const uint32_t tokeninfo[] = {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000405 0,
406 0,
407 OC_REGEXP,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200408 xS|'a', xS|'w', xS|'|',
409 OC_UNARY|xV|P(9)|'p', OC_UNARY|xV|P(9)|'m',
410 OC_UNARY|xV|P(9)|'P', OC_UNARY|xV|P(9)|'M', OC_FIELD|xV|P(5),
411 OC_COMPARE|VV|P(39)|5, OC_MOVE|VV|P(74), OC_REPLACE|NV|P(74)|'+', OC_REPLACE|NV|P(74)|'-',
412 OC_REPLACE|NV|P(74)|'*', OC_REPLACE|NV|P(74)|'/', OC_REPLACE|NV|P(74)|'%', OC_REPLACE|NV|P(74)|'&',
413 OC_BINARY|NV|P(29)|'+', OC_BINARY|NV|P(29)|'-', OC_REPLACE|NV|P(74)|'&', OC_BINARY|NV|P(15)|'&',
414 OC_BINARY|NV|P(25)|'/', OC_BINARY|NV|P(25)|'%', OC_BINARY|NV|P(15)|'&', OC_BINARY|NV|P(25)|'*',
415 OC_COMPARE|VV|P(39)|4, OC_COMPARE|VV|P(39)|3, OC_COMPARE|VV|P(39)|0, OC_COMPARE|VV|P(39)|1,
416 OC_COMPARE|VV|P(39)|2, OC_MATCH|Sx|P(45)|'!', OC_MATCH|Sx|P(45)|'~', OC_LAND|Vx|P(55),
417 OC_LOR|Vx|P(59), OC_TERNARY|Vx|P(64)|'?', OC_COLON|xx|P(67)|':',
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200418 OC_IN|SV|P(49), /* TC_IN */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000419 OC_COMMA|SS|P(80),
420 OC_PGETLINE|SV|P(37),
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200421 OC_UNARY|xV|P(19)|'+', OC_UNARY|xV|P(19)|'-', OC_UNARY|xV|P(19)|'!',
422 0, /* ] */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000423 0,
424 0,
425 0,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200426 0, /* \n */
427 ST_IF, ST_DO, ST_FOR, OC_BREAK,
428 OC_CONTINUE, OC_DELETE|Vx, OC_PRINT,
429 OC_PRINTF, OC_NEXT, OC_NEXTFILE,
430 OC_RETURN|Vx, OC_EXIT|Nx,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000431 ST_WHILE,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200432 0, /* else */
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000433 OC_B|B_an|P(0x83), OC_B|B_co|P(0x41), OC_B|B_ls|P(0x83), OC_B|B_or|P(0x83),
434 OC_B|B_rs|P(0x83), OC_B|B_xo|P(0x83),
Glenn L McGrath545106f2002-11-11 06:21:00 +0000435 OC_FBLTIN|Sx|F_cl, OC_FBLTIN|Sx|F_sy, OC_FBLTIN|Sx|F_ff, OC_B|B_a2|P(0x83),
436 OC_FBLTIN|Nx|F_co, OC_FBLTIN|Nx|F_ex, OC_FBLTIN|Nx|F_in, OC_FBLTIN|Nx|F_lg,
437 OC_FBLTIN|F_rn, OC_FBLTIN|Nx|F_si, OC_FBLTIN|Nx|F_sq, OC_FBLTIN|Nx|F_sr,
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200438 OC_B|B_ge|P(0xd6), OC_B|B_gs|P(0xb6), OC_B|B_ix|P(0x9b), /* OC_FBLTIN|Sx|F_le, was here */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000439 OC_B|B_ma|P(0x89), OC_B|B_sp|P(0x8b), OC_SPRINTF, OC_B|B_su|P(0xb6),
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +0200440 OC_B|B_ss|P(0x8f), OC_FBLTIN|F_ti, OC_B|B_ti|P(0x0b), OC_B|B_mt|P(0x0b),
Glenn L McGrath545106f2002-11-11 06:21:00 +0000441 OC_B|B_lo|P(0x49), OC_B|B_up|P(0x49),
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200442 OC_FBLTIN|Sx|F_le, /* TC_LENGTH */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000443 OC_GETLINE|SV|P(0),
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200444 0, 0,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000445 0,
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200446 0 /* TC_END */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000447};
448
449/* internal variable names and their initial values */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000450/* asterisk marks SPECIAL vars; $ is just no-named Field0 */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000451enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000452 CONVFMT, OFMT, FS, OFS,
Denis Vlasenkof782f522007-01-01 23:51:30 +0000453 ORS, RS, RT, FILENAME,
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000454 SUBSEP, F0, ARGIND, ARGC,
455 ARGV, ERRNO, FNR, NR,
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200456 NF, IGNORECASE, ENVIRON, NUM_INTERNAL_VARS
Glenn L McGrath545106f2002-11-11 06:21:00 +0000457};
458
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000459static const char vNames[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000460 "CONVFMT\0" "OFMT\0" "FS\0*" "OFS\0"
461 "ORS\0" "RS\0*" "RT\0" "FILENAME\0"
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000462 "SUBSEP\0" "$\0*" "ARGIND\0" "ARGC\0"
463 "ARGV\0" "ERRNO\0" "FNR\0" "NR\0"
464 "NF\0*" "IGNORECASE\0*" "ENVIRON\0" "\0";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000465
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000466static const char vValues[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000467 "%.6g\0" "%.6g\0" " \0" " \0"
468 "\n\0" "\n\0" "\0" "\0"
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000469 "\034\0" "\0" "\377";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000470
471/* hash size may grow to these values */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000472#define FIRST_PRIME 61
473static const uint16_t PRIMES[] ALIGN2 = { 251, 1021, 4093, 16381, 65521 };
Glenn L McGrath545106f2002-11-11 06:21:00 +0000474
Glenn L McGrath545106f2002-11-11 06:21:00 +0000475
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000476/* Globals. Split in two parts so that first one is addressed
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000477 * with (mostly short) negative offsets.
478 * NB: it's unsafe to put members of type "double"
479 * into globals2 (gcc may fail to align them).
480 */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000481struct globals {
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000482 double t_double;
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000483 chain beginseq, mainseq, endseq;
484 chain *seq;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000485 node *break_ptr, *continue_ptr;
486 rstream *iF;
487 xhash *vhash, *ahash, *fdhash, *fnhash;
488 const char *g_progname;
489 int g_lineno;
490 int nfields;
491 int maxfields; /* used in fsrealloc() only */
492 var *Fields;
493 nvblock *g_cb;
494 char *g_pos;
495 char *g_buf;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000496 smallint icase;
497 smallint exiting;
498 smallint nextrec;
499 smallint nextfile;
500 smallint is_f0_split;
Denys Vlasenko7b46d112011-09-11 00:30:56 +0200501 smallint t_rollback;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000502};
503struct globals2 {
504 uint32_t t_info; /* often used */
505 uint32_t t_tclass;
506 char *t_string;
507 int t_lineno;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000508
509 var *intvar[NUM_INTERNAL_VARS]; /* often used */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000510
511 /* former statics from various functions */
512 char *split_f0__fstrings;
513
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000514 uint32_t next_token__save_tclass;
515 uint32_t next_token__save_info;
516 uint32_t next_token__ltclass;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000517 smallint next_token__concat_inserted;
518
519 smallint next_input_file__files_happen;
520 rstream next_input_file__rsm;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000521
522 var *evaluate__fnargs;
523 unsigned evaluate__seed;
524 regex_t evaluate__sreg;
525
526 var ptest__v;
527
528 tsplitter exec_builtin__tspl;
529
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000530 /* biggest and least used members go last */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000531 tsplitter fsplitter, rsplitter;
Denys Vlasenko3dbc5a92010-02-05 14:54:22 +0100532};
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000533#define G1 (ptr_to_globals[-1])
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000534#define G (*(struct globals2 *)ptr_to_globals)
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000535/* For debug. nm --size-sort awk.o | grep -vi ' [tr] ' */
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000536/*char G1size[sizeof(G1)]; - 0x74 */
537/*char Gsize[sizeof(G)]; - 0x1c4 */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000538/* Trying to keep most of members accessible with short offsets: */
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000539/*char Gofs_seed[offsetof(struct globals2, evaluate__seed)]; - 0x90 */
540#define t_double (G1.t_double )
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000541#define beginseq (G1.beginseq )
542#define mainseq (G1.mainseq )
543#define endseq (G1.endseq )
544#define seq (G1.seq )
545#define break_ptr (G1.break_ptr )
546#define continue_ptr (G1.continue_ptr)
547#define iF (G1.iF )
548#define vhash (G1.vhash )
549#define ahash (G1.ahash )
550#define fdhash (G1.fdhash )
551#define fnhash (G1.fnhash )
552#define g_progname (G1.g_progname )
553#define g_lineno (G1.g_lineno )
554#define nfields (G1.nfields )
555#define maxfields (G1.maxfields )
556#define Fields (G1.Fields )
557#define g_cb (G1.g_cb )
558#define g_pos (G1.g_pos )
559#define g_buf (G1.g_buf )
560#define icase (G1.icase )
561#define exiting (G1.exiting )
562#define nextrec (G1.nextrec )
563#define nextfile (G1.nextfile )
564#define is_f0_split (G1.is_f0_split )
Denys Vlasenko7b46d112011-09-11 00:30:56 +0200565#define t_rollback (G1.t_rollback )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000566#define t_info (G.t_info )
567#define t_tclass (G.t_tclass )
568#define t_string (G.t_string )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000569#define t_lineno (G.t_lineno )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000570#define intvar (G.intvar )
571#define fsplitter (G.fsplitter )
572#define rsplitter (G.rsplitter )
573#define INIT_G() do { \
Denys Vlasenko90a99042009-09-06 02:36:23 +0200574 SET_PTR_TO_GLOBALS((char*)xzalloc(sizeof(G1)+sizeof(G)) + sizeof(G1)); \
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000575 G.next_token__ltclass = TC_OPTERM; \
576 G.evaluate__seed = 1; \
577} while (0)
578
Glenn L McGrath545106f2002-11-11 06:21:00 +0000579
580/* function prototypes */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000581static void handle_special(var *);
Mike Frysingerf87b3e32005-09-27 04:16:22 +0000582static node *parse_expr(uint32_t);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000583static void chain_group(void);
584static var *evaluate(node *, var *);
585static rstream *next_input_file(void);
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000586static int fmt_num(char *, int, const char *, double, int);
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000587static int awk_exit(int) NORETURN;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000588
589/* ---- error handling ---- */
590
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000591static const char EMSG_INTERNAL_ERROR[] ALIGN1 = "Internal error";
592static const char EMSG_UNEXP_EOS[] ALIGN1 = "Unexpected end of string";
593static const char EMSG_UNEXP_TOKEN[] ALIGN1 = "Unexpected token";
594static const char EMSG_DIV_BY_ZERO[] ALIGN1 = "Division by zero";
595static const char EMSG_INV_FMT[] ALIGN1 = "Invalid format specifier";
596static const char EMSG_TOO_FEW_ARGS[] ALIGN1 = "Too few arguments for builtin";
597static const char EMSG_NOT_ARRAY[] ALIGN1 = "Not an array";
598static const char EMSG_POSSIBLE_ERROR[] ALIGN1 = "Possible syntax error";
599static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000600static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in";
Denys Vlasenko2454e672018-04-23 10:53:18 +0200601static const char EMSG_NEGATIVE_FIELD[] ALIGN1 = "Access to negative field";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000602
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100603static void zero_out_var(var *vp)
Denis Vlasenkof782f522007-01-01 23:51:30 +0000604{
605 memset(vp, 0, sizeof(*vp));
606}
607
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000608static void syntax_error(const char *message) NORETURN;
609static void syntax_error(const char *message)
Glenn L McGrathd4036f82002-11-28 09:30:40 +0000610{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000611 bb_error_msg_and_die("%s:%i: %s", g_progname, g_lineno, message);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000612}
613
Glenn L McGrath545106f2002-11-11 06:21:00 +0000614/* ---- hash stuff ---- */
615
Denis Vlasenkof782f522007-01-01 23:51:30 +0000616static unsigned hashidx(const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000617{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000618 unsigned idx = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000619
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100620 while (*name)
621 idx = *name++ + (idx << 6) - idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000622 return idx;
623}
624
625/* create new hash */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000626static xhash *hash_init(void)
627{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000628 xhash *newhash;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000629
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100630 newhash = xzalloc(sizeof(*newhash));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000631 newhash->csize = FIRST_PRIME;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100632 newhash->items = xzalloc(FIRST_PRIME * sizeof(newhash->items[0]));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000633
634 return newhash;
635}
636
637/* find item in hash, return ptr to data, NULL if not found */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000638static void *hash_search(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000639{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000640 hash_item *hi;
641
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100642 hi = hash->items[hashidx(name) % hash->csize];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000643 while (hi) {
644 if (strcmp(hi->name, name) == 0)
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100645 return &hi->data;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000646 hi = hi->next;
647 }
648 return NULL;
649}
650
651/* grow hash if it becomes too big */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000652static void hash_rebuild(xhash *hash)
653{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000654 unsigned newsize, i, idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000655 hash_item **newitems, *hi, *thi;
656
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000657 if (hash->nprime == ARRAY_SIZE(PRIMES))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000658 return;
659
660 newsize = PRIMES[hash->nprime++];
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100661 newitems = xzalloc(newsize * sizeof(newitems[0]));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000662
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000663 for (i = 0; i < hash->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000664 hi = hash->items[i];
665 while (hi) {
666 thi = hi;
667 hi = thi->next;
668 idx = hashidx(thi->name) % newsize;
669 thi->next = newitems[idx];
670 newitems[idx] = thi;
671 }
672 }
673
674 free(hash->items);
675 hash->csize = newsize;
676 hash->items = newitems;
677}
678
679/* find item in hash, add it if necessary. Return ptr to data */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000680static void *hash_find(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000681{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000682 hash_item *hi;
Denis Vlasenkof782f522007-01-01 23:51:30 +0000683 unsigned idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000684 int l;
685
686 hi = hash_search(hash, name);
Denis Vlasenkob78c7822007-07-18 18:31:11 +0000687 if (!hi) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000688 if (++hash->nel / hash->csize > 10)
689 hash_rebuild(hash);
690
Rob Landleya3896512006-05-07 20:20:34 +0000691 l = strlen(name) + 1;
Denis Vlasenko7a676642009-03-15 22:20:31 +0000692 hi = xzalloc(sizeof(*hi) + l);
693 strcpy(hi->name, name);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000694
695 idx = hashidx(name) % hash->csize;
696 hi->next = hash->items[idx];
697 hash->items[idx] = hi;
698 hash->glen += l;
699 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100700 return &hi->data;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000701}
702
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000703#define findvar(hash, name) ((var*) hash_find((hash), (name)))
704#define newvar(name) ((var*) hash_find(vhash, (name)))
705#define newfile(name) ((rstream*)hash_find(fdhash, (name)))
706#define newfunc(name) ((func*) hash_find(fnhash, (name)))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000707
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000708static void hash_remove(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000709{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000710 hash_item *hi, **phi;
711
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100712 phi = &hash->items[hashidx(name) % hash->csize];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000713 while (*phi) {
714 hi = *phi;
715 if (strcmp(hi->name, name) == 0) {
Rob Landleya3896512006-05-07 20:20:34 +0000716 hash->glen -= (strlen(name) + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000717 hash->nel--;
718 *phi = hi->next;
719 free(hi);
720 break;
721 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100722 phi = &hi->next;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000723 }
724}
725
726/* ------ some useful functions ------ */
727
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100728static char *skip_spaces(char *p)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000729{
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000730 while (1) {
731 if (*p == '\\' && p[1] == '\n') {
732 p++;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000733 t_lineno++;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000734 } else if (*p != ' ' && *p != '\t') {
735 break;
736 }
Mike Frysingerde2b9382005-09-27 03:18:00 +0000737 p++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000738 }
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100739 return p;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000740}
741
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100742/* returns old *s, advances *s past word and terminating NUL */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000743static char *nextword(char **s)
744{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000745 char *p = *s;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100746 while (*(*s)++ != '\0')
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100747 continue;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000748 return p;
749}
750
Mike Frysinger10a11e22005-09-27 02:23:02 +0000751static char nextchar(char **s)
752{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000753 char c, *pps;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000754
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100755 c = *(*s)++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000756 pps = *s;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100757 if (c == '\\')
758 c = bb_process_escape_sequence((const char**)s);
Denys Vlasenkoea664dd2012-06-22 18:41:01 +0200759 /* Example awk statement:
760 * s = "abc\"def"
761 * we must treat \" as "
762 */
Denys Vlasenko2b299fe2010-10-24 01:58:04 +0200763 if (c == '\\' && *s == pps) { /* unrecognized \z? */
764 c = *(*s); /* yes, fetch z */
765 if (c)
766 (*s)++; /* advance unless z = NUL */
767 }
Glenn L McGrath545106f2002-11-11 06:21:00 +0000768 return c;
769}
770
Denys Vlasenkoea664dd2012-06-22 18:41:01 +0200771/* TODO: merge with strcpy_and_process_escape_sequences()?
772 */
773static void unescape_string_in_place(char *s1)
774{
775 char *s = s1;
776 while ((*s1 = nextchar(&s)) != '\0')
777 s1++;
778}
779
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000780static ALWAYS_INLINE int isalnum_(int c)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000781{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000782 return (isalnum(c) || c == '_');
783}
784
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000785static double my_strtod(char **pp)
786{
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200787 char *cp = *pp;
Rob Landleyd8205b32010-10-24 03:27:22 +0200788 if (ENABLE_DESKTOP && cp[0] == '0') {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200789 /* Might be hex or octal integer: 0x123abc or 07777 */
790 char c = (cp[1] | 0x20);
791 if (c == 'x' || isdigit(cp[1])) {
792 unsigned long long ull = strtoull(cp, pp, 0);
793 if (c == 'x')
794 return ull;
795 c = **pp;
796 if (!isdigit(c) && c != '.')
797 return ull;
798 /* else: it may be a floating number. Examples:
799 * 009.123 (*pp points to '9')
800 * 000.123 (*pp points to '.')
801 * fall through to strtod.
802 */
803 }
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000804 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200805 return strtod(cp, pp);
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000806}
807
Glenn L McGrath545106f2002-11-11 06:21:00 +0000808/* -------- working with variables (set/get/copy/etc) -------- */
809
Mike Frysinger10a11e22005-09-27 02:23:02 +0000810static xhash *iamarray(var *v)
811{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000812 var *a = v;
813
814 while (a->type & VF_CHILD)
815 a = a->x.parent;
816
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000817 if (!(a->type & VF_ARRAY)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000818 a->type |= VF_ARRAY;
819 a->x.array = hash_init();
820 }
821 return a->x.array;
822}
823
Mike Frysinger10a11e22005-09-27 02:23:02 +0000824static void clear_array(xhash *array)
825{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000826 unsigned i;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000827 hash_item *hi, *thi;
828
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000829 for (i = 0; i < array->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000830 hi = array->items[i];
831 while (hi) {
832 thi = hi;
833 hi = hi->next;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000834 free(thi->data.v.string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000835 free(thi);
836 }
837 array->items[i] = NULL;
838 }
839 array->glen = array->nel = 0;
840}
841
842/* clear a variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000843static var *clrvar(var *v)
844{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000845 if (!(v->type & VF_FSTR))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000846 free(v->string);
847
848 v->type &= VF_DONTTOUCH;
849 v->type |= VF_DIRTY;
850 v->string = NULL;
851 return v;
852}
853
854/* assign string value to variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000855static var *setvar_p(var *v, char *value)
856{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000857 clrvar(v);
858 v->string = value;
859 handle_special(v);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000860 return v;
861}
862
863/* same as setvar_p but make a copy of string */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000864static var *setvar_s(var *v, const char *value)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000865{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000866 return setvar_p(v, (value && *value) ? xstrdup(value) : NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000867}
868
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100869/* same as setvar_s but sets USER flag */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000870static var *setvar_u(var *v, const char *value)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000871{
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100872 v = setvar_s(v, value);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000873 v->type |= VF_USER;
874 return v;
875}
876
877/* set array element to user string */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000878static void setari_u(var *a, int idx, const char *s)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000879{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000880 var *v;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000881
Denys Vlasenko7bb346f2009-10-06 22:09:50 +0200882 v = findvar(iamarray(a), itoa(idx));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000883 setvar_u(v, s);
884}
885
886/* assign numeric value to variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000887static var *setvar_i(var *v, double value)
888{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000889 clrvar(v);
890 v->type |= VF_NUMBER;
891 v->number = value;
892 handle_special(v);
893 return v;
894}
895
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000896static const char *getvar_s(var *v)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000897{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000898 /* if v is numeric and has no cached string, convert it to string */
899 if ((v->type & (VF_NUMBER | VF_CACHED)) == VF_NUMBER) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000900 fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[CONVFMT]), v->number, TRUE);
901 v->string = xstrdup(g_buf);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000902 v->type |= VF_CACHED;
903 }
904 return (v->string == NULL) ? "" : v->string;
905}
906
Mike Frysinger10a11e22005-09-27 02:23:02 +0000907static double getvar_i(var *v)
908{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000909 char *s;
910
911 if ((v->type & (VF_NUMBER | VF_CACHED)) == 0) {
912 v->number = 0;
913 s = v->string;
914 if (s && *s) {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200915 debug_printf_eval("getvar_i: '%s'->", s);
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000916 v->number = my_strtod(&s);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200917 debug_printf_eval("%f (s:'%s')\n", v->number, s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000918 if (v->type & VF_USER) {
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100919 s = skip_spaces(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000920 if (*s != '\0')
921 v->type &= ~VF_USER;
922 }
923 } else {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200924 debug_printf_eval("getvar_i: '%s'->zero\n", s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000925 v->type &= ~VF_USER;
926 }
927 v->type |= VF_CACHED;
928 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200929 debug_printf_eval("getvar_i: %f\n", v->number);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000930 return v->number;
931}
932
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000933/* Used for operands of bitwise ops */
934static unsigned long getvar_i_int(var *v)
935{
936 double d = getvar_i(v);
937
938 /* Casting doubles to longs is undefined for values outside
939 * of target type range. Try to widen it as much as possible */
940 if (d >= 0)
941 return (unsigned long)d;
Denis Vlasenko665eaff2008-09-05 04:59:02 +0000942 /* Why? Think about d == -4294967295.0 (assuming 32bit longs) */
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000943 return - (long) (unsigned long) (-d);
944}
945
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000946static var *copyvar(var *dest, const var *src)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000947{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000948 if (dest != src) {
949 clrvar(dest);
Denis Vlasenko629563b2007-02-24 17:05:52 +0000950 dest->type |= (src->type & ~(VF_DONTTOUCH | VF_FSTR));
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200951 debug_printf_eval("copyvar: number:%f string:'%s'\n", src->number, src->string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000952 dest->number = src->number;
953 if (src->string)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000954 dest->string = xstrdup(src->string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000955 }
956 handle_special(dest);
957 return dest;
958}
959
Mike Frysinger10a11e22005-09-27 02:23:02 +0000960static var *incvar(var *v)
961{
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100962 return setvar_i(v, getvar_i(v) + 1.0);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000963}
964
965/* return true if v is number or numeric string */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000966static int is_numeric(var *v)
967{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000968 getvar_i(v);
969 return ((v->type ^ VF_DIRTY) & (VF_NUMBER | VF_USER | VF_DIRTY));
970}
971
972/* return 1 when value of v corresponds to true, 0 otherwise */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000973static int istrue(var *v)
974{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000975 if (is_numeric(v))
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100976 return (v->number != 0);
977 return (v->string && v->string[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000978}
979
Eric Andersenaff114c2004-04-14 17:51:38 +0000980/* temporary variables allocator. Last allocated should be first freed */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000981static var *nvalloc(int n)
982{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000983 nvblock *pb = NULL;
984 var *v, *r;
985 int size;
986
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000987 while (g_cb) {
988 pb = g_cb;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100989 if ((g_cb->pos - g_cb->nv) + n <= g_cb->size)
990 break;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000991 g_cb = g_cb->next;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000992 }
993
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000994 if (!g_cb) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000995 size = (n <= MINNVBLOCK) ? MINNVBLOCK : n;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000996 g_cb = xzalloc(sizeof(nvblock) + size * sizeof(var));
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000997 g_cb->size = size;
998 g_cb->pos = g_cb->nv;
999 g_cb->prev = pb;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +00001000 /*g_cb->next = NULL; - xzalloc did it */
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001001 if (pb)
1002 pb->next = g_cb;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001003 }
1004
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001005 v = r = g_cb->pos;
1006 g_cb->pos += n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001007
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001008 while (v < g_cb->pos) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001009 v->type = 0;
1010 v->string = NULL;
1011 v++;
1012 }
1013
1014 return r;
1015}
1016
Mike Frysinger10a11e22005-09-27 02:23:02 +00001017static void nvfree(var *v)
1018{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001019 var *p;
1020
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001021 if (v < g_cb->nv || v >= g_cb->pos)
1022 syntax_error(EMSG_INTERNAL_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001023
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001024 for (p = v; p < g_cb->pos; p++) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001025 if ((p->type & (VF_ARRAY | VF_CHILD)) == VF_ARRAY) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001026 clear_array(iamarray(p));
1027 free(p->x.array->items);
1028 free(p->x.array);
1029 }
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001030 if (p->type & VF_WALK) {
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001031 walker_list *n;
1032 walker_list *w = p->x.walker;
1033 debug_printf_walker("nvfree: freeing walker @%p\n", &p->x.walker);
1034 p->x.walker = NULL;
1035 while (w) {
1036 n = w->prev;
1037 debug_printf_walker(" free(%p)\n", w);
1038 free(w);
1039 w = n;
1040 }
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001041 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001042 clrvar(p);
1043 }
1044
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001045 g_cb->pos = v;
1046 while (g_cb->prev && g_cb->pos == g_cb->nv) {
1047 g_cb = g_cb->prev;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001048 }
1049}
1050
1051/* ------- awk program text parsing ------- */
1052
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001053/* Parse next token pointed by global pos, place results into global ttt.
Glenn L McGrath545106f2002-11-11 06:21:00 +00001054 * If token isn't expected, give away. Return token class
1055 */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001056static uint32_t next_token(uint32_t expected)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001057{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001058#define concat_inserted (G.next_token__concat_inserted)
1059#define save_tclass (G.next_token__save_tclass)
1060#define save_info (G.next_token__save_info)
1061/* Initialized to TC_OPTERM: */
1062#define ltclass (G.next_token__ltclass)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001063
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001064 char *p, *s;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001065 const char *tl;
1066 uint32_t tc;
1067 const uint32_t *ti;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001068
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001069 if (t_rollback) {
1070 t_rollback = FALSE;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001071 } else if (concat_inserted) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001072 concat_inserted = FALSE;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001073 t_tclass = save_tclass;
1074 t_info = save_info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001075 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001076 p = g_pos;
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001077 readnext:
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +01001078 p = skip_spaces(p);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001079 g_lineno = t_lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001080 if (*p == '#')
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001081 while (*p != '\n' && *p != '\0')
1082 p++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001083
1084 if (*p == '\n')
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001085 t_lineno++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001086
1087 if (*p == '\0') {
1088 tc = TC_EOF;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001089 debug_printf_parse("%s: token found: TC_EOF\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001090 } else if (*p == '\"') {
1091 /* it's a string */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001092 t_string = s = ++p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001093 while (*p != '\"') {
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02001094 char *pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001095 if (*p == '\0' || *p == '\n')
1096 syntax_error(EMSG_UNEXP_EOS);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02001097 pp = p;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001098 *s++ = nextchar(&pp);
1099 p = pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001100 }
1101 p++;
1102 *s = '\0';
1103 tc = TC_STRING;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001104 debug_printf_parse("%s: token found:'%s' TC_STRING\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001105 } else if ((expected & TC_REGEXP) && *p == '/') {
1106 /* it's regexp */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001107 t_string = s = ++p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001108 while (*p != '/') {
1109 if (*p == '\0' || *p == '\n')
1110 syntax_error(EMSG_UNEXP_EOS);
Denis Vlasenkod9b5ab82007-05-18 07:30:43 +00001111 *s = *p++;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001112 if (*s++ == '\\') {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001113 char *pp = p;
1114 s[-1] = bb_process_escape_sequence((const char **)&pp);
1115 if (*p == '\\')
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001116 *s++ = '\\';
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001117 if (pp == p)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001118 *s++ = *p++;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001119 else
1120 p = pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001121 }
1122 }
1123 p++;
1124 *s = '\0';
1125 tc = TC_REGEXP;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001126 debug_printf_parse("%s: token found:'%s' TC_REGEXP\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001127
1128 } else if (*p == '.' || isdigit(*p)) {
1129 /* it's a number */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001130 char *pp = p;
1131 t_double = my_strtod(&pp);
1132 p = pp;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001133 if (*p == '.')
Glenn L McGrath545106f2002-11-11 06:21:00 +00001134 syntax_error(EMSG_UNEXP_TOKEN);
1135 tc = TC_NUMBER;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001136 debug_printf_parse("%s: token found:%f TC_NUMBER\n", __func__, t_double);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001137 } else {
1138 /* search for something known */
1139 tl = tokenlist;
1140 tc = 0x00000001;
1141 ti = tokeninfo;
1142 while (*tl) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001143 int l = (unsigned char) *tl++;
1144 if (l == (unsigned char) NTCC) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001145 tc <<= 1;
1146 continue;
1147 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001148 /* if token class is expected,
1149 * token matches,
1150 * and it's not a longer word,
Glenn L McGrath545106f2002-11-11 06:21:00 +00001151 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001152 if ((tc & (expected | TC_WORD | TC_NEWLINE))
Denys Vlasenko28458c62010-10-05 16:49:03 +02001153 && strncmp(p, tl, l) == 0
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001154 && !((tc & TC_WORD) && isalnum_(p[l]))
1155 ) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001156 /* then this is what we are looking for */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001157 t_info = *ti;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001158 debug_printf_parse("%s: token found:'%.*s' t_info:%x\n", __func__, l, p, t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001159 p += l;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001160 goto token_found;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001161 }
1162 ti++;
1163 tl += l;
1164 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001165 /* not a known token */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001166
Denys Vlasenko28458c62010-10-05 16:49:03 +02001167 /* is it a name? (var/array/function) */
1168 if (!isalnum_(*p))
1169 syntax_error(EMSG_UNEXP_TOKEN); /* no */
1170 /* yes */
1171 t_string = --p;
1172 while (isalnum_(*++p)) {
1173 p[-1] = *p;
1174 }
1175 p[-1] = '\0';
1176 tc = TC_VARIABLE;
1177 /* also consume whitespace between functionname and bracket */
1178 if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY))
1179 p = skip_spaces(p);
1180 if (*p == '(') {
1181 tc = TC_FUNCTION;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001182 debug_printf_parse("%s: token found:'%s' TC_FUNCTION\n", __func__, t_string);
Denys Vlasenko28458c62010-10-05 16:49:03 +02001183 } else {
1184 if (*p == '[') {
1185 p++;
1186 tc = TC_ARRAY;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001187 debug_printf_parse("%s: token found:'%s' TC_ARRAY\n", __func__, t_string);
1188 } else
1189 debug_printf_parse("%s: token found:'%s' TC_VARIABLE\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001190 }
1191 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001192 token_found:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001193 g_pos = p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001194
1195 /* skipping newlines in some cases */
1196 if ((ltclass & TC_NOTERM) && (tc & TC_NEWLINE))
1197 goto readnext;
1198
1199 /* insert concatenation operator when needed */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001200 if ((ltclass & TC_CONCAT1) && (tc & TC_CONCAT2) && (expected & TC_BINOP)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001201 concat_inserted = TRUE;
1202 save_tclass = tc;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001203 save_info = t_info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001204 tc = TC_BINOP;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001205 t_info = OC_CONCAT | SS | P(35);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001206 }
1207
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001208 t_tclass = tc;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001209 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001210 ltclass = t_tclass;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001211
1212 /* Are we ready for this? */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001213 if (!(ltclass & expected)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001214 syntax_error((ltclass & (TC_NEWLINE | TC_EOF)) ?
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001215 EMSG_UNEXP_EOS : EMSG_UNEXP_TOKEN);
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001216 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001217
1218 return ltclass;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001219#undef concat_inserted
1220#undef save_tclass
1221#undef save_info
1222#undef ltclass
Glenn L McGrath545106f2002-11-11 06:21:00 +00001223}
1224
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001225static void rollback_token(void)
1226{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001227 t_rollback = TRUE;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001228}
Glenn L McGrath545106f2002-11-11 06:21:00 +00001229
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001230static node *new_node(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001231{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001232 node *n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001233
Denis Vlasenko4cccc032006-12-22 18:37:07 +00001234 n = xzalloc(sizeof(node));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001235 n->info = info;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001236 n->lineno = g_lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001237 return n;
1238}
1239
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001240static void mk_re_node(const char *s, node *n, regex_t *re)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001241{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001242 n->info = OC_REGEXP;
1243 n->l.re = re;
1244 n->r.ire = re + 1;
1245 xregcomp(re, s, REG_EXTENDED);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001246 xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001247}
1248
Mike Frysinger10a11e22005-09-27 02:23:02 +00001249static node *condition(void)
1250{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001251 next_token(TC_SEQSTART);
1252 return parse_expr(TC_SEQTERM);
1253}
1254
1255/* parse expression terminated by given argument, return ptr
1256 * to built subtree. Terminator is eaten by parse_expr */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001257static node *parse_expr(uint32_t iexp)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001258{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001259 node sn;
1260 node *cn = &sn;
1261 node *vn, *glptr;
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001262 uint32_t tc, xtc;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001263 var *v;
1264
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001265 debug_printf_parse("%s(%x)\n", __func__, iexp);
1266
Glenn L McGrath545106f2002-11-11 06:21:00 +00001267 sn.info = PRIMASK;
1268 sn.r.n = glptr = NULL;
1269 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp;
1270
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001271 while (!((tc = next_token(xtc)) & iexp)) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001272
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001273 if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001274 /* input redirection (<) attached to glptr node */
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001275 debug_printf_parse("%s: input redir\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001276 cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37));
Glenn L McGrath4bded582004-02-22 11:55:09 +00001277 cn->a.n = glptr;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001278 xtc = TC_OPERAND | TC_UOPPRE;
1279 glptr = NULL;
1280
1281 } else if (tc & (TC_BINOP | TC_UOPPOST)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001282 debug_printf_parse("%s: TC_BINOP | TC_UOPPOST\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001283 /* for binary and postfix-unary operators, jump back over
1284 * previous operators with higher priority */
1285 vn = cn;
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001286 while (((t_info & PRIMASK) > (vn->a.n->info & PRIMASK2))
1287 || ((t_info == vn->info) && ((t_info & OPCLSMASK) == OC_COLON))
1288 ) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001289 vn = vn->a.n;
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001290 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001291 if ((t_info & OPCLSMASK) == OC_TERNARY)
1292 t_info += P(6);
1293 cn = vn->a.n->r.n = new_node(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001294 cn->a.n = vn->a.n;
1295 if (tc & TC_BINOP) {
1296 cn->l.n = vn;
1297 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001298 if ((t_info & OPCLSMASK) == OC_PGETLINE) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001299 /* it's a pipe */
1300 next_token(TC_GETLINE);
1301 /* give maximum priority to this pipe */
1302 cn->info &= ~PRIMASK;
1303 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1304 }
1305 } else {
1306 cn->r.n = vn;
1307 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1308 }
1309 vn->a.n = cn;
1310
1311 } else {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001312 debug_printf_parse("%s: other\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001313 /* for operands and prefix-unary operators, attach them
1314 * to last node */
1315 vn = cn;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001316 cn = vn->r.n = new_node(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001317 cn->a.n = vn;
1318 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
1319 if (tc & (TC_OPERAND | TC_REGEXP)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001320 debug_printf_parse("%s: TC_OPERAND | TC_REGEXP\n", __func__);
Rob Landleyed830e82005-06-07 02:43:52 +00001321 xtc = TC_UOPPRE | TC_UOPPOST | TC_BINOP | TC_OPERAND | iexp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001322 /* one should be very careful with switch on tclass -
Glenn L McGrath545106f2002-11-11 06:21:00 +00001323 * only simple tclasses should be used! */
1324 switch (tc) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00001325 case TC_VARIABLE:
1326 case TC_ARRAY:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001327 debug_printf_parse("%s: TC_VARIABLE | TC_ARRAY\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001328 cn->info = OC_VAR;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001329 v = hash_search(ahash, t_string);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001330 if (v != NULL) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001331 cn->info = OC_FNARG;
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001332 cn->l.aidx = v->x.aidx;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001333 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001334 cn->l.v = newvar(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001335 }
1336 if (tc & TC_ARRAY) {
1337 cn->info |= xS;
1338 cn->r.n = parse_expr(TC_ARRTERM);
1339 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001340 break;
Mike Frysingerde2b9382005-09-27 03:18:00 +00001341
Denis Vlasenkof782f522007-01-01 23:51:30 +00001342 case TC_NUMBER:
1343 case TC_STRING:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001344 debug_printf_parse("%s: TC_NUMBER | TC_STRING\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001345 cn->info = OC_VAR;
Rob Landley9ffd4232006-05-21 18:30:35 +00001346 v = cn->l.v = xzalloc(sizeof(var));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001347 if (tc & TC_NUMBER)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001348 setvar_i(v, t_double);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001349 else
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001350 setvar_s(v, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001351 break;
1352
Denis Vlasenkof782f522007-01-01 23:51:30 +00001353 case TC_REGEXP:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001354 debug_printf_parse("%s: TC_REGEXP\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001355 mk_re_node(t_string, cn, xzalloc(sizeof(regex_t)*2));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001356 break;
1357
Denis Vlasenkof782f522007-01-01 23:51:30 +00001358 case TC_FUNCTION:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001359 debug_printf_parse("%s: TC_FUNCTION\n", __func__);
Mike Frysingerde2b9382005-09-27 03:18:00 +00001360 cn->info = OC_FUNC;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001361 cn->r.f = newfunc(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001362 cn->l.n = condition();
1363 break;
1364
Denis Vlasenkof782f522007-01-01 23:51:30 +00001365 case TC_SEQSTART:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001366 debug_printf_parse("%s: TC_SEQSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001367 cn = vn->r.n = parse_expr(TC_SEQTERM);
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001368 if (!cn)
1369 syntax_error("Empty sequence");
Glenn L McGrath545106f2002-11-11 06:21:00 +00001370 cn->a.n = vn;
1371 break;
1372
Denis Vlasenkof782f522007-01-01 23:51:30 +00001373 case TC_GETLINE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001374 debug_printf_parse("%s: TC_GETLINE\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001375 glptr = cn;
1376 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1377 break;
1378
Denis Vlasenkof782f522007-01-01 23:51:30 +00001379 case TC_BUILTIN:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001380 debug_printf_parse("%s: TC_BUILTIN\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001381 cn->l.n = condition();
1382 break;
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001383
1384 case TC_LENGTH:
1385 debug_printf_parse("%s: TC_LENGTH\n", __func__);
1386 next_token(TC_SEQSTART | TC_OPTERM | TC_GRPTERM);
1387 rollback_token();
1388 if (t_tclass & TC_SEQSTART) {
1389 /* It was a "(" token. Handle just like TC_BUILTIN */
1390 cn->l.n = condition();
1391 }
1392 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001393 }
1394 }
1395 }
1396 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001397
1398 debug_printf_parse("%s() returns %p\n", __func__, sn.r.n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001399 return sn.r.n;
1400}
1401
1402/* add node to chain. Return ptr to alloc'd node */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001403static node *chain_node(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001404{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001405 node *n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001406
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001407 if (!seq->first)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001408 seq->first = seq->last = new_node(0);
1409
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001410 if (seq->programname != g_progname) {
1411 seq->programname = g_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001412 n = chain_node(OC_NEWSOURCE);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001413 n->l.new_progname = xstrdup(g_progname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001414 }
1415
1416 n = seq->last;
1417 n->info = info;
1418 seq->last = n->a.n = new_node(OC_DONE);
1419
1420 return n;
1421}
1422
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001423static void chain_expr(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001424{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001425 node *n;
1426
1427 n = chain_node(info);
1428 n->l.n = parse_expr(TC_OPTERM | TC_GRPTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001429 if (t_tclass & TC_GRPTERM)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001430 rollback_token();
1431}
1432
Mike Frysinger10a11e22005-09-27 02:23:02 +00001433static node *chain_loop(node *nn)
1434{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001435 node *n, *n2, *save_brk, *save_cont;
1436
1437 save_brk = break_ptr;
1438 save_cont = continue_ptr;
1439
1440 n = chain_node(OC_BR | Vx);
1441 continue_ptr = new_node(OC_EXEC);
1442 break_ptr = new_node(OC_EXEC);
1443 chain_group();
1444 n2 = chain_node(OC_EXEC | Vx);
1445 n2->l.n = nn;
1446 n2->a.n = n;
1447 continue_ptr->a.n = n2;
1448 break_ptr->a.n = n->r.n = seq->last;
1449
1450 continue_ptr = save_cont;
1451 break_ptr = save_brk;
1452
1453 return n;
1454}
1455
1456/* parse group and attach it to chain */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001457static void chain_group(void)
1458{
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001459 uint32_t c;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001460 node *n, *n2, *n3;
1461
1462 do {
1463 c = next_token(TC_GRPSEQ);
1464 } while (c & TC_NEWLINE);
1465
1466 if (c & TC_GRPSTART) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001467 debug_printf_parse("%s: TC_GRPSTART\n", __func__);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001468 while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001469 debug_printf_parse("%s: !TC_GRPTERM\n", __func__);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001470 if (t_tclass & TC_NEWLINE)
1471 continue;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001472 rollback_token();
1473 chain_group();
1474 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001475 debug_printf_parse("%s: TC_GRPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001476 } else if (c & (TC_OPSEQ | TC_OPTERM)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001477 debug_printf_parse("%s: TC_OPSEQ | TC_OPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001478 rollback_token();
1479 chain_expr(OC_EXEC | Vx);
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001480 } else {
1481 /* TC_STATEMNT */
1482 debug_printf_parse("%s: TC_STATEMNT(?)\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001483 switch (t_info & OPCLSMASK) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001484 case ST_IF:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001485 debug_printf_parse("%s: ST_IF\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001486 n = chain_node(OC_BR | Vx);
1487 n->l.n = condition();
1488 chain_group();
1489 n2 = chain_node(OC_EXEC);
1490 n->r.n = seq->last;
1491 if (next_token(TC_GRPSEQ | TC_GRPTERM | TC_ELSE) == TC_ELSE) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001492 chain_group();
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001493 n2->a.n = seq->last;
1494 } else {
1495 rollback_token();
1496 }
1497 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001498
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001499 case ST_WHILE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001500 debug_printf_parse("%s: ST_WHILE\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001501 n2 = condition();
1502 n = chain_loop(NULL);
1503 n->l.n = n2;
1504 break;
1505
1506 case ST_DO:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001507 debug_printf_parse("%s: ST_DO\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001508 n2 = chain_node(OC_EXEC);
1509 n = chain_loop(NULL);
1510 n2->a.n = n->a.n;
1511 next_token(TC_WHILE);
1512 n->l.n = condition();
1513 break;
1514
1515 case ST_FOR:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001516 debug_printf_parse("%s: ST_FOR\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001517 next_token(TC_SEQSTART);
1518 n2 = parse_expr(TC_SEMICOL | TC_SEQTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001519 if (t_tclass & TC_SEQTERM) { /* for-in */
Brian Foley61d59972016-10-15 14:45:40 +01001520 if (!n2 || (n2->info & OPCLSMASK) != OC_IN)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001521 syntax_error(EMSG_UNEXP_TOKEN);
1522 n = chain_node(OC_WALKINIT | VV);
1523 n->l.n = n2->l.n;
1524 n->r.n = n2->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001525 n = chain_loop(NULL);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001526 n->info = OC_WALKNEXT | Vx;
1527 n->l.n = n2->l.n;
1528 } else { /* for (;;) */
1529 n = chain_node(OC_EXEC | Vx);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001530 n->l.n = n2;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001531 n2 = parse_expr(TC_SEMICOL);
1532 n3 = parse_expr(TC_SEQTERM);
1533 n = chain_loop(n3);
1534 n->l.n = n2;
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001535 if (!n2)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001536 n->info = OC_EXEC;
1537 }
1538 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001539
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001540 case OC_PRINT:
1541 case OC_PRINTF:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001542 debug_printf_parse("%s: OC_PRINT[F]\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001543 n = chain_node(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001544 n->l.n = parse_expr(TC_OPTERM | TC_OUTRDR | TC_GRPTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001545 if (t_tclass & TC_OUTRDR) {
1546 n->info |= t_info;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001547 n->r.n = parse_expr(TC_OPTERM | TC_GRPTERM);
1548 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001549 if (t_tclass & TC_GRPTERM)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001550 rollback_token();
1551 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001552
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001553 case OC_BREAK:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001554 debug_printf_parse("%s: OC_BREAK\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001555 n = chain_node(OC_EXEC);
1556 n->a.n = break_ptr;
Denys Vlasenko5f8daef2014-06-26 16:40:28 +02001557 chain_expr(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001558 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001559
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001560 case OC_CONTINUE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001561 debug_printf_parse("%s: OC_CONTINUE\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001562 n = chain_node(OC_EXEC);
1563 n->a.n = continue_ptr;
Denys Vlasenko5f8daef2014-06-26 16:40:28 +02001564 chain_expr(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001565 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001566
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001567 /* delete, next, nextfile, return, exit */
1568 default:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001569 debug_printf_parse("%s: default\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001570 chain_expr(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001571 }
1572 }
1573}
1574
Mike Frysinger10a11e22005-09-27 02:23:02 +00001575static void parse_program(char *p)
1576{
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001577 uint32_t tclass;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001578 node *cn;
1579 func *f;
1580 var *v;
1581
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001582 g_pos = p;
1583 t_lineno = 1;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001584 while ((tclass = next_token(TC_EOF | TC_OPSEQ | TC_GRPSTART |
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001585 TC_OPTERM | TC_BEGIN | TC_END | TC_FUNCDECL)) != TC_EOF) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001586
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001587 if (tclass & TC_OPTERM) {
1588 debug_printf_parse("%s: TC_OPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001589 continue;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001590 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001591
1592 seq = &mainseq;
1593 if (tclass & TC_BEGIN) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001594 debug_printf_parse("%s: TC_BEGIN\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001595 seq = &beginseq;
1596 chain_group();
Glenn L McGrath545106f2002-11-11 06:21:00 +00001597 } else if (tclass & TC_END) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001598 debug_printf_parse("%s: TC_END\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001599 seq = &endseq;
1600 chain_group();
Glenn L McGrath545106f2002-11-11 06:21:00 +00001601 } else if (tclass & TC_FUNCDECL) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001602 debug_printf_parse("%s: TC_FUNCDECL\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001603 next_token(TC_FUNCTION);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001604 g_pos++;
1605 f = newfunc(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001606 f->body.first = NULL;
1607 f->nargs = 0;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001608 while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001609 v = findvar(ahash, t_string);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001610 v->x.aidx = f->nargs++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001611
1612 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
1613 break;
1614 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001615 seq = &f->body;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001616 chain_group();
1617 clear_array(ahash);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001618 } else if (tclass & TC_OPSEQ) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001619 debug_printf_parse("%s: TC_OPSEQ\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001620 rollback_token();
1621 cn = chain_node(OC_TEST);
1622 cn->l.n = parse_expr(TC_OPTERM | TC_EOF | TC_GRPSTART);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001623 if (t_tclass & TC_GRPSTART) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001624 debug_printf_parse("%s: TC_GRPSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001625 rollback_token();
1626 chain_group();
1627 } else {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001628 debug_printf_parse("%s: !TC_GRPSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001629 chain_node(OC_PRINT);
1630 }
1631 cn->r.n = mainseq.last;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001632 } else /* if (tclass & TC_GRPSTART) */ {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001633 debug_printf_parse("%s: TC_GRPSTART(?)\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001634 rollback_token();
1635 chain_group();
1636 }
1637 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001638 debug_printf_parse("%s: TC_EOF\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001639}
1640
1641
1642/* -------- program execution part -------- */
1643
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001644static node *mk_splitter(const char *s, tsplitter *spl)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001645{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001646 regex_t *re, *ire;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001647 node *n;
1648
1649 re = &spl->re[0];
1650 ire = &spl->re[1];
1651 n = &spl->n;
Denis Vlasenko890ac9d2006-10-07 15:16:19 +00001652 if ((n->info & OPCLSMASK) == OC_REGEXP) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001653 regfree(re);
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001654 regfree(ire); // TODO: nuke ire, use re+1?
Glenn L McGrath545106f2002-11-11 06:21:00 +00001655 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001656 if (s[0] && s[1]) { /* strlen(s) > 1 */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001657 mk_re_node(s, n, re);
1658 } else {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001659 n->info = (uint32_t) s[0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00001660 }
1661
1662 return n;
1663}
1664
1665/* use node as a regular expression. Supplied with node ptr and regex_t
Eric Andersenaff114c2004-04-14 17:51:38 +00001666 * storage space. Return ptr to regex (if result points to preg, it should
Glenn L McGrath545106f2002-11-11 06:21:00 +00001667 * be later regfree'd manually
1668 */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001669static regex_t *as_regex(node *op, regex_t *preg)
1670{
Denis Vlasenko7a676642009-03-15 22:20:31 +00001671 int cflags;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001672 var *v;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001673 const char *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001674
1675 if ((op->info & OPCLSMASK) == OC_REGEXP) {
1676 return icase ? op->r.ire : op->l.re;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001677 }
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001678 v = nvalloc(1);
1679 s = getvar_s(evaluate(op, v));
Denis Vlasenko7a676642009-03-15 22:20:31 +00001680
1681 cflags = icase ? REG_EXTENDED | REG_ICASE : REG_EXTENDED;
1682 /* Testcase where REG_EXTENDED fails (unpaired '{'):
1683 * echo Hi | awk 'gsub("@(samp|code|file)\{","");'
1684 * gawk 3.1.5 eats this. We revert to ~REG_EXTENDED
1685 * (maybe gsub is not supposed to use REG_EXTENDED?).
1686 */
1687 if (regcomp(preg, s, cflags)) {
1688 cflags &= ~REG_EXTENDED;
1689 xregcomp(preg, s, cflags);
1690 }
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001691 nvfree(v);
1692 return preg;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001693}
1694
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001695/* gradually increasing buffer.
1696 * note that we reallocate even if n == old_size,
1697 * and thus there is at least one extra allocated byte.
1698 */
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001699static char* qrealloc(char *b, int n, int *size)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001700{
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001701 if (!b || n >= *size) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001702 *size = n + (n>>1) + 80;
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001703 b = xrealloc(b, *size);
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001704 }
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001705 return b;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001706}
1707
1708/* resize field storage space */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001709static void fsrealloc(int size)
1710{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001711 int i;
1712
1713 if (size >= maxfields) {
1714 i = maxfields;
1715 maxfields = size + 16;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001716 Fields = xrealloc(Fields, maxfields * sizeof(Fields[0]));
Denis Vlasenkof782f522007-01-01 23:51:30 +00001717 for (; i < maxfields; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001718 Fields[i].type = VF_SPECIAL;
1719 Fields[i].string = NULL;
1720 }
1721 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001722 /* if size < nfields, clear extra field variables */
1723 for (i = size; i < nfields; i++) {
1724 clrvar(Fields + i);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001725 }
1726 nfields = size;
1727}
1728
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001729static int awk_split(const char *s, node *spl, char **slist)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001730{
Denys Vlasenko28458c62010-10-05 16:49:03 +02001731 int l, n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001732 char c[4];
1733 char *s1;
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001734 regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough...
Glenn L McGrath545106f2002-11-11 06:21:00 +00001735
1736 /* in worst case, each char would be a separate field */
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001737 *slist = s1 = xzalloc(strlen(s) * 2 + 3);
1738 strcpy(s1, s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001739
1740 c[0] = c[1] = (char)spl->info;
1741 c[2] = c[3] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001742 if (*getvar_s(intvar[RS]) == '\0')
1743 c[2] = '\n';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001744
Denys Vlasenko28458c62010-10-05 16:49:03 +02001745 n = 0;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001746 if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
1747 if (!*s)
1748 return n; /* "": zero fields */
1749 n++; /* at least one field will be there */
1750 do {
1751 l = strcspn(s, c+2); /* len till next NUL or \n */
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001752 if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0
1753 && pmatch[0].rm_so <= l
1754 ) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001755 l = pmatch[0].rm_so;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001756 if (pmatch[0].rm_eo == 0) {
1757 l++;
1758 pmatch[0].rm_eo++;
1759 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001760 n++; /* we saw yet another delimiter */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001761 } else {
1762 pmatch[0].rm_eo = l;
Denys Vlasenkoe4244232009-05-18 23:50:03 +02001763 if (s[l])
1764 pmatch[0].rm_eo++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001765 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001766 memcpy(s1, s, l);
Denis Vlasenko67b5eeb2009-04-12 13:54:13 +00001767 /* make sure we remove *all* of the separator chars */
Denys Vlasenkoe4244232009-05-18 23:50:03 +02001768 do {
1769 s1[l] = '\0';
1770 } while (++l < pmatch[0].rm_eo);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001771 nextword(&s1);
1772 s += pmatch[0].rm_eo;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001773 } while (*s);
1774 return n;
1775 }
1776 if (c[0] == '\0') { /* null split */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001777 while (*s) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001778 *s1++ = *s++;
1779 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001780 n++;
1781 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001782 return n;
1783 }
1784 if (c[0] != ' ') { /* single-character split */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001785 if (icase) {
1786 c[0] = toupper(c[0]);
1787 c[1] = tolower(c[1]);
1788 }
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001789 if (*s1)
1790 n++;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001791 while ((s1 = strpbrk(s1, c)) != NULL) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001792 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001793 n++;
1794 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001795 return n;
1796 }
1797 /* space split */
1798 while (*s) {
1799 s = skip_whitespace(s);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001800 if (!*s)
1801 break;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001802 n++;
1803 while (*s && !isspace(*s))
1804 *s1++ = *s++;
1805 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001806 }
1807 return n;
1808}
1809
Mike Frysinger10a11e22005-09-27 02:23:02 +00001810static void split_f0(void)
1811{
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001812/* static char *fstrings; */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001813#define fstrings (G.split_f0__fstrings)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001814
Glenn L McGrath545106f2002-11-11 06:21:00 +00001815 int i, n;
1816 char *s;
1817
1818 if (is_f0_split)
1819 return;
1820
1821 is_f0_split = TRUE;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001822 free(fstrings);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001823 fsrealloc(0);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001824 n = awk_split(getvar_s(intvar[F0]), &fsplitter.n, &fstrings);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001825 fsrealloc(n);
1826 s = fstrings;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001827 for (i = 0; i < n; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001828 Fields[i].string = nextword(&s);
1829 Fields[i].type |= (VF_FSTR | VF_USER | VF_DIRTY);
1830 }
1831
1832 /* set NF manually to avoid side effects */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001833 clrvar(intvar[NF]);
1834 intvar[NF]->type = VF_NUMBER | VF_SPECIAL;
1835 intvar[NF]->number = nfields;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001836#undef fstrings
Glenn L McGrath545106f2002-11-11 06:21:00 +00001837}
1838
1839/* perform additional actions when some internal variables changed */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001840static void handle_special(var *v)
1841{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001842 int n;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001843 char *b;
1844 const char *sep, *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001845 int sl, l, len, i, bsize;
1846
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001847 if (!(v->type & VF_SPECIAL))
Glenn L McGrath545106f2002-11-11 06:21:00 +00001848 return;
1849
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001850 if (v == intvar[NF]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001851 n = (int)getvar_i(v);
Denys Vlasenko75a1c872018-11-28 12:16:29 +01001852 if (n < 0)
1853 syntax_error("NF set to negative value");
Glenn L McGrath545106f2002-11-11 06:21:00 +00001854 fsrealloc(n);
1855
1856 /* recalculate $0 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001857 sep = getvar_s(intvar[OFS]);
Rob Landleya3896512006-05-07 20:20:34 +00001858 sl = strlen(sep);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001859 b = NULL;
1860 len = 0;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001861 for (i = 0; i < n; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001862 s = getvar_s(&Fields[i]);
Rob Landleya3896512006-05-07 20:20:34 +00001863 l = strlen(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001864 if (b) {
1865 memcpy(b+len, sep, sl);
1866 len += sl;
1867 }
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001868 b = qrealloc(b, len+l+sl, &bsize);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001869 memcpy(b+len, s, l);
1870 len += l;
1871 }
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001872 if (b)
1873 b[len] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001874 setvar_p(intvar[F0], b);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001875 is_f0_split = TRUE;
1876
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001877 } else if (v == intvar[F0]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001878 is_f0_split = FALSE;
1879
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001880 } else if (v == intvar[FS]) {
Denys Vlasenkodf8066a2012-07-11 01:27:15 +02001881 /*
1882 * The POSIX-2008 standard says that changing FS should have no effect on the
1883 * current input line, but only on the next one. The language is:
1884 *
1885 * > Before the first reference to a field in the record is evaluated, the record
1886 * > shall be split into fields, according to the rules in Regular Expressions,
1887 * > using the value of FS that was current at the time the record was read.
1888 *
1889 * So, split up current line before assignment to FS:
1890 */
1891 split_f0();
1892
Glenn L McGrath545106f2002-11-11 06:21:00 +00001893 mk_splitter(getvar_s(v), &fsplitter);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001894 } else if (v == intvar[RS]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001895 mk_splitter(getvar_s(v), &rsplitter);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001896 } else if (v == intvar[IGNORECASE]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001897 icase = istrue(v);
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001898 } else { /* $n */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001899 n = getvar_i(intvar[NF]);
1900 setvar_i(intvar[NF], n > v-Fields ? n : v-Fields+1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001901 /* right here v is invalid. Just to note... */
1902 }
1903}
1904
1905/* step through func/builtin/etc arguments */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001906static node *nextarg(node **pn)
1907{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001908 node *n;
1909
1910 n = *pn;
1911 if (n && (n->info & OPCLSMASK) == OC_COMMA) {
1912 *pn = n->r.n;
1913 n = n->l.n;
1914 } else {
1915 *pn = NULL;
1916 }
1917 return n;
1918}
1919
Mike Frysinger10a11e22005-09-27 02:23:02 +00001920static void hashwalk_init(var *v, xhash *array)
1921{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001922 hash_item *hi;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00001923 unsigned i;
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001924 walker_list *w;
1925 walker_list *prev_walker;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001926
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001927 if (v->type & VF_WALK) {
1928 prev_walker = v->x.walker;
1929 } else {
1930 v->type |= VF_WALK;
1931 prev_walker = NULL;
1932 }
1933 debug_printf_walker("hashwalk_init: prev_walker:%p\n", prev_walker);
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001934
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001935 w = v->x.walker = xzalloc(sizeof(*w) + array->glen + 1); /* why + 1? */
1936 debug_printf_walker(" walker@%p=%p\n", &v->x.walker, w);
1937 w->cur = w->end = w->wbuf;
1938 w->prev = prev_walker;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001939 for (i = 0; i < array->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001940 hi = array->items[i];
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001941 while (hi) {
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001942 strcpy(w->end, hi->name);
1943 nextword(&w->end);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001944 hi = hi->next;
1945 }
1946 }
1947}
1948
Mike Frysinger10a11e22005-09-27 02:23:02 +00001949static int hashwalk_next(var *v)
1950{
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001951 walker_list *w = v->x.walker;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001952
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001953 if (w->cur >= w->end) {
1954 walker_list *prev_walker = w->prev;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001955
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001956 debug_printf_walker("end of iteration, free(walker@%p:%p), prev_walker:%p\n", &v->x.walker, w, prev_walker);
1957 free(w);
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001958 v->x.walker = prev_walker;
1959 return FALSE;
1960 }
1961
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001962 setvar_s(v, nextword(&w->cur));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001963 return TRUE;
1964}
1965
1966/* evaluate node, return 1 when result is true, 0 otherwise */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001967static int ptest(node *pattern)
1968{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001969 /* ptest__v is "static": to save stack space? */
1970 return istrue(evaluate(pattern, &G.ptest__v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001971}
1972
1973/* read next record from stream rsm into a variable v */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001974static int awk_getline(rstream *rsm, var *v)
1975{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001976 char *b;
1977 regmatch_t pmatch[2];
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001978 int size, a, p, pp = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001979 int fd, so, eo, r, rp;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001980 char c, *m, *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001981
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02001982 debug_printf_eval("entered %s()\n", __func__);
1983
Glenn L McGrath545106f2002-11-11 06:21:00 +00001984 /* we're using our own buffer since we need access to accumulating
1985 * characters
1986 */
1987 fd = fileno(rsm->F);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001988 m = rsm->buffer;
1989 a = rsm->adv;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001990 p = rsm->pos;
1991 size = rsm->size;
1992 c = (char) rsplitter.n.info;
1993 rp = 0;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001994
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001995 if (!m)
1996 m = qrealloc(m, 256, &size);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001997
Glenn L McGrath545106f2002-11-11 06:21:00 +00001998 do {
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001999 b = m + a;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002000 so = eo = p;
2001 r = 1;
2002 if (p > 0) {
2003 if ((rsplitter.n.info & OPCLSMASK) == OC_REGEXP) {
2004 if (regexec(icase ? rsplitter.n.r.ire : rsplitter.n.l.re,
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002005 b, 1, pmatch, 0) == 0) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002006 so = pmatch[0].rm_so;
2007 eo = pmatch[0].rm_eo;
2008 if (b[eo] != '\0')
2009 break;
2010 }
2011 } else if (c != '\0') {
2012 s = strchr(b+pp, c);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002013 if (!s)
2014 s = memchr(b+pp, '\0', p - pp);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002015 if (s) {
2016 so = eo = s-b;
2017 eo++;
2018 break;
2019 }
2020 } else {
2021 while (b[rp] == '\n')
2022 rp++;
2023 s = strstr(b+rp, "\n\n");
2024 if (s) {
2025 so = eo = s-b;
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002026 while (b[eo] == '\n')
2027 eo++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002028 if (b[eo] != '\0')
2029 break;
2030 }
2031 }
2032 }
2033
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002034 if (a > 0) {
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002035 memmove(m, m+a, p+1);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002036 b = m;
2037 a = 0;
2038 }
2039
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002040 m = qrealloc(m, a+p+128, &size);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002041 b = m + a;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002042 pp = p;
2043 p += safe_read(fd, b+p, size-p-1);
2044 if (p < pp) {
2045 p = 0;
2046 r = 0;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002047 setvar_i(intvar[ERRNO], errno);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002048 }
2049 b[p] = '\0';
2050
2051 } while (p > pp);
2052
2053 if (p == 0) {
2054 r--;
2055 } else {
2056 c = b[so]; b[so] = '\0';
2057 setvar_s(v, b+rp);
2058 v->type |= VF_USER;
2059 b[so] = c;
2060 c = b[eo]; b[eo] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002061 setvar_s(intvar[RT], b+so);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002062 b[eo] = c;
2063 }
2064
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002065 rsm->buffer = m;
2066 rsm->adv = a + eo;
2067 rsm->pos = p - eo;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002068 rsm->size = size;
2069
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002070 debug_printf_eval("returning from %s(): %d\n", __func__, r);
2071
Glenn L McGrath545106f2002-11-11 06:21:00 +00002072 return r;
2073}
2074
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00002075static int fmt_num(char *b, int size, const char *format, double n, int int_as_int)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002076{
Denis Vlasenkof782f522007-01-01 23:51:30 +00002077 int r = 0;
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00002078 char c;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002079 const char *s = format;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002080
Denys Vlasenko1390a012013-07-20 21:23:01 +02002081 if (int_as_int && n == (long long)n) {
2082 r = snprintf(b, size, "%lld", (long long)n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002083 } else {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002084 do { c = *s; } while (c && *++s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002085 if (strchr("diouxX", c)) {
2086 r = snprintf(b, size, format, (int)n);
2087 } else if (strchr("eEfgG", c)) {
2088 r = snprintf(b, size, format, n);
2089 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002090 syntax_error(EMSG_INV_FMT);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002091 }
2092 }
2093 return r;
2094}
2095
Glenn L McGrath545106f2002-11-11 06:21:00 +00002096/* formatted output into an allocated buffer, return ptr to buffer */
Mike Frysinger10a11e22005-09-27 02:23:02 +00002097static char *awk_printf(node *n)
2098{
Glenn L McGrath545106f2002-11-11 06:21:00 +00002099 char *b = NULL;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002100 char *fmt, *s, *f;
2101 const char *s1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002102 int i, j, incr, bsize;
2103 char c, c1;
2104 var *v, *arg;
2105
2106 v = nvalloc(1);
Rob Landleyd921b2e2006-08-03 15:41:12 +00002107 fmt = f = xstrdup(getvar_s(evaluate(nextarg(&n), v)));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002108
2109 i = 0;
2110 while (*f) {
2111 s = f;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002112 while (*f && (*f != '%' || *++f == '%'))
Glenn L McGrath545106f2002-11-11 06:21:00 +00002113 f++;
Denis Vlasenko389f9d52007-05-09 21:57:23 +00002114 while (*f && !isalpha(*f)) {
2115 if (*f == '*')
2116 syntax_error("%*x formats are not supported");
Glenn L McGrath545106f2002-11-11 06:21:00 +00002117 f++;
Denis Vlasenko389f9d52007-05-09 21:57:23 +00002118 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002119
2120 incr = (f - s) + MAXVARFMT;
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002121 b = qrealloc(b, incr + i, &bsize);
Denis Vlasenkof782f522007-01-01 23:51:30 +00002122 c = *f;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002123 if (c != '\0')
2124 f++;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002125 c1 = *f;
2126 *f = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00002127 arg = evaluate(nextarg(&n), v);
2128
2129 j = i;
2130 if (c == 'c' || !c) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002131 i += sprintf(b+i, s, is_numeric(arg) ?
2132 (char)getvar_i(arg) : *getvar_s(arg));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002133 } else if (c == 's') {
Denis Vlasenko92758142006-10-03 19:56:34 +00002134 s1 = getvar_s(arg);
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002135 b = qrealloc(b, incr+i+strlen(s1), &bsize);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002136 i += sprintf(b+i, s, s1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002137 } else {
2138 i += fmt_num(b+i, incr, s, getvar_i(arg), FALSE);
2139 }
2140 *f = c1;
2141
2142 /* if there was an error while sprintf, return value is negative */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002143 if (i < j)
2144 i = j;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002145 }
2146
Glenn L McGrath545106f2002-11-11 06:21:00 +00002147 free(fmt);
2148 nvfree(v);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002149 b = xrealloc(b, i + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002150 b[i] = '\0';
2151 return b;
2152}
2153
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002154/* Common substitution routine.
2155 * Replace (nm)'th substring of (src) that matches (rn) with (repl),
2156 * store result into (dest), return number of substitutions.
2157 * If nm = 0, replace all matches.
2158 * If src or dst is NULL, use $0.
2159 * If subexp != 0, enable subexpression matching (\1-\9).
Glenn L McGrath545106f2002-11-11 06:21:00 +00002160 */
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002161static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int subexp)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002162{
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002163 char *resbuf;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002164 const char *sp;
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002165 int match_no, residx, replen, resbufsize;
2166 int regexec_flags;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002167 regmatch_t pmatch[10];
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002168 regex_t sreg, *regex;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002169
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002170 resbuf = NULL;
2171 residx = 0;
2172 match_no = 0;
2173 regexec_flags = 0;
2174 regex = as_regex(rn, &sreg);
2175 sp = getvar_s(src ? src : intvar[F0]);
2176 replen = strlen(repl);
2177 while (regexec(regex, sp, 10, pmatch, regexec_flags) == 0) {
2178 int so = pmatch[0].rm_so;
2179 int eo = pmatch[0].rm_eo;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002180
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002181 //bb_error_msg("match %u: [%u,%u] '%s'%p", match_no+1, so, eo, sp,sp);
2182 resbuf = qrealloc(resbuf, residx + eo + replen, &resbufsize);
2183 memcpy(resbuf + residx, sp, eo);
2184 residx += eo;
2185 if (++match_no >= nm) {
2186 const char *s;
2187 int nbs;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002188
Glenn L McGrath545106f2002-11-11 06:21:00 +00002189 /* replace */
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002190 residx -= (eo - so);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002191 nbs = 0;
2192 for (s = repl; *s; s++) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002193 char c = resbuf[residx++] = *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002194 if (c == '\\') {
2195 nbs++;
2196 continue;
2197 }
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002198 if (c == '&' || (subexp && c >= '0' && c <= '9')) {
2199 int j;
2200 residx -= ((nbs + 3) >> 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002201 j = 0;
2202 if (c != '&') {
2203 j = c - '0';
2204 nbs++;
2205 }
2206 if (nbs % 2) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002207 resbuf[residx++] = c;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002208 } else {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002209 int n = pmatch[j].rm_eo - pmatch[j].rm_so;
2210 resbuf = qrealloc(resbuf, residx + replen + n, &resbufsize);
2211 memcpy(resbuf + residx, sp + pmatch[j].rm_so, n);
2212 residx += n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002213 }
2214 }
2215 nbs = 0;
2216 }
2217 }
2218
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002219 regexec_flags = REG_NOTBOL;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002220 sp += eo;
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002221 if (match_no == nm)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002222 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002223 if (eo == so) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002224 /* Empty match (e.g. "b*" will match anywhere).
2225 * Advance by one char. */
2226//BUG (bug 1333):
2227//gsub(/\<b*/,"") on "abc" will reach this point, advance to "bc"
2228//... and will erroneously match "b" even though it is NOT at the word start.
2229//we need REG_NOTBOW but it does not exist...
Denys Vlasenko7379cd12010-04-04 01:48:12 +02002230//TODO: if EXTRA_COMPAT=y, use GNU matching and re_search,
2231//it should be able to do it correctly.
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002232 /* Subtle: this is safe only because
2233 * qrealloc allocated at least one extra byte */
2234 resbuf[residx] = *sp;
2235 if (*sp == '\0')
2236 goto ret;
2237 sp++;
2238 residx++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002239 }
2240 }
2241
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002242 resbuf = qrealloc(resbuf, residx + strlen(sp), &resbufsize);
2243 strcpy(resbuf + residx, sp);
2244 ret:
2245 //bb_error_msg("end sp:'%s'%p", sp,sp);
2246 setvar_p(dest ? dest : intvar[F0], resbuf);
2247 if (regex == &sreg)
2248 regfree(regex);
2249 return match_no;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002250}
2251
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002252static NOINLINE int do_mktime(const char *ds)
2253{
2254 struct tm then;
2255 int count;
2256
2257 /*memset(&then, 0, sizeof(then)); - not needed */
2258 then.tm_isdst = -1; /* default is unknown */
2259
2260 /* manpage of mktime says these fields are ints,
2261 * so we can sscanf stuff directly into them */
2262 count = sscanf(ds, "%u %u %u %u %u %u %d",
2263 &then.tm_year, &then.tm_mon, &then.tm_mday,
2264 &then.tm_hour, &then.tm_min, &then.tm_sec,
2265 &then.tm_isdst);
2266
2267 if (count < 6
2268 || (unsigned)then.tm_mon < 1
2269 || (unsigned)then.tm_year < 1900
2270 ) {
2271 return -1;
2272 }
2273
2274 then.tm_mon -= 1;
Denys Vlasenkobc3e9472009-09-21 04:16:00 +02002275 then.tm_year -= 1900;
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002276
2277 return mktime(&then);
2278}
2279
2280static NOINLINE var *exec_builtin(node *op, var *res)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002281{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002282#define tspl (G.exec_builtin__tspl)
2283
Glenn L McGrath545106f2002-11-11 06:21:00 +00002284 var *tv;
2285 node *an[4];
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002286 var *av[4];
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002287 const char *as[4];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002288 regmatch_t pmatch[2];
2289 regex_t sreg, *re;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002290 node *spl;
Mike Frysingerf87b3e32005-09-27 04:16:22 +00002291 uint32_t isr, info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002292 int nargs;
2293 time_t tt;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002294 int i, l, ll, n;
2295
2296 tv = nvalloc(4);
2297 isr = info = op->info;
2298 op = op->l.n;
2299
2300 av[2] = av[3] = NULL;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002301 for (i = 0; i < 4 && op; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002302 an[i] = nextarg(&op);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002303 if (isr & 0x09000000)
2304 av[i] = evaluate(an[i], &tv[i]);
2305 if (isr & 0x08000000)
2306 as[i] = getvar_s(av[i]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002307 isr >>= 1;
2308 }
2309
2310 nargs = i;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00002311 if ((uint32_t)nargs < (info >> 30))
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002312 syntax_error(EMSG_TOO_FEW_ARGS);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002313
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02002314 info &= OPNMASK;
2315 switch (info) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002316
Denis Vlasenkof782f522007-01-01 23:51:30 +00002317 case B_a2:
Rob Landleyd8205b32010-10-24 03:27:22 +02002318 if (ENABLE_FEATURE_AWK_LIBM)
2319 setvar_i(res, atan2(getvar_i(av[0]), getvar_i(av[1])));
2320 else
2321 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002322 break;
2323
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002324 case B_sp: {
2325 char *s, *s1;
2326
Glenn L McGrath545106f2002-11-11 06:21:00 +00002327 if (nargs > 2) {
2328 spl = (an[2]->info & OPCLSMASK) == OC_REGEXP ?
2329 an[2] : mk_splitter(getvar_s(evaluate(an[2], &tv[2])), &tspl);
2330 } else {
2331 spl = &fsplitter.n;
2332 }
2333
2334 n = awk_split(as[0], spl, &s);
2335 s1 = s;
2336 clear_array(iamarray(av[1]));
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002337 for (i = 1; i <= n; i++)
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002338 setari_u(av[1], i, nextword(&s));
2339 free(s1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002340 setvar_i(res, n);
2341 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002342 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002343
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002344 case B_ss: {
2345 char *s;
2346
Rob Landleya3896512006-05-07 20:20:34 +00002347 l = strlen(as[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002348 i = getvar_i(av[1]) - 1;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002349 if (i > l)
2350 i = l;
2351 if (i < 0)
2352 i = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002353 n = (nargs > 2) ? getvar_i(av[2]) : l-i;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002354 if (n < 0)
2355 n = 0;
Denis Vlasenko8ae5b282008-07-02 22:47:49 +00002356 s = xstrndup(as[0]+i, n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002357 setvar_p(res, s);
2358 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002359 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +00002360
Denis Vlasenko7cbcd1c2008-08-28 23:16:58 +00002361 /* Bitwise ops must assume that operands are unsigned. GNU Awk 3.1.5:
2362 * awk '{ print or(-1,1) }' gives "4.29497e+09", not "-2.xxxe+09" */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002363 case B_an:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002364 setvar_i(res, getvar_i_int(av[0]) & getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002365 break;
Denis Vlasenkof7996f32007-01-11 17:20:00 +00002366
Denis Vlasenkof782f522007-01-01 23:51:30 +00002367 case B_co:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002368 setvar_i(res, ~getvar_i_int(av[0]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002369 break;
2370
Denis Vlasenkof782f522007-01-01 23:51:30 +00002371 case B_ls:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002372 setvar_i(res, getvar_i_int(av[0]) << getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002373 break;
2374
Denis Vlasenkof782f522007-01-01 23:51:30 +00002375 case B_or:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002376 setvar_i(res, getvar_i_int(av[0]) | getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002377 break;
2378
Denis Vlasenkof782f522007-01-01 23:51:30 +00002379 case B_rs:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002380 setvar_i(res, getvar_i_int(av[0]) >> getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002381 break;
2382
Denis Vlasenkof782f522007-01-01 23:51:30 +00002383 case B_xo:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002384 setvar_i(res, getvar_i_int(av[0]) ^ getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002385 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002386
Denis Vlasenkof782f522007-01-01 23:51:30 +00002387 case B_lo:
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002388 case B_up: {
2389 char *s, *s1;
Rob Landleyd921b2e2006-08-03 15:41:12 +00002390 s1 = s = xstrdup(as[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002391 while (*s1) {
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02002392 //*s1 = (info == B_up) ? toupper(*s1) : tolower(*s1);
2393 if ((unsigned char)((*s1 | 0x20) - 'a') <= ('z' - 'a'))
2394 *s1 = (info == B_up) ? (*s1 & 0xdf) : (*s1 | 0x20);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002395 s1++;
2396 }
2397 setvar_p(res, s);
2398 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002399 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002400
Denis Vlasenkof782f522007-01-01 23:51:30 +00002401 case B_ix:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002402 n = 0;
Rob Landleya3896512006-05-07 20:20:34 +00002403 ll = strlen(as[1]);
2404 l = strlen(as[0]) - ll;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002405 if (ll > 0 && l >= 0) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002406 if (!icase) {
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002407 char *s = strstr(as[0], as[1]);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002408 if (s)
2409 n = (s - as[0]) + 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002410 } else {
2411 /* this piece of code is terribly slow and
2412 * really should be rewritten
2413 */
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002414 for (i = 0; i <= l; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002415 if (strncasecmp(as[0]+i, as[1], ll) == 0) {
2416 n = i+1;
2417 break;
2418 }
2419 }
2420 }
2421 }
2422 setvar_i(res, n);
2423 break;
2424
Denis Vlasenkof782f522007-01-01 23:51:30 +00002425 case B_ti:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002426 if (nargs > 1)
2427 tt = getvar_i(av[1]);
2428 else
2429 time(&tt);
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002430 //s = (nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y";
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002431 i = strftime(g_buf, MAXVARFMT,
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002432 ((nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"),
2433 localtime(&tt));
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002434 g_buf[i] = '\0';
2435 setvar_s(res, g_buf);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002436 break;
2437
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002438 case B_mt:
2439 setvar_i(res, do_mktime(as[0]));
2440 break;
2441
Denis Vlasenkof782f522007-01-01 23:51:30 +00002442 case B_ma:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002443 re = as_regex(an[1], &sreg);
2444 n = regexec(re, as[0], 1, pmatch, 0);
2445 if (n == 0) {
2446 pmatch[0].rm_so++;
2447 pmatch[0].rm_eo++;
2448 } else {
2449 pmatch[0].rm_so = 0;
2450 pmatch[0].rm_eo = -1;
2451 }
2452 setvar_i(newvar("RSTART"), pmatch[0].rm_so);
2453 setvar_i(newvar("RLENGTH"), pmatch[0].rm_eo - pmatch[0].rm_so);
2454 setvar_i(res, pmatch[0].rm_so);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002455 if (re == &sreg)
2456 regfree(re);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002457 break;
2458
Denis Vlasenkof782f522007-01-01 23:51:30 +00002459 case B_ge:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002460 awk_sub(an[0], as[1], getvar_i(av[2]), av[3], res, TRUE);
2461 break;
2462
Denis Vlasenkof782f522007-01-01 23:51:30 +00002463 case B_gs:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002464 setvar_i(res, awk_sub(an[0], as[1], 0, av[2], av[2], FALSE));
2465 break;
2466
Denis Vlasenkof782f522007-01-01 23:51:30 +00002467 case B_su:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002468 setvar_i(res, awk_sub(an[0], as[1], 1, av[2], av[2], FALSE));
2469 break;
2470 }
2471
2472 nvfree(tv);
2473 return res;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002474#undef tspl
Glenn L McGrath545106f2002-11-11 06:21:00 +00002475}
2476
2477/*
2478 * Evaluate node - the heart of the program. Supplied with subtree
2479 * and place where to store result. returns ptr to result.
2480 */
2481#define XC(n) ((n) >> 8)
2482
Mike Frysinger10a11e22005-09-27 02:23:02 +00002483static var *evaluate(node *op, var *res)
2484{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002485/* This procedure is recursive so we should count every byte */
2486#define fnargs (G.evaluate__fnargs)
2487/* seed is initialized to 1 */
2488#define seed (G.evaluate__seed)
Denys Vlasenkofb132e42010-10-29 11:46:52 +02002489#define sreg (G.evaluate__sreg)
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002490
Glenn L McGrath545106f2002-11-11 06:21:00 +00002491 var *v1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002492
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002493 if (!op)
Glenn L McGrath545106f2002-11-11 06:21:00 +00002494 return setvar_s(res, NULL);
2495
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002496 debug_printf_eval("entered %s()\n", __func__);
2497
Glenn L McGrath545106f2002-11-11 06:21:00 +00002498 v1 = nvalloc(2);
2499
2500 while (op) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002501 struct {
2502 var *v;
2503 const char *s;
2504 } L = L; /* for compiler */
2505 struct {
2506 var *v;
2507 const char *s;
2508 } R = R;
2509 double L_d = L_d;
2510 uint32_t opinfo;
2511 int opn;
2512 node *op1;
2513
Glenn L McGrath545106f2002-11-11 06:21:00 +00002514 opinfo = op->info;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002515 opn = (opinfo & OPNMASK);
2516 g_lineno = op->lineno;
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002517 op1 = op->l.n;
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002518 debug_printf_eval("opinfo:%08x opn:%08x\n", opinfo, opn);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002519
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01002520 /* "delete" is special:
2521 * "delete array[var--]" must evaluate index expr only once,
2522 * must not evaluate it in "execute inevitable things" part.
2523 */
2524 if (XC(opinfo & OPCLSMASK) == XC(OC_DELETE)) {
2525 uint32_t info = op1->info & OPCLSMASK;
2526 var *v;
2527
2528 debug_printf_eval("DELETE\n");
2529 if (info == OC_VAR) {
2530 v = op1->l.v;
2531 } else if (info == OC_FNARG) {
2532 v = &fnargs[op1->l.aidx];
2533 } else {
2534 syntax_error(EMSG_NOT_ARRAY);
2535 }
2536 if (op1->r.n) { /* array ref? */
2537 const char *s;
2538 s = getvar_s(evaluate(op1->r.n, v1));
2539 hash_remove(iamarray(v), s);
2540 } else {
2541 clear_array(iamarray(v));
2542 }
2543 goto next;
2544 }
2545
Mike Frysingerde2b9382005-09-27 03:18:00 +00002546 /* execute inevitable things */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002547 if (opinfo & OF_RES1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002548 L.v = evaluate(op1, v1);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002549 if (opinfo & OF_RES2)
2550 R.v = evaluate(op->r.n, v1+1);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002551 if (opinfo & OF_STR1) {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002552 L.s = getvar_s(L.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002553 debug_printf_eval("L.s:'%s'\n", L.s);
2554 }
2555 if (opinfo & OF_STR2) {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002556 R.s = getvar_s(R.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002557 debug_printf_eval("R.s:'%s'\n", R.s);
2558 }
2559 if (opinfo & OF_NUM1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002560 L_d = getvar_i(L.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002561 debug_printf_eval("L_d:%f\n", L_d);
2562 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002563
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002564 debug_printf_eval("switch(0x%x)\n", XC(opinfo & OPCLSMASK));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002565 switch (XC(opinfo & OPCLSMASK)) {
2566
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002567 /* -- iterative node type -- */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002568
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002569 /* test pattern */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002570 case XC( OC_TEST ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002571 if ((op1->info & OPCLSMASK) == OC_COMMA) {
2572 /* it's range pattern */
2573 if ((opinfo & OF_CHECKED) || ptest(op1->l.n)) {
2574 op->info |= OF_CHECKED;
2575 if (ptest(op1->r.n))
2576 op->info &= ~OF_CHECKED;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002577 op = op->a.n;
2578 } else {
2579 op = op->r.n;
2580 }
2581 } else {
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002582 op = ptest(op1) ? op->a.n : op->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002583 }
2584 break;
2585
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002586 /* just evaluate an expression, also used as unconditional jump */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002587 case XC( OC_EXEC ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002588 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002589
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002590 /* branch, used in if-else and various loops */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002591 case XC( OC_BR ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002592 op = istrue(L.v) ? op->a.n : op->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002593 break;
2594
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002595 /* initialize for-in loop */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002596 case XC( OC_WALKINIT ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002597 hashwalk_init(L.v, iamarray(R.v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002598 break;
2599
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002600 /* get next array item */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002601 case XC( OC_WALKNEXT ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002602 op = hashwalk_next(L.v) ? op->a.n : op->r.n;
2603 break;
2604
Denis Vlasenkof782f522007-01-01 23:51:30 +00002605 case XC( OC_PRINT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002606 case XC( OC_PRINTF ): {
2607 FILE *F = stdout;
2608
Mike Frysingerde2b9382005-09-27 03:18:00 +00002609 if (op->r.n) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002610 rstream *rsm = newfile(R.s);
2611 if (!rsm->F) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002612 if (opn == '|') {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002613 rsm->F = popen(R.s, "w");
2614 if (rsm->F == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002615 bb_perror_msg_and_die("popen");
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002616 rsm->is_pipe = 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002617 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002618 rsm->F = xfopen(R.s, opn=='w' ? "w" : "a");
Glenn L McGrath545106f2002-11-11 06:21:00 +00002619 }
2620 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002621 F = rsm->F;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002622 }
2623
2624 if ((opinfo & OPCLSMASK) == OC_PRINT) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002625 if (!op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002626 fputs(getvar_s(intvar[F0]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002627 } else {
2628 while (op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002629 var *v = evaluate(nextarg(&op1), v1);
2630 if (v->type & VF_NUMBER) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002631 fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[OFMT]),
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002632 getvar_i(v), TRUE);
2633 fputs(g_buf, F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002634 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002635 fputs(getvar_s(v), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002636 }
2637
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002638 if (op1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002639 fputs(getvar_s(intvar[OFS]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002640 }
2641 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002642 fputs(getvar_s(intvar[ORS]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002643
2644 } else { /* OC_PRINTF */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002645 char *s = awk_printf(op1);
2646 fputs(s, F);
2647 free(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002648 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002649 fflush(F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002650 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002651 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002652
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01002653 /* case XC( OC_DELETE ): - moved to happen before arg evaluation */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002654
Denis Vlasenkof782f522007-01-01 23:51:30 +00002655 case XC( OC_NEWSOURCE ):
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002656 g_progname = op->l.new_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002657 break;
2658
Denis Vlasenkof782f522007-01-01 23:51:30 +00002659 case XC( OC_RETURN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002660 copyvar(res, L.v);
2661 break;
2662
Denis Vlasenkof782f522007-01-01 23:51:30 +00002663 case XC( OC_NEXTFILE ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002664 nextfile = TRUE;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002665 case XC( OC_NEXT ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002666 nextrec = TRUE;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002667 case XC( OC_DONE ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002668 clrvar(res);
2669 break;
2670
Denis Vlasenkof782f522007-01-01 23:51:30 +00002671 case XC( OC_EXIT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002672 awk_exit(L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002673
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002674 /* -- recursive node type -- */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002675
Denis Vlasenkof782f522007-01-01 23:51:30 +00002676 case XC( OC_VAR ):
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01002677 debug_printf_eval("VAR\n");
Mike Frysingerde2b9382005-09-27 03:18:00 +00002678 L.v = op->l.v;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002679 if (L.v == intvar[NF])
Glenn L McGrath545106f2002-11-11 06:21:00 +00002680 split_f0();
2681 goto v_cont;
2682
Denis Vlasenkof782f522007-01-01 23:51:30 +00002683 case XC( OC_FNARG ):
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01002684 debug_printf_eval("FNARG[%d]\n", op->l.aidx);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002685 L.v = &fnargs[op->l.aidx];
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002686 v_cont:
2687 res = op->r.n ? findvar(iamarray(L.v), R.s) : L.v;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002688 break;
2689
Denis Vlasenkof782f522007-01-01 23:51:30 +00002690 case XC( OC_IN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002691 setvar_i(res, hash_search(iamarray(R.v), L.s) ? 1 : 0);
2692 break;
2693
Denis Vlasenkof782f522007-01-01 23:51:30 +00002694 case XC( OC_REGEXP ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002695 op1 = op;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002696 L.s = getvar_s(intvar[F0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002697 goto re_cont;
2698
Denis Vlasenkof782f522007-01-01 23:51:30 +00002699 case XC( OC_MATCH ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002700 op1 = op->r.n;
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002701 re_cont:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002702 {
2703 regex_t *re = as_regex(op1, &sreg);
2704 int i = regexec(re, L.s, 0, NULL, 0);
2705 if (re == &sreg)
2706 regfree(re);
2707 setvar_i(res, (i == 0) ^ (opn == '!'));
2708 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002709 break;
2710
Denis Vlasenkof782f522007-01-01 23:51:30 +00002711 case XC( OC_MOVE ):
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002712 debug_printf_eval("MOVE\n");
Mike Frysingerde2b9382005-09-27 03:18:00 +00002713 /* if source is a temporary string, jusk relink it to dest */
Denys Vlasenko12847742009-11-30 01:15:04 +01002714//Disabled: if R.v is numeric but happens to have cached R.v->string,
2715//then L.v ends up being a string, which is wrong
2716// if (R.v == v1+1 && R.v->string) {
2717// res = setvar_p(L.v, R.v->string);
2718// R.v->string = NULL;
2719// } else {
Mike Frysingerde2b9382005-09-27 03:18:00 +00002720 res = copyvar(L.v, R.v);
Denys Vlasenko12847742009-11-30 01:15:04 +01002721// }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002722 break;
2723
Denis Vlasenkof782f522007-01-01 23:51:30 +00002724 case XC( OC_TERNARY ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002725 if ((op->r.n->info & OPCLSMASK) != OC_COLON)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002726 syntax_error(EMSG_POSSIBLE_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002727 res = evaluate(istrue(L.v) ? op->r.n->l.n : op->r.n->r.n, res);
2728 break;
2729
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002730 case XC( OC_FUNC ): {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002731 var *vbeg, *v;
2732 const char *sv_progname;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002733
Bernhard Reutner-Fischerb79a0fe2013-03-06 21:01:05 +01002734 /* The body might be empty, still has to eval the args */
Bernhard Reutner-Fischera060a1a2013-07-31 15:29:20 +02002735 if (!op->r.n->info && !op->r.f->body.first)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002736 syntax_error(EMSG_UNDEF_FUNC);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002737
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002738 vbeg = v = nvalloc(op->r.f->nargs + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002739 while (op1) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002740 var *arg = evaluate(nextarg(&op1), v1);
2741 copyvar(v, arg);
2742 v->type |= VF_CHILD;
2743 v->x.parent = arg;
2744 if (++v - vbeg >= op->r.f->nargs)
Glenn L McGrath545106f2002-11-11 06:21:00 +00002745 break;
2746 }
2747
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002748 v = fnargs;
2749 fnargs = vbeg;
2750 sv_progname = g_progname;
2751
2752 res = evaluate(op->r.f->body.first, res);
2753
2754 g_progname = sv_progname;
2755 nvfree(fnargs);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002756 fnargs = v;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002757
Glenn L McGrath545106f2002-11-11 06:21:00 +00002758 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002759 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002760
Denis Vlasenkof782f522007-01-01 23:51:30 +00002761 case XC( OC_GETLINE ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002762 case XC( OC_PGETLINE ): {
2763 rstream *rsm;
2764 int i;
2765
Mike Frysingerde2b9382005-09-27 03:18:00 +00002766 if (op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002767 rsm = newfile(L.s);
2768 if (!rsm->F) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002769 if ((opinfo & OPCLSMASK) == OC_PGETLINE) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002770 rsm->F = popen(L.s, "r");
2771 rsm->is_pipe = TRUE;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002772 } else {
Denys Vlasenkofb132e42010-10-29 11:46:52 +02002773 rsm->F = fopen_for_read(L.s); /* not xfopen! */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002774 }
2775 }
2776 } else {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002777 if (!iF)
2778 iF = next_input_file();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002779 rsm = iF;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002780 }
2781
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02002782 if (!rsm || !rsm->F) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002783 setvar_i(intvar[ERRNO], errno);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002784 setvar_i(res, -1);
2785 break;
2786 }
2787
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002788 if (!op->r.n)
2789 R.v = intvar[F0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002790
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002791 i = awk_getline(rsm, R.v);
2792 if (i > 0 && !op1) {
2793 incvar(intvar[FNR]);
2794 incvar(intvar[NR]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002795 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002796 setvar_i(res, i);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002797 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002798 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002799
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002800 /* simple builtins */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002801 case XC( OC_FBLTIN ): {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002802 double R_d = R_d; /* for compiler */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002803
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002804 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002805 case F_in:
Denys Vlasenko1390a012013-07-20 21:23:01 +02002806 R_d = (long long)L_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002807 break;
2808
Denis Vlasenkof782f522007-01-01 23:51:30 +00002809 case F_rn:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002810 R_d = (double)rand() / (double)RAND_MAX;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002811 break;
Rob Landleyd8205b32010-10-24 03:27:22 +02002812
Denis Vlasenkof782f522007-01-01 23:51:30 +00002813 case F_co:
Rob Landleyd8205b32010-10-24 03:27:22 +02002814 if (ENABLE_FEATURE_AWK_LIBM) {
2815 R_d = cos(L_d);
2816 break;
2817 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002818
Denis Vlasenkof782f522007-01-01 23:51:30 +00002819 case F_ex:
Rob Landleyd8205b32010-10-24 03:27:22 +02002820 if (ENABLE_FEATURE_AWK_LIBM) {
2821 R_d = exp(L_d);
2822 break;
2823 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002824
Denis Vlasenkof782f522007-01-01 23:51:30 +00002825 case F_lg:
Rob Landleyd8205b32010-10-24 03:27:22 +02002826 if (ENABLE_FEATURE_AWK_LIBM) {
2827 R_d = log(L_d);
2828 break;
2829 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002830
Denis Vlasenkof782f522007-01-01 23:51:30 +00002831 case F_si:
Rob Landleyd8205b32010-10-24 03:27:22 +02002832 if (ENABLE_FEATURE_AWK_LIBM) {
2833 R_d = sin(L_d);
2834 break;
2835 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002836
Denis Vlasenkof782f522007-01-01 23:51:30 +00002837 case F_sq:
Rob Landleyd8205b32010-10-24 03:27:22 +02002838 if (ENABLE_FEATURE_AWK_LIBM) {
2839 R_d = sqrt(L_d);
2840 break;
2841 }
2842
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002843 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002844 break;
Rob Landleyd8205b32010-10-24 03:27:22 +02002845
Denis Vlasenkof782f522007-01-01 23:51:30 +00002846 case F_sr:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002847 R_d = (double)seed;
2848 seed = op1 ? (unsigned)L_d : (unsigned)time(NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002849 srand(seed);
2850 break;
2851
Denis Vlasenkof782f522007-01-01 23:51:30 +00002852 case F_ti:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002853 R_d = time(NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002854 break;
2855
Denis Vlasenkof782f522007-01-01 23:51:30 +00002856 case F_le:
Denys Vlasenko7985bc12013-10-12 04:51:54 +02002857 debug_printf_eval("length: L.s:'%s'\n", L.s);
2858 if (!op1) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002859 L.s = getvar_s(intvar[F0]);
Denys Vlasenko7985bc12013-10-12 04:51:54 +02002860 debug_printf_eval("length: L.s='%s'\n", L.s);
2861 }
2862 else if (L.v->type & VF_ARRAY) {
2863 R_d = L.v->x.array->nel;
2864 debug_printf_eval("length: array_len:%d\n", L.v->x.array->nel);
2865 break;
2866 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002867 R_d = strlen(L.s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002868 break;
2869
Denis Vlasenkof782f522007-01-01 23:51:30 +00002870 case F_sy:
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002871 fflush_all();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002872 R_d = (ENABLE_FEATURE_ALLOW_EXEC && L.s && *L.s)
Denis Vlasenko249fabf2006-12-19 00:29:22 +00002873 ? (system(L.s) >> 8) : 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002874 break;
2875
Denis Vlasenkof782f522007-01-01 23:51:30 +00002876 case F_ff:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002877 if (!op1) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002878 fflush(stdout);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002879 } else if (L.s && *L.s) {
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002880 rstream *rsm = newfile(L.s);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002881 fflush(rsm->F);
2882 } else {
2883 fflush_all();
Glenn L McGrath545106f2002-11-11 06:21:00 +00002884 }
2885 break;
2886
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002887 case F_cl: {
2888 rstream *rsm;
2889 int err = 0;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002890 rsm = (rstream *)hash_search(fdhash, L.s);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002891 debug_printf_eval("OC_FBLTIN F_cl rsm:%p\n", rsm);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002892 if (rsm) {
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002893 debug_printf_eval("OC_FBLTIN F_cl "
2894 "rsm->is_pipe:%d, ->F:%p\n",
2895 rsm->is_pipe, rsm->F);
2896 /* Can be NULL if open failed. Example:
2897 * getline line <"doesnt_exist";
2898 * close("doesnt_exist"); <--- here rsm->F is NULL
2899 */
2900 if (rsm->F)
2901 err = rsm->is_pipe ? pclose(rsm->F) : fclose(rsm->F);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002902 free(rsm->buffer);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002903 hash_remove(fdhash, L.s);
2904 }
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002905 if (err)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002906 setvar_i(intvar[ERRNO], errno);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002907 R_d = (double)err;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002908 break;
2909 }
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002910 } /* switch */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002911 setvar_i(res, R_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002912 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002913 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002914
Denis Vlasenkof782f522007-01-01 23:51:30 +00002915 case XC( OC_BUILTIN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002916 res = exec_builtin(op, res);
2917 break;
2918
Denis Vlasenkof782f522007-01-01 23:51:30 +00002919 case XC( OC_SPRINTF ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002920 setvar_p(res, awk_printf(op1));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002921 break;
2922
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002923 case XC( OC_UNARY ): {
2924 double Ld, R_d;
2925
2926 Ld = R_d = getvar_i(R.v);
Mike Frysingerde2b9382005-09-27 03:18:00 +00002927 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002928 case 'P':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002929 Ld = ++R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002930 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002931 case 'p':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002932 R_d++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002933 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002934 case 'M':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002935 Ld = --R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002936 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002937 case 'm':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002938 R_d--;
2939 r_op_change:
2940 setvar_i(R.v, R_d);
2941 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002942 case '!':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002943 Ld = !istrue(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002944 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002945 case '-':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002946 Ld = -R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002947 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002948 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002949 setvar_i(res, Ld);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002950 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002951 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002952
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002953 case XC( OC_FIELD ): {
2954 int i = (int)getvar_i(R.v);
Denys Vlasenko2454e672018-04-23 10:53:18 +02002955 if (i < 0)
2956 syntax_error(EMSG_NEGATIVE_FIELD);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002957 if (i == 0) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002958 res = intvar[F0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002959 } else {
2960 split_f0();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002961 if (i > nfields)
2962 fsrealloc(i);
2963 res = &Fields[i - 1];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002964 }
2965 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002966 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002967
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002968 /* concatenation (" ") and index joining (",") */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002969 case XC( OC_CONCAT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002970 case XC( OC_COMMA ): {
2971 const char *sep = "";
2972 if ((opinfo & OPCLSMASK) == OC_COMMA)
2973 sep = getvar_s(intvar[SUBSEP]);
2974 setvar_p(res, xasprintf("%s%s%s", L.s, sep, R.s));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002975 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002976 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002977
Denis Vlasenkof782f522007-01-01 23:51:30 +00002978 case XC( OC_LAND ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002979 setvar_i(res, istrue(L.v) ? ptest(op->r.n) : 0);
2980 break;
2981
Denis Vlasenkof782f522007-01-01 23:51:30 +00002982 case XC( OC_LOR ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002983 setvar_i(res, istrue(L.v) ? 1 : ptest(op->r.n));
2984 break;
2985
Denis Vlasenkof782f522007-01-01 23:51:30 +00002986 case XC( OC_BINARY ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002987 case XC( OC_REPLACE ): {
2988 double R_d = getvar_i(R.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002989 debug_printf_eval("BINARY/REPLACE: R_d:%f opn:%c\n", R_d, opn);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002990 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002991 case '+':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002992 L_d += R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002993 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002994 case '-':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002995 L_d -= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002996 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002997 case '*':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002998 L_d *= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002999 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003000 case '/':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003001 if (R_d == 0)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003002 syntax_error(EMSG_DIV_BY_ZERO);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003003 L_d /= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003004 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003005 case '&':
Rob Landleyd8205b32010-10-24 03:27:22 +02003006 if (ENABLE_FEATURE_AWK_LIBM)
3007 L_d = pow(L_d, R_d);
3008 else
3009 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003010 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003011 case '%':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003012 if (R_d == 0)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003013 syntax_error(EMSG_DIV_BY_ZERO);
Denys Vlasenko1390a012013-07-20 21:23:01 +02003014 L_d -= (long long)(L_d / R_d) * R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003015 break;
3016 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02003017 debug_printf_eval("BINARY/REPLACE result:%f\n", L_d);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003018 res = setvar_i(((opinfo & OPCLSMASK) == OC_BINARY) ? res : L.v, L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003019 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003020 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003021
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003022 case XC( OC_COMPARE ): {
3023 int i = i; /* for compiler */
3024 double Ld;
3025
Glenn L McGrath545106f2002-11-11 06:21:00 +00003026 if (is_numeric(L.v) && is_numeric(R.v)) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003027 Ld = getvar_i(L.v) - getvar_i(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003028 } else {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01003029 const char *l = getvar_s(L.v);
3030 const char *r = getvar_s(R.v);
3031 Ld = icase ? strcasecmp(l, r) : strcmp(l, r);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003032 }
3033 switch (opn & 0xfe) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00003034 case 0:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003035 i = (Ld > 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003036 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003037 case 2:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003038 i = (Ld >= 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003039 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003040 case 4:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003041 i = (Ld == 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003042 break;
3043 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003044 setvar_i(res, (i == 0) ^ (opn & 1));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003045 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003046 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003047
Denis Vlasenkof782f522007-01-01 23:51:30 +00003048 default:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003049 syntax_error(EMSG_POSSIBLE_ERROR);
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01003050 } /* switch */
3051 next:
Glenn L McGrath545106f2002-11-11 06:21:00 +00003052 if ((opinfo & OPCLSMASK) <= SHIFT_TIL_THIS)
3053 op = op->a.n;
3054 if ((opinfo & OPCLSMASK) >= RECUR_FROM_THIS)
3055 break;
3056 if (nextrec)
3057 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003058 } /* while (op) */
3059
Glenn L McGrath545106f2002-11-11 06:21:00 +00003060 nvfree(v1);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003061 debug_printf_eval("returning from %s(): %p\n", __func__, res);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003062 return res;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003063#undef fnargs
3064#undef seed
3065#undef sreg
Glenn L McGrath545106f2002-11-11 06:21:00 +00003066}
3067
3068
3069/* -------- main & co. -------- */
3070
Mike Frysinger10a11e22005-09-27 02:23:02 +00003071static int awk_exit(int r)
3072{
Denis Vlasenkof782f522007-01-01 23:51:30 +00003073 var tv;
3074 unsigned i;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003075 hash_item *hi;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003076
Denis Vlasenkof782f522007-01-01 23:51:30 +00003077 zero_out_var(&tv);
3078
3079 if (!exiting) {
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003080 exiting = TRUE;
Glenn L McGrathca29ffc2004-09-24 09:24:27 +00003081 nextrec = FALSE;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003082 evaluate(endseq.first, &tv);
3083 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003084
3085 /* waiting for children */
Denis Vlasenkof782f522007-01-01 23:51:30 +00003086 for (i = 0; i < fdhash->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003087 hi = fdhash->items[i];
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00003088 while (hi) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003089 if (hi->data.rs.F && hi->data.rs.is_pipe)
3090 pclose(hi->data.rs.F);
3091 hi = hi->next;
3092 }
3093 }
3094
3095 exit(r);
3096}
3097
3098/* if expr looks like "var=value", perform assignment and return 1,
3099 * otherwise return 0 */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00003100static int is_assignment(const char *expr)
Mike Frysinger10a11e22005-09-27 02:23:02 +00003101{
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003102 char *exprc, *val;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003103
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003104 if (!isalnum_(*expr) || (val = strchr(expr, '=')) == NULL) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003105 return FALSE;
3106 }
3107
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003108 exprc = xstrdup(expr);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003109 val = exprc + (val - expr);
3110 *val++ = '\0';
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003111
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003112 unescape_string_in_place(val);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003113 setvar_u(newvar(exprc), val);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003114 free(exprc);
3115 return TRUE;
3116}
3117
3118/* switch to next input file */
Mike Frysinger10a11e22005-09-27 02:23:02 +00003119static rstream *next_input_file(void)
3120{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003121#define rsm (G.next_input_file__rsm)
3122#define files_happen (G.next_input_file__files_happen)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003123
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003124 FILE *F;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00003125 const char *fname, *ind;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003126
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003127 if (rsm.F)
3128 fclose(rsm.F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003129 rsm.F = NULL;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003130 rsm.pos = rsm.adv = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003131
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003132 for (;;) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003133 if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003134 if (files_happen)
3135 return NULL;
3136 fname = "-";
3137 F = stdin;
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003138 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003139 }
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003140 ind = getvar_s(incvar(intvar[ARGIND]));
3141 fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind));
3142 if (fname && *fname && !is_assignment(fname)) {
3143 F = xfopen_stdin(fname);
3144 break;
3145 }
3146 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003147
3148 files_happen = TRUE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003149 setvar_s(intvar[FILENAME], fname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003150 rsm.F = F;
3151 return &rsm;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003152#undef rsm
3153#undef files_happen
Glenn L McGrath545106f2002-11-11 06:21:00 +00003154}
3155
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00003156int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko844a6c52018-01-07 14:39:34 +01003157int awk_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger10a11e22005-09-27 02:23:02 +00003158{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00003159 unsigned opt;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02003160 char *opt_F;
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003161 llist_t *list_v = NULL;
3162 llist_t *list_f = NULL;
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003163#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
3164 llist_t *list_e = NULL;
3165#endif
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003166 int i, j;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003167 var *v;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003168 var tv;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003169 char **envp;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003170 char *vnames = (char *)vNames; /* cheat */
3171 char *vvalues = (char *)vValues;
3172
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003173 INIT_G();
3174
Denis Vlasenko150f4022007-01-13 21:06:21 +00003175 /* Undo busybox.c, or else strtod may eat ','! This breaks parsing:
Denis Vlasenko6dc6ebb2007-01-01 23:53:12 +00003176 * $1,$2 == '$1,' '$2', NOT '$1' ',' '$2' */
3177 if (ENABLE_LOCALE_SUPPORT)
3178 setlocale(LC_NUMERIC, "C");
3179
Denis Vlasenkof782f522007-01-01 23:51:30 +00003180 zero_out_var(&tv);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003181
3182 /* allocate global buffer */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003183 g_buf = xmalloc(MAXVARFMT + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003184
3185 vhash = hash_init();
3186 ahash = hash_init();
3187 fdhash = hash_init();
3188 fnhash = hash_init();
3189
3190 /* initialize variables */
Denis Vlasenkof782f522007-01-01 23:51:30 +00003191 for (i = 0; *vnames; i++) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003192 intvar[i] = v = newvar(nextword(&vnames));
Denis Vlasenkof782f522007-01-01 23:51:30 +00003193 if (*vvalues != '\377')
3194 setvar_s(v, nextword(&vvalues));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003195 else
3196 setvar_i(v, 0);
3197
Denis Vlasenkof782f522007-01-01 23:51:30 +00003198 if (*vnames == '*') {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003199 v->type |= VF_SPECIAL;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003200 vnames++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003201 }
3202 }
3203
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003204 handle_special(intvar[FS]);
3205 handle_special(intvar[RS]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003206
Denis Vlasenkof782f522007-01-01 23:51:30 +00003207 newfile("/dev/stdin")->F = stdin;
3208 newfile("/dev/stdout")->F = stdout;
3209 newfile("/dev/stderr")->F = stderr;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003210
Denis Vlasenkof71d9162007-05-03 22:57:56 +00003211 /* Huh, people report that sometimes environ is NULL. Oh well. */
3212 if (environ) for (envp = environ; *envp; envp++) {
Denis Vlasenkob78c7822007-07-18 18:31:11 +00003213 /* environ is writable, thus we don't strdup it needlessly */
3214 char *s = *envp;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003215 char *s1 = strchr(s, '=');
3216 if (s1) {
Denis Vlasenkob78c7822007-07-18 18:31:11 +00003217 *s1 = '\0';
3218 /* Both findvar and setvar_u take const char*
3219 * as 2nd arg -> environment is not trashed */
3220 setvar_u(findvar(iamarray(intvar[ENVIRON]), s), s1 + 1);
3221 *s1 = '=';
Eric Andersen67776be2004-07-30 23:52:08 +00003222 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003223 }
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003224 opt = getopt32(argv, OPTSTR_AWK, &opt_F, &list_v, &list_f, IF_FEATURE_AWK_GNU_EXTENSIONS(&list_e,) NULL);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003225 argv += optind;
Denys Vlasenko844a6c52018-01-07 14:39:34 +01003226 //argc -= optind;
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003227 if (opt & OPT_W)
3228 bb_error_msg("warning: option -W is ignored");
3229 if (opt & OPT_F) {
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003230 unescape_string_in_place(opt_F);
3231 setvar_s(intvar[FS], opt_F);
3232 }
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003233 while (list_v) {
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003234 if (!is_assignment(llist_pop(&list_v)))
Denis Vlasenkobe644a82007-03-10 17:22:14 +00003235 bb_show_usage();
3236 }
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003237 while (list_f) {
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003238 char *s = NULL;
3239 FILE *from_file;
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003240
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003241 g_progname = llist_pop(&list_f);
3242 from_file = xfopen_stdin(g_progname);
3243 /* one byte is reserved for some trick in next_token */
3244 for (i = j = 1; j > 0; i += j) {
3245 s = xrealloc(s, i + 4096);
3246 j = fread(s + i, 1, 4094, from_file);
3247 }
3248 s[i] = '\0';
3249 fclose(from_file);
3250 parse_program(s + 1);
3251 free(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003252 }
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003253 g_progname = "cmd. line";
3254#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003255 while (list_e) {
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003256 parse_program(llist_pop(&list_e));
3257 }
3258#endif
3259 if (!(opt & (OPT_f | OPT_e))) {
3260 if (!*argv)
3261 bb_show_usage();
3262 parse_program(*argv++);
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003263 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003264
Glenn L McGrath545106f2002-11-11 06:21:00 +00003265 /* fill in ARGV array */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003266 setari_u(intvar[ARGV], 0, "awk");
Denis Vlasenkof782f522007-01-01 23:51:30 +00003267 i = 0;
3268 while (*argv)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003269 setari_u(intvar[ARGV], ++i, *argv++);
Denys Vlasenko844a6c52018-01-07 14:39:34 +01003270 setvar_i(intvar[ARGC], i + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003271
3272 evaluate(beginseq.first, &tv);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003273 if (!mainseq.first && !endseq.first)
Glenn L McGrath545106f2002-11-11 06:21:00 +00003274 awk_exit(EXIT_SUCCESS);
3275
3276 /* input file could already be opened in BEGIN block */
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003277 if (!iF)
3278 iF = next_input_file();
Glenn L McGrath545106f2002-11-11 06:21:00 +00003279
3280 /* passing through input files */
3281 while (iF) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003282 nextfile = FALSE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003283 setvar_i(intvar[FNR], 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003284
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003285 while ((i = awk_getline(iF, intvar[F0])) > 0) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003286 nextrec = FALSE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003287 incvar(intvar[NR]);
3288 incvar(intvar[FNR]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003289 evaluate(mainseq.first, &tv);
3290
3291 if (nextfile)
3292 break;
3293 }
3294
Denis Vlasenkof782f522007-01-01 23:51:30 +00003295 if (i < 0)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003296 syntax_error(strerror(errno));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003297
3298 iF = next_input_file();
Glenn L McGrath545106f2002-11-11 06:21:00 +00003299 }
3300
Glenn L McGrath545106f2002-11-11 06:21:00 +00003301 awk_exit(EXIT_SUCCESS);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003302 /*return 0;*/
Glenn L McGrath545106f2002-11-11 06:21:00 +00003303}