blob: d54249bfd1894c211054c4e01acc693125f0f92f [file] [log] [blame]
Glenn L McGrath545106f2002-11-11 06:21:00 +00001/* vi: set sw=4 ts=4: */
2/*
3 * awk implementation for busybox
4 *
5 * Copyright (C) 2002 by Dmitry Zakharov <dmit@crp.bank.gov.ua>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath545106f2002-11-11 06:21:00 +00008 */
Sven-Göran Berghf200f732013-11-12 14:18:25 +01009//config:config AWK
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "awk (22 kb)"
Sven-Göran Berghf200f732013-11-12 14:18:25 +010011//config: default y
12//config: help
Denys Vlasenko68b653b2017-07-27 10:53:09 +020013//config: Awk is used as a pattern scanning and processing language.
Sven-Göran Berghf200f732013-11-12 14:18:25 +010014//config:
15//config:config FEATURE_AWK_LIBM
16//config: bool "Enable math functions (requires libm)"
17//config: default y
18//config: depends on AWK
19//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020020//config: Enable math functions of the Awk programming language.
21//config: NOTE: This requires libm to be present for linking.
Sven-Göran Berghf200f732013-11-12 14:18:25 +010022//config:
23//config:config FEATURE_AWK_GNU_EXTENSIONS
24//config: bool "Enable a few GNU extensions"
25//config: default y
26//config: depends on AWK
27//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020028//config: Enable a few features from gawk:
29//config: * command line option -e AWK_PROGRAM
30//config: * simultaneous use of -f and -e on the command line.
31//config: This enables the use of awk library files.
32//config: Example: awk -f mylib.awk -e '{print myfunction($1);}' ...
Sven-Göran Berghf200f732013-11-12 14:18:25 +010033
34//applet:IF_AWK(APPLET_NOEXEC(awk, awk, BB_DIR_USR_BIN, BB_SUID_DROP, awk))
35
36//kbuild:lib-$(CONFIG_AWK) += awk.o
37
Pere Orga6a3e01d2011-04-01 22:56:30 +020038//usage:#define awk_trivial_usage
39//usage: "[OPTIONS] [AWK_PROGRAM] [FILE]..."
40//usage:#define awk_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020041//usage: " -v VAR=VAL Set variable"
Pere Orga6a3e01d2011-04-01 22:56:30 +020042//usage: "\n -F SEP Use SEP as field separator"
43//usage: "\n -f FILE Read program from FILE"
Sven-Göran Berghf200f732013-11-12 14:18:25 +010044//usage: IF_FEATURE_AWK_GNU_EXTENSIONS(
45//usage: "\n -e AWK_PROGRAM"
46//usage: )
Pere Orga6a3e01d2011-04-01 22:56:30 +020047
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000048#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000049#include "xregex.h"
50#include <math.h>
Glenn L McGrath545106f2002-11-11 06:21:00 +000051
Denis Vlasenko99912ca2007-04-10 15:43:37 +000052/* This is a NOEXEC applet. Be very careful! */
53
Glenn L McGrath545106f2002-11-11 06:21:00 +000054
Denys Vlasenkoda62b092010-03-11 12:13:18 +010055/* If you comment out one of these below, it will be #defined later
56 * to perform debug printfs to stderr: */
57#define debug_printf_walker(...) do {} while (0)
Denys Vlasenkod527e0c2010-10-05 13:22:11 +020058#define debug_printf_eval(...) do {} while (0)
Denys Vlasenko7b46d112011-09-11 00:30:56 +020059#define debug_printf_parse(...) do {} while (0)
Denys Vlasenkoda62b092010-03-11 12:13:18 +010060
61#ifndef debug_printf_walker
62# define debug_printf_walker(...) (fprintf(stderr, __VA_ARGS__))
63#endif
Denys Vlasenkod527e0c2010-10-05 13:22:11 +020064#ifndef debug_printf_eval
65# define debug_printf_eval(...) (fprintf(stderr, __VA_ARGS__))
66#endif
Denys Vlasenko7b46d112011-09-11 00:30:56 +020067#ifndef debug_printf_parse
68# define debug_printf_parse(...) (fprintf(stderr, __VA_ARGS__))
69#endif
Denys Vlasenkoda62b092010-03-11 12:13:18 +010070
71
Denys Vlasenkoa1799db2017-08-16 19:07:53 +020072/* "+": stop on first non-option:
73 * $ awk 'BEGIN { for(i=1; i<ARGC; ++i) { print i ": " ARGV[i] }}' -argz
74 * 1: -argz
75 */
76#define OPTSTR_AWK "+" \
Denys Vlasenko237bedd2016-07-06 21:58:02 +020077 "F:v:*f:*" \
78 IF_FEATURE_AWK_GNU_EXTENSIONS("e:*") \
Sven-Göran Berghf200f732013-11-12 14:18:25 +010079 "W:"
Sven-Göran Berghf200f732013-11-12 14:18:25 +010080enum {
81 OPTBIT_F, /* define field separator */
82 OPTBIT_v, /* define variable */
83 OPTBIT_f, /* pull in awk program from file */
84 IF_FEATURE_AWK_GNU_EXTENSIONS(OPTBIT_e,) /* -e AWK_PROGRAM */
85 OPTBIT_W, /* -W ignored */
86 OPT_F = 1 << OPTBIT_F,
87 OPT_v = 1 << OPTBIT_v,
88 OPT_f = 1 << OPTBIT_f,
89 OPT_e = IF_FEATURE_AWK_GNU_EXTENSIONS((1 << OPTBIT_e)) + 0,
90 OPT_W = 1 << OPTBIT_W
91};
Denys Vlasenkoda62b092010-03-11 12:13:18 +010092
Denis Vlasenko629563b2007-02-24 17:05:52 +000093#define MAXVARFMT 240
94#define MINNVBLOCK 64
Glenn L McGrath545106f2002-11-11 06:21:00 +000095
96/* variable flags */
Denis Vlasenko629563b2007-02-24 17:05:52 +000097#define VF_NUMBER 0x0001 /* 1 = primary type is number */
98#define VF_ARRAY 0x0002 /* 1 = it's an array */
Glenn L McGrath545106f2002-11-11 06:21:00 +000099
Denis Vlasenko629563b2007-02-24 17:05:52 +0000100#define VF_CACHED 0x0100 /* 1 = num/str value has cached str/num eq */
101#define VF_USER 0x0200 /* 1 = user input (may be numeric string) */
102#define VF_SPECIAL 0x0400 /* 1 = requires extra handling when changed */
103#define VF_WALK 0x0800 /* 1 = variable has alloc'd x.walker list */
104#define VF_FSTR 0x1000 /* 1 = var::string points to fstring buffer */
105#define VF_CHILD 0x2000 /* 1 = function arg; x.parent points to source */
106#define VF_DIRTY 0x4000 /* 1 = variable was set explicitly */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000107
108/* these flags are static, don't change them when value is changed */
Denis Vlasenko629563b2007-02-24 17:05:52 +0000109#define VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000110
Denys Vlasenkoda62b092010-03-11 12:13:18 +0100111typedef struct walker_list {
112 char *end;
113 char *cur;
114 struct walker_list *prev;
115 char wbuf[1];
116} walker_list;
117
Glenn L McGrath545106f2002-11-11 06:21:00 +0000118/* Variable */
119typedef struct var_s {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000120 unsigned type; /* flags */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000121 double number;
122 char *string;
123 union {
Denis Vlasenko629563b2007-02-24 17:05:52 +0000124 int aidx; /* func arg idx (for compilation stage) */
125 struct xhash_s *array; /* array ptr */
126 struct var_s *parent; /* for func args, ptr to actual parameter */
Denys Vlasenkoda62b092010-03-11 12:13:18 +0100127 walker_list *walker; /* list of array elements (for..in) */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000128 } x;
129} var;
130
131/* Node chain (pattern-action chain, BEGIN, END, function bodies) */
132typedef struct chain_s {
133 struct node_s *first;
134 struct node_s *last;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000135 const char *programname;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000136} chain;
137
138/* Function */
139typedef struct func_s {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000140 unsigned nargs;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000141 struct chain_s body;
142} func;
143
144/* I/O stream */
145typedef struct rstream_s {
146 FILE *F;
147 char *buffer;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +0000148 int adv;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000149 int size;
150 int pos;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000151 smallint is_pipe;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000152} rstream;
153
154typedef struct hash_item_s {
155 union {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000156 struct var_s v; /* variable/array hash */
157 struct rstream_s rs; /* redirect streams hash */
158 struct func_s f; /* functions hash */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000159 } data;
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000160 struct hash_item_s *next; /* next in chain */
161 char name[1]; /* really it's longer */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000162} hash_item;
163
164typedef struct xhash_s {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000165 unsigned nel; /* num of elements */
166 unsigned csize; /* current hash size */
167 unsigned nprime; /* next hash size in PRIMES[] */
168 unsigned glen; /* summary length of item names */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000169 struct hash_item_s **items;
170} xhash;
171
172/* Tree node */
173typedef struct node_s {
Mike Frysingerf87b3e32005-09-27 04:16:22 +0000174 uint32_t info;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000175 unsigned lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000176 union {
177 struct node_s *n;
178 var *v;
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100179 int aidx;
180 char *new_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000181 regex_t *re;
182 } l;
183 union {
184 struct node_s *n;
185 regex_t *ire;
186 func *f;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000187 } r;
188 union {
189 struct node_s *n;
190 } a;
191} node;
192
193/* Block of temporary variables */
194typedef struct nvblock_s {
195 int size;
196 var *pos;
197 struct nvblock_s *prev;
198 struct nvblock_s *next;
Denys Vlasenkod069e532009-09-09 23:12:10 +0200199 var nv[];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000200} nvblock;
201
202typedef struct tsplitter_s {
203 node n;
204 regex_t re[2];
205} tsplitter;
206
207/* simple token classes */
208/* Order and hex values are very important!!! See next_token() */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200209#define TC_SEQSTART (1 << 0) /* ( */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000210#define TC_SEQTERM (1 << 1) /* ) */
211#define TC_REGEXP (1 << 2) /* /.../ */
212#define TC_OUTRDR (1 << 3) /* | > >> */
213#define TC_UOPPOST (1 << 4) /* unary postfix operator */
214#define TC_UOPPRE1 (1 << 5) /* unary prefix operator */
215#define TC_BINOPX (1 << 6) /* two-opnd operator */
216#define TC_IN (1 << 7)
217#define TC_COMMA (1 << 8)
218#define TC_PIPE (1 << 9) /* input redirection pipe */
219#define TC_UOPPRE2 (1 << 10) /* unary prefix operator */
220#define TC_ARRTERM (1 << 11) /* ] */
221#define TC_GRPSTART (1 << 12) /* { */
222#define TC_GRPTERM (1 << 13) /* } */
223#define TC_SEMICOL (1 << 14)
224#define TC_NEWLINE (1 << 15)
225#define TC_STATX (1 << 16) /* ctl statement (for, next...) */
226#define TC_WHILE (1 << 17)
227#define TC_ELSE (1 << 18)
228#define TC_BUILTIN (1 << 19)
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200229/* This costs ~50 bytes of code.
230 * A separate class to support deprecated "length" form. If we don't need that
231 * (i.e. if we demand that only "length()" with () is valid), then TC_LENGTH
232 * can be merged with TC_BUILTIN:
233 */
234#define TC_LENGTH (1 << 20)
235#define TC_GETLINE (1 << 21)
Denys Vlasenko95f79532017-08-02 14:26:33 +0200236#define TC_FUNCDECL (1 << 22) /* 'function' 'func' */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200237#define TC_BEGIN (1 << 23)
238#define TC_END (1 << 24)
239#define TC_EOF (1 << 25)
240#define TC_VARIABLE (1 << 26)
241#define TC_ARRAY (1 << 27)
242#define TC_FUNCTION (1 << 28)
243#define TC_STRING (1 << 29)
244#define TC_NUMBER (1 << 30)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000245
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000246#define TC_UOPPRE (TC_UOPPRE1 | TC_UOPPRE2)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000247
248/* combined token classes */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000249#define TC_BINOP (TC_BINOPX | TC_COMMA | TC_PIPE | TC_IN)
Denys Vlasenko1390a012013-07-20 21:23:01 +0200250//#define TC_UNARYOP (TC_UOPPRE | TC_UOPPOST)
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000251#define TC_OPERAND (TC_VARIABLE | TC_ARRAY | TC_FUNCTION \
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200252 | TC_BUILTIN | TC_LENGTH | TC_GETLINE \
253 | TC_SEQSTART | TC_STRING | TC_NUMBER)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000254
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000255#define TC_STATEMNT (TC_STATX | TC_WHILE)
256#define TC_OPTERM (TC_SEMICOL | TC_NEWLINE)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000257
258/* word tokens, cannot mean something else if not expected */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200259#define TC_WORD (TC_IN | TC_STATEMNT | TC_ELSE \
260 | TC_BUILTIN | TC_LENGTH | TC_GETLINE \
261 | TC_FUNCDECL | TC_BEGIN | TC_END)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000262
263/* discard newlines after these */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000264#define TC_NOTERM (TC_COMMA | TC_GRPSTART | TC_GRPTERM \
265 | TC_BINOP | TC_OPTERM)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000266
267/* what can expression begin with */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000268#define TC_OPSEQ (TC_OPERAND | TC_UOPPRE | TC_REGEXP)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000269/* what can group begin with */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000270#define TC_GRPSEQ (TC_OPSEQ | TC_OPTERM | TC_STATEMNT | TC_GRPSTART)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000271
272/* if previous token class is CONCAT1 and next is CONCAT2, concatenation */
273/* operator is inserted between them */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000274#define TC_CONCAT1 (TC_VARIABLE | TC_ARRTERM | TC_SEQTERM \
275 | TC_STRING | TC_NUMBER | TC_UOPPOST)
276#define TC_CONCAT2 (TC_OPERAND | TC_UOPPRE)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000277
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000278#define OF_RES1 0x010000
279#define OF_RES2 0x020000
280#define OF_STR1 0x040000
281#define OF_STR2 0x080000
282#define OF_NUM1 0x100000
283#define OF_CHECKED 0x200000
Glenn L McGrath545106f2002-11-11 06:21:00 +0000284
285/* combined operator flags */
286#define xx 0
287#define xV OF_RES2
288#define xS (OF_RES2 | OF_STR2)
289#define Vx OF_RES1
290#define VV (OF_RES1 | OF_RES2)
291#define Nx (OF_RES1 | OF_NUM1)
292#define NV (OF_RES1 | OF_NUM1 | OF_RES2)
293#define Sx (OF_RES1 | OF_STR1)
294#define SV (OF_RES1 | OF_STR1 | OF_RES2)
295#define SS (OF_RES1 | OF_STR1 | OF_RES2 | OF_STR2)
296
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000297#define OPCLSMASK 0xFF00
298#define OPNMASK 0x007F
Glenn L McGrath545106f2002-11-11 06:21:00 +0000299
300/* operator priority is a highest byte (even: r->l, odd: l->r grouping)
301 * For builtins it has different meaning: n n s3 s2 s1 v3 v2 v1,
302 * n - min. number of args, vN - resolve Nth arg to var, sN - resolve to string
303 */
Denys Vlasenko202a1b92011-09-10 04:51:09 +0200304#undef P
305#undef PRIMASK
306#undef PRIMASK2
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000307#define P(x) (x << 24)
308#define PRIMASK 0x7F000000
309#define PRIMASK2 0x7E000000
Glenn L McGrath545106f2002-11-11 06:21:00 +0000310
311/* Operation classes */
312
313#define SHIFT_TIL_THIS 0x0600
314#define RECUR_FROM_THIS 0x1000
315
316enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000317 OC_DELETE = 0x0100, OC_EXEC = 0x0200, OC_NEWSOURCE = 0x0300,
318 OC_PRINT = 0x0400, OC_PRINTF = 0x0500, OC_WALKINIT = 0x0600,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000319
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000320 OC_BR = 0x0700, OC_BREAK = 0x0800, OC_CONTINUE = 0x0900,
321 OC_EXIT = 0x0a00, OC_NEXT = 0x0b00, OC_NEXTFILE = 0x0c00,
322 OC_TEST = 0x0d00, OC_WALKNEXT = 0x0e00,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000323
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000324 OC_BINARY = 0x1000, OC_BUILTIN = 0x1100, OC_COLON = 0x1200,
325 OC_COMMA = 0x1300, OC_COMPARE = 0x1400, OC_CONCAT = 0x1500,
326 OC_FBLTIN = 0x1600, OC_FIELD = 0x1700, OC_FNARG = 0x1800,
327 OC_FUNC = 0x1900, OC_GETLINE = 0x1a00, OC_IN = 0x1b00,
328 OC_LAND = 0x1c00, OC_LOR = 0x1d00, OC_MATCH = 0x1e00,
329 OC_MOVE = 0x1f00, OC_PGETLINE = 0x2000, OC_REGEXP = 0x2100,
330 OC_REPLACE = 0x2200, OC_RETURN = 0x2300, OC_SPRINTF = 0x2400,
331 OC_TERNARY = 0x2500, OC_UNARY = 0x2600, OC_VAR = 0x2700,
332 OC_DONE = 0x2800,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000333
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000334 ST_IF = 0x3000, ST_DO = 0x3100, ST_FOR = 0x3200,
335 ST_WHILE = 0x3300
Glenn L McGrath545106f2002-11-11 06:21:00 +0000336};
337
338/* simple builtins */
339enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000340 F_in, F_rn, F_co, F_ex, F_lg, F_si, F_sq, F_sr,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000341 F_ti, F_le, F_sy, F_ff, F_cl
342};
343
344/* builtins */
345enum {
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +0200346 B_a2, B_ix, B_ma, B_sp, B_ss, B_ti, B_mt, B_lo, B_up,
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000347 B_ge, B_gs, B_su,
348 B_an, B_co, B_ls, B_or, B_rs, B_xo,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000349};
350
351/* tokens and their corresponding info values */
352
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200353#define NTC "\377" /* switch to next token class (tc<<1) */
354#define NTCC '\377'
Glenn L McGrath545106f2002-11-11 06:21:00 +0000355
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000356static const char tokenlist[] ALIGN1 =
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200357 "\1(" NTC /* TC_SEQSTART */
358 "\1)" NTC /* TC_SEQTERM */
359 "\1/" NTC /* TC_REGEXP */
360 "\2>>" "\1>" "\1|" NTC /* TC_OUTRDR */
361 "\2++" "\2--" NTC /* TC_UOPPOST */
362 "\2++" "\2--" "\1$" NTC /* TC_UOPPRE1 */
363 "\2==" "\1=" "\2+=" "\2-=" /* TC_BINOPX */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200364 "\2*=" "\2/=" "\2%=" "\2^="
365 "\1+" "\1-" "\3**=" "\2**"
366 "\1/" "\1%" "\1^" "\1*"
367 "\2!=" "\2>=" "\2<=" "\1>"
368 "\1<" "\2!~" "\1~" "\2&&"
369 "\2||" "\1?" "\1:" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200370 "\2in" NTC /* TC_IN */
371 "\1," NTC /* TC_COMMA */
372 "\1|" NTC /* TC_PIPE */
373 "\1+" "\1-" "\1!" NTC /* TC_UOPPRE2 */
374 "\1]" NTC /* TC_ARRTERM */
375 "\1{" NTC /* TC_GRPSTART */
376 "\1}" NTC /* TC_GRPTERM */
377 "\1;" NTC /* TC_SEMICOL */
378 "\1\n" NTC /* TC_NEWLINE */
379 "\2if" "\2do" "\3for" "\5break" /* TC_STATX */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200380 "\10continue" "\6delete" "\5print"
381 "\6printf" "\4next" "\10nextfile"
382 "\6return" "\4exit" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200383 "\5while" NTC /* TC_WHILE */
384 "\4else" NTC /* TC_ELSE */
385 "\3and" "\5compl" "\6lshift" "\2or" /* TC_BUILTIN */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200386 "\6rshift" "\3xor"
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200387 "\5close" "\6system" "\6fflush" "\5atan2"
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200388 "\3cos" "\3exp" "\3int" "\3log"
389 "\4rand" "\3sin" "\4sqrt" "\5srand"
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200390 "\6gensub" "\4gsub" "\5index" /* "\6length" was here */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200391 "\5match" "\5split" "\7sprintf" "\3sub"
392 "\6substr" "\7systime" "\10strftime" "\6mktime"
393 "\7tolower" "\7toupper" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200394 "\6length" NTC /* TC_LENGTH */
395 "\7getline" NTC /* TC_GETLINE */
396 "\4func" "\10function" NTC /* TC_FUNCDECL */
397 "\5BEGIN" NTC /* TC_BEGIN */
398 "\3END" /* TC_END */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200399 /* compiler adds trailing "\0" */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000400 ;
401
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200402#define OC_B OC_BUILTIN
403
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000404static const uint32_t tokeninfo[] = {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000405 0,
406 0,
407 OC_REGEXP,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200408 xS|'a', xS|'w', xS|'|',
409 OC_UNARY|xV|P(9)|'p', OC_UNARY|xV|P(9)|'m',
410 OC_UNARY|xV|P(9)|'P', OC_UNARY|xV|P(9)|'M', OC_FIELD|xV|P(5),
411 OC_COMPARE|VV|P(39)|5, OC_MOVE|VV|P(74), OC_REPLACE|NV|P(74)|'+', OC_REPLACE|NV|P(74)|'-',
412 OC_REPLACE|NV|P(74)|'*', OC_REPLACE|NV|P(74)|'/', OC_REPLACE|NV|P(74)|'%', OC_REPLACE|NV|P(74)|'&',
413 OC_BINARY|NV|P(29)|'+', OC_BINARY|NV|P(29)|'-', OC_REPLACE|NV|P(74)|'&', OC_BINARY|NV|P(15)|'&',
414 OC_BINARY|NV|P(25)|'/', OC_BINARY|NV|P(25)|'%', OC_BINARY|NV|P(15)|'&', OC_BINARY|NV|P(25)|'*',
415 OC_COMPARE|VV|P(39)|4, OC_COMPARE|VV|P(39)|3, OC_COMPARE|VV|P(39)|0, OC_COMPARE|VV|P(39)|1,
416 OC_COMPARE|VV|P(39)|2, OC_MATCH|Sx|P(45)|'!', OC_MATCH|Sx|P(45)|'~', OC_LAND|Vx|P(55),
417 OC_LOR|Vx|P(59), OC_TERNARY|Vx|P(64)|'?', OC_COLON|xx|P(67)|':',
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200418 OC_IN|SV|P(49), /* TC_IN */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000419 OC_COMMA|SS|P(80),
420 OC_PGETLINE|SV|P(37),
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200421 OC_UNARY|xV|P(19)|'+', OC_UNARY|xV|P(19)|'-', OC_UNARY|xV|P(19)|'!',
422 0, /* ] */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000423 0,
424 0,
425 0,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200426 0, /* \n */
427 ST_IF, ST_DO, ST_FOR, OC_BREAK,
428 OC_CONTINUE, OC_DELETE|Vx, OC_PRINT,
429 OC_PRINTF, OC_NEXT, OC_NEXTFILE,
430 OC_RETURN|Vx, OC_EXIT|Nx,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000431 ST_WHILE,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200432 0, /* else */
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000433 OC_B|B_an|P(0x83), OC_B|B_co|P(0x41), OC_B|B_ls|P(0x83), OC_B|B_or|P(0x83),
434 OC_B|B_rs|P(0x83), OC_B|B_xo|P(0x83),
Glenn L McGrath545106f2002-11-11 06:21:00 +0000435 OC_FBLTIN|Sx|F_cl, OC_FBLTIN|Sx|F_sy, OC_FBLTIN|Sx|F_ff, OC_B|B_a2|P(0x83),
436 OC_FBLTIN|Nx|F_co, OC_FBLTIN|Nx|F_ex, OC_FBLTIN|Nx|F_in, OC_FBLTIN|Nx|F_lg,
437 OC_FBLTIN|F_rn, OC_FBLTIN|Nx|F_si, OC_FBLTIN|Nx|F_sq, OC_FBLTIN|Nx|F_sr,
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200438 OC_B|B_ge|P(0xd6), OC_B|B_gs|P(0xb6), OC_B|B_ix|P(0x9b), /* OC_FBLTIN|Sx|F_le, was here */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000439 OC_B|B_ma|P(0x89), OC_B|B_sp|P(0x8b), OC_SPRINTF, OC_B|B_su|P(0xb6),
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +0200440 OC_B|B_ss|P(0x8f), OC_FBLTIN|F_ti, OC_B|B_ti|P(0x0b), OC_B|B_mt|P(0x0b),
Glenn L McGrath545106f2002-11-11 06:21:00 +0000441 OC_B|B_lo|P(0x49), OC_B|B_up|P(0x49),
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200442 OC_FBLTIN|Sx|F_le, /* TC_LENGTH */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000443 OC_GETLINE|SV|P(0),
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200444 0, 0,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000445 0,
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200446 0 /* TC_END */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000447};
448
449/* internal variable names and their initial values */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000450/* asterisk marks SPECIAL vars; $ is just no-named Field0 */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000451enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000452 CONVFMT, OFMT, FS, OFS,
Denis Vlasenkof782f522007-01-01 23:51:30 +0000453 ORS, RS, RT, FILENAME,
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000454 SUBSEP, F0, ARGIND, ARGC,
455 ARGV, ERRNO, FNR, NR,
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200456 NF, IGNORECASE, ENVIRON, NUM_INTERNAL_VARS
Glenn L McGrath545106f2002-11-11 06:21:00 +0000457};
458
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000459static const char vNames[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000460 "CONVFMT\0" "OFMT\0" "FS\0*" "OFS\0"
461 "ORS\0" "RS\0*" "RT\0" "FILENAME\0"
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000462 "SUBSEP\0" "$\0*" "ARGIND\0" "ARGC\0"
463 "ARGV\0" "ERRNO\0" "FNR\0" "NR\0"
464 "NF\0*" "IGNORECASE\0*" "ENVIRON\0" "\0";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000465
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000466static const char vValues[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000467 "%.6g\0" "%.6g\0" " \0" " \0"
468 "\n\0" "\n\0" "\0" "\0"
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000469 "\034\0" "\0" "\377";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000470
471/* hash size may grow to these values */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000472#define FIRST_PRIME 61
473static const uint16_t PRIMES[] ALIGN2 = { 251, 1021, 4093, 16381, 65521 };
Glenn L McGrath545106f2002-11-11 06:21:00 +0000474
Glenn L McGrath545106f2002-11-11 06:21:00 +0000475
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000476/* Globals. Split in two parts so that first one is addressed
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000477 * with (mostly short) negative offsets.
478 * NB: it's unsafe to put members of type "double"
479 * into globals2 (gcc may fail to align them).
480 */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000481struct globals {
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000482 double t_double;
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000483 chain beginseq, mainseq, endseq;
484 chain *seq;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000485 node *break_ptr, *continue_ptr;
486 rstream *iF;
487 xhash *vhash, *ahash, *fdhash, *fnhash;
488 const char *g_progname;
489 int g_lineno;
490 int nfields;
491 int maxfields; /* used in fsrealloc() only */
492 var *Fields;
493 nvblock *g_cb;
494 char *g_pos;
495 char *g_buf;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000496 smallint icase;
497 smallint exiting;
498 smallint nextrec;
499 smallint nextfile;
500 smallint is_f0_split;
Denys Vlasenko7b46d112011-09-11 00:30:56 +0200501 smallint t_rollback;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000502};
503struct globals2 {
504 uint32_t t_info; /* often used */
505 uint32_t t_tclass;
506 char *t_string;
507 int t_lineno;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000508
509 var *intvar[NUM_INTERNAL_VARS]; /* often used */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000510
511 /* former statics from various functions */
512 char *split_f0__fstrings;
513
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000514 uint32_t next_token__save_tclass;
515 uint32_t next_token__save_info;
516 uint32_t next_token__ltclass;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000517 smallint next_token__concat_inserted;
518
519 smallint next_input_file__files_happen;
520 rstream next_input_file__rsm;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000521
522 var *evaluate__fnargs;
523 unsigned evaluate__seed;
524 regex_t evaluate__sreg;
525
526 var ptest__v;
527
528 tsplitter exec_builtin__tspl;
529
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000530 /* biggest and least used members go last */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000531 tsplitter fsplitter, rsplitter;
Denys Vlasenko3dbc5a92010-02-05 14:54:22 +0100532};
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000533#define G1 (ptr_to_globals[-1])
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000534#define G (*(struct globals2 *)ptr_to_globals)
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000535/* For debug. nm --size-sort awk.o | grep -vi ' [tr] ' */
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000536/*char G1size[sizeof(G1)]; - 0x74 */
537/*char Gsize[sizeof(G)]; - 0x1c4 */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000538/* Trying to keep most of members accessible with short offsets: */
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000539/*char Gofs_seed[offsetof(struct globals2, evaluate__seed)]; - 0x90 */
540#define t_double (G1.t_double )
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000541#define beginseq (G1.beginseq )
542#define mainseq (G1.mainseq )
543#define endseq (G1.endseq )
544#define seq (G1.seq )
545#define break_ptr (G1.break_ptr )
546#define continue_ptr (G1.continue_ptr)
547#define iF (G1.iF )
548#define vhash (G1.vhash )
549#define ahash (G1.ahash )
550#define fdhash (G1.fdhash )
551#define fnhash (G1.fnhash )
552#define g_progname (G1.g_progname )
553#define g_lineno (G1.g_lineno )
554#define nfields (G1.nfields )
555#define maxfields (G1.maxfields )
556#define Fields (G1.Fields )
557#define g_cb (G1.g_cb )
558#define g_pos (G1.g_pos )
559#define g_buf (G1.g_buf )
560#define icase (G1.icase )
561#define exiting (G1.exiting )
562#define nextrec (G1.nextrec )
563#define nextfile (G1.nextfile )
564#define is_f0_split (G1.is_f0_split )
Denys Vlasenko7b46d112011-09-11 00:30:56 +0200565#define t_rollback (G1.t_rollback )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000566#define t_info (G.t_info )
567#define t_tclass (G.t_tclass )
568#define t_string (G.t_string )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000569#define t_lineno (G.t_lineno )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000570#define intvar (G.intvar )
571#define fsplitter (G.fsplitter )
572#define rsplitter (G.rsplitter )
573#define INIT_G() do { \
Denys Vlasenko90a99042009-09-06 02:36:23 +0200574 SET_PTR_TO_GLOBALS((char*)xzalloc(sizeof(G1)+sizeof(G)) + sizeof(G1)); \
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000575 G.next_token__ltclass = TC_OPTERM; \
576 G.evaluate__seed = 1; \
577} while (0)
578
Glenn L McGrath545106f2002-11-11 06:21:00 +0000579
580/* function prototypes */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000581static void handle_special(var *);
Mike Frysingerf87b3e32005-09-27 04:16:22 +0000582static node *parse_expr(uint32_t);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000583static void chain_group(void);
584static var *evaluate(node *, var *);
585static rstream *next_input_file(void);
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000586static int fmt_num(char *, int, const char *, double, int);
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000587static int awk_exit(int) NORETURN;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000588
589/* ---- error handling ---- */
590
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000591static const char EMSG_INTERNAL_ERROR[] ALIGN1 = "Internal error";
592static const char EMSG_UNEXP_EOS[] ALIGN1 = "Unexpected end of string";
593static const char EMSG_UNEXP_TOKEN[] ALIGN1 = "Unexpected token";
594static const char EMSG_DIV_BY_ZERO[] ALIGN1 = "Division by zero";
595static const char EMSG_INV_FMT[] ALIGN1 = "Invalid format specifier";
596static const char EMSG_TOO_FEW_ARGS[] ALIGN1 = "Too few arguments for builtin";
597static const char EMSG_NOT_ARRAY[] ALIGN1 = "Not an array";
598static const char EMSG_POSSIBLE_ERROR[] ALIGN1 = "Possible syntax error";
599static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000600static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000601
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100602static void zero_out_var(var *vp)
Denis Vlasenkof782f522007-01-01 23:51:30 +0000603{
604 memset(vp, 0, sizeof(*vp));
605}
606
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000607static void syntax_error(const char *message) NORETURN;
608static void syntax_error(const char *message)
Glenn L McGrathd4036f82002-11-28 09:30:40 +0000609{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000610 bb_error_msg_and_die("%s:%i: %s", g_progname, g_lineno, message);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000611}
612
Glenn L McGrath545106f2002-11-11 06:21:00 +0000613/* ---- hash stuff ---- */
614
Denis Vlasenkof782f522007-01-01 23:51:30 +0000615static unsigned hashidx(const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000616{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000617 unsigned idx = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000618
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100619 while (*name)
620 idx = *name++ + (idx << 6) - idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000621 return idx;
622}
623
624/* create new hash */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000625static xhash *hash_init(void)
626{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000627 xhash *newhash;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000628
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100629 newhash = xzalloc(sizeof(*newhash));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000630 newhash->csize = FIRST_PRIME;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100631 newhash->items = xzalloc(FIRST_PRIME * sizeof(newhash->items[0]));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000632
633 return newhash;
634}
635
636/* find item in hash, return ptr to data, NULL if not found */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000637static void *hash_search(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000638{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000639 hash_item *hi;
640
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100641 hi = hash->items[hashidx(name) % hash->csize];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000642 while (hi) {
643 if (strcmp(hi->name, name) == 0)
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100644 return &hi->data;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000645 hi = hi->next;
646 }
647 return NULL;
648}
649
650/* grow hash if it becomes too big */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000651static void hash_rebuild(xhash *hash)
652{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000653 unsigned newsize, i, idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000654 hash_item **newitems, *hi, *thi;
655
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000656 if (hash->nprime == ARRAY_SIZE(PRIMES))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000657 return;
658
659 newsize = PRIMES[hash->nprime++];
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100660 newitems = xzalloc(newsize * sizeof(newitems[0]));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000661
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000662 for (i = 0; i < hash->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000663 hi = hash->items[i];
664 while (hi) {
665 thi = hi;
666 hi = thi->next;
667 idx = hashidx(thi->name) % newsize;
668 thi->next = newitems[idx];
669 newitems[idx] = thi;
670 }
671 }
672
673 free(hash->items);
674 hash->csize = newsize;
675 hash->items = newitems;
676}
677
678/* find item in hash, add it if necessary. Return ptr to data */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000679static void *hash_find(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000680{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000681 hash_item *hi;
Denis Vlasenkof782f522007-01-01 23:51:30 +0000682 unsigned idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000683 int l;
684
685 hi = hash_search(hash, name);
Denis Vlasenkob78c7822007-07-18 18:31:11 +0000686 if (!hi) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000687 if (++hash->nel / hash->csize > 10)
688 hash_rebuild(hash);
689
Rob Landleya3896512006-05-07 20:20:34 +0000690 l = strlen(name) + 1;
Denis Vlasenko7a676642009-03-15 22:20:31 +0000691 hi = xzalloc(sizeof(*hi) + l);
692 strcpy(hi->name, name);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000693
694 idx = hashidx(name) % hash->csize;
695 hi->next = hash->items[idx];
696 hash->items[idx] = hi;
697 hash->glen += l;
698 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100699 return &hi->data;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000700}
701
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000702#define findvar(hash, name) ((var*) hash_find((hash), (name)))
703#define newvar(name) ((var*) hash_find(vhash, (name)))
704#define newfile(name) ((rstream*)hash_find(fdhash, (name)))
705#define newfunc(name) ((func*) hash_find(fnhash, (name)))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000706
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000707static void hash_remove(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000708{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000709 hash_item *hi, **phi;
710
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100711 phi = &hash->items[hashidx(name) % hash->csize];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000712 while (*phi) {
713 hi = *phi;
714 if (strcmp(hi->name, name) == 0) {
Rob Landleya3896512006-05-07 20:20:34 +0000715 hash->glen -= (strlen(name) + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000716 hash->nel--;
717 *phi = hi->next;
718 free(hi);
719 break;
720 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100721 phi = &hi->next;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000722 }
723}
724
725/* ------ some useful functions ------ */
726
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100727static char *skip_spaces(char *p)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000728{
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000729 while (1) {
730 if (*p == '\\' && p[1] == '\n') {
731 p++;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000732 t_lineno++;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000733 } else if (*p != ' ' && *p != '\t') {
734 break;
735 }
Mike Frysingerde2b9382005-09-27 03:18:00 +0000736 p++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000737 }
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100738 return p;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000739}
740
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100741/* returns old *s, advances *s past word and terminating NUL */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000742static char *nextword(char **s)
743{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000744 char *p = *s;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100745 while (*(*s)++ != '\0')
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100746 continue;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000747 return p;
748}
749
Mike Frysinger10a11e22005-09-27 02:23:02 +0000750static char nextchar(char **s)
751{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000752 char c, *pps;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000753
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100754 c = *(*s)++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000755 pps = *s;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100756 if (c == '\\')
757 c = bb_process_escape_sequence((const char**)s);
Denys Vlasenkoea664dd2012-06-22 18:41:01 +0200758 /* Example awk statement:
759 * s = "abc\"def"
760 * we must treat \" as "
761 */
Denys Vlasenko2b299fe2010-10-24 01:58:04 +0200762 if (c == '\\' && *s == pps) { /* unrecognized \z? */
763 c = *(*s); /* yes, fetch z */
764 if (c)
765 (*s)++; /* advance unless z = NUL */
766 }
Glenn L McGrath545106f2002-11-11 06:21:00 +0000767 return c;
768}
769
Denys Vlasenkoea664dd2012-06-22 18:41:01 +0200770/* TODO: merge with strcpy_and_process_escape_sequences()?
771 */
772static void unescape_string_in_place(char *s1)
773{
774 char *s = s1;
775 while ((*s1 = nextchar(&s)) != '\0')
776 s1++;
777}
778
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000779static ALWAYS_INLINE int isalnum_(int c)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000780{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000781 return (isalnum(c) || c == '_');
782}
783
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000784static double my_strtod(char **pp)
785{
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200786 char *cp = *pp;
Rob Landleyd8205b32010-10-24 03:27:22 +0200787 if (ENABLE_DESKTOP && cp[0] == '0') {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200788 /* Might be hex or octal integer: 0x123abc or 07777 */
789 char c = (cp[1] | 0x20);
790 if (c == 'x' || isdigit(cp[1])) {
791 unsigned long long ull = strtoull(cp, pp, 0);
792 if (c == 'x')
793 return ull;
794 c = **pp;
795 if (!isdigit(c) && c != '.')
796 return ull;
797 /* else: it may be a floating number. Examples:
798 * 009.123 (*pp points to '9')
799 * 000.123 (*pp points to '.')
800 * fall through to strtod.
801 */
802 }
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000803 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200804 return strtod(cp, pp);
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000805}
806
Glenn L McGrath545106f2002-11-11 06:21:00 +0000807/* -------- working with variables (set/get/copy/etc) -------- */
808
Mike Frysinger10a11e22005-09-27 02:23:02 +0000809static xhash *iamarray(var *v)
810{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000811 var *a = v;
812
813 while (a->type & VF_CHILD)
814 a = a->x.parent;
815
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000816 if (!(a->type & VF_ARRAY)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000817 a->type |= VF_ARRAY;
818 a->x.array = hash_init();
819 }
820 return a->x.array;
821}
822
Mike Frysinger10a11e22005-09-27 02:23:02 +0000823static void clear_array(xhash *array)
824{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000825 unsigned i;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000826 hash_item *hi, *thi;
827
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000828 for (i = 0; i < array->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000829 hi = array->items[i];
830 while (hi) {
831 thi = hi;
832 hi = hi->next;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000833 free(thi->data.v.string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000834 free(thi);
835 }
836 array->items[i] = NULL;
837 }
838 array->glen = array->nel = 0;
839}
840
841/* clear a variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000842static var *clrvar(var *v)
843{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000844 if (!(v->type & VF_FSTR))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000845 free(v->string);
846
847 v->type &= VF_DONTTOUCH;
848 v->type |= VF_DIRTY;
849 v->string = NULL;
850 return v;
851}
852
853/* assign string value to variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000854static var *setvar_p(var *v, char *value)
855{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000856 clrvar(v);
857 v->string = value;
858 handle_special(v);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000859 return v;
860}
861
862/* same as setvar_p but make a copy of string */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000863static var *setvar_s(var *v, const char *value)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000864{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000865 return setvar_p(v, (value && *value) ? xstrdup(value) : NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000866}
867
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100868/* same as setvar_s but sets USER flag */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000869static var *setvar_u(var *v, const char *value)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000870{
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100871 v = setvar_s(v, value);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000872 v->type |= VF_USER;
873 return v;
874}
875
876/* set array element to user string */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000877static void setari_u(var *a, int idx, const char *s)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000878{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000879 var *v;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000880
Denys Vlasenko7bb346f2009-10-06 22:09:50 +0200881 v = findvar(iamarray(a), itoa(idx));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000882 setvar_u(v, s);
883}
884
885/* assign numeric value to variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000886static var *setvar_i(var *v, double value)
887{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000888 clrvar(v);
889 v->type |= VF_NUMBER;
890 v->number = value;
891 handle_special(v);
892 return v;
893}
894
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000895static const char *getvar_s(var *v)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000896{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000897 /* if v is numeric and has no cached string, convert it to string */
898 if ((v->type & (VF_NUMBER | VF_CACHED)) == VF_NUMBER) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000899 fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[CONVFMT]), v->number, TRUE);
900 v->string = xstrdup(g_buf);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000901 v->type |= VF_CACHED;
902 }
903 return (v->string == NULL) ? "" : v->string;
904}
905
Mike Frysinger10a11e22005-09-27 02:23:02 +0000906static double getvar_i(var *v)
907{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000908 char *s;
909
910 if ((v->type & (VF_NUMBER | VF_CACHED)) == 0) {
911 v->number = 0;
912 s = v->string;
913 if (s && *s) {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200914 debug_printf_eval("getvar_i: '%s'->", s);
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000915 v->number = my_strtod(&s);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200916 debug_printf_eval("%f (s:'%s')\n", v->number, s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000917 if (v->type & VF_USER) {
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100918 s = skip_spaces(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000919 if (*s != '\0')
920 v->type &= ~VF_USER;
921 }
922 } else {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200923 debug_printf_eval("getvar_i: '%s'->zero\n", s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000924 v->type &= ~VF_USER;
925 }
926 v->type |= VF_CACHED;
927 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200928 debug_printf_eval("getvar_i: %f\n", v->number);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000929 return v->number;
930}
931
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000932/* Used for operands of bitwise ops */
933static unsigned long getvar_i_int(var *v)
934{
935 double d = getvar_i(v);
936
937 /* Casting doubles to longs is undefined for values outside
938 * of target type range. Try to widen it as much as possible */
939 if (d >= 0)
940 return (unsigned long)d;
Denis Vlasenko665eaff2008-09-05 04:59:02 +0000941 /* Why? Think about d == -4294967295.0 (assuming 32bit longs) */
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000942 return - (long) (unsigned long) (-d);
943}
944
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000945static var *copyvar(var *dest, const var *src)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000946{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000947 if (dest != src) {
948 clrvar(dest);
Denis Vlasenko629563b2007-02-24 17:05:52 +0000949 dest->type |= (src->type & ~(VF_DONTTOUCH | VF_FSTR));
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200950 debug_printf_eval("copyvar: number:%f string:'%s'\n", src->number, src->string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000951 dest->number = src->number;
952 if (src->string)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000953 dest->string = xstrdup(src->string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000954 }
955 handle_special(dest);
956 return dest;
957}
958
Mike Frysinger10a11e22005-09-27 02:23:02 +0000959static var *incvar(var *v)
960{
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100961 return setvar_i(v, getvar_i(v) + 1.0);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000962}
963
964/* return true if v is number or numeric string */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000965static int is_numeric(var *v)
966{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000967 getvar_i(v);
968 return ((v->type ^ VF_DIRTY) & (VF_NUMBER | VF_USER | VF_DIRTY));
969}
970
971/* return 1 when value of v corresponds to true, 0 otherwise */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000972static int istrue(var *v)
973{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000974 if (is_numeric(v))
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100975 return (v->number != 0);
976 return (v->string && v->string[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000977}
978
Eric Andersenaff114c2004-04-14 17:51:38 +0000979/* temporary variables allocator. Last allocated should be first freed */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000980static var *nvalloc(int n)
981{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000982 nvblock *pb = NULL;
983 var *v, *r;
984 int size;
985
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000986 while (g_cb) {
987 pb = g_cb;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100988 if ((g_cb->pos - g_cb->nv) + n <= g_cb->size)
989 break;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000990 g_cb = g_cb->next;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000991 }
992
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000993 if (!g_cb) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000994 size = (n <= MINNVBLOCK) ? MINNVBLOCK : n;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000995 g_cb = xzalloc(sizeof(nvblock) + size * sizeof(var));
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000996 g_cb->size = size;
997 g_cb->pos = g_cb->nv;
998 g_cb->prev = pb;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000999 /*g_cb->next = NULL; - xzalloc did it */
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001000 if (pb)
1001 pb->next = g_cb;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001002 }
1003
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001004 v = r = g_cb->pos;
1005 g_cb->pos += n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001006
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001007 while (v < g_cb->pos) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001008 v->type = 0;
1009 v->string = NULL;
1010 v++;
1011 }
1012
1013 return r;
1014}
1015
Mike Frysinger10a11e22005-09-27 02:23:02 +00001016static void nvfree(var *v)
1017{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001018 var *p;
1019
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001020 if (v < g_cb->nv || v >= g_cb->pos)
1021 syntax_error(EMSG_INTERNAL_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001022
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001023 for (p = v; p < g_cb->pos; p++) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001024 if ((p->type & (VF_ARRAY | VF_CHILD)) == VF_ARRAY) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001025 clear_array(iamarray(p));
1026 free(p->x.array->items);
1027 free(p->x.array);
1028 }
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001029 if (p->type & VF_WALK) {
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001030 walker_list *n;
1031 walker_list *w = p->x.walker;
1032 debug_printf_walker("nvfree: freeing walker @%p\n", &p->x.walker);
1033 p->x.walker = NULL;
1034 while (w) {
1035 n = w->prev;
1036 debug_printf_walker(" free(%p)\n", w);
1037 free(w);
1038 w = n;
1039 }
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001040 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001041 clrvar(p);
1042 }
1043
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001044 g_cb->pos = v;
1045 while (g_cb->prev && g_cb->pos == g_cb->nv) {
1046 g_cb = g_cb->prev;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001047 }
1048}
1049
1050/* ------- awk program text parsing ------- */
1051
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001052/* Parse next token pointed by global pos, place results into global ttt.
Glenn L McGrath545106f2002-11-11 06:21:00 +00001053 * If token isn't expected, give away. Return token class
1054 */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001055static uint32_t next_token(uint32_t expected)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001056{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001057#define concat_inserted (G.next_token__concat_inserted)
1058#define save_tclass (G.next_token__save_tclass)
1059#define save_info (G.next_token__save_info)
1060/* Initialized to TC_OPTERM: */
1061#define ltclass (G.next_token__ltclass)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001062
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001063 char *p, *s;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001064 const char *tl;
1065 uint32_t tc;
1066 const uint32_t *ti;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001067
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001068 if (t_rollback) {
1069 t_rollback = FALSE;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001070 } else if (concat_inserted) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001071 concat_inserted = FALSE;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001072 t_tclass = save_tclass;
1073 t_info = save_info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001074 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001075 p = g_pos;
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001076 readnext:
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +01001077 p = skip_spaces(p);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001078 g_lineno = t_lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001079 if (*p == '#')
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001080 while (*p != '\n' && *p != '\0')
1081 p++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001082
1083 if (*p == '\n')
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001084 t_lineno++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001085
1086 if (*p == '\0') {
1087 tc = TC_EOF;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001088 debug_printf_parse("%s: token found: TC_EOF\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001089 } else if (*p == '\"') {
1090 /* it's a string */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001091 t_string = s = ++p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001092 while (*p != '\"') {
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02001093 char *pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001094 if (*p == '\0' || *p == '\n')
1095 syntax_error(EMSG_UNEXP_EOS);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02001096 pp = p;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001097 *s++ = nextchar(&pp);
1098 p = pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001099 }
1100 p++;
1101 *s = '\0';
1102 tc = TC_STRING;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001103 debug_printf_parse("%s: token found:'%s' TC_STRING\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001104 } else if ((expected & TC_REGEXP) && *p == '/') {
1105 /* it's regexp */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001106 t_string = s = ++p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001107 while (*p != '/') {
1108 if (*p == '\0' || *p == '\n')
1109 syntax_error(EMSG_UNEXP_EOS);
Denis Vlasenkod9b5ab82007-05-18 07:30:43 +00001110 *s = *p++;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001111 if (*s++ == '\\') {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001112 char *pp = p;
1113 s[-1] = bb_process_escape_sequence((const char **)&pp);
1114 if (*p == '\\')
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001115 *s++ = '\\';
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001116 if (pp == p)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001117 *s++ = *p++;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001118 else
1119 p = pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001120 }
1121 }
1122 p++;
1123 *s = '\0';
1124 tc = TC_REGEXP;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001125 debug_printf_parse("%s: token found:'%s' TC_REGEXP\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001126
1127 } else if (*p == '.' || isdigit(*p)) {
1128 /* it's a number */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001129 char *pp = p;
1130 t_double = my_strtod(&pp);
1131 p = pp;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001132 if (*p == '.')
Glenn L McGrath545106f2002-11-11 06:21:00 +00001133 syntax_error(EMSG_UNEXP_TOKEN);
1134 tc = TC_NUMBER;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001135 debug_printf_parse("%s: token found:%f TC_NUMBER\n", __func__, t_double);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001136 } else {
1137 /* search for something known */
1138 tl = tokenlist;
1139 tc = 0x00000001;
1140 ti = tokeninfo;
1141 while (*tl) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001142 int l = (unsigned char) *tl++;
1143 if (l == (unsigned char) NTCC) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001144 tc <<= 1;
1145 continue;
1146 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001147 /* if token class is expected,
1148 * token matches,
1149 * and it's not a longer word,
Glenn L McGrath545106f2002-11-11 06:21:00 +00001150 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001151 if ((tc & (expected | TC_WORD | TC_NEWLINE))
Denys Vlasenko28458c62010-10-05 16:49:03 +02001152 && strncmp(p, tl, l) == 0
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001153 && !((tc & TC_WORD) && isalnum_(p[l]))
1154 ) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001155 /* then this is what we are looking for */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001156 t_info = *ti;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001157 debug_printf_parse("%s: token found:'%.*s' t_info:%x\n", __func__, l, p, t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001158 p += l;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001159 goto token_found;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001160 }
1161 ti++;
1162 tl += l;
1163 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001164 /* not a known token */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001165
Denys Vlasenko28458c62010-10-05 16:49:03 +02001166 /* is it a name? (var/array/function) */
1167 if (!isalnum_(*p))
1168 syntax_error(EMSG_UNEXP_TOKEN); /* no */
1169 /* yes */
1170 t_string = --p;
1171 while (isalnum_(*++p)) {
1172 p[-1] = *p;
1173 }
1174 p[-1] = '\0';
1175 tc = TC_VARIABLE;
1176 /* also consume whitespace between functionname and bracket */
1177 if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY))
1178 p = skip_spaces(p);
1179 if (*p == '(') {
1180 tc = TC_FUNCTION;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001181 debug_printf_parse("%s: token found:'%s' TC_FUNCTION\n", __func__, t_string);
Denys Vlasenko28458c62010-10-05 16:49:03 +02001182 } else {
1183 if (*p == '[') {
1184 p++;
1185 tc = TC_ARRAY;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001186 debug_printf_parse("%s: token found:'%s' TC_ARRAY\n", __func__, t_string);
1187 } else
1188 debug_printf_parse("%s: token found:'%s' TC_VARIABLE\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001189 }
1190 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001191 token_found:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001192 g_pos = p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001193
1194 /* skipping newlines in some cases */
1195 if ((ltclass & TC_NOTERM) && (tc & TC_NEWLINE))
1196 goto readnext;
1197
1198 /* insert concatenation operator when needed */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001199 if ((ltclass & TC_CONCAT1) && (tc & TC_CONCAT2) && (expected & TC_BINOP)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001200 concat_inserted = TRUE;
1201 save_tclass = tc;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001202 save_info = t_info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001203 tc = TC_BINOP;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001204 t_info = OC_CONCAT | SS | P(35);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001205 }
1206
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001207 t_tclass = tc;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001208 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001209 ltclass = t_tclass;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001210
1211 /* Are we ready for this? */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001212 if (!(ltclass & expected)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001213 syntax_error((ltclass & (TC_NEWLINE | TC_EOF)) ?
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001214 EMSG_UNEXP_EOS : EMSG_UNEXP_TOKEN);
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001215 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001216
1217 return ltclass;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001218#undef concat_inserted
1219#undef save_tclass
1220#undef save_info
1221#undef ltclass
Glenn L McGrath545106f2002-11-11 06:21:00 +00001222}
1223
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001224static void rollback_token(void)
1225{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001226 t_rollback = TRUE;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001227}
Glenn L McGrath545106f2002-11-11 06:21:00 +00001228
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001229static node *new_node(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001230{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001231 node *n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001232
Denis Vlasenko4cccc032006-12-22 18:37:07 +00001233 n = xzalloc(sizeof(node));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001234 n->info = info;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001235 n->lineno = g_lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001236 return n;
1237}
1238
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001239static void mk_re_node(const char *s, node *n, regex_t *re)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001240{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001241 n->info = OC_REGEXP;
1242 n->l.re = re;
1243 n->r.ire = re + 1;
1244 xregcomp(re, s, REG_EXTENDED);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001245 xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001246}
1247
Mike Frysinger10a11e22005-09-27 02:23:02 +00001248static node *condition(void)
1249{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001250 next_token(TC_SEQSTART);
1251 return parse_expr(TC_SEQTERM);
1252}
1253
1254/* parse expression terminated by given argument, return ptr
1255 * to built subtree. Terminator is eaten by parse_expr */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001256static node *parse_expr(uint32_t iexp)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001257{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001258 node sn;
1259 node *cn = &sn;
1260 node *vn, *glptr;
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001261 uint32_t tc, xtc;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001262 var *v;
1263
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001264 debug_printf_parse("%s(%x)\n", __func__, iexp);
1265
Glenn L McGrath545106f2002-11-11 06:21:00 +00001266 sn.info = PRIMASK;
1267 sn.r.n = glptr = NULL;
1268 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp;
1269
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001270 while (!((tc = next_token(xtc)) & iexp)) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001271
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001272 if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001273 /* input redirection (<) attached to glptr node */
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001274 debug_printf_parse("%s: input redir\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001275 cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37));
Glenn L McGrath4bded582004-02-22 11:55:09 +00001276 cn->a.n = glptr;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001277 xtc = TC_OPERAND | TC_UOPPRE;
1278 glptr = NULL;
1279
1280 } else if (tc & (TC_BINOP | TC_UOPPOST)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001281 debug_printf_parse("%s: TC_BINOP | TC_UOPPOST\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001282 /* for binary and postfix-unary operators, jump back over
1283 * previous operators with higher priority */
1284 vn = cn;
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001285 while (((t_info & PRIMASK) > (vn->a.n->info & PRIMASK2))
1286 || ((t_info == vn->info) && ((t_info & OPCLSMASK) == OC_COLON))
1287 ) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001288 vn = vn->a.n;
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001289 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001290 if ((t_info & OPCLSMASK) == OC_TERNARY)
1291 t_info += P(6);
1292 cn = vn->a.n->r.n = new_node(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001293 cn->a.n = vn->a.n;
1294 if (tc & TC_BINOP) {
1295 cn->l.n = vn;
1296 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001297 if ((t_info & OPCLSMASK) == OC_PGETLINE) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001298 /* it's a pipe */
1299 next_token(TC_GETLINE);
1300 /* give maximum priority to this pipe */
1301 cn->info &= ~PRIMASK;
1302 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1303 }
1304 } else {
1305 cn->r.n = vn;
1306 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1307 }
1308 vn->a.n = cn;
1309
1310 } else {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001311 debug_printf_parse("%s: other\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001312 /* for operands and prefix-unary operators, attach them
1313 * to last node */
1314 vn = cn;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001315 cn = vn->r.n = new_node(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001316 cn->a.n = vn;
1317 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
1318 if (tc & (TC_OPERAND | TC_REGEXP)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001319 debug_printf_parse("%s: TC_OPERAND | TC_REGEXP\n", __func__);
Rob Landleyed830e82005-06-07 02:43:52 +00001320 xtc = TC_UOPPRE | TC_UOPPOST | TC_BINOP | TC_OPERAND | iexp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001321 /* one should be very careful with switch on tclass -
Glenn L McGrath545106f2002-11-11 06:21:00 +00001322 * only simple tclasses should be used! */
1323 switch (tc) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00001324 case TC_VARIABLE:
1325 case TC_ARRAY:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001326 debug_printf_parse("%s: TC_VARIABLE | TC_ARRAY\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001327 cn->info = OC_VAR;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001328 v = hash_search(ahash, t_string);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001329 if (v != NULL) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001330 cn->info = OC_FNARG;
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001331 cn->l.aidx = v->x.aidx;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001332 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001333 cn->l.v = newvar(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001334 }
1335 if (tc & TC_ARRAY) {
1336 cn->info |= xS;
1337 cn->r.n = parse_expr(TC_ARRTERM);
1338 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001339 break;
Mike Frysingerde2b9382005-09-27 03:18:00 +00001340
Denis Vlasenkof782f522007-01-01 23:51:30 +00001341 case TC_NUMBER:
1342 case TC_STRING:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001343 debug_printf_parse("%s: TC_NUMBER | TC_STRING\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001344 cn->info = OC_VAR;
Rob Landley9ffd4232006-05-21 18:30:35 +00001345 v = cn->l.v = xzalloc(sizeof(var));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001346 if (tc & TC_NUMBER)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001347 setvar_i(v, t_double);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001348 else
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001349 setvar_s(v, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001350 break;
1351
Denis Vlasenkof782f522007-01-01 23:51:30 +00001352 case TC_REGEXP:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001353 debug_printf_parse("%s: TC_REGEXP\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001354 mk_re_node(t_string, cn, xzalloc(sizeof(regex_t)*2));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001355 break;
1356
Denis Vlasenkof782f522007-01-01 23:51:30 +00001357 case TC_FUNCTION:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001358 debug_printf_parse("%s: TC_FUNCTION\n", __func__);
Mike Frysingerde2b9382005-09-27 03:18:00 +00001359 cn->info = OC_FUNC;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001360 cn->r.f = newfunc(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001361 cn->l.n = condition();
1362 break;
1363
Denis Vlasenkof782f522007-01-01 23:51:30 +00001364 case TC_SEQSTART:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001365 debug_printf_parse("%s: TC_SEQSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001366 cn = vn->r.n = parse_expr(TC_SEQTERM);
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001367 if (!cn)
1368 syntax_error("Empty sequence");
Glenn L McGrath545106f2002-11-11 06:21:00 +00001369 cn->a.n = vn;
1370 break;
1371
Denis Vlasenkof782f522007-01-01 23:51:30 +00001372 case TC_GETLINE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001373 debug_printf_parse("%s: TC_GETLINE\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001374 glptr = cn;
1375 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1376 break;
1377
Denis Vlasenkof782f522007-01-01 23:51:30 +00001378 case TC_BUILTIN:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001379 debug_printf_parse("%s: TC_BUILTIN\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001380 cn->l.n = condition();
1381 break;
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001382
1383 case TC_LENGTH:
1384 debug_printf_parse("%s: TC_LENGTH\n", __func__);
1385 next_token(TC_SEQSTART | TC_OPTERM | TC_GRPTERM);
1386 rollback_token();
1387 if (t_tclass & TC_SEQSTART) {
1388 /* It was a "(" token. Handle just like TC_BUILTIN */
1389 cn->l.n = condition();
1390 }
1391 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001392 }
1393 }
1394 }
1395 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001396
1397 debug_printf_parse("%s() returns %p\n", __func__, sn.r.n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001398 return sn.r.n;
1399}
1400
1401/* add node to chain. Return ptr to alloc'd node */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001402static node *chain_node(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001403{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001404 node *n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001405
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001406 if (!seq->first)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001407 seq->first = seq->last = new_node(0);
1408
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001409 if (seq->programname != g_progname) {
1410 seq->programname = g_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001411 n = chain_node(OC_NEWSOURCE);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001412 n->l.new_progname = xstrdup(g_progname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001413 }
1414
1415 n = seq->last;
1416 n->info = info;
1417 seq->last = n->a.n = new_node(OC_DONE);
1418
1419 return n;
1420}
1421
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001422static void chain_expr(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001423{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001424 node *n;
1425
1426 n = chain_node(info);
1427 n->l.n = parse_expr(TC_OPTERM | TC_GRPTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001428 if (t_tclass & TC_GRPTERM)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001429 rollback_token();
1430}
1431
Mike Frysinger10a11e22005-09-27 02:23:02 +00001432static node *chain_loop(node *nn)
1433{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001434 node *n, *n2, *save_brk, *save_cont;
1435
1436 save_brk = break_ptr;
1437 save_cont = continue_ptr;
1438
1439 n = chain_node(OC_BR | Vx);
1440 continue_ptr = new_node(OC_EXEC);
1441 break_ptr = new_node(OC_EXEC);
1442 chain_group();
1443 n2 = chain_node(OC_EXEC | Vx);
1444 n2->l.n = nn;
1445 n2->a.n = n;
1446 continue_ptr->a.n = n2;
1447 break_ptr->a.n = n->r.n = seq->last;
1448
1449 continue_ptr = save_cont;
1450 break_ptr = save_brk;
1451
1452 return n;
1453}
1454
1455/* parse group and attach it to chain */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001456static void chain_group(void)
1457{
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001458 uint32_t c;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001459 node *n, *n2, *n3;
1460
1461 do {
1462 c = next_token(TC_GRPSEQ);
1463 } while (c & TC_NEWLINE);
1464
1465 if (c & TC_GRPSTART) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001466 debug_printf_parse("%s: TC_GRPSTART\n", __func__);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001467 while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001468 debug_printf_parse("%s: !TC_GRPTERM\n", __func__);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001469 if (t_tclass & TC_NEWLINE)
1470 continue;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001471 rollback_token();
1472 chain_group();
1473 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001474 debug_printf_parse("%s: TC_GRPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001475 } else if (c & (TC_OPSEQ | TC_OPTERM)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001476 debug_printf_parse("%s: TC_OPSEQ | TC_OPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001477 rollback_token();
1478 chain_expr(OC_EXEC | Vx);
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001479 } else {
1480 /* TC_STATEMNT */
1481 debug_printf_parse("%s: TC_STATEMNT(?)\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001482 switch (t_info & OPCLSMASK) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001483 case ST_IF:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001484 debug_printf_parse("%s: ST_IF\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001485 n = chain_node(OC_BR | Vx);
1486 n->l.n = condition();
1487 chain_group();
1488 n2 = chain_node(OC_EXEC);
1489 n->r.n = seq->last;
1490 if (next_token(TC_GRPSEQ | TC_GRPTERM | TC_ELSE) == TC_ELSE) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001491 chain_group();
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001492 n2->a.n = seq->last;
1493 } else {
1494 rollback_token();
1495 }
1496 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001497
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001498 case ST_WHILE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001499 debug_printf_parse("%s: ST_WHILE\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001500 n2 = condition();
1501 n = chain_loop(NULL);
1502 n->l.n = n2;
1503 break;
1504
1505 case ST_DO:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001506 debug_printf_parse("%s: ST_DO\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001507 n2 = chain_node(OC_EXEC);
1508 n = chain_loop(NULL);
1509 n2->a.n = n->a.n;
1510 next_token(TC_WHILE);
1511 n->l.n = condition();
1512 break;
1513
1514 case ST_FOR:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001515 debug_printf_parse("%s: ST_FOR\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001516 next_token(TC_SEQSTART);
1517 n2 = parse_expr(TC_SEMICOL | TC_SEQTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001518 if (t_tclass & TC_SEQTERM) { /* for-in */
Brian Foley61d59972016-10-15 14:45:40 +01001519 if (!n2 || (n2->info & OPCLSMASK) != OC_IN)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001520 syntax_error(EMSG_UNEXP_TOKEN);
1521 n = chain_node(OC_WALKINIT | VV);
1522 n->l.n = n2->l.n;
1523 n->r.n = n2->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001524 n = chain_loop(NULL);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001525 n->info = OC_WALKNEXT | Vx;
1526 n->l.n = n2->l.n;
1527 } else { /* for (;;) */
1528 n = chain_node(OC_EXEC | Vx);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001529 n->l.n = n2;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001530 n2 = parse_expr(TC_SEMICOL);
1531 n3 = parse_expr(TC_SEQTERM);
1532 n = chain_loop(n3);
1533 n->l.n = n2;
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001534 if (!n2)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001535 n->info = OC_EXEC;
1536 }
1537 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001538
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001539 case OC_PRINT:
1540 case OC_PRINTF:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001541 debug_printf_parse("%s: OC_PRINT[F]\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001542 n = chain_node(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001543 n->l.n = parse_expr(TC_OPTERM | TC_OUTRDR | TC_GRPTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001544 if (t_tclass & TC_OUTRDR) {
1545 n->info |= t_info;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001546 n->r.n = parse_expr(TC_OPTERM | TC_GRPTERM);
1547 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001548 if (t_tclass & TC_GRPTERM)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001549 rollback_token();
1550 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001551
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001552 case OC_BREAK:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001553 debug_printf_parse("%s: OC_BREAK\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001554 n = chain_node(OC_EXEC);
1555 n->a.n = break_ptr;
Denys Vlasenko5f8daef2014-06-26 16:40:28 +02001556 chain_expr(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001557 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001558
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001559 case OC_CONTINUE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001560 debug_printf_parse("%s: OC_CONTINUE\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001561 n = chain_node(OC_EXEC);
1562 n->a.n = continue_ptr;
Denys Vlasenko5f8daef2014-06-26 16:40:28 +02001563 chain_expr(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001564 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001565
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001566 /* delete, next, nextfile, return, exit */
1567 default:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001568 debug_printf_parse("%s: default\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001569 chain_expr(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001570 }
1571 }
1572}
1573
Mike Frysinger10a11e22005-09-27 02:23:02 +00001574static void parse_program(char *p)
1575{
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001576 uint32_t tclass;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001577 node *cn;
1578 func *f;
1579 var *v;
1580
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001581 g_pos = p;
1582 t_lineno = 1;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001583 while ((tclass = next_token(TC_EOF | TC_OPSEQ | TC_GRPSTART |
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001584 TC_OPTERM | TC_BEGIN | TC_END | TC_FUNCDECL)) != TC_EOF) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001585
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001586 if (tclass & TC_OPTERM) {
1587 debug_printf_parse("%s: TC_OPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001588 continue;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001589 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001590
1591 seq = &mainseq;
1592 if (tclass & TC_BEGIN) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001593 debug_printf_parse("%s: TC_BEGIN\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001594 seq = &beginseq;
1595 chain_group();
Glenn L McGrath545106f2002-11-11 06:21:00 +00001596 } else if (tclass & TC_END) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001597 debug_printf_parse("%s: TC_END\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001598 seq = &endseq;
1599 chain_group();
Glenn L McGrath545106f2002-11-11 06:21:00 +00001600 } else if (tclass & TC_FUNCDECL) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001601 debug_printf_parse("%s: TC_FUNCDECL\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001602 next_token(TC_FUNCTION);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001603 g_pos++;
1604 f = newfunc(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001605 f->body.first = NULL;
1606 f->nargs = 0;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001607 while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001608 v = findvar(ahash, t_string);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001609 v->x.aidx = f->nargs++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001610
1611 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
1612 break;
1613 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001614 seq = &f->body;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001615 chain_group();
1616 clear_array(ahash);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001617 } else if (tclass & TC_OPSEQ) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001618 debug_printf_parse("%s: TC_OPSEQ\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001619 rollback_token();
1620 cn = chain_node(OC_TEST);
1621 cn->l.n = parse_expr(TC_OPTERM | TC_EOF | TC_GRPSTART);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001622 if (t_tclass & TC_GRPSTART) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001623 debug_printf_parse("%s: TC_GRPSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001624 rollback_token();
1625 chain_group();
1626 } else {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001627 debug_printf_parse("%s: !TC_GRPSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001628 chain_node(OC_PRINT);
1629 }
1630 cn->r.n = mainseq.last;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001631 } else /* if (tclass & TC_GRPSTART) */ {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001632 debug_printf_parse("%s: TC_GRPSTART(?)\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001633 rollback_token();
1634 chain_group();
1635 }
1636 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001637 debug_printf_parse("%s: TC_EOF\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001638}
1639
1640
1641/* -------- program execution part -------- */
1642
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001643static node *mk_splitter(const char *s, tsplitter *spl)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001644{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001645 regex_t *re, *ire;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001646 node *n;
1647
1648 re = &spl->re[0];
1649 ire = &spl->re[1];
1650 n = &spl->n;
Denis Vlasenko890ac9d2006-10-07 15:16:19 +00001651 if ((n->info & OPCLSMASK) == OC_REGEXP) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001652 regfree(re);
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001653 regfree(ire); // TODO: nuke ire, use re+1?
Glenn L McGrath545106f2002-11-11 06:21:00 +00001654 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001655 if (s[0] && s[1]) { /* strlen(s) > 1 */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001656 mk_re_node(s, n, re);
1657 } else {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001658 n->info = (uint32_t) s[0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00001659 }
1660
1661 return n;
1662}
1663
1664/* use node as a regular expression. Supplied with node ptr and regex_t
Eric Andersenaff114c2004-04-14 17:51:38 +00001665 * storage space. Return ptr to regex (if result points to preg, it should
Glenn L McGrath545106f2002-11-11 06:21:00 +00001666 * be later regfree'd manually
1667 */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001668static regex_t *as_regex(node *op, regex_t *preg)
1669{
Denis Vlasenko7a676642009-03-15 22:20:31 +00001670 int cflags;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001671 var *v;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001672 const char *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001673
1674 if ((op->info & OPCLSMASK) == OC_REGEXP) {
1675 return icase ? op->r.ire : op->l.re;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001676 }
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001677 v = nvalloc(1);
1678 s = getvar_s(evaluate(op, v));
Denis Vlasenko7a676642009-03-15 22:20:31 +00001679
1680 cflags = icase ? REG_EXTENDED | REG_ICASE : REG_EXTENDED;
1681 /* Testcase where REG_EXTENDED fails (unpaired '{'):
1682 * echo Hi | awk 'gsub("@(samp|code|file)\{","");'
1683 * gawk 3.1.5 eats this. We revert to ~REG_EXTENDED
1684 * (maybe gsub is not supposed to use REG_EXTENDED?).
1685 */
1686 if (regcomp(preg, s, cflags)) {
1687 cflags &= ~REG_EXTENDED;
1688 xregcomp(preg, s, cflags);
1689 }
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001690 nvfree(v);
1691 return preg;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001692}
1693
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001694/* gradually increasing buffer.
1695 * note that we reallocate even if n == old_size,
1696 * and thus there is at least one extra allocated byte.
1697 */
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001698static char* qrealloc(char *b, int n, int *size)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001699{
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001700 if (!b || n >= *size) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001701 *size = n + (n>>1) + 80;
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001702 b = xrealloc(b, *size);
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001703 }
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001704 return b;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001705}
1706
1707/* resize field storage space */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001708static void fsrealloc(int size)
1709{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001710 int i;
1711
1712 if (size >= maxfields) {
1713 i = maxfields;
1714 maxfields = size + 16;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001715 Fields = xrealloc(Fields, maxfields * sizeof(Fields[0]));
Denis Vlasenkof782f522007-01-01 23:51:30 +00001716 for (; i < maxfields; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001717 Fields[i].type = VF_SPECIAL;
1718 Fields[i].string = NULL;
1719 }
1720 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001721 /* if size < nfields, clear extra field variables */
1722 for (i = size; i < nfields; i++) {
1723 clrvar(Fields + i);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001724 }
1725 nfields = size;
1726}
1727
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001728static int awk_split(const char *s, node *spl, char **slist)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001729{
Denys Vlasenko28458c62010-10-05 16:49:03 +02001730 int l, n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001731 char c[4];
1732 char *s1;
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001733 regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough...
Glenn L McGrath545106f2002-11-11 06:21:00 +00001734
1735 /* in worst case, each char would be a separate field */
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001736 *slist = s1 = xzalloc(strlen(s) * 2 + 3);
1737 strcpy(s1, s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001738
1739 c[0] = c[1] = (char)spl->info;
1740 c[2] = c[3] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001741 if (*getvar_s(intvar[RS]) == '\0')
1742 c[2] = '\n';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001743
Denys Vlasenko28458c62010-10-05 16:49:03 +02001744 n = 0;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001745 if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
1746 if (!*s)
1747 return n; /* "": zero fields */
1748 n++; /* at least one field will be there */
1749 do {
1750 l = strcspn(s, c+2); /* len till next NUL or \n */
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001751 if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0
1752 && pmatch[0].rm_so <= l
1753 ) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001754 l = pmatch[0].rm_so;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001755 if (pmatch[0].rm_eo == 0) {
1756 l++;
1757 pmatch[0].rm_eo++;
1758 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001759 n++; /* we saw yet another delimiter */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001760 } else {
1761 pmatch[0].rm_eo = l;
Denys Vlasenkoe4244232009-05-18 23:50:03 +02001762 if (s[l])
1763 pmatch[0].rm_eo++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001764 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001765 memcpy(s1, s, l);
Denis Vlasenko67b5eeb2009-04-12 13:54:13 +00001766 /* make sure we remove *all* of the separator chars */
Denys Vlasenkoe4244232009-05-18 23:50:03 +02001767 do {
1768 s1[l] = '\0';
1769 } while (++l < pmatch[0].rm_eo);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001770 nextword(&s1);
1771 s += pmatch[0].rm_eo;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001772 } while (*s);
1773 return n;
1774 }
1775 if (c[0] == '\0') { /* null split */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001776 while (*s) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001777 *s1++ = *s++;
1778 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001779 n++;
1780 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001781 return n;
1782 }
1783 if (c[0] != ' ') { /* single-character split */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001784 if (icase) {
1785 c[0] = toupper(c[0]);
1786 c[1] = tolower(c[1]);
1787 }
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001788 if (*s1)
1789 n++;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001790 while ((s1 = strpbrk(s1, c)) != NULL) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001791 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001792 n++;
1793 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001794 return n;
1795 }
1796 /* space split */
1797 while (*s) {
1798 s = skip_whitespace(s);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001799 if (!*s)
1800 break;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001801 n++;
1802 while (*s && !isspace(*s))
1803 *s1++ = *s++;
1804 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001805 }
1806 return n;
1807}
1808
Mike Frysinger10a11e22005-09-27 02:23:02 +00001809static void split_f0(void)
1810{
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001811/* static char *fstrings; */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001812#define fstrings (G.split_f0__fstrings)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001813
Glenn L McGrath545106f2002-11-11 06:21:00 +00001814 int i, n;
1815 char *s;
1816
1817 if (is_f0_split)
1818 return;
1819
1820 is_f0_split = TRUE;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001821 free(fstrings);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001822 fsrealloc(0);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001823 n = awk_split(getvar_s(intvar[F0]), &fsplitter.n, &fstrings);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001824 fsrealloc(n);
1825 s = fstrings;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001826 for (i = 0; i < n; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001827 Fields[i].string = nextword(&s);
1828 Fields[i].type |= (VF_FSTR | VF_USER | VF_DIRTY);
1829 }
1830
1831 /* set NF manually to avoid side effects */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001832 clrvar(intvar[NF]);
1833 intvar[NF]->type = VF_NUMBER | VF_SPECIAL;
1834 intvar[NF]->number = nfields;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001835#undef fstrings
Glenn L McGrath545106f2002-11-11 06:21:00 +00001836}
1837
1838/* perform additional actions when some internal variables changed */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001839static void handle_special(var *v)
1840{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001841 int n;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001842 char *b;
1843 const char *sep, *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001844 int sl, l, len, i, bsize;
1845
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001846 if (!(v->type & VF_SPECIAL))
Glenn L McGrath545106f2002-11-11 06:21:00 +00001847 return;
1848
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001849 if (v == intvar[NF]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001850 n = (int)getvar_i(v);
1851 fsrealloc(n);
1852
1853 /* recalculate $0 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001854 sep = getvar_s(intvar[OFS]);
Rob Landleya3896512006-05-07 20:20:34 +00001855 sl = strlen(sep);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001856 b = NULL;
1857 len = 0;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001858 for (i = 0; i < n; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001859 s = getvar_s(&Fields[i]);
Rob Landleya3896512006-05-07 20:20:34 +00001860 l = strlen(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001861 if (b) {
1862 memcpy(b+len, sep, sl);
1863 len += sl;
1864 }
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001865 b = qrealloc(b, len+l+sl, &bsize);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001866 memcpy(b+len, s, l);
1867 len += l;
1868 }
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001869 if (b)
1870 b[len] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001871 setvar_p(intvar[F0], b);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001872 is_f0_split = TRUE;
1873
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001874 } else if (v == intvar[F0]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001875 is_f0_split = FALSE;
1876
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001877 } else if (v == intvar[FS]) {
Denys Vlasenkodf8066a2012-07-11 01:27:15 +02001878 /*
1879 * The POSIX-2008 standard says that changing FS should have no effect on the
1880 * current input line, but only on the next one. The language is:
1881 *
1882 * > Before the first reference to a field in the record is evaluated, the record
1883 * > shall be split into fields, according to the rules in Regular Expressions,
1884 * > using the value of FS that was current at the time the record was read.
1885 *
1886 * So, split up current line before assignment to FS:
1887 */
1888 split_f0();
1889
Glenn L McGrath545106f2002-11-11 06:21:00 +00001890 mk_splitter(getvar_s(v), &fsplitter);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001891 } else if (v == intvar[RS]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001892 mk_splitter(getvar_s(v), &rsplitter);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001893 } else if (v == intvar[IGNORECASE]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001894 icase = istrue(v);
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001895 } else { /* $n */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001896 n = getvar_i(intvar[NF]);
1897 setvar_i(intvar[NF], n > v-Fields ? n : v-Fields+1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001898 /* right here v is invalid. Just to note... */
1899 }
1900}
1901
1902/* step through func/builtin/etc arguments */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001903static node *nextarg(node **pn)
1904{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001905 node *n;
1906
1907 n = *pn;
1908 if (n && (n->info & OPCLSMASK) == OC_COMMA) {
1909 *pn = n->r.n;
1910 n = n->l.n;
1911 } else {
1912 *pn = NULL;
1913 }
1914 return n;
1915}
1916
Mike Frysinger10a11e22005-09-27 02:23:02 +00001917static void hashwalk_init(var *v, xhash *array)
1918{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001919 hash_item *hi;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00001920 unsigned i;
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001921 walker_list *w;
1922 walker_list *prev_walker;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001923
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001924 if (v->type & VF_WALK) {
1925 prev_walker = v->x.walker;
1926 } else {
1927 v->type |= VF_WALK;
1928 prev_walker = NULL;
1929 }
1930 debug_printf_walker("hashwalk_init: prev_walker:%p\n", prev_walker);
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001931
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001932 w = v->x.walker = xzalloc(sizeof(*w) + array->glen + 1); /* why + 1? */
1933 debug_printf_walker(" walker@%p=%p\n", &v->x.walker, w);
1934 w->cur = w->end = w->wbuf;
1935 w->prev = prev_walker;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001936 for (i = 0; i < array->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001937 hi = array->items[i];
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001938 while (hi) {
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001939 strcpy(w->end, hi->name);
1940 nextword(&w->end);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001941 hi = hi->next;
1942 }
1943 }
1944}
1945
Mike Frysinger10a11e22005-09-27 02:23:02 +00001946static int hashwalk_next(var *v)
1947{
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001948 walker_list *w = v->x.walker;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001949
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001950 if (w->cur >= w->end) {
1951 walker_list *prev_walker = w->prev;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001952
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001953 debug_printf_walker("end of iteration, free(walker@%p:%p), prev_walker:%p\n", &v->x.walker, w, prev_walker);
1954 free(w);
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001955 v->x.walker = prev_walker;
1956 return FALSE;
1957 }
1958
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001959 setvar_s(v, nextword(&w->cur));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001960 return TRUE;
1961}
1962
1963/* evaluate node, return 1 when result is true, 0 otherwise */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001964static int ptest(node *pattern)
1965{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001966 /* ptest__v is "static": to save stack space? */
1967 return istrue(evaluate(pattern, &G.ptest__v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001968}
1969
1970/* read next record from stream rsm into a variable v */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001971static int awk_getline(rstream *rsm, var *v)
1972{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001973 char *b;
1974 regmatch_t pmatch[2];
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001975 int size, a, p, pp = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001976 int fd, so, eo, r, rp;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001977 char c, *m, *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001978
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02001979 debug_printf_eval("entered %s()\n", __func__);
1980
Glenn L McGrath545106f2002-11-11 06:21:00 +00001981 /* we're using our own buffer since we need access to accumulating
1982 * characters
1983 */
1984 fd = fileno(rsm->F);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001985 m = rsm->buffer;
1986 a = rsm->adv;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001987 p = rsm->pos;
1988 size = rsm->size;
1989 c = (char) rsplitter.n.info;
1990 rp = 0;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001991
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001992 if (!m)
1993 m = qrealloc(m, 256, &size);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001994
Glenn L McGrath545106f2002-11-11 06:21:00 +00001995 do {
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001996 b = m + a;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001997 so = eo = p;
1998 r = 1;
1999 if (p > 0) {
2000 if ((rsplitter.n.info & OPCLSMASK) == OC_REGEXP) {
2001 if (regexec(icase ? rsplitter.n.r.ire : rsplitter.n.l.re,
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002002 b, 1, pmatch, 0) == 0) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002003 so = pmatch[0].rm_so;
2004 eo = pmatch[0].rm_eo;
2005 if (b[eo] != '\0')
2006 break;
2007 }
2008 } else if (c != '\0') {
2009 s = strchr(b+pp, c);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002010 if (!s)
2011 s = memchr(b+pp, '\0', p - pp);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002012 if (s) {
2013 so = eo = s-b;
2014 eo++;
2015 break;
2016 }
2017 } else {
2018 while (b[rp] == '\n')
2019 rp++;
2020 s = strstr(b+rp, "\n\n");
2021 if (s) {
2022 so = eo = s-b;
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002023 while (b[eo] == '\n')
2024 eo++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002025 if (b[eo] != '\0')
2026 break;
2027 }
2028 }
2029 }
2030
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002031 if (a > 0) {
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002032 memmove(m, m+a, p+1);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002033 b = m;
2034 a = 0;
2035 }
2036
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002037 m = qrealloc(m, a+p+128, &size);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002038 b = m + a;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002039 pp = p;
2040 p += safe_read(fd, b+p, size-p-1);
2041 if (p < pp) {
2042 p = 0;
2043 r = 0;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002044 setvar_i(intvar[ERRNO], errno);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002045 }
2046 b[p] = '\0';
2047
2048 } while (p > pp);
2049
2050 if (p == 0) {
2051 r--;
2052 } else {
2053 c = b[so]; b[so] = '\0';
2054 setvar_s(v, b+rp);
2055 v->type |= VF_USER;
2056 b[so] = c;
2057 c = b[eo]; b[eo] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002058 setvar_s(intvar[RT], b+so);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002059 b[eo] = c;
2060 }
2061
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002062 rsm->buffer = m;
2063 rsm->adv = a + eo;
2064 rsm->pos = p - eo;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002065 rsm->size = size;
2066
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002067 debug_printf_eval("returning from %s(): %d\n", __func__, r);
2068
Glenn L McGrath545106f2002-11-11 06:21:00 +00002069 return r;
2070}
2071
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00002072static int fmt_num(char *b, int size, const char *format, double n, int int_as_int)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002073{
Denis Vlasenkof782f522007-01-01 23:51:30 +00002074 int r = 0;
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00002075 char c;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002076 const char *s = format;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002077
Denys Vlasenko1390a012013-07-20 21:23:01 +02002078 if (int_as_int && n == (long long)n) {
2079 r = snprintf(b, size, "%lld", (long long)n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002080 } else {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002081 do { c = *s; } while (c && *++s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002082 if (strchr("diouxX", c)) {
2083 r = snprintf(b, size, format, (int)n);
2084 } else if (strchr("eEfgG", c)) {
2085 r = snprintf(b, size, format, n);
2086 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002087 syntax_error(EMSG_INV_FMT);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002088 }
2089 }
2090 return r;
2091}
2092
Glenn L McGrath545106f2002-11-11 06:21:00 +00002093/* formatted output into an allocated buffer, return ptr to buffer */
Mike Frysinger10a11e22005-09-27 02:23:02 +00002094static char *awk_printf(node *n)
2095{
Glenn L McGrath545106f2002-11-11 06:21:00 +00002096 char *b = NULL;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002097 char *fmt, *s, *f;
2098 const char *s1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002099 int i, j, incr, bsize;
2100 char c, c1;
2101 var *v, *arg;
2102
2103 v = nvalloc(1);
Rob Landleyd921b2e2006-08-03 15:41:12 +00002104 fmt = f = xstrdup(getvar_s(evaluate(nextarg(&n), v)));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002105
2106 i = 0;
2107 while (*f) {
2108 s = f;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002109 while (*f && (*f != '%' || *++f == '%'))
Glenn L McGrath545106f2002-11-11 06:21:00 +00002110 f++;
Denis Vlasenko389f9d52007-05-09 21:57:23 +00002111 while (*f && !isalpha(*f)) {
2112 if (*f == '*')
2113 syntax_error("%*x formats are not supported");
Glenn L McGrath545106f2002-11-11 06:21:00 +00002114 f++;
Denis Vlasenko389f9d52007-05-09 21:57:23 +00002115 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002116
2117 incr = (f - s) + MAXVARFMT;
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002118 b = qrealloc(b, incr + i, &bsize);
Denis Vlasenkof782f522007-01-01 23:51:30 +00002119 c = *f;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002120 if (c != '\0')
2121 f++;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002122 c1 = *f;
2123 *f = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00002124 arg = evaluate(nextarg(&n), v);
2125
2126 j = i;
2127 if (c == 'c' || !c) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002128 i += sprintf(b+i, s, is_numeric(arg) ?
2129 (char)getvar_i(arg) : *getvar_s(arg));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002130 } else if (c == 's') {
Denis Vlasenko92758142006-10-03 19:56:34 +00002131 s1 = getvar_s(arg);
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002132 b = qrealloc(b, incr+i+strlen(s1), &bsize);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002133 i += sprintf(b+i, s, s1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002134 } else {
2135 i += fmt_num(b+i, incr, s, getvar_i(arg), FALSE);
2136 }
2137 *f = c1;
2138
2139 /* if there was an error while sprintf, return value is negative */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002140 if (i < j)
2141 i = j;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002142 }
2143
Glenn L McGrath545106f2002-11-11 06:21:00 +00002144 free(fmt);
2145 nvfree(v);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002146 b = xrealloc(b, i + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002147 b[i] = '\0';
2148 return b;
2149}
2150
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002151/* Common substitution routine.
2152 * Replace (nm)'th substring of (src) that matches (rn) with (repl),
2153 * store result into (dest), return number of substitutions.
2154 * If nm = 0, replace all matches.
2155 * If src or dst is NULL, use $0.
2156 * If subexp != 0, enable subexpression matching (\1-\9).
Glenn L McGrath545106f2002-11-11 06:21:00 +00002157 */
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002158static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int subexp)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002159{
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002160 char *resbuf;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002161 const char *sp;
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002162 int match_no, residx, replen, resbufsize;
2163 int regexec_flags;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002164 regmatch_t pmatch[10];
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002165 regex_t sreg, *regex;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002166
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002167 resbuf = NULL;
2168 residx = 0;
2169 match_no = 0;
2170 regexec_flags = 0;
2171 regex = as_regex(rn, &sreg);
2172 sp = getvar_s(src ? src : intvar[F0]);
2173 replen = strlen(repl);
2174 while (regexec(regex, sp, 10, pmatch, regexec_flags) == 0) {
2175 int so = pmatch[0].rm_so;
2176 int eo = pmatch[0].rm_eo;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002177
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002178 //bb_error_msg("match %u: [%u,%u] '%s'%p", match_no+1, so, eo, sp,sp);
2179 resbuf = qrealloc(resbuf, residx + eo + replen, &resbufsize);
2180 memcpy(resbuf + residx, sp, eo);
2181 residx += eo;
2182 if (++match_no >= nm) {
2183 const char *s;
2184 int nbs;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002185
Glenn L McGrath545106f2002-11-11 06:21:00 +00002186 /* replace */
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002187 residx -= (eo - so);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002188 nbs = 0;
2189 for (s = repl; *s; s++) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002190 char c = resbuf[residx++] = *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002191 if (c == '\\') {
2192 nbs++;
2193 continue;
2194 }
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002195 if (c == '&' || (subexp && c >= '0' && c <= '9')) {
2196 int j;
2197 residx -= ((nbs + 3) >> 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002198 j = 0;
2199 if (c != '&') {
2200 j = c - '0';
2201 nbs++;
2202 }
2203 if (nbs % 2) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002204 resbuf[residx++] = c;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002205 } else {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002206 int n = pmatch[j].rm_eo - pmatch[j].rm_so;
2207 resbuf = qrealloc(resbuf, residx + replen + n, &resbufsize);
2208 memcpy(resbuf + residx, sp + pmatch[j].rm_so, n);
2209 residx += n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002210 }
2211 }
2212 nbs = 0;
2213 }
2214 }
2215
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002216 regexec_flags = REG_NOTBOL;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002217 sp += eo;
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002218 if (match_no == nm)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002219 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002220 if (eo == so) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002221 /* Empty match (e.g. "b*" will match anywhere).
2222 * Advance by one char. */
2223//BUG (bug 1333):
2224//gsub(/\<b*/,"") on "abc" will reach this point, advance to "bc"
2225//... and will erroneously match "b" even though it is NOT at the word start.
2226//we need REG_NOTBOW but it does not exist...
Denys Vlasenko7379cd12010-04-04 01:48:12 +02002227//TODO: if EXTRA_COMPAT=y, use GNU matching and re_search,
2228//it should be able to do it correctly.
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002229 /* Subtle: this is safe only because
2230 * qrealloc allocated at least one extra byte */
2231 resbuf[residx] = *sp;
2232 if (*sp == '\0')
2233 goto ret;
2234 sp++;
2235 residx++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002236 }
2237 }
2238
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002239 resbuf = qrealloc(resbuf, residx + strlen(sp), &resbufsize);
2240 strcpy(resbuf + residx, sp);
2241 ret:
2242 //bb_error_msg("end sp:'%s'%p", sp,sp);
2243 setvar_p(dest ? dest : intvar[F0], resbuf);
2244 if (regex == &sreg)
2245 regfree(regex);
2246 return match_no;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002247}
2248
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002249static NOINLINE int do_mktime(const char *ds)
2250{
2251 struct tm then;
2252 int count;
2253
2254 /*memset(&then, 0, sizeof(then)); - not needed */
2255 then.tm_isdst = -1; /* default is unknown */
2256
2257 /* manpage of mktime says these fields are ints,
2258 * so we can sscanf stuff directly into them */
2259 count = sscanf(ds, "%u %u %u %u %u %u %d",
2260 &then.tm_year, &then.tm_mon, &then.tm_mday,
2261 &then.tm_hour, &then.tm_min, &then.tm_sec,
2262 &then.tm_isdst);
2263
2264 if (count < 6
2265 || (unsigned)then.tm_mon < 1
2266 || (unsigned)then.tm_year < 1900
2267 ) {
2268 return -1;
2269 }
2270
2271 then.tm_mon -= 1;
Denys Vlasenkobc3e9472009-09-21 04:16:00 +02002272 then.tm_year -= 1900;
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002273
2274 return mktime(&then);
2275}
2276
2277static NOINLINE var *exec_builtin(node *op, var *res)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002278{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002279#define tspl (G.exec_builtin__tspl)
2280
Glenn L McGrath545106f2002-11-11 06:21:00 +00002281 var *tv;
2282 node *an[4];
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002283 var *av[4];
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002284 const char *as[4];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002285 regmatch_t pmatch[2];
2286 regex_t sreg, *re;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002287 node *spl;
Mike Frysingerf87b3e32005-09-27 04:16:22 +00002288 uint32_t isr, info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002289 int nargs;
2290 time_t tt;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002291 int i, l, ll, n;
2292
2293 tv = nvalloc(4);
2294 isr = info = op->info;
2295 op = op->l.n;
2296
2297 av[2] = av[3] = NULL;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002298 for (i = 0; i < 4 && op; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002299 an[i] = nextarg(&op);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002300 if (isr & 0x09000000)
2301 av[i] = evaluate(an[i], &tv[i]);
2302 if (isr & 0x08000000)
2303 as[i] = getvar_s(av[i]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002304 isr >>= 1;
2305 }
2306
2307 nargs = i;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00002308 if ((uint32_t)nargs < (info >> 30))
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002309 syntax_error(EMSG_TOO_FEW_ARGS);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002310
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02002311 info &= OPNMASK;
2312 switch (info) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002313
Denis Vlasenkof782f522007-01-01 23:51:30 +00002314 case B_a2:
Rob Landleyd8205b32010-10-24 03:27:22 +02002315 if (ENABLE_FEATURE_AWK_LIBM)
2316 setvar_i(res, atan2(getvar_i(av[0]), getvar_i(av[1])));
2317 else
2318 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002319 break;
2320
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002321 case B_sp: {
2322 char *s, *s1;
2323
Glenn L McGrath545106f2002-11-11 06:21:00 +00002324 if (nargs > 2) {
2325 spl = (an[2]->info & OPCLSMASK) == OC_REGEXP ?
2326 an[2] : mk_splitter(getvar_s(evaluate(an[2], &tv[2])), &tspl);
2327 } else {
2328 spl = &fsplitter.n;
2329 }
2330
2331 n = awk_split(as[0], spl, &s);
2332 s1 = s;
2333 clear_array(iamarray(av[1]));
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002334 for (i = 1; i <= n; i++)
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002335 setari_u(av[1], i, nextword(&s));
2336 free(s1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002337 setvar_i(res, n);
2338 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002339 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002340
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002341 case B_ss: {
2342 char *s;
2343
Rob Landleya3896512006-05-07 20:20:34 +00002344 l = strlen(as[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002345 i = getvar_i(av[1]) - 1;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002346 if (i > l)
2347 i = l;
2348 if (i < 0)
2349 i = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002350 n = (nargs > 2) ? getvar_i(av[2]) : l-i;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002351 if (n < 0)
2352 n = 0;
Denis Vlasenko8ae5b282008-07-02 22:47:49 +00002353 s = xstrndup(as[0]+i, n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002354 setvar_p(res, s);
2355 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002356 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +00002357
Denis Vlasenko7cbcd1c2008-08-28 23:16:58 +00002358 /* Bitwise ops must assume that operands are unsigned. GNU Awk 3.1.5:
2359 * awk '{ print or(-1,1) }' gives "4.29497e+09", not "-2.xxxe+09" */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002360 case B_an:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002361 setvar_i(res, getvar_i_int(av[0]) & getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002362 break;
Denis Vlasenkof7996f32007-01-11 17:20:00 +00002363
Denis Vlasenkof782f522007-01-01 23:51:30 +00002364 case B_co:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002365 setvar_i(res, ~getvar_i_int(av[0]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002366 break;
2367
Denis Vlasenkof782f522007-01-01 23:51:30 +00002368 case B_ls:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002369 setvar_i(res, getvar_i_int(av[0]) << getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002370 break;
2371
Denis Vlasenkof782f522007-01-01 23:51:30 +00002372 case B_or:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002373 setvar_i(res, getvar_i_int(av[0]) | getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002374 break;
2375
Denis Vlasenkof782f522007-01-01 23:51:30 +00002376 case B_rs:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002377 setvar_i(res, getvar_i_int(av[0]) >> getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002378 break;
2379
Denis Vlasenkof782f522007-01-01 23:51:30 +00002380 case B_xo:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002381 setvar_i(res, getvar_i_int(av[0]) ^ getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002382 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002383
Denis Vlasenkof782f522007-01-01 23:51:30 +00002384 case B_lo:
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002385 case B_up: {
2386 char *s, *s1;
Rob Landleyd921b2e2006-08-03 15:41:12 +00002387 s1 = s = xstrdup(as[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002388 while (*s1) {
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02002389 //*s1 = (info == B_up) ? toupper(*s1) : tolower(*s1);
2390 if ((unsigned char)((*s1 | 0x20) - 'a') <= ('z' - 'a'))
2391 *s1 = (info == B_up) ? (*s1 & 0xdf) : (*s1 | 0x20);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002392 s1++;
2393 }
2394 setvar_p(res, s);
2395 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002396 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002397
Denis Vlasenkof782f522007-01-01 23:51:30 +00002398 case B_ix:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002399 n = 0;
Rob Landleya3896512006-05-07 20:20:34 +00002400 ll = strlen(as[1]);
2401 l = strlen(as[0]) - ll;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002402 if (ll > 0 && l >= 0) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002403 if (!icase) {
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002404 char *s = strstr(as[0], as[1]);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002405 if (s)
2406 n = (s - as[0]) + 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002407 } else {
2408 /* this piece of code is terribly slow and
2409 * really should be rewritten
2410 */
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002411 for (i = 0; i <= l; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002412 if (strncasecmp(as[0]+i, as[1], ll) == 0) {
2413 n = i+1;
2414 break;
2415 }
2416 }
2417 }
2418 }
2419 setvar_i(res, n);
2420 break;
2421
Denis Vlasenkof782f522007-01-01 23:51:30 +00002422 case B_ti:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002423 if (nargs > 1)
2424 tt = getvar_i(av[1]);
2425 else
2426 time(&tt);
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002427 //s = (nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y";
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002428 i = strftime(g_buf, MAXVARFMT,
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002429 ((nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"),
2430 localtime(&tt));
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002431 g_buf[i] = '\0';
2432 setvar_s(res, g_buf);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002433 break;
2434
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002435 case B_mt:
2436 setvar_i(res, do_mktime(as[0]));
2437 break;
2438
Denis Vlasenkof782f522007-01-01 23:51:30 +00002439 case B_ma:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002440 re = as_regex(an[1], &sreg);
2441 n = regexec(re, as[0], 1, pmatch, 0);
2442 if (n == 0) {
2443 pmatch[0].rm_so++;
2444 pmatch[0].rm_eo++;
2445 } else {
2446 pmatch[0].rm_so = 0;
2447 pmatch[0].rm_eo = -1;
2448 }
2449 setvar_i(newvar("RSTART"), pmatch[0].rm_so);
2450 setvar_i(newvar("RLENGTH"), pmatch[0].rm_eo - pmatch[0].rm_so);
2451 setvar_i(res, pmatch[0].rm_so);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002452 if (re == &sreg)
2453 regfree(re);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002454 break;
2455
Denis Vlasenkof782f522007-01-01 23:51:30 +00002456 case B_ge:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002457 awk_sub(an[0], as[1], getvar_i(av[2]), av[3], res, TRUE);
2458 break;
2459
Denis Vlasenkof782f522007-01-01 23:51:30 +00002460 case B_gs:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002461 setvar_i(res, awk_sub(an[0], as[1], 0, av[2], av[2], FALSE));
2462 break;
2463
Denis Vlasenkof782f522007-01-01 23:51:30 +00002464 case B_su:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002465 setvar_i(res, awk_sub(an[0], as[1], 1, av[2], av[2], FALSE));
2466 break;
2467 }
2468
2469 nvfree(tv);
2470 return res;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002471#undef tspl
Glenn L McGrath545106f2002-11-11 06:21:00 +00002472}
2473
2474/*
2475 * Evaluate node - the heart of the program. Supplied with subtree
2476 * and place where to store result. returns ptr to result.
2477 */
2478#define XC(n) ((n) >> 8)
2479
Mike Frysinger10a11e22005-09-27 02:23:02 +00002480static var *evaluate(node *op, var *res)
2481{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002482/* This procedure is recursive so we should count every byte */
2483#define fnargs (G.evaluate__fnargs)
2484/* seed is initialized to 1 */
2485#define seed (G.evaluate__seed)
Denys Vlasenkofb132e42010-10-29 11:46:52 +02002486#define sreg (G.evaluate__sreg)
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002487
Glenn L McGrath545106f2002-11-11 06:21:00 +00002488 var *v1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002489
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002490 if (!op)
Glenn L McGrath545106f2002-11-11 06:21:00 +00002491 return setvar_s(res, NULL);
2492
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002493 debug_printf_eval("entered %s()\n", __func__);
2494
Glenn L McGrath545106f2002-11-11 06:21:00 +00002495 v1 = nvalloc(2);
2496
2497 while (op) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002498 struct {
2499 var *v;
2500 const char *s;
2501 } L = L; /* for compiler */
2502 struct {
2503 var *v;
2504 const char *s;
2505 } R = R;
2506 double L_d = L_d;
2507 uint32_t opinfo;
2508 int opn;
2509 node *op1;
2510
Glenn L McGrath545106f2002-11-11 06:21:00 +00002511 opinfo = op->info;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002512 opn = (opinfo & OPNMASK);
2513 g_lineno = op->lineno;
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002514 op1 = op->l.n;
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002515 debug_printf_eval("opinfo:%08x opn:%08x\n", opinfo, opn);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002516
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01002517 /* "delete" is special:
2518 * "delete array[var--]" must evaluate index expr only once,
2519 * must not evaluate it in "execute inevitable things" part.
2520 */
2521 if (XC(opinfo & OPCLSMASK) == XC(OC_DELETE)) {
2522 uint32_t info = op1->info & OPCLSMASK;
2523 var *v;
2524
2525 debug_printf_eval("DELETE\n");
2526 if (info == OC_VAR) {
2527 v = op1->l.v;
2528 } else if (info == OC_FNARG) {
2529 v = &fnargs[op1->l.aidx];
2530 } else {
2531 syntax_error(EMSG_NOT_ARRAY);
2532 }
2533 if (op1->r.n) { /* array ref? */
2534 const char *s;
2535 s = getvar_s(evaluate(op1->r.n, v1));
2536 hash_remove(iamarray(v), s);
2537 } else {
2538 clear_array(iamarray(v));
2539 }
2540 goto next;
2541 }
2542
Mike Frysingerde2b9382005-09-27 03:18:00 +00002543 /* execute inevitable things */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002544 if (opinfo & OF_RES1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002545 L.v = evaluate(op1, v1);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002546 if (opinfo & OF_RES2)
2547 R.v = evaluate(op->r.n, v1+1);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002548 if (opinfo & OF_STR1) {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002549 L.s = getvar_s(L.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002550 debug_printf_eval("L.s:'%s'\n", L.s);
2551 }
2552 if (opinfo & OF_STR2) {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002553 R.s = getvar_s(R.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002554 debug_printf_eval("R.s:'%s'\n", R.s);
2555 }
2556 if (opinfo & OF_NUM1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002557 L_d = getvar_i(L.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002558 debug_printf_eval("L_d:%f\n", L_d);
2559 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002560
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002561 debug_printf_eval("switch(0x%x)\n", XC(opinfo & OPCLSMASK));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002562 switch (XC(opinfo & OPCLSMASK)) {
2563
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002564 /* -- iterative node type -- */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002565
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002566 /* test pattern */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002567 case XC( OC_TEST ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002568 if ((op1->info & OPCLSMASK) == OC_COMMA) {
2569 /* it's range pattern */
2570 if ((opinfo & OF_CHECKED) || ptest(op1->l.n)) {
2571 op->info |= OF_CHECKED;
2572 if (ptest(op1->r.n))
2573 op->info &= ~OF_CHECKED;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002574 op = op->a.n;
2575 } else {
2576 op = op->r.n;
2577 }
2578 } else {
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002579 op = ptest(op1) ? op->a.n : op->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002580 }
2581 break;
2582
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002583 /* just evaluate an expression, also used as unconditional jump */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002584 case XC( OC_EXEC ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002585 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002586
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002587 /* branch, used in if-else and various loops */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002588 case XC( OC_BR ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002589 op = istrue(L.v) ? op->a.n : op->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002590 break;
2591
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002592 /* initialize for-in loop */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002593 case XC( OC_WALKINIT ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002594 hashwalk_init(L.v, iamarray(R.v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002595 break;
2596
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002597 /* get next array item */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002598 case XC( OC_WALKNEXT ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002599 op = hashwalk_next(L.v) ? op->a.n : op->r.n;
2600 break;
2601
Denis Vlasenkof782f522007-01-01 23:51:30 +00002602 case XC( OC_PRINT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002603 case XC( OC_PRINTF ): {
2604 FILE *F = stdout;
2605
Mike Frysingerde2b9382005-09-27 03:18:00 +00002606 if (op->r.n) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002607 rstream *rsm = newfile(R.s);
2608 if (!rsm->F) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002609 if (opn == '|') {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002610 rsm->F = popen(R.s, "w");
2611 if (rsm->F == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002612 bb_perror_msg_and_die("popen");
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002613 rsm->is_pipe = 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002614 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002615 rsm->F = xfopen(R.s, opn=='w' ? "w" : "a");
Glenn L McGrath545106f2002-11-11 06:21:00 +00002616 }
2617 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002618 F = rsm->F;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002619 }
2620
2621 if ((opinfo & OPCLSMASK) == OC_PRINT) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002622 if (!op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002623 fputs(getvar_s(intvar[F0]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002624 } else {
2625 while (op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002626 var *v = evaluate(nextarg(&op1), v1);
2627 if (v->type & VF_NUMBER) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002628 fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[OFMT]),
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002629 getvar_i(v), TRUE);
2630 fputs(g_buf, F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002631 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002632 fputs(getvar_s(v), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002633 }
2634
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002635 if (op1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002636 fputs(getvar_s(intvar[OFS]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002637 }
2638 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002639 fputs(getvar_s(intvar[ORS]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002640
2641 } else { /* OC_PRINTF */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002642 char *s = awk_printf(op1);
2643 fputs(s, F);
2644 free(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002645 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002646 fflush(F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002647 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002648 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002649
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01002650 /* case XC( OC_DELETE ): - moved to happen before arg evaluation */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002651
Denis Vlasenkof782f522007-01-01 23:51:30 +00002652 case XC( OC_NEWSOURCE ):
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002653 g_progname = op->l.new_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002654 break;
2655
Denis Vlasenkof782f522007-01-01 23:51:30 +00002656 case XC( OC_RETURN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002657 copyvar(res, L.v);
2658 break;
2659
Denis Vlasenkof782f522007-01-01 23:51:30 +00002660 case XC( OC_NEXTFILE ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002661 nextfile = TRUE;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002662 case XC( OC_NEXT ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002663 nextrec = TRUE;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002664 case XC( OC_DONE ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002665 clrvar(res);
2666 break;
2667
Denis Vlasenkof782f522007-01-01 23:51:30 +00002668 case XC( OC_EXIT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002669 awk_exit(L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002670
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002671 /* -- recursive node type -- */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002672
Denis Vlasenkof782f522007-01-01 23:51:30 +00002673 case XC( OC_VAR ):
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01002674 debug_printf_eval("VAR\n");
Mike Frysingerde2b9382005-09-27 03:18:00 +00002675 L.v = op->l.v;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002676 if (L.v == intvar[NF])
Glenn L McGrath545106f2002-11-11 06:21:00 +00002677 split_f0();
2678 goto v_cont;
2679
Denis Vlasenkof782f522007-01-01 23:51:30 +00002680 case XC( OC_FNARG ):
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01002681 debug_printf_eval("FNARG[%d]\n", op->l.aidx);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002682 L.v = &fnargs[op->l.aidx];
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002683 v_cont:
2684 res = op->r.n ? findvar(iamarray(L.v), R.s) : L.v;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002685 break;
2686
Denis Vlasenkof782f522007-01-01 23:51:30 +00002687 case XC( OC_IN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002688 setvar_i(res, hash_search(iamarray(R.v), L.s) ? 1 : 0);
2689 break;
2690
Denis Vlasenkof782f522007-01-01 23:51:30 +00002691 case XC( OC_REGEXP ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002692 op1 = op;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002693 L.s = getvar_s(intvar[F0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002694 goto re_cont;
2695
Denis Vlasenkof782f522007-01-01 23:51:30 +00002696 case XC( OC_MATCH ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002697 op1 = op->r.n;
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002698 re_cont:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002699 {
2700 regex_t *re = as_regex(op1, &sreg);
2701 int i = regexec(re, L.s, 0, NULL, 0);
2702 if (re == &sreg)
2703 regfree(re);
2704 setvar_i(res, (i == 0) ^ (opn == '!'));
2705 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002706 break;
2707
Denis Vlasenkof782f522007-01-01 23:51:30 +00002708 case XC( OC_MOVE ):
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002709 debug_printf_eval("MOVE\n");
Mike Frysingerde2b9382005-09-27 03:18:00 +00002710 /* if source is a temporary string, jusk relink it to dest */
Denys Vlasenko12847742009-11-30 01:15:04 +01002711//Disabled: if R.v is numeric but happens to have cached R.v->string,
2712//then L.v ends up being a string, which is wrong
2713// if (R.v == v1+1 && R.v->string) {
2714// res = setvar_p(L.v, R.v->string);
2715// R.v->string = NULL;
2716// } else {
Mike Frysingerde2b9382005-09-27 03:18:00 +00002717 res = copyvar(L.v, R.v);
Denys Vlasenko12847742009-11-30 01:15:04 +01002718// }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002719 break;
2720
Denis Vlasenkof782f522007-01-01 23:51:30 +00002721 case XC( OC_TERNARY ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002722 if ((op->r.n->info & OPCLSMASK) != OC_COLON)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002723 syntax_error(EMSG_POSSIBLE_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002724 res = evaluate(istrue(L.v) ? op->r.n->l.n : op->r.n->r.n, res);
2725 break;
2726
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002727 case XC( OC_FUNC ): {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002728 var *vbeg, *v;
2729 const char *sv_progname;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002730
Bernhard Reutner-Fischerb79a0fe2013-03-06 21:01:05 +01002731 /* The body might be empty, still has to eval the args */
Bernhard Reutner-Fischera060a1a2013-07-31 15:29:20 +02002732 if (!op->r.n->info && !op->r.f->body.first)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002733 syntax_error(EMSG_UNDEF_FUNC);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002734
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002735 vbeg = v = nvalloc(op->r.f->nargs + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002736 while (op1) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002737 var *arg = evaluate(nextarg(&op1), v1);
2738 copyvar(v, arg);
2739 v->type |= VF_CHILD;
2740 v->x.parent = arg;
2741 if (++v - vbeg >= op->r.f->nargs)
Glenn L McGrath545106f2002-11-11 06:21:00 +00002742 break;
2743 }
2744
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002745 v = fnargs;
2746 fnargs = vbeg;
2747 sv_progname = g_progname;
2748
2749 res = evaluate(op->r.f->body.first, res);
2750
2751 g_progname = sv_progname;
2752 nvfree(fnargs);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002753 fnargs = v;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002754
Glenn L McGrath545106f2002-11-11 06:21:00 +00002755 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002756 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002757
Denis Vlasenkof782f522007-01-01 23:51:30 +00002758 case XC( OC_GETLINE ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002759 case XC( OC_PGETLINE ): {
2760 rstream *rsm;
2761 int i;
2762
Mike Frysingerde2b9382005-09-27 03:18:00 +00002763 if (op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002764 rsm = newfile(L.s);
2765 if (!rsm->F) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002766 if ((opinfo & OPCLSMASK) == OC_PGETLINE) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002767 rsm->F = popen(L.s, "r");
2768 rsm->is_pipe = TRUE;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002769 } else {
Denys Vlasenkofb132e42010-10-29 11:46:52 +02002770 rsm->F = fopen_for_read(L.s); /* not xfopen! */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002771 }
2772 }
2773 } else {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002774 if (!iF)
2775 iF = next_input_file();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002776 rsm = iF;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002777 }
2778
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02002779 if (!rsm || !rsm->F) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002780 setvar_i(intvar[ERRNO], errno);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002781 setvar_i(res, -1);
2782 break;
2783 }
2784
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002785 if (!op->r.n)
2786 R.v = intvar[F0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002787
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002788 i = awk_getline(rsm, R.v);
2789 if (i > 0 && !op1) {
2790 incvar(intvar[FNR]);
2791 incvar(intvar[NR]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002792 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002793 setvar_i(res, i);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002794 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002795 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002796
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002797 /* simple builtins */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002798 case XC( OC_FBLTIN ): {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002799 double R_d = R_d; /* for compiler */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002800
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002801 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002802 case F_in:
Denys Vlasenko1390a012013-07-20 21:23:01 +02002803 R_d = (long long)L_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002804 break;
2805
Denis Vlasenkof782f522007-01-01 23:51:30 +00002806 case F_rn:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002807 R_d = (double)rand() / (double)RAND_MAX;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002808 break;
Rob Landleyd8205b32010-10-24 03:27:22 +02002809
Denis Vlasenkof782f522007-01-01 23:51:30 +00002810 case F_co:
Rob Landleyd8205b32010-10-24 03:27:22 +02002811 if (ENABLE_FEATURE_AWK_LIBM) {
2812 R_d = cos(L_d);
2813 break;
2814 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002815
Denis Vlasenkof782f522007-01-01 23:51:30 +00002816 case F_ex:
Rob Landleyd8205b32010-10-24 03:27:22 +02002817 if (ENABLE_FEATURE_AWK_LIBM) {
2818 R_d = exp(L_d);
2819 break;
2820 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002821
Denis Vlasenkof782f522007-01-01 23:51:30 +00002822 case F_lg:
Rob Landleyd8205b32010-10-24 03:27:22 +02002823 if (ENABLE_FEATURE_AWK_LIBM) {
2824 R_d = log(L_d);
2825 break;
2826 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002827
Denis Vlasenkof782f522007-01-01 23:51:30 +00002828 case F_si:
Rob Landleyd8205b32010-10-24 03:27:22 +02002829 if (ENABLE_FEATURE_AWK_LIBM) {
2830 R_d = sin(L_d);
2831 break;
2832 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002833
Denis Vlasenkof782f522007-01-01 23:51:30 +00002834 case F_sq:
Rob Landleyd8205b32010-10-24 03:27:22 +02002835 if (ENABLE_FEATURE_AWK_LIBM) {
2836 R_d = sqrt(L_d);
2837 break;
2838 }
2839
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002840 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002841 break;
Rob Landleyd8205b32010-10-24 03:27:22 +02002842
Denis Vlasenkof782f522007-01-01 23:51:30 +00002843 case F_sr:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002844 R_d = (double)seed;
2845 seed = op1 ? (unsigned)L_d : (unsigned)time(NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002846 srand(seed);
2847 break;
2848
Denis Vlasenkof782f522007-01-01 23:51:30 +00002849 case F_ti:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002850 R_d = time(NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002851 break;
2852
Denis Vlasenkof782f522007-01-01 23:51:30 +00002853 case F_le:
Denys Vlasenko7985bc12013-10-12 04:51:54 +02002854 debug_printf_eval("length: L.s:'%s'\n", L.s);
2855 if (!op1) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002856 L.s = getvar_s(intvar[F0]);
Denys Vlasenko7985bc12013-10-12 04:51:54 +02002857 debug_printf_eval("length: L.s='%s'\n", L.s);
2858 }
2859 else if (L.v->type & VF_ARRAY) {
2860 R_d = L.v->x.array->nel;
2861 debug_printf_eval("length: array_len:%d\n", L.v->x.array->nel);
2862 break;
2863 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002864 R_d = strlen(L.s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002865 break;
2866
Denis Vlasenkof782f522007-01-01 23:51:30 +00002867 case F_sy:
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002868 fflush_all();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002869 R_d = (ENABLE_FEATURE_ALLOW_EXEC && L.s && *L.s)
Denis Vlasenko249fabf2006-12-19 00:29:22 +00002870 ? (system(L.s) >> 8) : 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002871 break;
2872
Denis Vlasenkof782f522007-01-01 23:51:30 +00002873 case F_ff:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002874 if (!op1) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002875 fflush(stdout);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002876 } else if (L.s && *L.s) {
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002877 rstream *rsm = newfile(L.s);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002878 fflush(rsm->F);
2879 } else {
2880 fflush_all();
Glenn L McGrath545106f2002-11-11 06:21:00 +00002881 }
2882 break;
2883
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002884 case F_cl: {
2885 rstream *rsm;
2886 int err = 0;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002887 rsm = (rstream *)hash_search(fdhash, L.s);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002888 debug_printf_eval("OC_FBLTIN F_cl rsm:%p\n", rsm);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002889 if (rsm) {
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002890 debug_printf_eval("OC_FBLTIN F_cl "
2891 "rsm->is_pipe:%d, ->F:%p\n",
2892 rsm->is_pipe, rsm->F);
2893 /* Can be NULL if open failed. Example:
2894 * getline line <"doesnt_exist";
2895 * close("doesnt_exist"); <--- here rsm->F is NULL
2896 */
2897 if (rsm->F)
2898 err = rsm->is_pipe ? pclose(rsm->F) : fclose(rsm->F);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002899 free(rsm->buffer);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002900 hash_remove(fdhash, L.s);
2901 }
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002902 if (err)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002903 setvar_i(intvar[ERRNO], errno);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002904 R_d = (double)err;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002905 break;
2906 }
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002907 } /* switch */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002908 setvar_i(res, R_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002909 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002910 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002911
Denis Vlasenkof782f522007-01-01 23:51:30 +00002912 case XC( OC_BUILTIN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002913 res = exec_builtin(op, res);
2914 break;
2915
Denis Vlasenkof782f522007-01-01 23:51:30 +00002916 case XC( OC_SPRINTF ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002917 setvar_p(res, awk_printf(op1));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002918 break;
2919
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002920 case XC( OC_UNARY ): {
2921 double Ld, R_d;
2922
2923 Ld = R_d = getvar_i(R.v);
Mike Frysingerde2b9382005-09-27 03:18:00 +00002924 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002925 case 'P':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002926 Ld = ++R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002927 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002928 case 'p':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002929 R_d++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002930 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002931 case 'M':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002932 Ld = --R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002933 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002934 case 'm':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002935 R_d--;
2936 r_op_change:
2937 setvar_i(R.v, R_d);
2938 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002939 case '!':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002940 Ld = !istrue(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002941 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002942 case '-':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002943 Ld = -R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002944 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002945 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002946 setvar_i(res, Ld);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002947 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002948 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002949
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002950 case XC( OC_FIELD ): {
2951 int i = (int)getvar_i(R.v);
2952 if (i == 0) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002953 res = intvar[F0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002954 } else {
2955 split_f0();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002956 if (i > nfields)
2957 fsrealloc(i);
2958 res = &Fields[i - 1];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002959 }
2960 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002961 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002962
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002963 /* concatenation (" ") and index joining (",") */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002964 case XC( OC_CONCAT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002965 case XC( OC_COMMA ): {
2966 const char *sep = "";
2967 if ((opinfo & OPCLSMASK) == OC_COMMA)
2968 sep = getvar_s(intvar[SUBSEP]);
2969 setvar_p(res, xasprintf("%s%s%s", L.s, sep, R.s));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002970 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002971 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002972
Denis Vlasenkof782f522007-01-01 23:51:30 +00002973 case XC( OC_LAND ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002974 setvar_i(res, istrue(L.v) ? ptest(op->r.n) : 0);
2975 break;
2976
Denis Vlasenkof782f522007-01-01 23:51:30 +00002977 case XC( OC_LOR ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002978 setvar_i(res, istrue(L.v) ? 1 : ptest(op->r.n));
2979 break;
2980
Denis Vlasenkof782f522007-01-01 23:51:30 +00002981 case XC( OC_BINARY ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002982 case XC( OC_REPLACE ): {
2983 double R_d = getvar_i(R.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002984 debug_printf_eval("BINARY/REPLACE: R_d:%f opn:%c\n", R_d, opn);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002985 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002986 case '+':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002987 L_d += R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002988 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002989 case '-':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002990 L_d -= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002991 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002992 case '*':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002993 L_d *= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002994 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002995 case '/':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002996 if (R_d == 0)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002997 syntax_error(EMSG_DIV_BY_ZERO);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002998 L_d /= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002999 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003000 case '&':
Rob Landleyd8205b32010-10-24 03:27:22 +02003001 if (ENABLE_FEATURE_AWK_LIBM)
3002 L_d = pow(L_d, R_d);
3003 else
3004 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003005 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003006 case '%':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003007 if (R_d == 0)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003008 syntax_error(EMSG_DIV_BY_ZERO);
Denys Vlasenko1390a012013-07-20 21:23:01 +02003009 L_d -= (long long)(L_d / R_d) * R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003010 break;
3011 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02003012 debug_printf_eval("BINARY/REPLACE result:%f\n", L_d);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003013 res = setvar_i(((opinfo & OPCLSMASK) == OC_BINARY) ? res : L.v, L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003014 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003015 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003016
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003017 case XC( OC_COMPARE ): {
3018 int i = i; /* for compiler */
3019 double Ld;
3020
Glenn L McGrath545106f2002-11-11 06:21:00 +00003021 if (is_numeric(L.v) && is_numeric(R.v)) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003022 Ld = getvar_i(L.v) - getvar_i(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003023 } else {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01003024 const char *l = getvar_s(L.v);
3025 const char *r = getvar_s(R.v);
3026 Ld = icase ? strcasecmp(l, r) : strcmp(l, r);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003027 }
3028 switch (opn & 0xfe) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00003029 case 0:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003030 i = (Ld > 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003031 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003032 case 2:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003033 i = (Ld >= 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003034 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003035 case 4:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003036 i = (Ld == 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003037 break;
3038 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003039 setvar_i(res, (i == 0) ^ (opn & 1));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003040 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003041 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003042
Denis Vlasenkof782f522007-01-01 23:51:30 +00003043 default:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003044 syntax_error(EMSG_POSSIBLE_ERROR);
Denys Vlasenko6f4a7852018-01-07 01:19:08 +01003045 } /* switch */
3046 next:
Glenn L McGrath545106f2002-11-11 06:21:00 +00003047 if ((opinfo & OPCLSMASK) <= SHIFT_TIL_THIS)
3048 op = op->a.n;
3049 if ((opinfo & OPCLSMASK) >= RECUR_FROM_THIS)
3050 break;
3051 if (nextrec)
3052 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003053 } /* while (op) */
3054
Glenn L McGrath545106f2002-11-11 06:21:00 +00003055 nvfree(v1);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003056 debug_printf_eval("returning from %s(): %p\n", __func__, res);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003057 return res;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003058#undef fnargs
3059#undef seed
3060#undef sreg
Glenn L McGrath545106f2002-11-11 06:21:00 +00003061}
3062
3063
3064/* -------- main & co. -------- */
3065
Mike Frysinger10a11e22005-09-27 02:23:02 +00003066static int awk_exit(int r)
3067{
Denis Vlasenkof782f522007-01-01 23:51:30 +00003068 var tv;
3069 unsigned i;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003070 hash_item *hi;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003071
Denis Vlasenkof782f522007-01-01 23:51:30 +00003072 zero_out_var(&tv);
3073
3074 if (!exiting) {
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003075 exiting = TRUE;
Glenn L McGrathca29ffc2004-09-24 09:24:27 +00003076 nextrec = FALSE;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003077 evaluate(endseq.first, &tv);
3078 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003079
3080 /* waiting for children */
Denis Vlasenkof782f522007-01-01 23:51:30 +00003081 for (i = 0; i < fdhash->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003082 hi = fdhash->items[i];
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00003083 while (hi) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003084 if (hi->data.rs.F && hi->data.rs.is_pipe)
3085 pclose(hi->data.rs.F);
3086 hi = hi->next;
3087 }
3088 }
3089
3090 exit(r);
3091}
3092
3093/* if expr looks like "var=value", perform assignment and return 1,
3094 * otherwise return 0 */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00003095static int is_assignment(const char *expr)
Mike Frysinger10a11e22005-09-27 02:23:02 +00003096{
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003097 char *exprc, *val;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003098
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003099 if (!isalnum_(*expr) || (val = strchr(expr, '=')) == NULL) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003100 return FALSE;
3101 }
3102
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003103 exprc = xstrdup(expr);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003104 val = exprc + (val - expr);
3105 *val++ = '\0';
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003106
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003107 unescape_string_in_place(val);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003108 setvar_u(newvar(exprc), val);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003109 free(exprc);
3110 return TRUE;
3111}
3112
3113/* switch to next input file */
Mike Frysinger10a11e22005-09-27 02:23:02 +00003114static rstream *next_input_file(void)
3115{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003116#define rsm (G.next_input_file__rsm)
3117#define files_happen (G.next_input_file__files_happen)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003118
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003119 FILE *F;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00003120 const char *fname, *ind;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003121
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003122 if (rsm.F)
3123 fclose(rsm.F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003124 rsm.F = NULL;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003125 rsm.pos = rsm.adv = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003126
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003127 for (;;) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003128 if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003129 if (files_happen)
3130 return NULL;
3131 fname = "-";
3132 F = stdin;
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003133 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003134 }
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003135 ind = getvar_s(incvar(intvar[ARGIND]));
3136 fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind));
3137 if (fname && *fname && !is_assignment(fname)) {
3138 F = xfopen_stdin(fname);
3139 break;
3140 }
3141 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003142
3143 files_happen = TRUE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003144 setvar_s(intvar[FILENAME], fname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003145 rsm.F = F;
3146 return &rsm;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003147#undef rsm
3148#undef files_happen
Glenn L McGrath545106f2002-11-11 06:21:00 +00003149}
3150
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00003151int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko844a6c52018-01-07 14:39:34 +01003152int awk_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger10a11e22005-09-27 02:23:02 +00003153{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00003154 unsigned opt;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02003155 char *opt_F;
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003156 llist_t *list_v = NULL;
3157 llist_t *list_f = NULL;
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003158#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
3159 llist_t *list_e = NULL;
3160#endif
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003161 int i, j;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003162 var *v;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003163 var tv;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003164 char **envp;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003165 char *vnames = (char *)vNames; /* cheat */
3166 char *vvalues = (char *)vValues;
3167
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003168 INIT_G();
3169
Denis Vlasenko150f4022007-01-13 21:06:21 +00003170 /* Undo busybox.c, or else strtod may eat ','! This breaks parsing:
Denis Vlasenko6dc6ebb2007-01-01 23:53:12 +00003171 * $1,$2 == '$1,' '$2', NOT '$1' ',' '$2' */
3172 if (ENABLE_LOCALE_SUPPORT)
3173 setlocale(LC_NUMERIC, "C");
3174
Denis Vlasenkof782f522007-01-01 23:51:30 +00003175 zero_out_var(&tv);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003176
3177 /* allocate global buffer */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003178 g_buf = xmalloc(MAXVARFMT + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003179
3180 vhash = hash_init();
3181 ahash = hash_init();
3182 fdhash = hash_init();
3183 fnhash = hash_init();
3184
3185 /* initialize variables */
Denis Vlasenkof782f522007-01-01 23:51:30 +00003186 for (i = 0; *vnames; i++) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003187 intvar[i] = v = newvar(nextword(&vnames));
Denis Vlasenkof782f522007-01-01 23:51:30 +00003188 if (*vvalues != '\377')
3189 setvar_s(v, nextword(&vvalues));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003190 else
3191 setvar_i(v, 0);
3192
Denis Vlasenkof782f522007-01-01 23:51:30 +00003193 if (*vnames == '*') {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003194 v->type |= VF_SPECIAL;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003195 vnames++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003196 }
3197 }
3198
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003199 handle_special(intvar[FS]);
3200 handle_special(intvar[RS]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003201
Denis Vlasenkof782f522007-01-01 23:51:30 +00003202 newfile("/dev/stdin")->F = stdin;
3203 newfile("/dev/stdout")->F = stdout;
3204 newfile("/dev/stderr")->F = stderr;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003205
Denis Vlasenkof71d9162007-05-03 22:57:56 +00003206 /* Huh, people report that sometimes environ is NULL. Oh well. */
3207 if (environ) for (envp = environ; *envp; envp++) {
Denis Vlasenkob78c7822007-07-18 18:31:11 +00003208 /* environ is writable, thus we don't strdup it needlessly */
3209 char *s = *envp;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003210 char *s1 = strchr(s, '=');
3211 if (s1) {
Denis Vlasenkob78c7822007-07-18 18:31:11 +00003212 *s1 = '\0';
3213 /* Both findvar and setvar_u take const char*
3214 * as 2nd arg -> environment is not trashed */
3215 setvar_u(findvar(iamarray(intvar[ENVIRON]), s), s1 + 1);
3216 *s1 = '=';
Eric Andersen67776be2004-07-30 23:52:08 +00003217 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003218 }
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003219 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 +00003220 argv += optind;
Denys Vlasenko844a6c52018-01-07 14:39:34 +01003221 //argc -= optind;
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003222 if (opt & OPT_W)
3223 bb_error_msg("warning: option -W is ignored");
3224 if (opt & OPT_F) {
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003225 unescape_string_in_place(opt_F);
3226 setvar_s(intvar[FS], opt_F);
3227 }
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003228 while (list_v) {
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003229 if (!is_assignment(llist_pop(&list_v)))
Denis Vlasenkobe644a82007-03-10 17:22:14 +00003230 bb_show_usage();
3231 }
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003232 while (list_f) {
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003233 char *s = NULL;
3234 FILE *from_file;
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003235
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003236 g_progname = llist_pop(&list_f);
3237 from_file = xfopen_stdin(g_progname);
3238 /* one byte is reserved for some trick in next_token */
3239 for (i = j = 1; j > 0; i += j) {
3240 s = xrealloc(s, i + 4096);
3241 j = fread(s + i, 1, 4094, from_file);
3242 }
3243 s[i] = '\0';
3244 fclose(from_file);
3245 parse_program(s + 1);
3246 free(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003247 }
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003248 g_progname = "cmd. line";
3249#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003250 while (list_e) {
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003251 parse_program(llist_pop(&list_e));
3252 }
3253#endif
3254 if (!(opt & (OPT_f | OPT_e))) {
3255 if (!*argv)
3256 bb_show_usage();
3257 parse_program(*argv++);
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003258 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003259
Glenn L McGrath545106f2002-11-11 06:21:00 +00003260 /* fill in ARGV array */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003261 setari_u(intvar[ARGV], 0, "awk");
Denis Vlasenkof782f522007-01-01 23:51:30 +00003262 i = 0;
3263 while (*argv)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003264 setari_u(intvar[ARGV], ++i, *argv++);
Denys Vlasenko844a6c52018-01-07 14:39:34 +01003265 setvar_i(intvar[ARGC], i + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003266
3267 evaluate(beginseq.first, &tv);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003268 if (!mainseq.first && !endseq.first)
Glenn L McGrath545106f2002-11-11 06:21:00 +00003269 awk_exit(EXIT_SUCCESS);
3270
3271 /* input file could already be opened in BEGIN block */
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003272 if (!iF)
3273 iF = next_input_file();
Glenn L McGrath545106f2002-11-11 06:21:00 +00003274
3275 /* passing through input files */
3276 while (iF) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003277 nextfile = FALSE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003278 setvar_i(intvar[FNR], 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003279
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003280 while ((i = awk_getline(iF, intvar[F0])) > 0) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003281 nextrec = FALSE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003282 incvar(intvar[NR]);
3283 incvar(intvar[FNR]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003284 evaluate(mainseq.first, &tv);
3285
3286 if (nextfile)
3287 break;
3288 }
3289
Denis Vlasenkof782f522007-01-01 23:51:30 +00003290 if (i < 0)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003291 syntax_error(strerror(errno));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003292
3293 iF = next_input_file();
Glenn L McGrath545106f2002-11-11 06:21:00 +00003294 }
3295
Glenn L McGrath545106f2002-11-11 06:21:00 +00003296 awk_exit(EXIT_SUCCESS);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003297 /*return 0;*/
Glenn L McGrath545106f2002-11-11 06:21:00 +00003298}