blob: b4f6a374147d918946fd26c51addd8edccf645d6 [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 Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "awk (23 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 \
Denys Vlasenkobd8b05b2020-02-02 23:28:55 +0100275 | TC_STRING | TC_NUMBER | TC_UOPPOST \
276 | TC_LENGTH)
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000277#define TC_CONCAT2 (TC_OPERAND | TC_UOPPRE)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000278
Brian Foley08a514c2019-01-01 13:40:59 -0800279#define OF_RES1 0x010000
280#define OF_RES2 0x020000
281#define OF_STR1 0x040000
282#define OF_STR2 0x080000
283#define OF_NUM1 0x100000
284#define OF_CHECKED 0x200000
285#define OF_REQUIRED 0x400000
286
Glenn L McGrath545106f2002-11-11 06:21:00 +0000287
288/* combined operator flags */
289#define xx 0
290#define xV OF_RES2
291#define xS (OF_RES2 | OF_STR2)
292#define Vx OF_RES1
Brian Foley08a514c2019-01-01 13:40:59 -0800293#define Rx (OF_RES1 | OF_NUM1 | OF_REQUIRED)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000294#define VV (OF_RES1 | OF_RES2)
295#define Nx (OF_RES1 | OF_NUM1)
296#define NV (OF_RES1 | OF_NUM1 | OF_RES2)
297#define Sx (OF_RES1 | OF_STR1)
298#define SV (OF_RES1 | OF_STR1 | OF_RES2)
299#define SS (OF_RES1 | OF_STR1 | OF_RES2 | OF_STR2)
300
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000301#define OPCLSMASK 0xFF00
302#define OPNMASK 0x007F
Glenn L McGrath545106f2002-11-11 06:21:00 +0000303
304/* operator priority is a highest byte (even: r->l, odd: l->r grouping)
305 * For builtins it has different meaning: n n s3 s2 s1 v3 v2 v1,
306 * n - min. number of args, vN - resolve Nth arg to var, sN - resolve to string
307 */
Denys Vlasenko202a1b92011-09-10 04:51:09 +0200308#undef P
309#undef PRIMASK
310#undef PRIMASK2
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000311#define P(x) (x << 24)
312#define PRIMASK 0x7F000000
313#define PRIMASK2 0x7E000000
Glenn L McGrath545106f2002-11-11 06:21:00 +0000314
315/* Operation classes */
316
317#define SHIFT_TIL_THIS 0x0600
318#define RECUR_FROM_THIS 0x1000
319
320enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000321 OC_DELETE = 0x0100, OC_EXEC = 0x0200, OC_NEWSOURCE = 0x0300,
322 OC_PRINT = 0x0400, OC_PRINTF = 0x0500, OC_WALKINIT = 0x0600,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000323
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000324 OC_BR = 0x0700, OC_BREAK = 0x0800, OC_CONTINUE = 0x0900,
325 OC_EXIT = 0x0a00, OC_NEXT = 0x0b00, OC_NEXTFILE = 0x0c00,
326 OC_TEST = 0x0d00, OC_WALKNEXT = 0x0e00,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000327
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000328 OC_BINARY = 0x1000, OC_BUILTIN = 0x1100, OC_COLON = 0x1200,
329 OC_COMMA = 0x1300, OC_COMPARE = 0x1400, OC_CONCAT = 0x1500,
330 OC_FBLTIN = 0x1600, OC_FIELD = 0x1700, OC_FNARG = 0x1800,
331 OC_FUNC = 0x1900, OC_GETLINE = 0x1a00, OC_IN = 0x1b00,
332 OC_LAND = 0x1c00, OC_LOR = 0x1d00, OC_MATCH = 0x1e00,
333 OC_MOVE = 0x1f00, OC_PGETLINE = 0x2000, OC_REGEXP = 0x2100,
334 OC_REPLACE = 0x2200, OC_RETURN = 0x2300, OC_SPRINTF = 0x2400,
335 OC_TERNARY = 0x2500, OC_UNARY = 0x2600, OC_VAR = 0x2700,
336 OC_DONE = 0x2800,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000337
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000338 ST_IF = 0x3000, ST_DO = 0x3100, ST_FOR = 0x3200,
339 ST_WHILE = 0x3300
Glenn L McGrath545106f2002-11-11 06:21:00 +0000340};
341
342/* simple builtins */
343enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000344 F_in, F_rn, F_co, F_ex, F_lg, F_si, F_sq, F_sr,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000345 F_ti, F_le, F_sy, F_ff, F_cl
346};
347
348/* builtins */
349enum {
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +0200350 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 +0000351 B_ge, B_gs, B_su,
352 B_an, B_co, B_ls, B_or, B_rs, B_xo,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000353};
354
355/* tokens and their corresponding info values */
356
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200357#define NTC "\377" /* switch to next token class (tc<<1) */
358#define NTCC '\377'
Glenn L McGrath545106f2002-11-11 06:21:00 +0000359
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000360static const char tokenlist[] ALIGN1 =
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200361 "\1(" NTC /* TC_SEQSTART */
362 "\1)" NTC /* TC_SEQTERM */
363 "\1/" NTC /* TC_REGEXP */
364 "\2>>" "\1>" "\1|" NTC /* TC_OUTRDR */
365 "\2++" "\2--" NTC /* TC_UOPPOST */
366 "\2++" "\2--" "\1$" NTC /* TC_UOPPRE1 */
367 "\2==" "\1=" "\2+=" "\2-=" /* TC_BINOPX */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200368 "\2*=" "\2/=" "\2%=" "\2^="
369 "\1+" "\1-" "\3**=" "\2**"
370 "\1/" "\1%" "\1^" "\1*"
371 "\2!=" "\2>=" "\2<=" "\1>"
372 "\1<" "\2!~" "\1~" "\2&&"
373 "\2||" "\1?" "\1:" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200374 "\2in" NTC /* TC_IN */
375 "\1," NTC /* TC_COMMA */
376 "\1|" NTC /* TC_PIPE */
377 "\1+" "\1-" "\1!" NTC /* TC_UOPPRE2 */
378 "\1]" NTC /* TC_ARRTERM */
379 "\1{" NTC /* TC_GRPSTART */
380 "\1}" NTC /* TC_GRPTERM */
381 "\1;" NTC /* TC_SEMICOL */
382 "\1\n" NTC /* TC_NEWLINE */
383 "\2if" "\2do" "\3for" "\5break" /* TC_STATX */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200384 "\10continue" "\6delete" "\5print"
385 "\6printf" "\4next" "\10nextfile"
386 "\6return" "\4exit" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200387 "\5while" NTC /* TC_WHILE */
388 "\4else" NTC /* TC_ELSE */
389 "\3and" "\5compl" "\6lshift" "\2or" /* TC_BUILTIN */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200390 "\6rshift" "\3xor"
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200391 "\5close" "\6system" "\6fflush" "\5atan2"
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200392 "\3cos" "\3exp" "\3int" "\3log"
393 "\4rand" "\3sin" "\4sqrt" "\5srand"
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200394 "\6gensub" "\4gsub" "\5index" /* "\6length" was here */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200395 "\5match" "\5split" "\7sprintf" "\3sub"
396 "\6substr" "\7systime" "\10strftime" "\6mktime"
397 "\7tolower" "\7toupper" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200398 "\6length" NTC /* TC_LENGTH */
399 "\7getline" NTC /* TC_GETLINE */
400 "\4func" "\10function" NTC /* TC_FUNCDECL */
401 "\5BEGIN" NTC /* TC_BEGIN */
402 "\3END" /* TC_END */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200403 /* compiler adds trailing "\0" */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000404 ;
405
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200406#define OC_B OC_BUILTIN
407
Denys Vlasenko965b7952020-11-30 13:03:03 +0100408static const uint32_t tokeninfo[] ALIGN4 = {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000409 0,
410 0,
411 OC_REGEXP,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200412 xS|'a', xS|'w', xS|'|',
413 OC_UNARY|xV|P(9)|'p', OC_UNARY|xV|P(9)|'m',
414 OC_UNARY|xV|P(9)|'P', OC_UNARY|xV|P(9)|'M', OC_FIELD|xV|P(5),
415 OC_COMPARE|VV|P(39)|5, OC_MOVE|VV|P(74), OC_REPLACE|NV|P(74)|'+', OC_REPLACE|NV|P(74)|'-',
416 OC_REPLACE|NV|P(74)|'*', OC_REPLACE|NV|P(74)|'/', OC_REPLACE|NV|P(74)|'%', OC_REPLACE|NV|P(74)|'&',
417 OC_BINARY|NV|P(29)|'+', OC_BINARY|NV|P(29)|'-', OC_REPLACE|NV|P(74)|'&', OC_BINARY|NV|P(15)|'&',
418 OC_BINARY|NV|P(25)|'/', OC_BINARY|NV|P(25)|'%', OC_BINARY|NV|P(15)|'&', OC_BINARY|NV|P(25)|'*',
419 OC_COMPARE|VV|P(39)|4, OC_COMPARE|VV|P(39)|3, OC_COMPARE|VV|P(39)|0, OC_COMPARE|VV|P(39)|1,
420 OC_COMPARE|VV|P(39)|2, OC_MATCH|Sx|P(45)|'!', OC_MATCH|Sx|P(45)|'~', OC_LAND|Vx|P(55),
421 OC_LOR|Vx|P(59), OC_TERNARY|Vx|P(64)|'?', OC_COLON|xx|P(67)|':',
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200422 OC_IN|SV|P(49), /* TC_IN */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000423 OC_COMMA|SS|P(80),
424 OC_PGETLINE|SV|P(37),
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200425 OC_UNARY|xV|P(19)|'+', OC_UNARY|xV|P(19)|'-', OC_UNARY|xV|P(19)|'!',
426 0, /* ] */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000427 0,
428 0,
429 0,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200430 0, /* \n */
431 ST_IF, ST_DO, ST_FOR, OC_BREAK,
Brian Foley08a514c2019-01-01 13:40:59 -0800432 OC_CONTINUE, OC_DELETE|Rx, OC_PRINT,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200433 OC_PRINTF, OC_NEXT, OC_NEXTFILE,
434 OC_RETURN|Vx, OC_EXIT|Nx,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000435 ST_WHILE,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200436 0, /* else */
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000437 OC_B|B_an|P(0x83), OC_B|B_co|P(0x41), OC_B|B_ls|P(0x83), OC_B|B_or|P(0x83),
438 OC_B|B_rs|P(0x83), OC_B|B_xo|P(0x83),
Glenn L McGrath545106f2002-11-11 06:21:00 +0000439 OC_FBLTIN|Sx|F_cl, OC_FBLTIN|Sx|F_sy, OC_FBLTIN|Sx|F_ff, OC_B|B_a2|P(0x83),
440 OC_FBLTIN|Nx|F_co, OC_FBLTIN|Nx|F_ex, OC_FBLTIN|Nx|F_in, OC_FBLTIN|Nx|F_lg,
441 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 +0200442 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 +0000443 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 +0200444 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 +0000445 OC_B|B_lo|P(0x49), OC_B|B_up|P(0x49),
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200446 OC_FBLTIN|Sx|F_le, /* TC_LENGTH */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000447 OC_GETLINE|SV|P(0),
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200448 0, 0,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000449 0,
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200450 0 /* TC_END */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000451};
452
453/* internal variable names and their initial values */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000454/* asterisk marks SPECIAL vars; $ is just no-named Field0 */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000455enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000456 CONVFMT, OFMT, FS, OFS,
Denis Vlasenkof782f522007-01-01 23:51:30 +0000457 ORS, RS, RT, FILENAME,
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000458 SUBSEP, F0, ARGIND, ARGC,
459 ARGV, ERRNO, FNR, NR,
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200460 NF, IGNORECASE, ENVIRON, NUM_INTERNAL_VARS
Glenn L McGrath545106f2002-11-11 06:21:00 +0000461};
462
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000463static const char vNames[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000464 "CONVFMT\0" "OFMT\0" "FS\0*" "OFS\0"
465 "ORS\0" "RS\0*" "RT\0" "FILENAME\0"
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000466 "SUBSEP\0" "$\0*" "ARGIND\0" "ARGC\0"
467 "ARGV\0" "ERRNO\0" "FNR\0" "NR\0"
468 "NF\0*" "IGNORECASE\0*" "ENVIRON\0" "\0";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000469
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000470static const char vValues[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000471 "%.6g\0" "%.6g\0" " \0" " \0"
472 "\n\0" "\n\0" "\0" "\0"
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000473 "\034\0" "\0" "\377";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000474
475/* hash size may grow to these values */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000476#define FIRST_PRIME 61
477static const uint16_t PRIMES[] ALIGN2 = { 251, 1021, 4093, 16381, 65521 };
Glenn L McGrath545106f2002-11-11 06:21:00 +0000478
Glenn L McGrath545106f2002-11-11 06:21:00 +0000479
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000480/* Globals. Split in two parts so that first one is addressed
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000481 * with (mostly short) negative offsets.
482 * NB: it's unsafe to put members of type "double"
483 * into globals2 (gcc may fail to align them).
484 */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000485struct globals {
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000486 double t_double;
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000487 chain beginseq, mainseq, endseq;
488 chain *seq;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000489 node *break_ptr, *continue_ptr;
490 rstream *iF;
491 xhash *vhash, *ahash, *fdhash, *fnhash;
492 const char *g_progname;
493 int g_lineno;
494 int nfields;
495 int maxfields; /* used in fsrealloc() only */
496 var *Fields;
497 nvblock *g_cb;
498 char *g_pos;
499 char *g_buf;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000500 smallint icase;
501 smallint exiting;
502 smallint nextrec;
503 smallint nextfile;
504 smallint is_f0_split;
Denys Vlasenko7b46d112011-09-11 00:30:56 +0200505 smallint t_rollback;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000506};
507struct globals2 {
508 uint32_t t_info; /* often used */
509 uint32_t t_tclass;
510 char *t_string;
511 int t_lineno;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000512
513 var *intvar[NUM_INTERNAL_VARS]; /* often used */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000514
515 /* former statics from various functions */
516 char *split_f0__fstrings;
517
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000518 uint32_t next_token__save_tclass;
519 uint32_t next_token__save_info;
520 uint32_t next_token__ltclass;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000521 smallint next_token__concat_inserted;
522
523 smallint next_input_file__files_happen;
524 rstream next_input_file__rsm;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000525
526 var *evaluate__fnargs;
527 unsigned evaluate__seed;
528 regex_t evaluate__sreg;
529
530 var ptest__v;
531
532 tsplitter exec_builtin__tspl;
533
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000534 /* biggest and least used members go last */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000535 tsplitter fsplitter, rsplitter;
Denys Vlasenko3dbc5a92010-02-05 14:54:22 +0100536};
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000537#define G1 (ptr_to_globals[-1])
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000538#define G (*(struct globals2 *)ptr_to_globals)
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000539/* For debug. nm --size-sort awk.o | grep -vi ' [tr] ' */
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000540/*char G1size[sizeof(G1)]; - 0x74 */
541/*char Gsize[sizeof(G)]; - 0x1c4 */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000542/* Trying to keep most of members accessible with short offsets: */
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000543/*char Gofs_seed[offsetof(struct globals2, evaluate__seed)]; - 0x90 */
544#define t_double (G1.t_double )
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000545#define beginseq (G1.beginseq )
546#define mainseq (G1.mainseq )
547#define endseq (G1.endseq )
548#define seq (G1.seq )
549#define break_ptr (G1.break_ptr )
550#define continue_ptr (G1.continue_ptr)
551#define iF (G1.iF )
552#define vhash (G1.vhash )
553#define ahash (G1.ahash )
554#define fdhash (G1.fdhash )
555#define fnhash (G1.fnhash )
556#define g_progname (G1.g_progname )
557#define g_lineno (G1.g_lineno )
558#define nfields (G1.nfields )
559#define maxfields (G1.maxfields )
560#define Fields (G1.Fields )
561#define g_cb (G1.g_cb )
562#define g_pos (G1.g_pos )
563#define g_buf (G1.g_buf )
564#define icase (G1.icase )
565#define exiting (G1.exiting )
566#define nextrec (G1.nextrec )
567#define nextfile (G1.nextfile )
568#define is_f0_split (G1.is_f0_split )
Denys Vlasenko7b46d112011-09-11 00:30:56 +0200569#define t_rollback (G1.t_rollback )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000570#define t_info (G.t_info )
571#define t_tclass (G.t_tclass )
572#define t_string (G.t_string )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000573#define t_lineno (G.t_lineno )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000574#define intvar (G.intvar )
575#define fsplitter (G.fsplitter )
576#define rsplitter (G.rsplitter )
577#define INIT_G() do { \
Denys Vlasenko90a99042009-09-06 02:36:23 +0200578 SET_PTR_TO_GLOBALS((char*)xzalloc(sizeof(G1)+sizeof(G)) + sizeof(G1)); \
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000579 G.next_token__ltclass = TC_OPTERM; \
580 G.evaluate__seed = 1; \
581} while (0)
582
Glenn L McGrath545106f2002-11-11 06:21:00 +0000583
584/* function prototypes */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000585static void handle_special(var *);
Mike Frysingerf87b3e32005-09-27 04:16:22 +0000586static node *parse_expr(uint32_t);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000587static void chain_group(void);
588static var *evaluate(node *, var *);
589static rstream *next_input_file(void);
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000590static int fmt_num(char *, int, const char *, double, int);
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000591static int awk_exit(int) NORETURN;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000592
593/* ---- error handling ---- */
594
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000595static const char EMSG_INTERNAL_ERROR[] ALIGN1 = "Internal error";
596static const char EMSG_UNEXP_EOS[] ALIGN1 = "Unexpected end of string";
597static const char EMSG_UNEXP_TOKEN[] ALIGN1 = "Unexpected token";
598static const char EMSG_DIV_BY_ZERO[] ALIGN1 = "Division by zero";
599static const char EMSG_INV_FMT[] ALIGN1 = "Invalid format specifier";
Brian Foley08a514c2019-01-01 13:40:59 -0800600static const char EMSG_TOO_FEW_ARGS[] ALIGN1 = "Too few arguments";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000601static const char EMSG_NOT_ARRAY[] ALIGN1 = "Not an array";
602static const char EMSG_POSSIBLE_ERROR[] ALIGN1 = "Possible syntax error";
603static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000604static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in";
Denys Vlasenko2454e672018-04-23 10:53:18 +0200605static const char EMSG_NEGATIVE_FIELD[] ALIGN1 = "Access to negative field";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000606
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100607static void zero_out_var(var *vp)
Denis Vlasenkof782f522007-01-01 23:51:30 +0000608{
609 memset(vp, 0, sizeof(*vp));
610}
611
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000612static void syntax_error(const char *message) NORETURN;
613static void syntax_error(const char *message)
Glenn L McGrathd4036f82002-11-28 09:30:40 +0000614{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000615 bb_error_msg_and_die("%s:%i: %s", g_progname, g_lineno, message);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000616}
617
Glenn L McGrath545106f2002-11-11 06:21:00 +0000618/* ---- hash stuff ---- */
619
Denis Vlasenkof782f522007-01-01 23:51:30 +0000620static unsigned hashidx(const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000621{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000622 unsigned idx = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000623
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100624 while (*name)
625 idx = *name++ + (idx << 6) - idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000626 return idx;
627}
628
629/* create new hash */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000630static xhash *hash_init(void)
631{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000632 xhash *newhash;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000633
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100634 newhash = xzalloc(sizeof(*newhash));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000635 newhash->csize = FIRST_PRIME;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100636 newhash->items = xzalloc(FIRST_PRIME * sizeof(newhash->items[0]));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000637
638 return newhash;
639}
640
641/* find item in hash, return ptr to data, NULL if not found */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000642static void *hash_search(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000643{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000644 hash_item *hi;
645
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100646 hi = hash->items[hashidx(name) % hash->csize];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000647 while (hi) {
648 if (strcmp(hi->name, name) == 0)
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100649 return &hi->data;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000650 hi = hi->next;
651 }
652 return NULL;
653}
654
655/* grow hash if it becomes too big */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000656static void hash_rebuild(xhash *hash)
657{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000658 unsigned newsize, i, idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000659 hash_item **newitems, *hi, *thi;
660
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000661 if (hash->nprime == ARRAY_SIZE(PRIMES))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000662 return;
663
664 newsize = PRIMES[hash->nprime++];
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100665 newitems = xzalloc(newsize * sizeof(newitems[0]));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000666
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000667 for (i = 0; i < hash->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000668 hi = hash->items[i];
669 while (hi) {
670 thi = hi;
671 hi = thi->next;
672 idx = hashidx(thi->name) % newsize;
673 thi->next = newitems[idx];
674 newitems[idx] = thi;
675 }
676 }
677
678 free(hash->items);
679 hash->csize = newsize;
680 hash->items = newitems;
681}
682
683/* find item in hash, add it if necessary. Return ptr to data */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000684static void *hash_find(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000685{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000686 hash_item *hi;
Denis Vlasenkof782f522007-01-01 23:51:30 +0000687 unsigned idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000688 int l;
689
690 hi = hash_search(hash, name);
Denis Vlasenkob78c7822007-07-18 18:31:11 +0000691 if (!hi) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000692 if (++hash->nel / hash->csize > 10)
693 hash_rebuild(hash);
694
Rob Landleya3896512006-05-07 20:20:34 +0000695 l = strlen(name) + 1;
Denis Vlasenko7a676642009-03-15 22:20:31 +0000696 hi = xzalloc(sizeof(*hi) + l);
697 strcpy(hi->name, name);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000698
699 idx = hashidx(name) % hash->csize;
700 hi->next = hash->items[idx];
701 hash->items[idx] = hi;
702 hash->glen += l;
703 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100704 return &hi->data;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000705}
706
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000707#define findvar(hash, name) ((var*) hash_find((hash), (name)))
708#define newvar(name) ((var*) hash_find(vhash, (name)))
709#define newfile(name) ((rstream*)hash_find(fdhash, (name)))
710#define newfunc(name) ((func*) hash_find(fnhash, (name)))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000711
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000712static void hash_remove(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000713{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000714 hash_item *hi, **phi;
715
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100716 phi = &hash->items[hashidx(name) % hash->csize];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000717 while (*phi) {
718 hi = *phi;
719 if (strcmp(hi->name, name) == 0) {
Rob Landleya3896512006-05-07 20:20:34 +0000720 hash->glen -= (strlen(name) + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000721 hash->nel--;
722 *phi = hi->next;
723 free(hi);
724 break;
725 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100726 phi = &hi->next;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000727 }
728}
729
730/* ------ some useful functions ------ */
731
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100732static char *skip_spaces(char *p)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000733{
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000734 while (1) {
735 if (*p == '\\' && p[1] == '\n') {
736 p++;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000737 t_lineno++;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000738 } else if (*p != ' ' && *p != '\t') {
739 break;
740 }
Mike Frysingerde2b9382005-09-27 03:18:00 +0000741 p++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000742 }
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100743 return p;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000744}
745
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100746/* returns old *s, advances *s past word and terminating NUL */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000747static char *nextword(char **s)
748{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000749 char *p = *s;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100750 while (*(*s)++ != '\0')
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100751 continue;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000752 return p;
753}
754
Mike Frysinger10a11e22005-09-27 02:23:02 +0000755static char nextchar(char **s)
756{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000757 char c, *pps;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000758
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100759 c = *(*s)++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000760 pps = *s;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100761 if (c == '\\')
762 c = bb_process_escape_sequence((const char**)s);
Denys Vlasenkoea664dd2012-06-22 18:41:01 +0200763 /* Example awk statement:
764 * s = "abc\"def"
765 * we must treat \" as "
766 */
Denys Vlasenko2b299fe2010-10-24 01:58:04 +0200767 if (c == '\\' && *s == pps) { /* unrecognized \z? */
768 c = *(*s); /* yes, fetch z */
769 if (c)
770 (*s)++; /* advance unless z = NUL */
771 }
Glenn L McGrath545106f2002-11-11 06:21:00 +0000772 return c;
773}
774
Denys Vlasenkoea664dd2012-06-22 18:41:01 +0200775/* TODO: merge with strcpy_and_process_escape_sequences()?
776 */
777static void unescape_string_in_place(char *s1)
778{
779 char *s = s1;
780 while ((*s1 = nextchar(&s)) != '\0')
781 s1++;
782}
783
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000784static ALWAYS_INLINE int isalnum_(int c)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000785{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000786 return (isalnum(c) || c == '_');
787}
788
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000789static double my_strtod(char **pp)
790{
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200791 char *cp = *pp;
Rob Landleyd8205b32010-10-24 03:27:22 +0200792 if (ENABLE_DESKTOP && cp[0] == '0') {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200793 /* Might be hex or octal integer: 0x123abc or 07777 */
794 char c = (cp[1] | 0x20);
795 if (c == 'x' || isdigit(cp[1])) {
796 unsigned long long ull = strtoull(cp, pp, 0);
797 if (c == 'x')
798 return ull;
799 c = **pp;
800 if (!isdigit(c) && c != '.')
801 return ull;
802 /* else: it may be a floating number. Examples:
803 * 009.123 (*pp points to '9')
804 * 000.123 (*pp points to '.')
805 * fall through to strtod.
806 */
807 }
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000808 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200809 return strtod(cp, pp);
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000810}
811
Glenn L McGrath545106f2002-11-11 06:21:00 +0000812/* -------- working with variables (set/get/copy/etc) -------- */
813
Mike Frysinger10a11e22005-09-27 02:23:02 +0000814static xhash *iamarray(var *v)
815{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000816 var *a = v;
817
818 while (a->type & VF_CHILD)
819 a = a->x.parent;
820
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000821 if (!(a->type & VF_ARRAY)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000822 a->type |= VF_ARRAY;
823 a->x.array = hash_init();
824 }
825 return a->x.array;
826}
827
Mike Frysinger10a11e22005-09-27 02:23:02 +0000828static void clear_array(xhash *array)
829{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000830 unsigned i;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000831 hash_item *hi, *thi;
832
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000833 for (i = 0; i < array->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000834 hi = array->items[i];
835 while (hi) {
836 thi = hi;
837 hi = hi->next;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000838 free(thi->data.v.string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000839 free(thi);
840 }
841 array->items[i] = NULL;
842 }
843 array->glen = array->nel = 0;
844}
845
846/* clear a variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000847static var *clrvar(var *v)
848{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000849 if (!(v->type & VF_FSTR))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000850 free(v->string);
851
852 v->type &= VF_DONTTOUCH;
853 v->type |= VF_DIRTY;
854 v->string = NULL;
855 return v;
856}
857
858/* assign string value to variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000859static var *setvar_p(var *v, char *value)
860{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000861 clrvar(v);
862 v->string = value;
863 handle_special(v);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000864 return v;
865}
866
867/* same as setvar_p but make a copy of string */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000868static var *setvar_s(var *v, const char *value)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000869{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000870 return setvar_p(v, (value && *value) ? xstrdup(value) : NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000871}
872
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100873/* same as setvar_s but sets USER flag */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000874static var *setvar_u(var *v, const char *value)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000875{
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100876 v = setvar_s(v, value);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000877 v->type |= VF_USER;
878 return v;
879}
880
881/* set array element to user string */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000882static void setari_u(var *a, int idx, const char *s)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000883{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000884 var *v;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000885
Denys Vlasenko7bb346f2009-10-06 22:09:50 +0200886 v = findvar(iamarray(a), itoa(idx));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000887 setvar_u(v, s);
888}
889
890/* assign numeric value to variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000891static var *setvar_i(var *v, double value)
892{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000893 clrvar(v);
894 v->type |= VF_NUMBER;
895 v->number = value;
896 handle_special(v);
897 return v;
898}
899
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000900static const char *getvar_s(var *v)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000901{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000902 /* if v is numeric and has no cached string, convert it to string */
903 if ((v->type & (VF_NUMBER | VF_CACHED)) == VF_NUMBER) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000904 fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[CONVFMT]), v->number, TRUE);
905 v->string = xstrdup(g_buf);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000906 v->type |= VF_CACHED;
907 }
908 return (v->string == NULL) ? "" : v->string;
909}
910
Mike Frysinger10a11e22005-09-27 02:23:02 +0000911static double getvar_i(var *v)
912{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000913 char *s;
914
915 if ((v->type & (VF_NUMBER | VF_CACHED)) == 0) {
916 v->number = 0;
917 s = v->string;
918 if (s && *s) {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200919 debug_printf_eval("getvar_i: '%s'->", s);
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000920 v->number = my_strtod(&s);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200921 debug_printf_eval("%f (s:'%s')\n", v->number, s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000922 if (v->type & VF_USER) {
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100923 s = skip_spaces(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000924 if (*s != '\0')
925 v->type &= ~VF_USER;
926 }
927 } else {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200928 debug_printf_eval("getvar_i: '%s'->zero\n", s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000929 v->type &= ~VF_USER;
930 }
931 v->type |= VF_CACHED;
932 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200933 debug_printf_eval("getvar_i: %f\n", v->number);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000934 return v->number;
935}
936
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000937/* Used for operands of bitwise ops */
938static unsigned long getvar_i_int(var *v)
939{
940 double d = getvar_i(v);
941
942 /* Casting doubles to longs is undefined for values outside
943 * of target type range. Try to widen it as much as possible */
944 if (d >= 0)
945 return (unsigned long)d;
Denis Vlasenko665eaff2008-09-05 04:59:02 +0000946 /* Why? Think about d == -4294967295.0 (assuming 32bit longs) */
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000947 return - (long) (unsigned long) (-d);
948}
949
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000950static var *copyvar(var *dest, const var *src)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000951{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000952 if (dest != src) {
953 clrvar(dest);
Denis Vlasenko629563b2007-02-24 17:05:52 +0000954 dest->type |= (src->type & ~(VF_DONTTOUCH | VF_FSTR));
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200955 debug_printf_eval("copyvar: number:%f string:'%s'\n", src->number, src->string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000956 dest->number = src->number;
957 if (src->string)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000958 dest->string = xstrdup(src->string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000959 }
960 handle_special(dest);
961 return dest;
962}
963
Mike Frysinger10a11e22005-09-27 02:23:02 +0000964static var *incvar(var *v)
965{
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100966 return setvar_i(v, getvar_i(v) + 1.0);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000967}
968
969/* return true if v is number or numeric string */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000970static int is_numeric(var *v)
971{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000972 getvar_i(v);
973 return ((v->type ^ VF_DIRTY) & (VF_NUMBER | VF_USER | VF_DIRTY));
974}
975
976/* return 1 when value of v corresponds to true, 0 otherwise */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000977static int istrue(var *v)
978{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000979 if (is_numeric(v))
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100980 return (v->number != 0);
981 return (v->string && v->string[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000982}
983
Eric Andersenaff114c2004-04-14 17:51:38 +0000984/* temporary variables allocator. Last allocated should be first freed */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000985static var *nvalloc(int n)
986{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000987 nvblock *pb = NULL;
988 var *v, *r;
989 int size;
990
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000991 while (g_cb) {
992 pb = g_cb;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100993 if ((g_cb->pos - g_cb->nv) + n <= g_cb->size)
994 break;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000995 g_cb = g_cb->next;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000996 }
997
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000998 if (!g_cb) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000999 size = (n <= MINNVBLOCK) ? MINNVBLOCK : n;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +00001000 g_cb = xzalloc(sizeof(nvblock) + size * sizeof(var));
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001001 g_cb->size = size;
1002 g_cb->pos = g_cb->nv;
1003 g_cb->prev = pb;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +00001004 /*g_cb->next = NULL; - xzalloc did it */
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001005 if (pb)
1006 pb->next = g_cb;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001007 }
1008
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001009 v = r = g_cb->pos;
1010 g_cb->pos += n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001011
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001012 while (v < g_cb->pos) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001013 v->type = 0;
1014 v->string = NULL;
1015 v++;
1016 }
1017
1018 return r;
1019}
1020
Mike Frysinger10a11e22005-09-27 02:23:02 +00001021static void nvfree(var *v)
1022{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001023 var *p;
1024
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001025 if (v < g_cb->nv || v >= g_cb->pos)
1026 syntax_error(EMSG_INTERNAL_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001027
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001028 for (p = v; p < g_cb->pos; p++) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001029 if ((p->type & (VF_ARRAY | VF_CHILD)) == VF_ARRAY) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001030 clear_array(iamarray(p));
1031 free(p->x.array->items);
1032 free(p->x.array);
1033 }
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001034 if (p->type & VF_WALK) {
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001035 walker_list *n;
1036 walker_list *w = p->x.walker;
1037 debug_printf_walker("nvfree: freeing walker @%p\n", &p->x.walker);
1038 p->x.walker = NULL;
1039 while (w) {
1040 n = w->prev;
1041 debug_printf_walker(" free(%p)\n", w);
1042 free(w);
1043 w = n;
1044 }
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001045 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001046 clrvar(p);
1047 }
1048
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001049 g_cb->pos = v;
1050 while (g_cb->prev && g_cb->pos == g_cb->nv) {
1051 g_cb = g_cb->prev;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001052 }
1053}
1054
1055/* ------- awk program text parsing ------- */
1056
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001057/* Parse next token pointed by global pos, place results into global ttt.
Glenn L McGrath545106f2002-11-11 06:21:00 +00001058 * If token isn't expected, give away. Return token class
1059 */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001060static uint32_t next_token(uint32_t expected)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001061{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001062#define concat_inserted (G.next_token__concat_inserted)
1063#define save_tclass (G.next_token__save_tclass)
1064#define save_info (G.next_token__save_info)
1065/* Initialized to TC_OPTERM: */
1066#define ltclass (G.next_token__ltclass)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001067
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001068 char *p, *s;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001069 const char *tl;
1070 uint32_t tc;
1071 const uint32_t *ti;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001072
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001073 if (t_rollback) {
Denys Vlasenkobd8b05b2020-02-02 23:28:55 +01001074 debug_printf_parse("%s: using rolled-back token\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001075 t_rollback = FALSE;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001076 } else if (concat_inserted) {
Denys Vlasenkobd8b05b2020-02-02 23:28:55 +01001077 debug_printf_parse("%s: using concat-inserted token\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001078 concat_inserted = FALSE;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001079 t_tclass = save_tclass;
1080 t_info = save_info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001081 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001082 p = g_pos;
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001083 readnext:
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +01001084 p = skip_spaces(p);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001085 g_lineno = t_lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001086 if (*p == '#')
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001087 while (*p != '\n' && *p != '\0')
1088 p++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001089
1090 if (*p == '\n')
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001091 t_lineno++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001092
1093 if (*p == '\0') {
1094 tc = TC_EOF;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001095 debug_printf_parse("%s: token found: TC_EOF\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001096 } else if (*p == '\"') {
1097 /* it's a string */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001098 t_string = s = ++p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001099 while (*p != '\"') {
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02001100 char *pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001101 if (*p == '\0' || *p == '\n')
1102 syntax_error(EMSG_UNEXP_EOS);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02001103 pp = p;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001104 *s++ = nextchar(&pp);
1105 p = pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001106 }
1107 p++;
1108 *s = '\0';
1109 tc = TC_STRING;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001110 debug_printf_parse("%s: token found:'%s' TC_STRING\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001111 } else if ((expected & TC_REGEXP) && *p == '/') {
1112 /* it's regexp */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001113 t_string = s = ++p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001114 while (*p != '/') {
1115 if (*p == '\0' || *p == '\n')
1116 syntax_error(EMSG_UNEXP_EOS);
Denis Vlasenkod9b5ab82007-05-18 07:30:43 +00001117 *s = *p++;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001118 if (*s++ == '\\') {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001119 char *pp = p;
1120 s[-1] = bb_process_escape_sequence((const char **)&pp);
1121 if (*p == '\\')
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001122 *s++ = '\\';
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001123 if (pp == p)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001124 *s++ = *p++;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001125 else
1126 p = pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001127 }
1128 }
1129 p++;
1130 *s = '\0';
1131 tc = TC_REGEXP;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001132 debug_printf_parse("%s: token found:'%s' TC_REGEXP\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001133
1134 } else if (*p == '.' || isdigit(*p)) {
1135 /* it's a number */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001136 char *pp = p;
1137 t_double = my_strtod(&pp);
1138 p = pp;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001139 if (*p == '.')
Glenn L McGrath545106f2002-11-11 06:21:00 +00001140 syntax_error(EMSG_UNEXP_TOKEN);
1141 tc = TC_NUMBER;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001142 debug_printf_parse("%s: token found:%f TC_NUMBER\n", __func__, t_double);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001143 } else {
1144 /* search for something known */
1145 tl = tokenlist;
1146 tc = 0x00000001;
1147 ti = tokeninfo;
1148 while (*tl) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001149 int l = (unsigned char) *tl++;
1150 if (l == (unsigned char) NTCC) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001151 tc <<= 1;
1152 continue;
1153 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001154 /* if token class is expected,
1155 * token matches,
1156 * and it's not a longer word,
Glenn L McGrath545106f2002-11-11 06:21:00 +00001157 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001158 if ((tc & (expected | TC_WORD | TC_NEWLINE))
Denys Vlasenko28458c62010-10-05 16:49:03 +02001159 && strncmp(p, tl, l) == 0
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001160 && !((tc & TC_WORD) && isalnum_(p[l]))
1161 ) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001162 /* then this is what we are looking for */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001163 t_info = *ti;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001164 debug_printf_parse("%s: token found:'%.*s' t_info:%x\n", __func__, l, p, t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001165 p += l;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001166 goto token_found;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001167 }
1168 ti++;
1169 tl += l;
1170 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001171 /* not a known token */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001172
Denys Vlasenko28458c62010-10-05 16:49:03 +02001173 /* is it a name? (var/array/function) */
1174 if (!isalnum_(*p))
1175 syntax_error(EMSG_UNEXP_TOKEN); /* no */
1176 /* yes */
1177 t_string = --p;
1178 while (isalnum_(*++p)) {
1179 p[-1] = *p;
1180 }
1181 p[-1] = '\0';
1182 tc = TC_VARIABLE;
1183 /* also consume whitespace between functionname and bracket */
1184 if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY))
1185 p = skip_spaces(p);
1186 if (*p == '(') {
1187 tc = TC_FUNCTION;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001188 debug_printf_parse("%s: token found:'%s' TC_FUNCTION\n", __func__, t_string);
Denys Vlasenko28458c62010-10-05 16:49:03 +02001189 } else {
1190 if (*p == '[') {
1191 p++;
1192 tc = TC_ARRAY;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001193 debug_printf_parse("%s: token found:'%s' TC_ARRAY\n", __func__, t_string);
1194 } else
1195 debug_printf_parse("%s: token found:'%s' TC_VARIABLE\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001196 }
1197 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001198 token_found:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001199 g_pos = p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001200
1201 /* skipping newlines in some cases */
1202 if ((ltclass & TC_NOTERM) && (tc & TC_NEWLINE))
1203 goto readnext;
1204
1205 /* insert concatenation operator when needed */
Denys Vlasenkobd8b05b2020-02-02 23:28:55 +01001206 debug_printf_parse("%s: %x %x %x concat_inserted?\n", __func__,
1207 (ltclass & TC_CONCAT1), (tc & TC_CONCAT2), (expected & TC_BINOP));
1208 if ((ltclass & TC_CONCAT1) && (tc & TC_CONCAT2) && (expected & TC_BINOP)
1209 && !(ltclass == TC_LENGTH && tc == TC_SEQSTART) /* but not for "length(..." */
1210 ) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001211 concat_inserted = TRUE;
1212 save_tclass = tc;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001213 save_info = t_info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001214 tc = TC_BINOP;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001215 t_info = OC_CONCAT | SS | P(35);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001216 }
1217
Denys Vlasenkobd8b05b2020-02-02 23:28:55 +01001218 debug_printf_parse("%s: t_tclass=tc=%x\n", __func__, t_tclass);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001219 t_tclass = tc;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001220 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001221 ltclass = t_tclass;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001222
1223 /* Are we ready for this? */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001224 if (!(ltclass & expected)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001225 syntax_error((ltclass & (TC_NEWLINE | TC_EOF)) ?
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001226 EMSG_UNEXP_EOS : EMSG_UNEXP_TOKEN);
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001227 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001228
Denys Vlasenkobd8b05b2020-02-02 23:28:55 +01001229 debug_printf_parse("%s: returning, ltclass:%x t_double:%f\n", __func__, ltclass, t_double);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001230 return ltclass;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001231#undef concat_inserted
1232#undef save_tclass
1233#undef save_info
1234#undef ltclass
Glenn L McGrath545106f2002-11-11 06:21:00 +00001235}
1236
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001237static void rollback_token(void)
1238{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001239 t_rollback = TRUE;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001240}
Glenn L McGrath545106f2002-11-11 06:21:00 +00001241
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001242static node *new_node(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001243{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001244 node *n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001245
Denis Vlasenko4cccc032006-12-22 18:37:07 +00001246 n = xzalloc(sizeof(node));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001247 n->info = info;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001248 n->lineno = g_lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001249 return n;
1250}
1251
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001252static void mk_re_node(const char *s, node *n, regex_t *re)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001253{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001254 n->info = OC_REGEXP;
1255 n->l.re = re;
1256 n->r.ire = re + 1;
1257 xregcomp(re, s, REG_EXTENDED);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001258 xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001259}
1260
Mike Frysinger10a11e22005-09-27 02:23:02 +00001261static node *condition(void)
1262{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001263 next_token(TC_SEQSTART);
1264 return parse_expr(TC_SEQTERM);
1265}
1266
1267/* parse expression terminated by given argument, return ptr
1268 * to built subtree. Terminator is eaten by parse_expr */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001269static node *parse_expr(uint32_t iexp)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001270{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001271 node sn;
1272 node *cn = &sn;
1273 node *vn, *glptr;
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001274 uint32_t tc, xtc;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001275 var *v;
1276
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001277 debug_printf_parse("%s(%x)\n", __func__, iexp);
1278
Glenn L McGrath545106f2002-11-11 06:21:00 +00001279 sn.info = PRIMASK;
Brian Foleydac15a12019-01-01 13:40:58 -08001280 sn.r.n = sn.a.n = glptr = NULL;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001281 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp;
1282
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001283 while (!((tc = next_token(xtc)) & iexp)) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001284
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001285 if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001286 /* input redirection (<) attached to glptr node */
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001287 debug_printf_parse("%s: input redir\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001288 cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37));
Glenn L McGrath4bded582004-02-22 11:55:09 +00001289 cn->a.n = glptr;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001290 xtc = TC_OPERAND | TC_UOPPRE;
1291 glptr = NULL;
1292
1293 } else if (tc & (TC_BINOP | TC_UOPPOST)) {
Denys Vlasenkobd8b05b2020-02-02 23:28:55 +01001294 debug_printf_parse("%s: TC_BINOP | TC_UOPPOST tc:%x\n", __func__, tc);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001295 /* for binary and postfix-unary operators, jump back over
1296 * previous operators with higher priority */
1297 vn = cn;
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001298 while (((t_info & PRIMASK) > (vn->a.n->info & PRIMASK2))
1299 || ((t_info == vn->info) && ((t_info & OPCLSMASK) == OC_COLON))
1300 ) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001301 vn = vn->a.n;
Brian Foleydac15a12019-01-01 13:40:58 -08001302 if (!vn->a.n) syntax_error(EMSG_UNEXP_TOKEN);
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001303 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001304 if ((t_info & OPCLSMASK) == OC_TERNARY)
1305 t_info += P(6);
1306 cn = vn->a.n->r.n = new_node(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001307 cn->a.n = vn->a.n;
1308 if (tc & TC_BINOP) {
1309 cn->l.n = vn;
1310 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001311 if ((t_info & OPCLSMASK) == OC_PGETLINE) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001312 /* it's a pipe */
1313 next_token(TC_GETLINE);
1314 /* give maximum priority to this pipe */
1315 cn->info &= ~PRIMASK;
1316 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1317 }
1318 } else {
1319 cn->r.n = vn;
1320 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1321 }
1322 vn->a.n = cn;
1323
1324 } else {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001325 debug_printf_parse("%s: other\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001326 /* for operands and prefix-unary operators, attach them
1327 * to last node */
1328 vn = cn;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001329 cn = vn->r.n = new_node(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001330 cn->a.n = vn;
1331 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
1332 if (tc & (TC_OPERAND | TC_REGEXP)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001333 debug_printf_parse("%s: TC_OPERAND | TC_REGEXP\n", __func__);
Rob Landleyed830e82005-06-07 02:43:52 +00001334 xtc = TC_UOPPRE | TC_UOPPOST | TC_BINOP | TC_OPERAND | iexp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001335 /* one should be very careful with switch on tclass -
Glenn L McGrath545106f2002-11-11 06:21:00 +00001336 * only simple tclasses should be used! */
1337 switch (tc) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00001338 case TC_VARIABLE:
1339 case TC_ARRAY:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001340 debug_printf_parse("%s: TC_VARIABLE | TC_ARRAY\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001341 cn->info = OC_VAR;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001342 v = hash_search(ahash, t_string);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001343 if (v != NULL) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001344 cn->info = OC_FNARG;
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001345 cn->l.aidx = v->x.aidx;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001346 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001347 cn->l.v = newvar(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001348 }
1349 if (tc & TC_ARRAY) {
1350 cn->info |= xS;
1351 cn->r.n = parse_expr(TC_ARRTERM);
1352 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001353 break;
Mike Frysingerde2b9382005-09-27 03:18:00 +00001354
Denis Vlasenkof782f522007-01-01 23:51:30 +00001355 case TC_NUMBER:
1356 case TC_STRING:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001357 debug_printf_parse("%s: TC_NUMBER | TC_STRING\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001358 cn->info = OC_VAR;
Rob Landley9ffd4232006-05-21 18:30:35 +00001359 v = cn->l.v = xzalloc(sizeof(var));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001360 if (tc & TC_NUMBER)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001361 setvar_i(v, t_double);
Denys Vlasenko6f7a0092020-06-09 01:33:54 +02001362 else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001363 setvar_s(v, t_string);
Denys Vlasenko6f7a0092020-06-09 01:33:54 +02001364 xtc &= ~TC_UOPPOST; /* "str"++ is not allowed */
1365 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001366 break;
1367
Denis Vlasenkof782f522007-01-01 23:51:30 +00001368 case TC_REGEXP:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001369 debug_printf_parse("%s: TC_REGEXP\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001370 mk_re_node(t_string, cn, xzalloc(sizeof(regex_t)*2));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001371 break;
1372
Denis Vlasenkof782f522007-01-01 23:51:30 +00001373 case TC_FUNCTION:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001374 debug_printf_parse("%s: TC_FUNCTION\n", __func__);
Mike Frysingerde2b9382005-09-27 03:18:00 +00001375 cn->info = OC_FUNC;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001376 cn->r.f = newfunc(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001377 cn->l.n = condition();
1378 break;
1379
Denis Vlasenkof782f522007-01-01 23:51:30 +00001380 case TC_SEQSTART:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001381 debug_printf_parse("%s: TC_SEQSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001382 cn = vn->r.n = parse_expr(TC_SEQTERM);
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001383 if (!cn)
1384 syntax_error("Empty sequence");
Glenn L McGrath545106f2002-11-11 06:21:00 +00001385 cn->a.n = vn;
1386 break;
1387
Denis Vlasenkof782f522007-01-01 23:51:30 +00001388 case TC_GETLINE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001389 debug_printf_parse("%s: TC_GETLINE\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001390 glptr = cn;
1391 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1392 break;
1393
Denis Vlasenkof782f522007-01-01 23:51:30 +00001394 case TC_BUILTIN:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001395 debug_printf_parse("%s: TC_BUILTIN\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001396 cn->l.n = condition();
1397 break;
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001398
1399 case TC_LENGTH:
1400 debug_printf_parse("%s: TC_LENGTH\n", __func__);
Denys Vlasenkobd8b05b2020-02-02 23:28:55 +01001401 next_token(TC_SEQSTART /* length(...) */
1402 | TC_OPTERM /* length; (or newline)*/
1403 | TC_GRPTERM /* length } */
1404 | TC_BINOPX /* length <op> NUM */
1405 | TC_COMMA /* print length, 1 */
1406 );
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001407 rollback_token();
1408 if (t_tclass & TC_SEQSTART) {
1409 /* It was a "(" token. Handle just like TC_BUILTIN */
1410 cn->l.n = condition();
1411 }
1412 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001413 }
1414 }
1415 }
1416 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001417
1418 debug_printf_parse("%s() returns %p\n", __func__, sn.r.n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001419 return sn.r.n;
1420}
1421
1422/* add node to chain. Return ptr to alloc'd node */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001423static node *chain_node(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001424{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001425 node *n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001426
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001427 if (!seq->first)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001428 seq->first = seq->last = new_node(0);
1429
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001430 if (seq->programname != g_progname) {
1431 seq->programname = g_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001432 n = chain_node(OC_NEWSOURCE);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001433 n->l.new_progname = xstrdup(g_progname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001434 }
1435
1436 n = seq->last;
1437 n->info = info;
1438 seq->last = n->a.n = new_node(OC_DONE);
1439
1440 return n;
1441}
1442
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001443static void chain_expr(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001444{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001445 node *n;
1446
1447 n = chain_node(info);
Brian Foley08a514c2019-01-01 13:40:59 -08001448
Glenn L McGrath545106f2002-11-11 06:21:00 +00001449 n->l.n = parse_expr(TC_OPTERM | TC_GRPTERM);
Brian Foley08a514c2019-01-01 13:40:59 -08001450 if ((info & OF_REQUIRED) && !n->l.n)
1451 syntax_error(EMSG_TOO_FEW_ARGS);
1452
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001453 if (t_tclass & TC_GRPTERM)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001454 rollback_token();
1455}
1456
Mike Frysinger10a11e22005-09-27 02:23:02 +00001457static node *chain_loop(node *nn)
1458{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001459 node *n, *n2, *save_brk, *save_cont;
1460
1461 save_brk = break_ptr;
1462 save_cont = continue_ptr;
1463
1464 n = chain_node(OC_BR | Vx);
1465 continue_ptr = new_node(OC_EXEC);
1466 break_ptr = new_node(OC_EXEC);
1467 chain_group();
1468 n2 = chain_node(OC_EXEC | Vx);
1469 n2->l.n = nn;
1470 n2->a.n = n;
1471 continue_ptr->a.n = n2;
1472 break_ptr->a.n = n->r.n = seq->last;
1473
1474 continue_ptr = save_cont;
1475 break_ptr = save_brk;
1476
1477 return n;
1478}
1479
1480/* parse group and attach it to chain */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001481static void chain_group(void)
1482{
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001483 uint32_t c;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001484 node *n, *n2, *n3;
1485
1486 do {
1487 c = next_token(TC_GRPSEQ);
1488 } while (c & TC_NEWLINE);
1489
1490 if (c & TC_GRPSTART) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001491 debug_printf_parse("%s: TC_GRPSTART\n", __func__);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001492 while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001493 debug_printf_parse("%s: !TC_GRPTERM\n", __func__);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001494 if (t_tclass & TC_NEWLINE)
1495 continue;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001496 rollback_token();
1497 chain_group();
1498 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001499 debug_printf_parse("%s: TC_GRPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001500 } else if (c & (TC_OPSEQ | TC_OPTERM)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001501 debug_printf_parse("%s: TC_OPSEQ | TC_OPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001502 rollback_token();
1503 chain_expr(OC_EXEC | Vx);
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001504 } else {
1505 /* TC_STATEMNT */
1506 debug_printf_parse("%s: TC_STATEMNT(?)\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001507 switch (t_info & OPCLSMASK) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001508 case ST_IF:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001509 debug_printf_parse("%s: ST_IF\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001510 n = chain_node(OC_BR | Vx);
1511 n->l.n = condition();
1512 chain_group();
1513 n2 = chain_node(OC_EXEC);
1514 n->r.n = seq->last;
1515 if (next_token(TC_GRPSEQ | TC_GRPTERM | TC_ELSE) == TC_ELSE) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001516 chain_group();
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001517 n2->a.n = seq->last;
1518 } else {
1519 rollback_token();
1520 }
1521 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001522
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001523 case ST_WHILE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001524 debug_printf_parse("%s: ST_WHILE\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001525 n2 = condition();
1526 n = chain_loop(NULL);
1527 n->l.n = n2;
1528 break;
1529
1530 case ST_DO:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001531 debug_printf_parse("%s: ST_DO\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001532 n2 = chain_node(OC_EXEC);
1533 n = chain_loop(NULL);
1534 n2->a.n = n->a.n;
1535 next_token(TC_WHILE);
1536 n->l.n = condition();
1537 break;
1538
1539 case ST_FOR:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001540 debug_printf_parse("%s: ST_FOR\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001541 next_token(TC_SEQSTART);
1542 n2 = parse_expr(TC_SEMICOL | TC_SEQTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001543 if (t_tclass & TC_SEQTERM) { /* for-in */
Brian Foley61d59972016-10-15 14:45:40 +01001544 if (!n2 || (n2->info & OPCLSMASK) != OC_IN)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001545 syntax_error(EMSG_UNEXP_TOKEN);
1546 n = chain_node(OC_WALKINIT | VV);
1547 n->l.n = n2->l.n;
1548 n->r.n = n2->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001549 n = chain_loop(NULL);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001550 n->info = OC_WALKNEXT | Vx;
1551 n->l.n = n2->l.n;
1552 } else { /* for (;;) */
1553 n = chain_node(OC_EXEC | Vx);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001554 n->l.n = n2;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001555 n2 = parse_expr(TC_SEMICOL);
1556 n3 = parse_expr(TC_SEQTERM);
1557 n = chain_loop(n3);
1558 n->l.n = n2;
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001559 if (!n2)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001560 n->info = OC_EXEC;
1561 }
1562 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001563
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001564 case OC_PRINT:
1565 case OC_PRINTF:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001566 debug_printf_parse("%s: OC_PRINT[F]\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001567 n = chain_node(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001568 n->l.n = parse_expr(TC_OPTERM | TC_OUTRDR | TC_GRPTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001569 if (t_tclass & TC_OUTRDR) {
1570 n->info |= t_info;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001571 n->r.n = parse_expr(TC_OPTERM | TC_GRPTERM);
1572 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001573 if (t_tclass & TC_GRPTERM)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001574 rollback_token();
1575 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001576
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001577 case OC_BREAK:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001578 debug_printf_parse("%s: OC_BREAK\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001579 n = chain_node(OC_EXEC);
1580 n->a.n = break_ptr;
Denys Vlasenko5f8daef2014-06-26 16:40:28 +02001581 chain_expr(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001582 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001583
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001584 case OC_CONTINUE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001585 debug_printf_parse("%s: OC_CONTINUE\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001586 n = chain_node(OC_EXEC);
1587 n->a.n = continue_ptr;
Denys Vlasenko5f8daef2014-06-26 16:40:28 +02001588 chain_expr(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001589 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001590
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001591 /* delete, next, nextfile, return, exit */
1592 default:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001593 debug_printf_parse("%s: default\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001594 chain_expr(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001595 }
1596 }
1597}
1598
Mike Frysinger10a11e22005-09-27 02:23:02 +00001599static void parse_program(char *p)
1600{
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001601 uint32_t tclass;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001602 node *cn;
1603 func *f;
1604 var *v;
1605
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001606 g_pos = p;
1607 t_lineno = 1;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001608 while ((tclass = next_token(TC_EOF | TC_OPSEQ | TC_GRPSTART |
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001609 TC_OPTERM | TC_BEGIN | TC_END | TC_FUNCDECL)) != TC_EOF) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001610
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001611 if (tclass & TC_OPTERM) {
1612 debug_printf_parse("%s: TC_OPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001613 continue;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001614 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001615
1616 seq = &mainseq;
1617 if (tclass & TC_BEGIN) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001618 debug_printf_parse("%s: TC_BEGIN\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001619 seq = &beginseq;
1620 chain_group();
Glenn L McGrath545106f2002-11-11 06:21:00 +00001621 } else if (tclass & TC_END) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001622 debug_printf_parse("%s: TC_END\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001623 seq = &endseq;
1624 chain_group();
Glenn L McGrath545106f2002-11-11 06:21:00 +00001625 } else if (tclass & TC_FUNCDECL) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001626 debug_printf_parse("%s: TC_FUNCDECL\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001627 next_token(TC_FUNCTION);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001628 g_pos++;
1629 f = newfunc(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001630 f->body.first = NULL;
1631 f->nargs = 0;
Brian Foley1c42c182019-01-06 18:32:59 -08001632 /* Match func arg list: a comma sep list of >= 0 args, and a close paren */
1633 while (next_token(TC_VARIABLE | TC_SEQTERM | TC_COMMA)) {
1634 /* Either an empty arg list, or trailing comma from prev iter
1635 * must be followed by an arg */
1636 if (f->nargs == 0 && t_tclass == TC_SEQTERM)
1637 break;
1638
1639 /* TC_SEQSTART/TC_COMMA must be followed by TC_VARIABLE */
1640 if (t_tclass != TC_VARIABLE)
1641 syntax_error(EMSG_UNEXP_TOKEN);
1642
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001643 v = findvar(ahash, t_string);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001644 v->x.aidx = f->nargs++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001645
Brian Foley1c42c182019-01-06 18:32:59 -08001646 /* Arg followed either by end of arg list or 1 comma */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001647 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
1648 break;
Brian Foley1c42c182019-01-06 18:32:59 -08001649 if (t_tclass != TC_COMMA)
1650 syntax_error(EMSG_UNEXP_TOKEN);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001651 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001652 seq = &f->body;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001653 chain_group();
1654 clear_array(ahash);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001655 } else if (tclass & TC_OPSEQ) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001656 debug_printf_parse("%s: TC_OPSEQ\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001657 rollback_token();
1658 cn = chain_node(OC_TEST);
1659 cn->l.n = parse_expr(TC_OPTERM | TC_EOF | TC_GRPSTART);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001660 if (t_tclass & TC_GRPSTART) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001661 debug_printf_parse("%s: TC_GRPSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001662 rollback_token();
1663 chain_group();
1664 } else {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001665 debug_printf_parse("%s: !TC_GRPSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001666 chain_node(OC_PRINT);
1667 }
1668 cn->r.n = mainseq.last;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001669 } else /* if (tclass & TC_GRPSTART) */ {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001670 debug_printf_parse("%s: TC_GRPSTART(?)\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001671 rollback_token();
1672 chain_group();
1673 }
1674 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001675 debug_printf_parse("%s: TC_EOF\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001676}
1677
1678
1679/* -------- program execution part -------- */
1680
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001681static node *mk_splitter(const char *s, tsplitter *spl)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001682{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001683 regex_t *re, *ire;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001684 node *n;
1685
1686 re = &spl->re[0];
1687 ire = &spl->re[1];
1688 n = &spl->n;
Denis Vlasenko890ac9d2006-10-07 15:16:19 +00001689 if ((n->info & OPCLSMASK) == OC_REGEXP) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001690 regfree(re);
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001691 regfree(ire); // TODO: nuke ire, use re+1?
Glenn L McGrath545106f2002-11-11 06:21:00 +00001692 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001693 if (s[0] && s[1]) { /* strlen(s) > 1 */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001694 mk_re_node(s, n, re);
1695 } else {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001696 n->info = (uint32_t) s[0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00001697 }
1698
1699 return n;
1700}
1701
1702/* use node as a regular expression. Supplied with node ptr and regex_t
Eric Andersenaff114c2004-04-14 17:51:38 +00001703 * storage space. Return ptr to regex (if result points to preg, it should
Glenn L McGrath545106f2002-11-11 06:21:00 +00001704 * be later regfree'd manually
1705 */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001706static regex_t *as_regex(node *op, regex_t *preg)
1707{
Denis Vlasenko7a676642009-03-15 22:20:31 +00001708 int cflags;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001709 var *v;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001710 const char *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001711
1712 if ((op->info & OPCLSMASK) == OC_REGEXP) {
1713 return icase ? op->r.ire : op->l.re;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001714 }
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001715 v = nvalloc(1);
1716 s = getvar_s(evaluate(op, v));
Denis Vlasenko7a676642009-03-15 22:20:31 +00001717
1718 cflags = icase ? REG_EXTENDED | REG_ICASE : REG_EXTENDED;
1719 /* Testcase where REG_EXTENDED fails (unpaired '{'):
1720 * echo Hi | awk 'gsub("@(samp|code|file)\{","");'
1721 * gawk 3.1.5 eats this. We revert to ~REG_EXTENDED
1722 * (maybe gsub is not supposed to use REG_EXTENDED?).
1723 */
1724 if (regcomp(preg, s, cflags)) {
1725 cflags &= ~REG_EXTENDED;
1726 xregcomp(preg, s, cflags);
1727 }
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001728 nvfree(v);
1729 return preg;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001730}
1731
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001732/* gradually increasing buffer.
1733 * note that we reallocate even if n == old_size,
1734 * and thus there is at least one extra allocated byte.
1735 */
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001736static char* qrealloc(char *b, int n, int *size)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001737{
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001738 if (!b || n >= *size) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001739 *size = n + (n>>1) + 80;
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001740 b = xrealloc(b, *size);
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001741 }
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001742 return b;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001743}
1744
1745/* resize field storage space */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001746static void fsrealloc(int size)
1747{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001748 int i;
1749
1750 if (size >= maxfields) {
1751 i = maxfields;
1752 maxfields = size + 16;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001753 Fields = xrealloc(Fields, maxfields * sizeof(Fields[0]));
Denis Vlasenkof782f522007-01-01 23:51:30 +00001754 for (; i < maxfields; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001755 Fields[i].type = VF_SPECIAL;
1756 Fields[i].string = NULL;
1757 }
1758 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001759 /* if size < nfields, clear extra field variables */
1760 for (i = size; i < nfields; i++) {
1761 clrvar(Fields + i);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001762 }
1763 nfields = size;
1764}
1765
Denys Vlasenko665a6592020-12-02 19:07:31 +01001766static int regexec1_nonempty(const regex_t *preg, const char *s, regmatch_t pmatch[])
1767{
1768 int r = regexec(preg, s, 1, pmatch, 0);
1769 if (r == 0 && pmatch[0].rm_eo == 0) {
1770 /* For example, happens when FS can match
1771 * an empty string (awk -F ' *'). Logically,
1772 * this should split into one-char fields.
1773 * However, gawk 5.0.1 searches for first
1774 * _non-empty_ separator string match:
1775 */
1776 size_t ofs = 0;
1777 do {
1778 ofs++;
1779 if (!s[ofs])
1780 return REG_NOMATCH;
1781 regexec(preg, s + ofs, 1, pmatch, 0);
1782 } while (pmatch[0].rm_eo == 0);
1783 pmatch[0].rm_so += ofs;
1784 pmatch[0].rm_eo += ofs;
1785 }
1786 return r;
1787}
1788
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001789static int awk_split(const char *s, node *spl, char **slist)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001790{
Denys Vlasenko5323af72020-11-16 10:40:32 +01001791 int n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001792 char c[4];
1793 char *s1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001794
1795 /* in worst case, each char would be a separate field */
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001796 *slist = s1 = xzalloc(strlen(s) * 2 + 3);
1797 strcpy(s1, s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001798
1799 c[0] = c[1] = (char)spl->info;
1800 c[2] = c[3] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001801 if (*getvar_s(intvar[RS]) == '\0')
1802 c[2] = '\n';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001803
Denys Vlasenko28458c62010-10-05 16:49:03 +02001804 n = 0;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001805 if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
1806 if (!*s)
1807 return n; /* "": zero fields */
1808 n++; /* at least one field will be there */
1809 do {
Denys Vlasenko5323af72020-11-16 10:40:32 +01001810 int l;
1811 regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough...
1812
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001813 l = strcspn(s, c+2); /* len till next NUL or \n */
Denys Vlasenko665a6592020-12-02 19:07:31 +01001814 if (regexec1_nonempty(icase ? spl->r.ire : spl->l.re, s, pmatch) == 0
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001815 && pmatch[0].rm_so <= l
1816 ) {
Denys Vlasenko665a6592020-12-02 19:07:31 +01001817 /* if (pmatch[0].rm_eo == 0) ... - impossible */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001818 l = pmatch[0].rm_so;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001819 n++; /* we saw yet another delimiter */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001820 } else {
1821 pmatch[0].rm_eo = l;
Denys Vlasenkoe4244232009-05-18 23:50:03 +02001822 if (s[l])
1823 pmatch[0].rm_eo++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001824 }
Denys Vlasenko5323af72020-11-16 10:40:32 +01001825 s1 = mempcpy(s1, s, l);
1826 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001827 s += pmatch[0].rm_eo;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001828 } while (*s);
Denys Vlasenko5323af72020-11-16 10:40:32 +01001829
1830 /* echo a-- | awk -F-- '{ print NF, length($NF), $NF }'
1831 * should print "2 0 ":
1832 */
1833 *s1 = '\0';
1834
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001835 return n;
1836 }
1837 if (c[0] == '\0') { /* null split */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001838 while (*s) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001839 *s1++ = *s++;
1840 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001841 n++;
1842 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001843 return n;
1844 }
1845 if (c[0] != ' ') { /* single-character split */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001846 if (icase) {
1847 c[0] = toupper(c[0]);
1848 c[1] = tolower(c[1]);
1849 }
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001850 if (*s1)
1851 n++;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001852 while ((s1 = strpbrk(s1, c)) != NULL) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001853 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001854 n++;
1855 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001856 return n;
1857 }
1858 /* space split */
1859 while (*s) {
1860 s = skip_whitespace(s);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001861 if (!*s)
1862 break;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001863 n++;
1864 while (*s && !isspace(*s))
1865 *s1++ = *s++;
1866 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001867 }
1868 return n;
1869}
1870
Mike Frysinger10a11e22005-09-27 02:23:02 +00001871static void split_f0(void)
1872{
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001873/* static char *fstrings; */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001874#define fstrings (G.split_f0__fstrings)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001875
Glenn L McGrath545106f2002-11-11 06:21:00 +00001876 int i, n;
1877 char *s;
1878
1879 if (is_f0_split)
1880 return;
1881
1882 is_f0_split = TRUE;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001883 free(fstrings);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001884 fsrealloc(0);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001885 n = awk_split(getvar_s(intvar[F0]), &fsplitter.n, &fstrings);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001886 fsrealloc(n);
1887 s = fstrings;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001888 for (i = 0; i < n; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001889 Fields[i].string = nextword(&s);
1890 Fields[i].type |= (VF_FSTR | VF_USER | VF_DIRTY);
1891 }
1892
1893 /* set NF manually to avoid side effects */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001894 clrvar(intvar[NF]);
1895 intvar[NF]->type = VF_NUMBER | VF_SPECIAL;
1896 intvar[NF]->number = nfields;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001897#undef fstrings
Glenn L McGrath545106f2002-11-11 06:21:00 +00001898}
1899
1900/* perform additional actions when some internal variables changed */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001901static void handle_special(var *v)
1902{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001903 int n;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001904 char *b;
1905 const char *sep, *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001906 int sl, l, len, i, bsize;
1907
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001908 if (!(v->type & VF_SPECIAL))
Glenn L McGrath545106f2002-11-11 06:21:00 +00001909 return;
1910
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001911 if (v == intvar[NF]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001912 n = (int)getvar_i(v);
Denys Vlasenko75a1c872018-11-28 12:16:29 +01001913 if (n < 0)
1914 syntax_error("NF set to negative value");
Glenn L McGrath545106f2002-11-11 06:21:00 +00001915 fsrealloc(n);
1916
1917 /* recalculate $0 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001918 sep = getvar_s(intvar[OFS]);
Rob Landleya3896512006-05-07 20:20:34 +00001919 sl = strlen(sep);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001920 b = NULL;
1921 len = 0;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001922 for (i = 0; i < n; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001923 s = getvar_s(&Fields[i]);
Rob Landleya3896512006-05-07 20:20:34 +00001924 l = strlen(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001925 if (b) {
1926 memcpy(b+len, sep, sl);
1927 len += sl;
1928 }
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001929 b = qrealloc(b, len+l+sl, &bsize);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001930 memcpy(b+len, s, l);
1931 len += l;
1932 }
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001933 if (b)
1934 b[len] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001935 setvar_p(intvar[F0], b);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001936 is_f0_split = TRUE;
1937
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001938 } else if (v == intvar[F0]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001939 is_f0_split = FALSE;
1940
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001941 } else if (v == intvar[FS]) {
Denys Vlasenkodf8066a2012-07-11 01:27:15 +02001942 /*
1943 * The POSIX-2008 standard says that changing FS should have no effect on the
1944 * current input line, but only on the next one. The language is:
1945 *
1946 * > Before the first reference to a field in the record is evaluated, the record
1947 * > shall be split into fields, according to the rules in Regular Expressions,
1948 * > using the value of FS that was current at the time the record was read.
1949 *
1950 * So, split up current line before assignment to FS:
1951 */
1952 split_f0();
1953
Glenn L McGrath545106f2002-11-11 06:21:00 +00001954 mk_splitter(getvar_s(v), &fsplitter);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001955 } else if (v == intvar[RS]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001956 mk_splitter(getvar_s(v), &rsplitter);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001957 } else if (v == intvar[IGNORECASE]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001958 icase = istrue(v);
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001959 } else { /* $n */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001960 n = getvar_i(intvar[NF]);
1961 setvar_i(intvar[NF], n > v-Fields ? n : v-Fields+1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001962 /* right here v is invalid. Just to note... */
1963 }
1964}
1965
1966/* step through func/builtin/etc arguments */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001967static node *nextarg(node **pn)
1968{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001969 node *n;
1970
1971 n = *pn;
1972 if (n && (n->info & OPCLSMASK) == OC_COMMA) {
1973 *pn = n->r.n;
1974 n = n->l.n;
1975 } else {
1976 *pn = NULL;
1977 }
1978 return n;
1979}
1980
Mike Frysinger10a11e22005-09-27 02:23:02 +00001981static void hashwalk_init(var *v, xhash *array)
1982{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001983 hash_item *hi;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00001984 unsigned i;
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001985 walker_list *w;
1986 walker_list *prev_walker;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001987
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001988 if (v->type & VF_WALK) {
1989 prev_walker = v->x.walker;
1990 } else {
1991 v->type |= VF_WALK;
1992 prev_walker = NULL;
1993 }
1994 debug_printf_walker("hashwalk_init: prev_walker:%p\n", prev_walker);
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001995
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001996 w = v->x.walker = xzalloc(sizeof(*w) + array->glen + 1); /* why + 1? */
1997 debug_printf_walker(" walker@%p=%p\n", &v->x.walker, w);
1998 w->cur = w->end = w->wbuf;
1999 w->prev = prev_walker;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002000 for (i = 0; i < array->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002001 hi = array->items[i];
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00002002 while (hi) {
Denys Vlasenkoda62b092010-03-11 12:13:18 +01002003 strcpy(w->end, hi->name);
2004 nextword(&w->end);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002005 hi = hi->next;
2006 }
2007 }
2008}
2009
Mike Frysinger10a11e22005-09-27 02:23:02 +00002010static int hashwalk_next(var *v)
2011{
Denys Vlasenkoda62b092010-03-11 12:13:18 +01002012 walker_list *w = v->x.walker;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002013
Denys Vlasenkoda62b092010-03-11 12:13:18 +01002014 if (w->cur >= w->end) {
2015 walker_list *prev_walker = w->prev;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002016
Denys Vlasenkoda62b092010-03-11 12:13:18 +01002017 debug_printf_walker("end of iteration, free(walker@%p:%p), prev_walker:%p\n", &v->x.walker, w, prev_walker);
2018 free(w);
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01002019 v->x.walker = prev_walker;
2020 return FALSE;
2021 }
2022
Denys Vlasenkoda62b092010-03-11 12:13:18 +01002023 setvar_s(v, nextword(&w->cur));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002024 return TRUE;
2025}
2026
2027/* evaluate node, return 1 when result is true, 0 otherwise */
Mike Frysinger10a11e22005-09-27 02:23:02 +00002028static int ptest(node *pattern)
2029{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002030 /* ptest__v is "static": to save stack space? */
2031 return istrue(evaluate(pattern, &G.ptest__v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002032}
2033
2034/* read next record from stream rsm into a variable v */
Mike Frysinger10a11e22005-09-27 02:23:02 +00002035static int awk_getline(rstream *rsm, var *v)
2036{
Glenn L McGrath545106f2002-11-11 06:21:00 +00002037 char *b;
Denys Vlasenko5323af72020-11-16 10:40:32 +01002038 regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough...
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002039 int size, a, p, pp = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002040 int fd, so, eo, r, rp;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002041 char c, *m, *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002042
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002043 debug_printf_eval("entered %s()\n", __func__);
2044
Glenn L McGrath545106f2002-11-11 06:21:00 +00002045 /* we're using our own buffer since we need access to accumulating
2046 * characters
2047 */
2048 fd = fileno(rsm->F);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002049 m = rsm->buffer;
2050 a = rsm->adv;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002051 p = rsm->pos;
2052 size = rsm->size;
2053 c = (char) rsplitter.n.info;
2054 rp = 0;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002055
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002056 if (!m)
2057 m = qrealloc(m, 256, &size);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002058
Glenn L McGrath545106f2002-11-11 06:21:00 +00002059 do {
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002060 b = m + a;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002061 so = eo = p;
2062 r = 1;
2063 if (p > 0) {
2064 if ((rsplitter.n.info & OPCLSMASK) == OC_REGEXP) {
2065 if (regexec(icase ? rsplitter.n.r.ire : rsplitter.n.l.re,
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002066 b, 1, pmatch, 0) == 0) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002067 so = pmatch[0].rm_so;
2068 eo = pmatch[0].rm_eo;
2069 if (b[eo] != '\0')
2070 break;
2071 }
2072 } else if (c != '\0') {
2073 s = strchr(b+pp, c);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002074 if (!s)
2075 s = memchr(b+pp, '\0', p - pp);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002076 if (s) {
2077 so = eo = s-b;
2078 eo++;
2079 break;
2080 }
2081 } else {
2082 while (b[rp] == '\n')
2083 rp++;
2084 s = strstr(b+rp, "\n\n");
2085 if (s) {
2086 so = eo = s-b;
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002087 while (b[eo] == '\n')
2088 eo++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002089 if (b[eo] != '\0')
2090 break;
2091 }
2092 }
2093 }
2094
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002095 if (a > 0) {
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002096 memmove(m, m+a, p+1);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002097 b = m;
2098 a = 0;
2099 }
2100
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002101 m = qrealloc(m, a+p+128, &size);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002102 b = m + a;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002103 pp = p;
2104 p += safe_read(fd, b+p, size-p-1);
2105 if (p < pp) {
2106 p = 0;
2107 r = 0;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002108 setvar_i(intvar[ERRNO], errno);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002109 }
2110 b[p] = '\0';
2111
2112 } while (p > pp);
2113
2114 if (p == 0) {
2115 r--;
2116 } else {
2117 c = b[so]; b[so] = '\0';
2118 setvar_s(v, b+rp);
2119 v->type |= VF_USER;
2120 b[so] = c;
2121 c = b[eo]; b[eo] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002122 setvar_s(intvar[RT], b+so);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002123 b[eo] = c;
2124 }
2125
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002126 rsm->buffer = m;
2127 rsm->adv = a + eo;
2128 rsm->pos = p - eo;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002129 rsm->size = size;
2130
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002131 debug_printf_eval("returning from %s(): %d\n", __func__, r);
2132
Glenn L McGrath545106f2002-11-11 06:21:00 +00002133 return r;
2134}
2135
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00002136static int fmt_num(char *b, int size, const char *format, double n, int int_as_int)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002137{
Denis Vlasenkof782f522007-01-01 23:51:30 +00002138 int r = 0;
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00002139 char c;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002140 const char *s = format;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002141
Denys Vlasenko1390a012013-07-20 21:23:01 +02002142 if (int_as_int && n == (long long)n) {
2143 r = snprintf(b, size, "%lld", (long long)n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002144 } else {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002145 do { c = *s; } while (c && *++s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002146 if (strchr("diouxX", c)) {
2147 r = snprintf(b, size, format, (int)n);
2148 } else if (strchr("eEfgG", c)) {
2149 r = snprintf(b, size, format, n);
2150 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002151 syntax_error(EMSG_INV_FMT);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002152 }
2153 }
2154 return r;
2155}
2156
Glenn L McGrath545106f2002-11-11 06:21:00 +00002157/* formatted output into an allocated buffer, return ptr to buffer */
Ron Yorstone8fe9f92021-01-27 11:19:14 +00002158#if !ENABLE_FEATURE_AWK_GNU_EXTENSIONS
2159# define awk_printf(a, b) awk_printf(a)
2160#endif
2161static char *awk_printf(node *n, int *len)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002162{
Glenn L McGrath545106f2002-11-11 06:21:00 +00002163 char *b = NULL;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002164 char *fmt, *s, *f;
2165 const char *s1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002166 int i, j, incr, bsize;
2167 char c, c1;
2168 var *v, *arg;
2169
2170 v = nvalloc(1);
Rob Landleyd921b2e2006-08-03 15:41:12 +00002171 fmt = f = xstrdup(getvar_s(evaluate(nextarg(&n), v)));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002172
2173 i = 0;
2174 while (*f) {
2175 s = f;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002176 while (*f && (*f != '%' || *++f == '%'))
Glenn L McGrath545106f2002-11-11 06:21:00 +00002177 f++;
Denis Vlasenko389f9d52007-05-09 21:57:23 +00002178 while (*f && !isalpha(*f)) {
2179 if (*f == '*')
2180 syntax_error("%*x formats are not supported");
Glenn L McGrath545106f2002-11-11 06:21:00 +00002181 f++;
Denis Vlasenko389f9d52007-05-09 21:57:23 +00002182 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002183
2184 incr = (f - s) + MAXVARFMT;
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002185 b = qrealloc(b, incr + i, &bsize);
Denis Vlasenkof782f522007-01-01 23:51:30 +00002186 c = *f;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002187 if (c != '\0')
2188 f++;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002189 c1 = *f;
2190 *f = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00002191 arg = evaluate(nextarg(&n), v);
2192
2193 j = i;
2194 if (c == 'c' || !c) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002195 i += sprintf(b+i, s, is_numeric(arg) ?
2196 (char)getvar_i(arg) : *getvar_s(arg));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002197 } else if (c == 's') {
Denis Vlasenko92758142006-10-03 19:56:34 +00002198 s1 = getvar_s(arg);
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002199 b = qrealloc(b, incr+i+strlen(s1), &bsize);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002200 i += sprintf(b+i, s, s1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002201 } else {
2202 i += fmt_num(b+i, incr, s, getvar_i(arg), FALSE);
2203 }
2204 *f = c1;
2205
2206 /* if there was an error while sprintf, return value is negative */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002207 if (i < j)
2208 i = j;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002209 }
2210
Glenn L McGrath545106f2002-11-11 06:21:00 +00002211 free(fmt);
2212 nvfree(v);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002213 b = xrealloc(b, i + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002214 b[i] = '\0';
Ron Yorstone8fe9f92021-01-27 11:19:14 +00002215#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
2216 if (len)
2217 *len = i;
2218#endif
Glenn L McGrath545106f2002-11-11 06:21:00 +00002219 return b;
2220}
2221
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002222/* Common substitution routine.
2223 * Replace (nm)'th substring of (src) that matches (rn) with (repl),
2224 * store result into (dest), return number of substitutions.
2225 * If nm = 0, replace all matches.
2226 * If src or dst is NULL, use $0.
2227 * If subexp != 0, enable subexpression matching (\1-\9).
Glenn L McGrath545106f2002-11-11 06:21:00 +00002228 */
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002229static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int subexp)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002230{
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002231 char *resbuf;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002232 const char *sp;
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002233 int match_no, residx, replen, resbufsize;
2234 int regexec_flags;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002235 regmatch_t pmatch[10];
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002236 regex_t sreg, *regex;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002237
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002238 resbuf = NULL;
2239 residx = 0;
2240 match_no = 0;
2241 regexec_flags = 0;
2242 regex = as_regex(rn, &sreg);
2243 sp = getvar_s(src ? src : intvar[F0]);
2244 replen = strlen(repl);
2245 while (regexec(regex, sp, 10, pmatch, regexec_flags) == 0) {
2246 int so = pmatch[0].rm_so;
2247 int eo = pmatch[0].rm_eo;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002248
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002249 //bb_error_msg("match %u: [%u,%u] '%s'%p", match_no+1, so, eo, sp,sp);
2250 resbuf = qrealloc(resbuf, residx + eo + replen, &resbufsize);
2251 memcpy(resbuf + residx, sp, eo);
2252 residx += eo;
2253 if (++match_no >= nm) {
2254 const char *s;
2255 int nbs;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002256
Glenn L McGrath545106f2002-11-11 06:21:00 +00002257 /* replace */
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002258 residx -= (eo - so);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002259 nbs = 0;
2260 for (s = repl; *s; s++) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002261 char c = resbuf[residx++] = *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002262 if (c == '\\') {
2263 nbs++;
2264 continue;
2265 }
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002266 if (c == '&' || (subexp && c >= '0' && c <= '9')) {
2267 int j;
2268 residx -= ((nbs + 3) >> 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002269 j = 0;
2270 if (c != '&') {
2271 j = c - '0';
2272 nbs++;
2273 }
2274 if (nbs % 2) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002275 resbuf[residx++] = c;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002276 } else {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002277 int n = pmatch[j].rm_eo - pmatch[j].rm_so;
2278 resbuf = qrealloc(resbuf, residx + replen + n, &resbufsize);
2279 memcpy(resbuf + residx, sp + pmatch[j].rm_so, n);
2280 residx += n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002281 }
2282 }
2283 nbs = 0;
2284 }
2285 }
2286
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002287 regexec_flags = REG_NOTBOL;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002288 sp += eo;
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002289 if (match_no == nm)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002290 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002291 if (eo == so) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002292 /* Empty match (e.g. "b*" will match anywhere).
2293 * Advance by one char. */
2294//BUG (bug 1333):
2295//gsub(/\<b*/,"") on "abc" will reach this point, advance to "bc"
2296//... and will erroneously match "b" even though it is NOT at the word start.
2297//we need REG_NOTBOW but it does not exist...
Denys Vlasenko7379cd12010-04-04 01:48:12 +02002298//TODO: if EXTRA_COMPAT=y, use GNU matching and re_search,
2299//it should be able to do it correctly.
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002300 /* Subtle: this is safe only because
2301 * qrealloc allocated at least one extra byte */
2302 resbuf[residx] = *sp;
2303 if (*sp == '\0')
2304 goto ret;
2305 sp++;
2306 residx++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002307 }
2308 }
2309
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002310 resbuf = qrealloc(resbuf, residx + strlen(sp), &resbufsize);
2311 strcpy(resbuf + residx, sp);
2312 ret:
2313 //bb_error_msg("end sp:'%s'%p", sp,sp);
2314 setvar_p(dest ? dest : intvar[F0], resbuf);
2315 if (regex == &sreg)
2316 regfree(regex);
2317 return match_no;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002318}
2319
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002320static NOINLINE int do_mktime(const char *ds)
2321{
2322 struct tm then;
2323 int count;
2324
2325 /*memset(&then, 0, sizeof(then)); - not needed */
2326 then.tm_isdst = -1; /* default is unknown */
2327
2328 /* manpage of mktime says these fields are ints,
2329 * so we can sscanf stuff directly into them */
2330 count = sscanf(ds, "%u %u %u %u %u %u %d",
2331 &then.tm_year, &then.tm_mon, &then.tm_mday,
2332 &then.tm_hour, &then.tm_min, &then.tm_sec,
2333 &then.tm_isdst);
2334
2335 if (count < 6
2336 || (unsigned)then.tm_mon < 1
2337 || (unsigned)then.tm_year < 1900
2338 ) {
2339 return -1;
2340 }
2341
2342 then.tm_mon -= 1;
Denys Vlasenkobc3e9472009-09-21 04:16:00 +02002343 then.tm_year -= 1900;
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002344
2345 return mktime(&then);
2346}
2347
2348static NOINLINE var *exec_builtin(node *op, var *res)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002349{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002350#define tspl (G.exec_builtin__tspl)
2351
Glenn L McGrath545106f2002-11-11 06:21:00 +00002352 var *tv;
2353 node *an[4];
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002354 var *av[4];
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002355 const char *as[4];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002356 regmatch_t pmatch[2];
2357 regex_t sreg, *re;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002358 node *spl;
Mike Frysingerf87b3e32005-09-27 04:16:22 +00002359 uint32_t isr, info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002360 int nargs;
2361 time_t tt;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002362 int i, l, ll, n;
2363
2364 tv = nvalloc(4);
2365 isr = info = op->info;
2366 op = op->l.n;
2367
2368 av[2] = av[3] = NULL;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002369 for (i = 0; i < 4 && op; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002370 an[i] = nextarg(&op);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002371 if (isr & 0x09000000)
2372 av[i] = evaluate(an[i], &tv[i]);
2373 if (isr & 0x08000000)
2374 as[i] = getvar_s(av[i]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002375 isr >>= 1;
2376 }
2377
2378 nargs = i;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00002379 if ((uint32_t)nargs < (info >> 30))
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002380 syntax_error(EMSG_TOO_FEW_ARGS);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002381
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02002382 info &= OPNMASK;
2383 switch (info) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002384
Denis Vlasenkof782f522007-01-01 23:51:30 +00002385 case B_a2:
Rob Landleyd8205b32010-10-24 03:27:22 +02002386 if (ENABLE_FEATURE_AWK_LIBM)
2387 setvar_i(res, atan2(getvar_i(av[0]), getvar_i(av[1])));
2388 else
2389 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002390 break;
2391
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002392 case B_sp: {
2393 char *s, *s1;
2394
Glenn L McGrath545106f2002-11-11 06:21:00 +00002395 if (nargs > 2) {
2396 spl = (an[2]->info & OPCLSMASK) == OC_REGEXP ?
2397 an[2] : mk_splitter(getvar_s(evaluate(an[2], &tv[2])), &tspl);
2398 } else {
2399 spl = &fsplitter.n;
2400 }
2401
2402 n = awk_split(as[0], spl, &s);
2403 s1 = s;
2404 clear_array(iamarray(av[1]));
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002405 for (i = 1; i <= n; i++)
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002406 setari_u(av[1], i, nextword(&s));
2407 free(s1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002408 setvar_i(res, n);
2409 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002410 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002411
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002412 case B_ss: {
2413 char *s;
2414
Rob Landleya3896512006-05-07 20:20:34 +00002415 l = strlen(as[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002416 i = getvar_i(av[1]) - 1;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002417 if (i > l)
2418 i = l;
2419 if (i < 0)
2420 i = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002421 n = (nargs > 2) ? getvar_i(av[2]) : l-i;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002422 if (n < 0)
2423 n = 0;
Denis Vlasenko8ae5b282008-07-02 22:47:49 +00002424 s = xstrndup(as[0]+i, n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002425 setvar_p(res, s);
2426 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002427 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +00002428
Denis Vlasenko7cbcd1c2008-08-28 23:16:58 +00002429 /* Bitwise ops must assume that operands are unsigned. GNU Awk 3.1.5:
2430 * awk '{ print or(-1,1) }' gives "4.29497e+09", not "-2.xxxe+09" */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002431 case B_an:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002432 setvar_i(res, getvar_i_int(av[0]) & getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002433 break;
Denis Vlasenkof7996f32007-01-11 17:20:00 +00002434
Denis Vlasenkof782f522007-01-01 23:51:30 +00002435 case B_co:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002436 setvar_i(res, ~getvar_i_int(av[0]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002437 break;
2438
Denis Vlasenkof782f522007-01-01 23:51:30 +00002439 case B_ls:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002440 setvar_i(res, getvar_i_int(av[0]) << getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002441 break;
2442
Denis Vlasenkof782f522007-01-01 23:51:30 +00002443 case B_or:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002444 setvar_i(res, getvar_i_int(av[0]) | getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002445 break;
2446
Denis Vlasenkof782f522007-01-01 23:51:30 +00002447 case B_rs:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002448 setvar_i(res, getvar_i_int(av[0]) >> getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002449 break;
2450
Denis Vlasenkof782f522007-01-01 23:51:30 +00002451 case B_xo:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002452 setvar_i(res, getvar_i_int(av[0]) ^ getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002453 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002454
Denis Vlasenkof782f522007-01-01 23:51:30 +00002455 case B_lo:
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002456 case B_up: {
2457 char *s, *s1;
Rob Landleyd921b2e2006-08-03 15:41:12 +00002458 s1 = s = xstrdup(as[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002459 while (*s1) {
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02002460 //*s1 = (info == B_up) ? toupper(*s1) : tolower(*s1);
2461 if ((unsigned char)((*s1 | 0x20) - 'a') <= ('z' - 'a'))
2462 *s1 = (info == B_up) ? (*s1 & 0xdf) : (*s1 | 0x20);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002463 s1++;
2464 }
2465 setvar_p(res, s);
2466 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002467 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002468
Denis Vlasenkof782f522007-01-01 23:51:30 +00002469 case B_ix:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002470 n = 0;
Rob Landleya3896512006-05-07 20:20:34 +00002471 ll = strlen(as[1]);
2472 l = strlen(as[0]) - ll;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002473 if (ll > 0 && l >= 0) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002474 if (!icase) {
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002475 char *s = strstr(as[0], as[1]);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002476 if (s)
2477 n = (s - as[0]) + 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002478 } else {
2479 /* this piece of code is terribly slow and
2480 * really should be rewritten
2481 */
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002482 for (i = 0; i <= l; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002483 if (strncasecmp(as[0]+i, as[1], ll) == 0) {
2484 n = i+1;
2485 break;
2486 }
2487 }
2488 }
2489 }
2490 setvar_i(res, n);
2491 break;
2492
Denis Vlasenkof782f522007-01-01 23:51:30 +00002493 case B_ti:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002494 if (nargs > 1)
2495 tt = getvar_i(av[1]);
2496 else
2497 time(&tt);
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002498 //s = (nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y";
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002499 i = strftime(g_buf, MAXVARFMT,
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002500 ((nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"),
2501 localtime(&tt));
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002502 g_buf[i] = '\0';
2503 setvar_s(res, g_buf);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002504 break;
2505
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002506 case B_mt:
2507 setvar_i(res, do_mktime(as[0]));
2508 break;
2509
Denis Vlasenkof782f522007-01-01 23:51:30 +00002510 case B_ma:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002511 re = as_regex(an[1], &sreg);
2512 n = regexec(re, as[0], 1, pmatch, 0);
2513 if (n == 0) {
2514 pmatch[0].rm_so++;
2515 pmatch[0].rm_eo++;
2516 } else {
2517 pmatch[0].rm_so = 0;
2518 pmatch[0].rm_eo = -1;
2519 }
2520 setvar_i(newvar("RSTART"), pmatch[0].rm_so);
2521 setvar_i(newvar("RLENGTH"), pmatch[0].rm_eo - pmatch[0].rm_so);
2522 setvar_i(res, pmatch[0].rm_so);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002523 if (re == &sreg)
2524 regfree(re);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002525 break;
2526
Denis Vlasenkof782f522007-01-01 23:51:30 +00002527 case B_ge:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002528 awk_sub(an[0], as[1], getvar_i(av[2]), av[3], res, TRUE);
2529 break;
2530
Denis Vlasenkof782f522007-01-01 23:51:30 +00002531 case B_gs:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002532 setvar_i(res, awk_sub(an[0], as[1], 0, av[2], av[2], FALSE));
2533 break;
2534
Denis Vlasenkof782f522007-01-01 23:51:30 +00002535 case B_su:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002536 setvar_i(res, awk_sub(an[0], as[1], 1, av[2], av[2], FALSE));
2537 break;
2538 }
2539
2540 nvfree(tv);
2541 return res;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002542#undef tspl
Glenn L McGrath545106f2002-11-11 06:21:00 +00002543}
2544
2545/*
2546 * Evaluate node - the heart of the program. Supplied with subtree
2547 * and place where to store result. returns ptr to result.
2548 */
2549#define XC(n) ((n) >> 8)
2550
Mike Frysinger10a11e22005-09-27 02:23:02 +00002551static var *evaluate(node *op, var *res)
2552{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002553/* This procedure is recursive so we should count every byte */
2554#define fnargs (G.evaluate__fnargs)
2555/* seed is initialized to 1 */
2556#define seed (G.evaluate__seed)
Denys Vlasenkofb132e42010-10-29 11:46:52 +02002557#define sreg (G.evaluate__sreg)
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002558
Glenn L McGrath545106f2002-11-11 06:21:00 +00002559 var *v1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002560
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002561 if (!op)
Glenn L McGrath545106f2002-11-11 06:21:00 +00002562 return setvar_s(res, NULL);
2563
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002564 debug_printf_eval("entered %s()\n", __func__);
2565
Glenn L McGrath545106f2002-11-11 06:21:00 +00002566 v1 = nvalloc(2);
2567
2568 while (op) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002569 struct {
2570 var *v;
2571 const char *s;
2572 } L = L; /* for compiler */
2573 struct {
2574 var *v;
2575 const char *s;
2576 } R = R;
2577 double L_d = L_d;
2578 uint32_t opinfo;
2579 int opn;
2580 node *op1;
2581
Glenn L McGrath545106f2002-11-11 06:21:00 +00002582 opinfo = op->info;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002583 opn = (opinfo & OPNMASK);
2584 g_lineno = op->lineno;
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002585 op1 = op->l.n;
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002586 debug_printf_eval("opinfo:%08x opn:%08x\n", opinfo, opn);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002587
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01002588 /* "delete" is special:
2589 * "delete array[var--]" must evaluate index expr only once,
2590 * must not evaluate it in "execute inevitable things" part.
2591 */
2592 if (XC(opinfo & OPCLSMASK) == XC(OC_DELETE)) {
2593 uint32_t info = op1->info & OPCLSMASK;
2594 var *v;
2595
2596 debug_printf_eval("DELETE\n");
2597 if (info == OC_VAR) {
2598 v = op1->l.v;
2599 } else if (info == OC_FNARG) {
2600 v = &fnargs[op1->l.aidx];
2601 } else {
2602 syntax_error(EMSG_NOT_ARRAY);
2603 }
2604 if (op1->r.n) { /* array ref? */
2605 const char *s;
2606 s = getvar_s(evaluate(op1->r.n, v1));
2607 hash_remove(iamarray(v), s);
2608 } else {
2609 clear_array(iamarray(v));
2610 }
2611 goto next;
2612 }
2613
Mike Frysingerde2b9382005-09-27 03:18:00 +00002614 /* execute inevitable things */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002615 if (opinfo & OF_RES1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002616 L.v = evaluate(op1, v1);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002617 if (opinfo & OF_RES2)
2618 R.v = evaluate(op->r.n, v1+1);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002619 if (opinfo & OF_STR1) {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002620 L.s = getvar_s(L.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002621 debug_printf_eval("L.s:'%s'\n", L.s);
2622 }
2623 if (opinfo & OF_STR2) {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002624 R.s = getvar_s(R.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002625 debug_printf_eval("R.s:'%s'\n", R.s);
2626 }
2627 if (opinfo & OF_NUM1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002628 L_d = getvar_i(L.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002629 debug_printf_eval("L_d:%f\n", L_d);
2630 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002631
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002632 debug_printf_eval("switch(0x%x)\n", XC(opinfo & OPCLSMASK));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002633 switch (XC(opinfo & OPCLSMASK)) {
2634
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002635 /* -- iterative node type -- */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002636
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002637 /* test pattern */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002638 case XC( OC_TEST ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002639 if ((op1->info & OPCLSMASK) == OC_COMMA) {
2640 /* it's range pattern */
2641 if ((opinfo & OF_CHECKED) || ptest(op1->l.n)) {
2642 op->info |= OF_CHECKED;
2643 if (ptest(op1->r.n))
2644 op->info &= ~OF_CHECKED;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002645 op = op->a.n;
2646 } else {
2647 op = op->r.n;
2648 }
2649 } else {
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002650 op = ptest(op1) ? op->a.n : op->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002651 }
2652 break;
2653
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002654 /* just evaluate an expression, also used as unconditional jump */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002655 case XC( OC_EXEC ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002656 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002657
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002658 /* branch, used in if-else and various loops */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002659 case XC( OC_BR ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002660 op = istrue(L.v) ? op->a.n : op->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002661 break;
2662
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002663 /* initialize for-in loop */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002664 case XC( OC_WALKINIT ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002665 hashwalk_init(L.v, iamarray(R.v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002666 break;
2667
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002668 /* get next array item */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002669 case XC( OC_WALKNEXT ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002670 op = hashwalk_next(L.v) ? op->a.n : op->r.n;
2671 break;
2672
Denis Vlasenkof782f522007-01-01 23:51:30 +00002673 case XC( OC_PRINT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002674 case XC( OC_PRINTF ): {
2675 FILE *F = stdout;
Ron Yorstone8fe9f92021-01-27 11:19:14 +00002676 IF_FEATURE_AWK_GNU_EXTENSIONS(int len;)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002677
Mike Frysingerde2b9382005-09-27 03:18:00 +00002678 if (op->r.n) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002679 rstream *rsm = newfile(R.s);
2680 if (!rsm->F) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002681 if (opn == '|') {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002682 rsm->F = popen(R.s, "w");
2683 if (rsm->F == NULL)
James Byrne69374872019-07-02 11:35:03 +02002684 bb_simple_perror_msg_and_die("popen");
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002685 rsm->is_pipe = 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002686 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002687 rsm->F = xfopen(R.s, opn=='w' ? "w" : "a");
Glenn L McGrath545106f2002-11-11 06:21:00 +00002688 }
2689 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002690 F = rsm->F;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002691 }
2692
2693 if ((opinfo & OPCLSMASK) == OC_PRINT) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002694 if (!op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002695 fputs(getvar_s(intvar[F0]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002696 } else {
2697 while (op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002698 var *v = evaluate(nextarg(&op1), v1);
2699 if (v->type & VF_NUMBER) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002700 fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[OFMT]),
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002701 getvar_i(v), TRUE);
2702 fputs(g_buf, F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002703 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002704 fputs(getvar_s(v), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002705 }
2706
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002707 if (op1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002708 fputs(getvar_s(intvar[OFS]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002709 }
2710 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002711 fputs(getvar_s(intvar[ORS]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002712
2713 } else { /* OC_PRINTF */
Ron Yorstone8fe9f92021-01-27 11:19:14 +00002714 char *s = awk_printf(op1, &len);
2715#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
2716 fwrite(s, len, 1, F);
2717#else
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002718 fputs(s, F);
Ron Yorstone8fe9f92021-01-27 11:19:14 +00002719#endif
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002720 free(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002721 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002722 fflush(F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002723 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002724 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002725
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01002726 /* case XC( OC_DELETE ): - moved to happen before arg evaluation */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002727
Denis Vlasenkof782f522007-01-01 23:51:30 +00002728 case XC( OC_NEWSOURCE ):
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002729 g_progname = op->l.new_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002730 break;
2731
Denis Vlasenkof782f522007-01-01 23:51:30 +00002732 case XC( OC_RETURN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002733 copyvar(res, L.v);
2734 break;
2735
Denis Vlasenkof782f522007-01-01 23:51:30 +00002736 case XC( OC_NEXTFILE ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002737 nextfile = TRUE;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002738 case XC( OC_NEXT ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002739 nextrec = TRUE;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002740 case XC( OC_DONE ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002741 clrvar(res);
2742 break;
2743
Denis Vlasenkof782f522007-01-01 23:51:30 +00002744 case XC( OC_EXIT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002745 awk_exit(L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002746
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002747 /* -- recursive node type -- */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002748
Denis Vlasenkof782f522007-01-01 23:51:30 +00002749 case XC( OC_VAR ):
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01002750 debug_printf_eval("VAR\n");
Mike Frysingerde2b9382005-09-27 03:18:00 +00002751 L.v = op->l.v;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002752 if (L.v == intvar[NF])
Glenn L McGrath545106f2002-11-11 06:21:00 +00002753 split_f0();
2754 goto v_cont;
2755
Denis Vlasenkof782f522007-01-01 23:51:30 +00002756 case XC( OC_FNARG ):
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01002757 debug_printf_eval("FNARG[%d]\n", op->l.aidx);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002758 L.v = &fnargs[op->l.aidx];
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002759 v_cont:
2760 res = op->r.n ? findvar(iamarray(L.v), R.s) : L.v;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002761 break;
2762
Denis Vlasenkof782f522007-01-01 23:51:30 +00002763 case XC( OC_IN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002764 setvar_i(res, hash_search(iamarray(R.v), L.s) ? 1 : 0);
2765 break;
2766
Denis Vlasenkof782f522007-01-01 23:51:30 +00002767 case XC( OC_REGEXP ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002768 op1 = op;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002769 L.s = getvar_s(intvar[F0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002770 goto re_cont;
2771
Denis Vlasenkof782f522007-01-01 23:51:30 +00002772 case XC( OC_MATCH ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002773 op1 = op->r.n;
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002774 re_cont:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002775 {
2776 regex_t *re = as_regex(op1, &sreg);
2777 int i = regexec(re, L.s, 0, NULL, 0);
2778 if (re == &sreg)
2779 regfree(re);
2780 setvar_i(res, (i == 0) ^ (opn == '!'));
2781 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002782 break;
2783
Denis Vlasenkof782f522007-01-01 23:51:30 +00002784 case XC( OC_MOVE ):
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002785 debug_printf_eval("MOVE\n");
Mike Frysingerde2b9382005-09-27 03:18:00 +00002786 /* if source is a temporary string, jusk relink it to dest */
Denys Vlasenko12847742009-11-30 01:15:04 +01002787//Disabled: if R.v is numeric but happens to have cached R.v->string,
2788//then L.v ends up being a string, which is wrong
2789// if (R.v == v1+1 && R.v->string) {
2790// res = setvar_p(L.v, R.v->string);
2791// R.v->string = NULL;
2792// } else {
Mike Frysingerde2b9382005-09-27 03:18:00 +00002793 res = copyvar(L.v, R.v);
Denys Vlasenko12847742009-11-30 01:15:04 +01002794// }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002795 break;
2796
Denis Vlasenkof782f522007-01-01 23:51:30 +00002797 case XC( OC_TERNARY ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002798 if ((op->r.n->info & OPCLSMASK) != OC_COLON)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002799 syntax_error(EMSG_POSSIBLE_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002800 res = evaluate(istrue(L.v) ? op->r.n->l.n : op->r.n->r.n, res);
2801 break;
2802
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002803 case XC( OC_FUNC ): {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002804 var *vbeg, *v;
2805 const char *sv_progname;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002806
Bernhard Reutner-Fischerb79a0fe2013-03-06 21:01:05 +01002807 /* The body might be empty, still has to eval the args */
Bernhard Reutner-Fischera060a1a2013-07-31 15:29:20 +02002808 if (!op->r.n->info && !op->r.f->body.first)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002809 syntax_error(EMSG_UNDEF_FUNC);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002810
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002811 vbeg = v = nvalloc(op->r.f->nargs + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002812 while (op1) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002813 var *arg = evaluate(nextarg(&op1), v1);
2814 copyvar(v, arg);
2815 v->type |= VF_CHILD;
2816 v->x.parent = arg;
2817 if (++v - vbeg >= op->r.f->nargs)
Glenn L McGrath545106f2002-11-11 06:21:00 +00002818 break;
2819 }
2820
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002821 v = fnargs;
2822 fnargs = vbeg;
2823 sv_progname = g_progname;
2824
2825 res = evaluate(op->r.f->body.first, res);
2826
2827 g_progname = sv_progname;
2828 nvfree(fnargs);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002829 fnargs = v;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002830
Glenn L McGrath545106f2002-11-11 06:21:00 +00002831 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002832 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002833
Denis Vlasenkof782f522007-01-01 23:51:30 +00002834 case XC( OC_GETLINE ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002835 case XC( OC_PGETLINE ): {
2836 rstream *rsm;
2837 int i;
2838
Mike Frysingerde2b9382005-09-27 03:18:00 +00002839 if (op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002840 rsm = newfile(L.s);
2841 if (!rsm->F) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002842 if ((opinfo & OPCLSMASK) == OC_PGETLINE) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002843 rsm->F = popen(L.s, "r");
2844 rsm->is_pipe = TRUE;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002845 } else {
Denys Vlasenkofb132e42010-10-29 11:46:52 +02002846 rsm->F = fopen_for_read(L.s); /* not xfopen! */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002847 }
2848 }
2849 } else {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002850 if (!iF)
2851 iF = next_input_file();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002852 rsm = iF;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002853 }
2854
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02002855 if (!rsm || !rsm->F) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002856 setvar_i(intvar[ERRNO], errno);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002857 setvar_i(res, -1);
2858 break;
2859 }
2860
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002861 if (!op->r.n)
2862 R.v = intvar[F0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002863
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002864 i = awk_getline(rsm, R.v);
2865 if (i > 0 && !op1) {
2866 incvar(intvar[FNR]);
2867 incvar(intvar[NR]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002868 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002869 setvar_i(res, i);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002870 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002871 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002872
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002873 /* simple builtins */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002874 case XC( OC_FBLTIN ): {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002875 double R_d = R_d; /* for compiler */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002876
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002877 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002878 case F_in:
Denys Vlasenko1390a012013-07-20 21:23:01 +02002879 R_d = (long long)L_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002880 break;
2881
Denis Vlasenkof782f522007-01-01 23:51:30 +00002882 case F_rn:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002883 R_d = (double)rand() / (double)RAND_MAX;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002884 break;
Rob Landleyd8205b32010-10-24 03:27:22 +02002885
Denis Vlasenkof782f522007-01-01 23:51:30 +00002886 case F_co:
Rob Landleyd8205b32010-10-24 03:27:22 +02002887 if (ENABLE_FEATURE_AWK_LIBM) {
2888 R_d = cos(L_d);
2889 break;
2890 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002891
Denis Vlasenkof782f522007-01-01 23:51:30 +00002892 case F_ex:
Rob Landleyd8205b32010-10-24 03:27:22 +02002893 if (ENABLE_FEATURE_AWK_LIBM) {
2894 R_d = exp(L_d);
2895 break;
2896 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002897
Denis Vlasenkof782f522007-01-01 23:51:30 +00002898 case F_lg:
Rob Landleyd8205b32010-10-24 03:27:22 +02002899 if (ENABLE_FEATURE_AWK_LIBM) {
2900 R_d = log(L_d);
2901 break;
2902 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002903
Denis Vlasenkof782f522007-01-01 23:51:30 +00002904 case F_si:
Rob Landleyd8205b32010-10-24 03:27:22 +02002905 if (ENABLE_FEATURE_AWK_LIBM) {
2906 R_d = sin(L_d);
2907 break;
2908 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002909
Denis Vlasenkof782f522007-01-01 23:51:30 +00002910 case F_sq:
Rob Landleyd8205b32010-10-24 03:27:22 +02002911 if (ENABLE_FEATURE_AWK_LIBM) {
2912 R_d = sqrt(L_d);
2913 break;
2914 }
2915
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002916 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002917 break;
Rob Landleyd8205b32010-10-24 03:27:22 +02002918
Denis Vlasenkof782f522007-01-01 23:51:30 +00002919 case F_sr:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002920 R_d = (double)seed;
2921 seed = op1 ? (unsigned)L_d : (unsigned)time(NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002922 srand(seed);
2923 break;
2924
Denis Vlasenkof782f522007-01-01 23:51:30 +00002925 case F_ti:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002926 R_d = time(NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002927 break;
2928
Denis Vlasenkof782f522007-01-01 23:51:30 +00002929 case F_le:
Denys Vlasenko7985bc12013-10-12 04:51:54 +02002930 debug_printf_eval("length: L.s:'%s'\n", L.s);
2931 if (!op1) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002932 L.s = getvar_s(intvar[F0]);
Denys Vlasenko7985bc12013-10-12 04:51:54 +02002933 debug_printf_eval("length: L.s='%s'\n", L.s);
2934 }
2935 else if (L.v->type & VF_ARRAY) {
2936 R_d = L.v->x.array->nel;
2937 debug_printf_eval("length: array_len:%d\n", L.v->x.array->nel);
2938 break;
2939 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002940 R_d = strlen(L.s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002941 break;
2942
Denis Vlasenkof782f522007-01-01 23:51:30 +00002943 case F_sy:
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002944 fflush_all();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002945 R_d = (ENABLE_FEATURE_ALLOW_EXEC && L.s && *L.s)
Denis Vlasenko249fabf2006-12-19 00:29:22 +00002946 ? (system(L.s) >> 8) : 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002947 break;
2948
Denis Vlasenkof782f522007-01-01 23:51:30 +00002949 case F_ff:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002950 if (!op1) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002951 fflush(stdout);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002952 } else if (L.s && *L.s) {
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002953 rstream *rsm = newfile(L.s);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002954 fflush(rsm->F);
2955 } else {
2956 fflush_all();
Glenn L McGrath545106f2002-11-11 06:21:00 +00002957 }
2958 break;
2959
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002960 case F_cl: {
2961 rstream *rsm;
2962 int err = 0;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002963 rsm = (rstream *)hash_search(fdhash, L.s);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002964 debug_printf_eval("OC_FBLTIN F_cl rsm:%p\n", rsm);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002965 if (rsm) {
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002966 debug_printf_eval("OC_FBLTIN F_cl "
2967 "rsm->is_pipe:%d, ->F:%p\n",
2968 rsm->is_pipe, rsm->F);
2969 /* Can be NULL if open failed. Example:
2970 * getline line <"doesnt_exist";
2971 * close("doesnt_exist"); <--- here rsm->F is NULL
2972 */
2973 if (rsm->F)
2974 err = rsm->is_pipe ? pclose(rsm->F) : fclose(rsm->F);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002975 free(rsm->buffer);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002976 hash_remove(fdhash, L.s);
2977 }
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002978 if (err)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002979 setvar_i(intvar[ERRNO], errno);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002980 R_d = (double)err;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002981 break;
2982 }
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002983 } /* switch */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002984 setvar_i(res, R_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002985 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002986 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002987
Denis Vlasenkof782f522007-01-01 23:51:30 +00002988 case XC( OC_BUILTIN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002989 res = exec_builtin(op, res);
2990 break;
2991
Denis Vlasenkof782f522007-01-01 23:51:30 +00002992 case XC( OC_SPRINTF ):
Ron Yorstone8fe9f92021-01-27 11:19:14 +00002993 setvar_p(res, awk_printf(op1, NULL));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002994 break;
2995
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002996 case XC( OC_UNARY ): {
2997 double Ld, R_d;
2998
2999 Ld = R_d = getvar_i(R.v);
Mike Frysingerde2b9382005-09-27 03:18:00 +00003000 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00003001 case 'P':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003002 Ld = ++R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003003 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003004 case 'p':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003005 R_d++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003006 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003007 case 'M':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003008 Ld = --R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003009 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003010 case 'm':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003011 R_d--;
3012 r_op_change:
3013 setvar_i(R.v, R_d);
3014 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003015 case '!':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003016 Ld = !istrue(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003017 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003018 case '-':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003019 Ld = -R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003020 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003021 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003022 setvar_i(res, Ld);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003023 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003024 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003025
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003026 case XC( OC_FIELD ): {
3027 int i = (int)getvar_i(R.v);
Denys Vlasenko2454e672018-04-23 10:53:18 +02003028 if (i < 0)
3029 syntax_error(EMSG_NEGATIVE_FIELD);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003030 if (i == 0) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003031 res = intvar[F0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00003032 } else {
3033 split_f0();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003034 if (i > nfields)
3035 fsrealloc(i);
3036 res = &Fields[i - 1];
Glenn L McGrath545106f2002-11-11 06:21:00 +00003037 }
3038 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003039 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003040
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00003041 /* concatenation (" ") and index joining (",") */
Denis Vlasenkof782f522007-01-01 23:51:30 +00003042 case XC( OC_CONCAT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003043 case XC( OC_COMMA ): {
3044 const char *sep = "";
3045 if ((opinfo & OPCLSMASK) == OC_COMMA)
3046 sep = getvar_s(intvar[SUBSEP]);
3047 setvar_p(res, xasprintf("%s%s%s", L.s, sep, R.s));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003048 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003049 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003050
Denis Vlasenkof782f522007-01-01 23:51:30 +00003051 case XC( OC_LAND ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00003052 setvar_i(res, istrue(L.v) ? ptest(op->r.n) : 0);
3053 break;
3054
Denis Vlasenkof782f522007-01-01 23:51:30 +00003055 case XC( OC_LOR ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00003056 setvar_i(res, istrue(L.v) ? 1 : ptest(op->r.n));
3057 break;
3058
Denis Vlasenkof782f522007-01-01 23:51:30 +00003059 case XC( OC_BINARY ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003060 case XC( OC_REPLACE ): {
3061 double R_d = getvar_i(R.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02003062 debug_printf_eval("BINARY/REPLACE: R_d:%f opn:%c\n", R_d, opn);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003063 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00003064 case '+':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003065 L_d += R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003066 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003067 case '-':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003068 L_d -= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003069 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003070 case '*':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003071 L_d *= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003072 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003073 case '/':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003074 if (R_d == 0)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003075 syntax_error(EMSG_DIV_BY_ZERO);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003076 L_d /= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003077 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003078 case '&':
Rob Landleyd8205b32010-10-24 03:27:22 +02003079 if (ENABLE_FEATURE_AWK_LIBM)
3080 L_d = pow(L_d, R_d);
3081 else
3082 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003083 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003084 case '%':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003085 if (R_d == 0)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003086 syntax_error(EMSG_DIV_BY_ZERO);
Denys Vlasenko1390a012013-07-20 21:23:01 +02003087 L_d -= (long long)(L_d / R_d) * R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003088 break;
3089 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02003090 debug_printf_eval("BINARY/REPLACE result:%f\n", L_d);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003091 res = setvar_i(((opinfo & OPCLSMASK) == OC_BINARY) ? res : L.v, L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003092 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003093 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003094
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003095 case XC( OC_COMPARE ): {
3096 int i = i; /* for compiler */
3097 double Ld;
3098
Glenn L McGrath545106f2002-11-11 06:21:00 +00003099 if (is_numeric(L.v) && is_numeric(R.v)) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003100 Ld = getvar_i(L.v) - getvar_i(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003101 } else {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01003102 const char *l = getvar_s(L.v);
3103 const char *r = getvar_s(R.v);
3104 Ld = icase ? strcasecmp(l, r) : strcmp(l, r);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003105 }
3106 switch (opn & 0xfe) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00003107 case 0:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003108 i = (Ld > 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003109 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003110 case 2:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003111 i = (Ld >= 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003112 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003113 case 4:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003114 i = (Ld == 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003115 break;
3116 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003117 setvar_i(res, (i == 0) ^ (opn & 1));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003118 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003119 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003120
Denis Vlasenkof782f522007-01-01 23:51:30 +00003121 default:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003122 syntax_error(EMSG_POSSIBLE_ERROR);
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01003123 } /* switch */
3124 next:
Glenn L McGrath545106f2002-11-11 06:21:00 +00003125 if ((opinfo & OPCLSMASK) <= SHIFT_TIL_THIS)
3126 op = op->a.n;
3127 if ((opinfo & OPCLSMASK) >= RECUR_FROM_THIS)
3128 break;
3129 if (nextrec)
3130 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003131 } /* while (op) */
3132
Glenn L McGrath545106f2002-11-11 06:21:00 +00003133 nvfree(v1);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003134 debug_printf_eval("returning from %s(): %p\n", __func__, res);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003135 return res;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003136#undef fnargs
3137#undef seed
3138#undef sreg
Glenn L McGrath545106f2002-11-11 06:21:00 +00003139}
3140
3141
3142/* -------- main & co. -------- */
3143
Mike Frysinger10a11e22005-09-27 02:23:02 +00003144static int awk_exit(int r)
3145{
Denis Vlasenkof782f522007-01-01 23:51:30 +00003146 var tv;
3147 unsigned i;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003148 hash_item *hi;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003149
Denis Vlasenkof782f522007-01-01 23:51:30 +00003150 zero_out_var(&tv);
3151
3152 if (!exiting) {
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003153 exiting = TRUE;
Glenn L McGrathca29ffc2004-09-24 09:24:27 +00003154 nextrec = FALSE;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003155 evaluate(endseq.first, &tv);
3156 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003157
3158 /* waiting for children */
Denis Vlasenkof782f522007-01-01 23:51:30 +00003159 for (i = 0; i < fdhash->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003160 hi = fdhash->items[i];
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00003161 while (hi) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003162 if (hi->data.rs.F && hi->data.rs.is_pipe)
3163 pclose(hi->data.rs.F);
3164 hi = hi->next;
3165 }
3166 }
3167
3168 exit(r);
3169}
3170
3171/* if expr looks like "var=value", perform assignment and return 1,
3172 * otherwise return 0 */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00003173static int is_assignment(const char *expr)
Mike Frysinger10a11e22005-09-27 02:23:02 +00003174{
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003175 char *exprc, *val;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003176
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003177 if (!isalnum_(*expr) || (val = strchr(expr, '=')) == NULL) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003178 return FALSE;
3179 }
3180
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003181 exprc = xstrdup(expr);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003182 val = exprc + (val - expr);
3183 *val++ = '\0';
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003184
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003185 unescape_string_in_place(val);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003186 setvar_u(newvar(exprc), val);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003187 free(exprc);
3188 return TRUE;
3189}
3190
3191/* switch to next input file */
Mike Frysinger10a11e22005-09-27 02:23:02 +00003192static rstream *next_input_file(void)
3193{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003194#define rsm (G.next_input_file__rsm)
3195#define files_happen (G.next_input_file__files_happen)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003196
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003197 FILE *F;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00003198 const char *fname, *ind;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003199
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003200 if (rsm.F)
3201 fclose(rsm.F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003202 rsm.F = NULL;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003203 rsm.pos = rsm.adv = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003204
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003205 for (;;) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003206 if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003207 if (files_happen)
3208 return NULL;
3209 fname = "-";
3210 F = stdin;
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003211 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003212 }
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003213 ind = getvar_s(incvar(intvar[ARGIND]));
3214 fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind));
3215 if (fname && *fname && !is_assignment(fname)) {
3216 F = xfopen_stdin(fname);
3217 break;
3218 }
3219 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003220
3221 files_happen = TRUE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003222 setvar_s(intvar[FILENAME], fname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003223 rsm.F = F;
3224 return &rsm;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003225#undef rsm
3226#undef files_happen
Glenn L McGrath545106f2002-11-11 06:21:00 +00003227}
3228
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00003229int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko844a6c52018-01-07 14:39:34 +01003230int awk_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger10a11e22005-09-27 02:23:02 +00003231{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00003232 unsigned opt;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02003233 char *opt_F;
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003234 llist_t *list_v = NULL;
3235 llist_t *list_f = NULL;
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003236#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
3237 llist_t *list_e = NULL;
3238#endif
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003239 int i, j;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003240 var *v;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003241 var tv;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003242 char **envp;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003243 char *vnames = (char *)vNames; /* cheat */
3244 char *vvalues = (char *)vValues;
3245
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003246 INIT_G();
3247
Denis Vlasenko150f4022007-01-13 21:06:21 +00003248 /* Undo busybox.c, or else strtod may eat ','! This breaks parsing:
Denis Vlasenko6dc6ebb2007-01-01 23:53:12 +00003249 * $1,$2 == '$1,' '$2', NOT '$1' ',' '$2' */
3250 if (ENABLE_LOCALE_SUPPORT)
3251 setlocale(LC_NUMERIC, "C");
3252
Denis Vlasenkof782f522007-01-01 23:51:30 +00003253 zero_out_var(&tv);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003254
3255 /* allocate global buffer */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003256 g_buf = xmalloc(MAXVARFMT + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003257
3258 vhash = hash_init();
3259 ahash = hash_init();
3260 fdhash = hash_init();
3261 fnhash = hash_init();
3262
3263 /* initialize variables */
Denis Vlasenkof782f522007-01-01 23:51:30 +00003264 for (i = 0; *vnames; i++) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003265 intvar[i] = v = newvar(nextword(&vnames));
Denis Vlasenkof782f522007-01-01 23:51:30 +00003266 if (*vvalues != '\377')
3267 setvar_s(v, nextword(&vvalues));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003268 else
3269 setvar_i(v, 0);
3270
Denis Vlasenkof782f522007-01-01 23:51:30 +00003271 if (*vnames == '*') {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003272 v->type |= VF_SPECIAL;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003273 vnames++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003274 }
3275 }
3276
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003277 handle_special(intvar[FS]);
3278 handle_special(intvar[RS]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003279
Denis Vlasenkof782f522007-01-01 23:51:30 +00003280 newfile("/dev/stdin")->F = stdin;
3281 newfile("/dev/stdout")->F = stdout;
3282 newfile("/dev/stderr")->F = stderr;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003283
Denis Vlasenkof71d9162007-05-03 22:57:56 +00003284 /* Huh, people report that sometimes environ is NULL. Oh well. */
3285 if (environ) for (envp = environ; *envp; envp++) {
Denis Vlasenkob78c7822007-07-18 18:31:11 +00003286 /* environ is writable, thus we don't strdup it needlessly */
3287 char *s = *envp;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003288 char *s1 = strchr(s, '=');
3289 if (s1) {
Denis Vlasenkob78c7822007-07-18 18:31:11 +00003290 *s1 = '\0';
3291 /* Both findvar and setvar_u take const char*
3292 * as 2nd arg -> environment is not trashed */
3293 setvar_u(findvar(iamarray(intvar[ENVIRON]), s), s1 + 1);
3294 *s1 = '=';
Eric Andersen67776be2004-07-30 23:52:08 +00003295 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003296 }
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003297 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 +00003298 argv += optind;
Denys Vlasenko844a6c52018-01-07 14:39:34 +01003299 //argc -= optind;
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003300 if (opt & OPT_W)
James Byrne69374872019-07-02 11:35:03 +02003301 bb_simple_error_msg("warning: option -W is ignored");
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003302 if (opt & OPT_F) {
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003303 unescape_string_in_place(opt_F);
3304 setvar_s(intvar[FS], opt_F);
3305 }
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003306 while (list_v) {
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003307 if (!is_assignment(llist_pop(&list_v)))
Denis Vlasenkobe644a82007-03-10 17:22:14 +00003308 bb_show_usage();
3309 }
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003310 while (list_f) {
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003311 char *s = NULL;
3312 FILE *from_file;
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003313
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003314 g_progname = llist_pop(&list_f);
3315 from_file = xfopen_stdin(g_progname);
3316 /* one byte is reserved for some trick in next_token */
3317 for (i = j = 1; j > 0; i += j) {
3318 s = xrealloc(s, i + 4096);
3319 j = fread(s + i, 1, 4094, from_file);
3320 }
3321 s[i] = '\0';
3322 fclose(from_file);
3323 parse_program(s + 1);
3324 free(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003325 }
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003326 g_progname = "cmd. line";
3327#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003328 while (list_e) {
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003329 parse_program(llist_pop(&list_e));
3330 }
3331#endif
3332 if (!(opt & (OPT_f | OPT_e))) {
3333 if (!*argv)
3334 bb_show_usage();
3335 parse_program(*argv++);
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003336 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003337
Glenn L McGrath545106f2002-11-11 06:21:00 +00003338 /* fill in ARGV array */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003339 setari_u(intvar[ARGV], 0, "awk");
Denis Vlasenkof782f522007-01-01 23:51:30 +00003340 i = 0;
3341 while (*argv)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003342 setari_u(intvar[ARGV], ++i, *argv++);
Denys Vlasenko844a6c52018-01-07 14:39:34 +01003343 setvar_i(intvar[ARGC], i + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003344
3345 evaluate(beginseq.first, &tv);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003346 if (!mainseq.first && !endseq.first)
Glenn L McGrath545106f2002-11-11 06:21:00 +00003347 awk_exit(EXIT_SUCCESS);
3348
3349 /* input file could already be opened in BEGIN block */
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003350 if (!iF)
3351 iF = next_input_file();
Glenn L McGrath545106f2002-11-11 06:21:00 +00003352
3353 /* passing through input files */
3354 while (iF) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003355 nextfile = FALSE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003356 setvar_i(intvar[FNR], 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003357
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003358 while ((i = awk_getline(iF, intvar[F0])) > 0) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003359 nextrec = FALSE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003360 incvar(intvar[NR]);
3361 incvar(intvar[FNR]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003362 evaluate(mainseq.first, &tv);
3363
3364 if (nextfile)
3365 break;
3366 }
3367
Denis Vlasenkof782f522007-01-01 23:51:30 +00003368 if (i < 0)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003369 syntax_error(strerror(errno));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003370
3371 iF = next_input_file();
Glenn L McGrath545106f2002-11-11 06:21:00 +00003372 }
3373
Glenn L McGrath545106f2002-11-11 06:21:00 +00003374 awk_exit(EXIT_SUCCESS);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003375 /*return 0;*/
Glenn L McGrath545106f2002-11-11 06:21:00 +00003376}