blob: cc17ad438c83ae02c2b6ce32798e1228c64fdaba [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 */
9
Sven-Göran Berghf200f732013-11-12 14:18:25 +010010//config:config AWK
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "awk (22 kb)"
Sven-Göran Berghf200f732013-11-12 14:18:25 +010012//config: default y
13//config: help
Denys Vlasenko68b653b2017-07-27 10:53:09 +020014//config: Awk is used as a pattern scanning and processing language.
Sven-Göran Berghf200f732013-11-12 14:18:25 +010015//config:
16//config:config FEATURE_AWK_LIBM
17//config: bool "Enable math functions (requires libm)"
18//config: default y
19//config: depends on AWK
20//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020021//config: Enable math functions of the Awk programming language.
22//config: NOTE: This requires libm to be present for linking.
Sven-Göran Berghf200f732013-11-12 14:18:25 +010023//config:
24//config:config FEATURE_AWK_GNU_EXTENSIONS
25//config: bool "Enable a few GNU extensions"
26//config: default y
27//config: depends on AWK
28//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020029//config: Enable a few features from gawk:
30//config: * command line option -e AWK_PROGRAM
31//config: * simultaneous use of -f and -e on the command line.
32//config: This enables the use of awk library files.
33//config: Example: awk -f mylib.awk -e '{print myfunction($1);}' ...
Sven-Göran Berghf200f732013-11-12 14:18:25 +010034
35//applet:IF_AWK(APPLET_NOEXEC(awk, awk, BB_DIR_USR_BIN, BB_SUID_DROP, awk))
36
37//kbuild:lib-$(CONFIG_AWK) += awk.o
38
Pere Orga6a3e01d2011-04-01 22:56:30 +020039//usage:#define awk_trivial_usage
40//usage: "[OPTIONS] [AWK_PROGRAM] [FILE]..."
41//usage:#define awk_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020042//usage: " -v VAR=VAL Set variable"
Pere Orga6a3e01d2011-04-01 22:56:30 +020043//usage: "\n -F SEP Use SEP as field separator"
44//usage: "\n -f FILE Read program from FILE"
Sven-Göran Berghf200f732013-11-12 14:18:25 +010045//usage: IF_FEATURE_AWK_GNU_EXTENSIONS(
46//usage: "\n -e AWK_PROGRAM"
47//usage: )
Pere Orga6a3e01d2011-04-01 22:56:30 +020048
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000049#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000050#include "xregex.h"
51#include <math.h>
Glenn L McGrath545106f2002-11-11 06:21:00 +000052
Denis Vlasenko99912ca2007-04-10 15:43:37 +000053/* This is a NOEXEC applet. Be very careful! */
54
Glenn L McGrath545106f2002-11-11 06:21:00 +000055
Denys Vlasenkoda62b092010-03-11 12:13:18 +010056/* If you comment out one of these below, it will be #defined later
57 * to perform debug printfs to stderr: */
58#define debug_printf_walker(...) do {} while (0)
Denys Vlasenkod527e0c2010-10-05 13:22:11 +020059#define debug_printf_eval(...) do {} while (0)
Denys Vlasenko7b46d112011-09-11 00:30:56 +020060#define debug_printf_parse(...) do {} while (0)
Denys Vlasenkoda62b092010-03-11 12:13:18 +010061
62#ifndef debug_printf_walker
63# define debug_printf_walker(...) (fprintf(stderr, __VA_ARGS__))
64#endif
Denys Vlasenkod527e0c2010-10-05 13:22:11 +020065#ifndef debug_printf_eval
66# define debug_printf_eval(...) (fprintf(stderr, __VA_ARGS__))
67#endif
Denys Vlasenko7b46d112011-09-11 00:30:56 +020068#ifndef debug_printf_parse
69# define debug_printf_parse(...) (fprintf(stderr, __VA_ARGS__))
70#endif
Denys Vlasenkoda62b092010-03-11 12:13:18 +010071
72
Sven-Göran Berghf200f732013-11-12 14:18:25 +010073#define OPTSTR_AWK \
Denys Vlasenko237bedd2016-07-06 21:58:02 +020074 "F:v:*f:*" \
75 IF_FEATURE_AWK_GNU_EXTENSIONS("e:*") \
Sven-Göran Berghf200f732013-11-12 14:18:25 +010076 "W:"
Sven-Göran Berghf200f732013-11-12 14:18:25 +010077enum {
78 OPTBIT_F, /* define field separator */
79 OPTBIT_v, /* define variable */
80 OPTBIT_f, /* pull in awk program from file */
81 IF_FEATURE_AWK_GNU_EXTENSIONS(OPTBIT_e,) /* -e AWK_PROGRAM */
82 OPTBIT_W, /* -W ignored */
83 OPT_F = 1 << OPTBIT_F,
84 OPT_v = 1 << OPTBIT_v,
85 OPT_f = 1 << OPTBIT_f,
86 OPT_e = IF_FEATURE_AWK_GNU_EXTENSIONS((1 << OPTBIT_e)) + 0,
87 OPT_W = 1 << OPTBIT_W
88};
Denys Vlasenkoda62b092010-03-11 12:13:18 +010089
Denis Vlasenko629563b2007-02-24 17:05:52 +000090#define MAXVARFMT 240
91#define MINNVBLOCK 64
Glenn L McGrath545106f2002-11-11 06:21:00 +000092
93/* variable flags */
Denis Vlasenko629563b2007-02-24 17:05:52 +000094#define VF_NUMBER 0x0001 /* 1 = primary type is number */
95#define VF_ARRAY 0x0002 /* 1 = it's an array */
Glenn L McGrath545106f2002-11-11 06:21:00 +000096
Denis Vlasenko629563b2007-02-24 17:05:52 +000097#define VF_CACHED 0x0100 /* 1 = num/str value has cached str/num eq */
98#define VF_USER 0x0200 /* 1 = user input (may be numeric string) */
99#define VF_SPECIAL 0x0400 /* 1 = requires extra handling when changed */
100#define VF_WALK 0x0800 /* 1 = variable has alloc'd x.walker list */
101#define VF_FSTR 0x1000 /* 1 = var::string points to fstring buffer */
102#define VF_CHILD 0x2000 /* 1 = function arg; x.parent points to source */
103#define VF_DIRTY 0x4000 /* 1 = variable was set explicitly */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000104
105/* these flags are static, don't change them when value is changed */
Denis Vlasenko629563b2007-02-24 17:05:52 +0000106#define VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000107
Denys Vlasenkoda62b092010-03-11 12:13:18 +0100108typedef struct walker_list {
109 char *end;
110 char *cur;
111 struct walker_list *prev;
112 char wbuf[1];
113} walker_list;
114
Glenn L McGrath545106f2002-11-11 06:21:00 +0000115/* Variable */
116typedef struct var_s {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000117 unsigned type; /* flags */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000118 double number;
119 char *string;
120 union {
Denis Vlasenko629563b2007-02-24 17:05:52 +0000121 int aidx; /* func arg idx (for compilation stage) */
122 struct xhash_s *array; /* array ptr */
123 struct var_s *parent; /* for func args, ptr to actual parameter */
Denys Vlasenkoda62b092010-03-11 12:13:18 +0100124 walker_list *walker; /* list of array elements (for..in) */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000125 } x;
126} var;
127
128/* Node chain (pattern-action chain, BEGIN, END, function bodies) */
129typedef struct chain_s {
130 struct node_s *first;
131 struct node_s *last;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000132 const char *programname;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000133} chain;
134
135/* Function */
136typedef struct func_s {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000137 unsigned nargs;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000138 struct chain_s body;
139} func;
140
141/* I/O stream */
142typedef struct rstream_s {
143 FILE *F;
144 char *buffer;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +0000145 int adv;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000146 int size;
147 int pos;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000148 smallint is_pipe;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000149} rstream;
150
151typedef struct hash_item_s {
152 union {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000153 struct var_s v; /* variable/array hash */
154 struct rstream_s rs; /* redirect streams hash */
155 struct func_s f; /* functions hash */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000156 } data;
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000157 struct hash_item_s *next; /* next in chain */
158 char name[1]; /* really it's longer */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000159} hash_item;
160
161typedef struct xhash_s {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000162 unsigned nel; /* num of elements */
163 unsigned csize; /* current hash size */
164 unsigned nprime; /* next hash size in PRIMES[] */
165 unsigned glen; /* summary length of item names */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000166 struct hash_item_s **items;
167} xhash;
168
169/* Tree node */
170typedef struct node_s {
Mike Frysingerf87b3e32005-09-27 04:16:22 +0000171 uint32_t info;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000172 unsigned lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000173 union {
174 struct node_s *n;
175 var *v;
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100176 int aidx;
177 char *new_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000178 regex_t *re;
179 } l;
180 union {
181 struct node_s *n;
182 regex_t *ire;
183 func *f;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000184 } r;
185 union {
186 struct node_s *n;
187 } a;
188} node;
189
190/* Block of temporary variables */
191typedef struct nvblock_s {
192 int size;
193 var *pos;
194 struct nvblock_s *prev;
195 struct nvblock_s *next;
Denys Vlasenkod069e532009-09-09 23:12:10 +0200196 var nv[];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000197} nvblock;
198
199typedef struct tsplitter_s {
200 node n;
201 regex_t re[2];
202} tsplitter;
203
204/* simple token classes */
205/* Order and hex values are very important!!! See next_token() */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200206#define TC_SEQSTART (1 << 0) /* ( */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000207#define TC_SEQTERM (1 << 1) /* ) */
208#define TC_REGEXP (1 << 2) /* /.../ */
209#define TC_OUTRDR (1 << 3) /* | > >> */
210#define TC_UOPPOST (1 << 4) /* unary postfix operator */
211#define TC_UOPPRE1 (1 << 5) /* unary prefix operator */
212#define TC_BINOPX (1 << 6) /* two-opnd operator */
213#define TC_IN (1 << 7)
214#define TC_COMMA (1 << 8)
215#define TC_PIPE (1 << 9) /* input redirection pipe */
216#define TC_UOPPRE2 (1 << 10) /* unary prefix operator */
217#define TC_ARRTERM (1 << 11) /* ] */
218#define TC_GRPSTART (1 << 12) /* { */
219#define TC_GRPTERM (1 << 13) /* } */
220#define TC_SEMICOL (1 << 14)
221#define TC_NEWLINE (1 << 15)
222#define TC_STATX (1 << 16) /* ctl statement (for, next...) */
223#define TC_WHILE (1 << 17)
224#define TC_ELSE (1 << 18)
225#define TC_BUILTIN (1 << 19)
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200226/* This costs ~50 bytes of code.
227 * A separate class to support deprecated "length" form. If we don't need that
228 * (i.e. if we demand that only "length()" with () is valid), then TC_LENGTH
229 * can be merged with TC_BUILTIN:
230 */
231#define TC_LENGTH (1 << 20)
232#define TC_GETLINE (1 << 21)
233#define TC_FUNCDECL (1 << 22) /* `function' `func' */
234#define TC_BEGIN (1 << 23)
235#define TC_END (1 << 24)
236#define TC_EOF (1 << 25)
237#define TC_VARIABLE (1 << 26)
238#define TC_ARRAY (1 << 27)
239#define TC_FUNCTION (1 << 28)
240#define TC_STRING (1 << 29)
241#define TC_NUMBER (1 << 30)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000242
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000243#define TC_UOPPRE (TC_UOPPRE1 | TC_UOPPRE2)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000244
245/* combined token classes */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000246#define TC_BINOP (TC_BINOPX | TC_COMMA | TC_PIPE | TC_IN)
Denys Vlasenko1390a012013-07-20 21:23:01 +0200247//#define TC_UNARYOP (TC_UOPPRE | TC_UOPPOST)
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000248#define TC_OPERAND (TC_VARIABLE | TC_ARRAY | TC_FUNCTION \
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200249 | TC_BUILTIN | TC_LENGTH | TC_GETLINE \
250 | TC_SEQSTART | TC_STRING | TC_NUMBER)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000251
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000252#define TC_STATEMNT (TC_STATX | TC_WHILE)
253#define TC_OPTERM (TC_SEMICOL | TC_NEWLINE)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000254
255/* word tokens, cannot mean something else if not expected */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200256#define TC_WORD (TC_IN | TC_STATEMNT | TC_ELSE \
257 | TC_BUILTIN | TC_LENGTH | TC_GETLINE \
258 | TC_FUNCDECL | TC_BEGIN | TC_END)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000259
260/* discard newlines after these */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000261#define TC_NOTERM (TC_COMMA | TC_GRPSTART | TC_GRPTERM \
262 | TC_BINOP | TC_OPTERM)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000263
264/* what can expression begin with */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000265#define TC_OPSEQ (TC_OPERAND | TC_UOPPRE | TC_REGEXP)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000266/* what can group begin with */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000267#define TC_GRPSEQ (TC_OPSEQ | TC_OPTERM | TC_STATEMNT | TC_GRPSTART)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000268
269/* if previous token class is CONCAT1 and next is CONCAT2, concatenation */
270/* operator is inserted between them */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000271#define TC_CONCAT1 (TC_VARIABLE | TC_ARRTERM | TC_SEQTERM \
272 | TC_STRING | TC_NUMBER | TC_UOPPOST)
273#define TC_CONCAT2 (TC_OPERAND | TC_UOPPRE)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000274
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000275#define OF_RES1 0x010000
276#define OF_RES2 0x020000
277#define OF_STR1 0x040000
278#define OF_STR2 0x080000
279#define OF_NUM1 0x100000
280#define OF_CHECKED 0x200000
Glenn L McGrath545106f2002-11-11 06:21:00 +0000281
282/* combined operator flags */
283#define xx 0
284#define xV OF_RES2
285#define xS (OF_RES2 | OF_STR2)
286#define Vx OF_RES1
287#define VV (OF_RES1 | OF_RES2)
288#define Nx (OF_RES1 | OF_NUM1)
289#define NV (OF_RES1 | OF_NUM1 | OF_RES2)
290#define Sx (OF_RES1 | OF_STR1)
291#define SV (OF_RES1 | OF_STR1 | OF_RES2)
292#define SS (OF_RES1 | OF_STR1 | OF_RES2 | OF_STR2)
293
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000294#define OPCLSMASK 0xFF00
295#define OPNMASK 0x007F
Glenn L McGrath545106f2002-11-11 06:21:00 +0000296
297/* operator priority is a highest byte (even: r->l, odd: l->r grouping)
298 * For builtins it has different meaning: n n s3 s2 s1 v3 v2 v1,
299 * n - min. number of args, vN - resolve Nth arg to var, sN - resolve to string
300 */
Denys Vlasenko202a1b92011-09-10 04:51:09 +0200301#undef P
302#undef PRIMASK
303#undef PRIMASK2
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000304#define P(x) (x << 24)
305#define PRIMASK 0x7F000000
306#define PRIMASK2 0x7E000000
Glenn L McGrath545106f2002-11-11 06:21:00 +0000307
308/* Operation classes */
309
310#define SHIFT_TIL_THIS 0x0600
311#define RECUR_FROM_THIS 0x1000
312
313enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000314 OC_DELETE = 0x0100, OC_EXEC = 0x0200, OC_NEWSOURCE = 0x0300,
315 OC_PRINT = 0x0400, OC_PRINTF = 0x0500, OC_WALKINIT = 0x0600,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000316
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000317 OC_BR = 0x0700, OC_BREAK = 0x0800, OC_CONTINUE = 0x0900,
318 OC_EXIT = 0x0a00, OC_NEXT = 0x0b00, OC_NEXTFILE = 0x0c00,
319 OC_TEST = 0x0d00, OC_WALKNEXT = 0x0e00,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000320
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000321 OC_BINARY = 0x1000, OC_BUILTIN = 0x1100, OC_COLON = 0x1200,
322 OC_COMMA = 0x1300, OC_COMPARE = 0x1400, OC_CONCAT = 0x1500,
323 OC_FBLTIN = 0x1600, OC_FIELD = 0x1700, OC_FNARG = 0x1800,
324 OC_FUNC = 0x1900, OC_GETLINE = 0x1a00, OC_IN = 0x1b00,
325 OC_LAND = 0x1c00, OC_LOR = 0x1d00, OC_MATCH = 0x1e00,
326 OC_MOVE = 0x1f00, OC_PGETLINE = 0x2000, OC_REGEXP = 0x2100,
327 OC_REPLACE = 0x2200, OC_RETURN = 0x2300, OC_SPRINTF = 0x2400,
328 OC_TERNARY = 0x2500, OC_UNARY = 0x2600, OC_VAR = 0x2700,
329 OC_DONE = 0x2800,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000330
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000331 ST_IF = 0x3000, ST_DO = 0x3100, ST_FOR = 0x3200,
332 ST_WHILE = 0x3300
Glenn L McGrath545106f2002-11-11 06:21:00 +0000333};
334
335/* simple builtins */
336enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000337 F_in, F_rn, F_co, F_ex, F_lg, F_si, F_sq, F_sr,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000338 F_ti, F_le, F_sy, F_ff, F_cl
339};
340
341/* builtins */
342enum {
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +0200343 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 +0000344 B_ge, B_gs, B_su,
345 B_an, B_co, B_ls, B_or, B_rs, B_xo,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000346};
347
348/* tokens and their corresponding info values */
349
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200350#define NTC "\377" /* switch to next token class (tc<<1) */
351#define NTCC '\377'
Glenn L McGrath545106f2002-11-11 06:21:00 +0000352
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000353static const char tokenlist[] ALIGN1 =
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200354 "\1(" NTC /* TC_SEQSTART */
355 "\1)" NTC /* TC_SEQTERM */
356 "\1/" NTC /* TC_REGEXP */
357 "\2>>" "\1>" "\1|" NTC /* TC_OUTRDR */
358 "\2++" "\2--" NTC /* TC_UOPPOST */
359 "\2++" "\2--" "\1$" NTC /* TC_UOPPRE1 */
360 "\2==" "\1=" "\2+=" "\2-=" /* TC_BINOPX */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200361 "\2*=" "\2/=" "\2%=" "\2^="
362 "\1+" "\1-" "\3**=" "\2**"
363 "\1/" "\1%" "\1^" "\1*"
364 "\2!=" "\2>=" "\2<=" "\1>"
365 "\1<" "\2!~" "\1~" "\2&&"
366 "\2||" "\1?" "\1:" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200367 "\2in" NTC /* TC_IN */
368 "\1," NTC /* TC_COMMA */
369 "\1|" NTC /* TC_PIPE */
370 "\1+" "\1-" "\1!" NTC /* TC_UOPPRE2 */
371 "\1]" NTC /* TC_ARRTERM */
372 "\1{" NTC /* TC_GRPSTART */
373 "\1}" NTC /* TC_GRPTERM */
374 "\1;" NTC /* TC_SEMICOL */
375 "\1\n" NTC /* TC_NEWLINE */
376 "\2if" "\2do" "\3for" "\5break" /* TC_STATX */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200377 "\10continue" "\6delete" "\5print"
378 "\6printf" "\4next" "\10nextfile"
379 "\6return" "\4exit" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200380 "\5while" NTC /* TC_WHILE */
381 "\4else" NTC /* TC_ELSE */
382 "\3and" "\5compl" "\6lshift" "\2or" /* TC_BUILTIN */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200383 "\6rshift" "\3xor"
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200384 "\5close" "\6system" "\6fflush" "\5atan2"
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200385 "\3cos" "\3exp" "\3int" "\3log"
386 "\4rand" "\3sin" "\4sqrt" "\5srand"
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200387 "\6gensub" "\4gsub" "\5index" /* "\6length" was here */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200388 "\5match" "\5split" "\7sprintf" "\3sub"
389 "\6substr" "\7systime" "\10strftime" "\6mktime"
390 "\7tolower" "\7toupper" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200391 "\6length" NTC /* TC_LENGTH */
392 "\7getline" NTC /* TC_GETLINE */
393 "\4func" "\10function" NTC /* TC_FUNCDECL */
394 "\5BEGIN" NTC /* TC_BEGIN */
395 "\3END" /* TC_END */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200396 /* compiler adds trailing "\0" */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000397 ;
398
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200399#define OC_B OC_BUILTIN
400
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000401static const uint32_t tokeninfo[] = {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000402 0,
403 0,
404 OC_REGEXP,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200405 xS|'a', xS|'w', xS|'|',
406 OC_UNARY|xV|P(9)|'p', OC_UNARY|xV|P(9)|'m',
407 OC_UNARY|xV|P(9)|'P', OC_UNARY|xV|P(9)|'M', OC_FIELD|xV|P(5),
408 OC_COMPARE|VV|P(39)|5, OC_MOVE|VV|P(74), OC_REPLACE|NV|P(74)|'+', OC_REPLACE|NV|P(74)|'-',
409 OC_REPLACE|NV|P(74)|'*', OC_REPLACE|NV|P(74)|'/', OC_REPLACE|NV|P(74)|'%', OC_REPLACE|NV|P(74)|'&',
410 OC_BINARY|NV|P(29)|'+', OC_BINARY|NV|P(29)|'-', OC_REPLACE|NV|P(74)|'&', OC_BINARY|NV|P(15)|'&',
411 OC_BINARY|NV|P(25)|'/', OC_BINARY|NV|P(25)|'%', OC_BINARY|NV|P(15)|'&', OC_BINARY|NV|P(25)|'*',
412 OC_COMPARE|VV|P(39)|4, OC_COMPARE|VV|P(39)|3, OC_COMPARE|VV|P(39)|0, OC_COMPARE|VV|P(39)|1,
413 OC_COMPARE|VV|P(39)|2, OC_MATCH|Sx|P(45)|'!', OC_MATCH|Sx|P(45)|'~', OC_LAND|Vx|P(55),
414 OC_LOR|Vx|P(59), OC_TERNARY|Vx|P(64)|'?', OC_COLON|xx|P(67)|':',
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200415 OC_IN|SV|P(49), /* TC_IN */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000416 OC_COMMA|SS|P(80),
417 OC_PGETLINE|SV|P(37),
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200418 OC_UNARY|xV|P(19)|'+', OC_UNARY|xV|P(19)|'-', OC_UNARY|xV|P(19)|'!',
419 0, /* ] */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000420 0,
421 0,
422 0,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200423 0, /* \n */
424 ST_IF, ST_DO, ST_FOR, OC_BREAK,
425 OC_CONTINUE, OC_DELETE|Vx, OC_PRINT,
426 OC_PRINTF, OC_NEXT, OC_NEXTFILE,
427 OC_RETURN|Vx, OC_EXIT|Nx,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000428 ST_WHILE,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200429 0, /* else */
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000430 OC_B|B_an|P(0x83), OC_B|B_co|P(0x41), OC_B|B_ls|P(0x83), OC_B|B_or|P(0x83),
431 OC_B|B_rs|P(0x83), OC_B|B_xo|P(0x83),
Glenn L McGrath545106f2002-11-11 06:21:00 +0000432 OC_FBLTIN|Sx|F_cl, OC_FBLTIN|Sx|F_sy, OC_FBLTIN|Sx|F_ff, OC_B|B_a2|P(0x83),
433 OC_FBLTIN|Nx|F_co, OC_FBLTIN|Nx|F_ex, OC_FBLTIN|Nx|F_in, OC_FBLTIN|Nx|F_lg,
434 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 +0200435 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 +0000436 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 +0200437 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 +0000438 OC_B|B_lo|P(0x49), OC_B|B_up|P(0x49),
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200439 OC_FBLTIN|Sx|F_le, /* TC_LENGTH */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000440 OC_GETLINE|SV|P(0),
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200441 0, 0,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000442 0,
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200443 0 /* TC_END */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000444};
445
446/* internal variable names and their initial values */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000447/* asterisk marks SPECIAL vars; $ is just no-named Field0 */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000448enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000449 CONVFMT, OFMT, FS, OFS,
Denis Vlasenkof782f522007-01-01 23:51:30 +0000450 ORS, RS, RT, FILENAME,
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000451 SUBSEP, F0, ARGIND, ARGC,
452 ARGV, ERRNO, FNR, NR,
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200453 NF, IGNORECASE, ENVIRON, NUM_INTERNAL_VARS
Glenn L McGrath545106f2002-11-11 06:21:00 +0000454};
455
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000456static const char vNames[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000457 "CONVFMT\0" "OFMT\0" "FS\0*" "OFS\0"
458 "ORS\0" "RS\0*" "RT\0" "FILENAME\0"
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000459 "SUBSEP\0" "$\0*" "ARGIND\0" "ARGC\0"
460 "ARGV\0" "ERRNO\0" "FNR\0" "NR\0"
461 "NF\0*" "IGNORECASE\0*" "ENVIRON\0" "\0";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000462
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000463static const char vValues[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000464 "%.6g\0" "%.6g\0" " \0" " \0"
465 "\n\0" "\n\0" "\0" "\0"
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000466 "\034\0" "\0" "\377";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000467
468/* hash size may grow to these values */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000469#define FIRST_PRIME 61
470static const uint16_t PRIMES[] ALIGN2 = { 251, 1021, 4093, 16381, 65521 };
Glenn L McGrath545106f2002-11-11 06:21:00 +0000471
Glenn L McGrath545106f2002-11-11 06:21:00 +0000472
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000473/* Globals. Split in two parts so that first one is addressed
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000474 * with (mostly short) negative offsets.
475 * NB: it's unsafe to put members of type "double"
476 * into globals2 (gcc may fail to align them).
477 */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000478struct globals {
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000479 double t_double;
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000480 chain beginseq, mainseq, endseq;
481 chain *seq;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000482 node *break_ptr, *continue_ptr;
483 rstream *iF;
484 xhash *vhash, *ahash, *fdhash, *fnhash;
485 const char *g_progname;
486 int g_lineno;
487 int nfields;
488 int maxfields; /* used in fsrealloc() only */
489 var *Fields;
490 nvblock *g_cb;
491 char *g_pos;
492 char *g_buf;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000493 smallint icase;
494 smallint exiting;
495 smallint nextrec;
496 smallint nextfile;
497 smallint is_f0_split;
Denys Vlasenko7b46d112011-09-11 00:30:56 +0200498 smallint t_rollback;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000499};
500struct globals2 {
501 uint32_t t_info; /* often used */
502 uint32_t t_tclass;
503 char *t_string;
504 int t_lineno;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000505
506 var *intvar[NUM_INTERNAL_VARS]; /* often used */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000507
508 /* former statics from various functions */
509 char *split_f0__fstrings;
510
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000511 uint32_t next_token__save_tclass;
512 uint32_t next_token__save_info;
513 uint32_t next_token__ltclass;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000514 smallint next_token__concat_inserted;
515
516 smallint next_input_file__files_happen;
517 rstream next_input_file__rsm;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000518
519 var *evaluate__fnargs;
520 unsigned evaluate__seed;
521 regex_t evaluate__sreg;
522
523 var ptest__v;
524
525 tsplitter exec_builtin__tspl;
526
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000527 /* biggest and least used members go last */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000528 tsplitter fsplitter, rsplitter;
Denys Vlasenko3dbc5a92010-02-05 14:54:22 +0100529};
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000530#define G1 (ptr_to_globals[-1])
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000531#define G (*(struct globals2 *)ptr_to_globals)
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000532/* For debug. nm --size-sort awk.o | grep -vi ' [tr] ' */
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000533/*char G1size[sizeof(G1)]; - 0x74 */
534/*char Gsize[sizeof(G)]; - 0x1c4 */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000535/* Trying to keep most of members accessible with short offsets: */
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000536/*char Gofs_seed[offsetof(struct globals2, evaluate__seed)]; - 0x90 */
537#define t_double (G1.t_double )
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000538#define beginseq (G1.beginseq )
539#define mainseq (G1.mainseq )
540#define endseq (G1.endseq )
541#define seq (G1.seq )
542#define break_ptr (G1.break_ptr )
543#define continue_ptr (G1.continue_ptr)
544#define iF (G1.iF )
545#define vhash (G1.vhash )
546#define ahash (G1.ahash )
547#define fdhash (G1.fdhash )
548#define fnhash (G1.fnhash )
549#define g_progname (G1.g_progname )
550#define g_lineno (G1.g_lineno )
551#define nfields (G1.nfields )
552#define maxfields (G1.maxfields )
553#define Fields (G1.Fields )
554#define g_cb (G1.g_cb )
555#define g_pos (G1.g_pos )
556#define g_buf (G1.g_buf )
557#define icase (G1.icase )
558#define exiting (G1.exiting )
559#define nextrec (G1.nextrec )
560#define nextfile (G1.nextfile )
561#define is_f0_split (G1.is_f0_split )
Denys Vlasenko7b46d112011-09-11 00:30:56 +0200562#define t_rollback (G1.t_rollback )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000563#define t_info (G.t_info )
564#define t_tclass (G.t_tclass )
565#define t_string (G.t_string )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000566#define t_lineno (G.t_lineno )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000567#define intvar (G.intvar )
568#define fsplitter (G.fsplitter )
569#define rsplitter (G.rsplitter )
570#define INIT_G() do { \
Denys Vlasenko90a99042009-09-06 02:36:23 +0200571 SET_PTR_TO_GLOBALS((char*)xzalloc(sizeof(G1)+sizeof(G)) + sizeof(G1)); \
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000572 G.next_token__ltclass = TC_OPTERM; \
573 G.evaluate__seed = 1; \
574} while (0)
575
Glenn L McGrath545106f2002-11-11 06:21:00 +0000576
577/* function prototypes */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000578static void handle_special(var *);
Mike Frysingerf87b3e32005-09-27 04:16:22 +0000579static node *parse_expr(uint32_t);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000580static void chain_group(void);
581static var *evaluate(node *, var *);
582static rstream *next_input_file(void);
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000583static int fmt_num(char *, int, const char *, double, int);
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000584static int awk_exit(int) NORETURN;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000585
586/* ---- error handling ---- */
587
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000588static const char EMSG_INTERNAL_ERROR[] ALIGN1 = "Internal error";
589static const char EMSG_UNEXP_EOS[] ALIGN1 = "Unexpected end of string";
590static const char EMSG_UNEXP_TOKEN[] ALIGN1 = "Unexpected token";
591static const char EMSG_DIV_BY_ZERO[] ALIGN1 = "Division by zero";
592static const char EMSG_INV_FMT[] ALIGN1 = "Invalid format specifier";
593static const char EMSG_TOO_FEW_ARGS[] ALIGN1 = "Too few arguments for builtin";
594static const char EMSG_NOT_ARRAY[] ALIGN1 = "Not an array";
595static const char EMSG_POSSIBLE_ERROR[] ALIGN1 = "Possible syntax error";
596static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000597static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000598
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100599static void zero_out_var(var *vp)
Denis Vlasenkof782f522007-01-01 23:51:30 +0000600{
601 memset(vp, 0, sizeof(*vp));
602}
603
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000604static void syntax_error(const char *message) NORETURN;
605static void syntax_error(const char *message)
Glenn L McGrathd4036f82002-11-28 09:30:40 +0000606{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000607 bb_error_msg_and_die("%s:%i: %s", g_progname, g_lineno, message);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000608}
609
Glenn L McGrath545106f2002-11-11 06:21:00 +0000610/* ---- hash stuff ---- */
611
Denis Vlasenkof782f522007-01-01 23:51:30 +0000612static unsigned hashidx(const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000613{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000614 unsigned idx = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000615
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100616 while (*name)
617 idx = *name++ + (idx << 6) - idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000618 return idx;
619}
620
621/* create new hash */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000622static xhash *hash_init(void)
623{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000624 xhash *newhash;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000625
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100626 newhash = xzalloc(sizeof(*newhash));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000627 newhash->csize = FIRST_PRIME;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100628 newhash->items = xzalloc(FIRST_PRIME * sizeof(newhash->items[0]));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000629
630 return newhash;
631}
632
633/* find item in hash, return ptr to data, NULL if not found */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000634static void *hash_search(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000635{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000636 hash_item *hi;
637
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100638 hi = hash->items[hashidx(name) % hash->csize];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000639 while (hi) {
640 if (strcmp(hi->name, name) == 0)
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100641 return &hi->data;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000642 hi = hi->next;
643 }
644 return NULL;
645}
646
647/* grow hash if it becomes too big */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000648static void hash_rebuild(xhash *hash)
649{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000650 unsigned newsize, i, idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000651 hash_item **newitems, *hi, *thi;
652
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000653 if (hash->nprime == ARRAY_SIZE(PRIMES))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000654 return;
655
656 newsize = PRIMES[hash->nprime++];
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100657 newitems = xzalloc(newsize * sizeof(newitems[0]));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000658
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000659 for (i = 0; i < hash->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000660 hi = hash->items[i];
661 while (hi) {
662 thi = hi;
663 hi = thi->next;
664 idx = hashidx(thi->name) % newsize;
665 thi->next = newitems[idx];
666 newitems[idx] = thi;
667 }
668 }
669
670 free(hash->items);
671 hash->csize = newsize;
672 hash->items = newitems;
673}
674
675/* find item in hash, add it if necessary. Return ptr to data */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000676static void *hash_find(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000677{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000678 hash_item *hi;
Denis Vlasenkof782f522007-01-01 23:51:30 +0000679 unsigned idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000680 int l;
681
682 hi = hash_search(hash, name);
Denis Vlasenkob78c7822007-07-18 18:31:11 +0000683 if (!hi) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000684 if (++hash->nel / hash->csize > 10)
685 hash_rebuild(hash);
686
Rob Landleya3896512006-05-07 20:20:34 +0000687 l = strlen(name) + 1;
Denis Vlasenko7a676642009-03-15 22:20:31 +0000688 hi = xzalloc(sizeof(*hi) + l);
689 strcpy(hi->name, name);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000690
691 idx = hashidx(name) % hash->csize;
692 hi->next = hash->items[idx];
693 hash->items[idx] = hi;
694 hash->glen += l;
695 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100696 return &hi->data;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000697}
698
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000699#define findvar(hash, name) ((var*) hash_find((hash), (name)))
700#define newvar(name) ((var*) hash_find(vhash, (name)))
701#define newfile(name) ((rstream*)hash_find(fdhash, (name)))
702#define newfunc(name) ((func*) hash_find(fnhash, (name)))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000703
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000704static void hash_remove(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000705{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000706 hash_item *hi, **phi;
707
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100708 phi = &hash->items[hashidx(name) % hash->csize];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000709 while (*phi) {
710 hi = *phi;
711 if (strcmp(hi->name, name) == 0) {
Rob Landleya3896512006-05-07 20:20:34 +0000712 hash->glen -= (strlen(name) + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000713 hash->nel--;
714 *phi = hi->next;
715 free(hi);
716 break;
717 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100718 phi = &hi->next;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000719 }
720}
721
722/* ------ some useful functions ------ */
723
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100724static char *skip_spaces(char *p)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000725{
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000726 while (1) {
727 if (*p == '\\' && p[1] == '\n') {
728 p++;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000729 t_lineno++;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000730 } else if (*p != ' ' && *p != '\t') {
731 break;
732 }
Mike Frysingerde2b9382005-09-27 03:18:00 +0000733 p++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000734 }
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100735 return p;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000736}
737
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100738/* returns old *s, advances *s past word and terminating NUL */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000739static char *nextword(char **s)
740{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000741 char *p = *s;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100742 while (*(*s)++ != '\0')
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100743 continue;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000744 return p;
745}
746
Mike Frysinger10a11e22005-09-27 02:23:02 +0000747static char nextchar(char **s)
748{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000749 char c, *pps;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000750
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100751 c = *(*s)++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000752 pps = *s;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100753 if (c == '\\')
754 c = bb_process_escape_sequence((const char**)s);
Denys Vlasenkoea664dd2012-06-22 18:41:01 +0200755 /* Example awk statement:
756 * s = "abc\"def"
757 * we must treat \" as "
758 */
Denys Vlasenko2b299fe2010-10-24 01:58:04 +0200759 if (c == '\\' && *s == pps) { /* unrecognized \z? */
760 c = *(*s); /* yes, fetch z */
761 if (c)
762 (*s)++; /* advance unless z = NUL */
763 }
Glenn L McGrath545106f2002-11-11 06:21:00 +0000764 return c;
765}
766
Denys Vlasenkoea664dd2012-06-22 18:41:01 +0200767/* TODO: merge with strcpy_and_process_escape_sequences()?
768 */
769static void unescape_string_in_place(char *s1)
770{
771 char *s = s1;
772 while ((*s1 = nextchar(&s)) != '\0')
773 s1++;
774}
775
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000776static ALWAYS_INLINE int isalnum_(int c)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000777{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000778 return (isalnum(c) || c == '_');
779}
780
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000781static double my_strtod(char **pp)
782{
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200783 char *cp = *pp;
Rob Landleyd8205b32010-10-24 03:27:22 +0200784 if (ENABLE_DESKTOP && cp[0] == '0') {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200785 /* Might be hex or octal integer: 0x123abc or 07777 */
786 char c = (cp[1] | 0x20);
787 if (c == 'x' || isdigit(cp[1])) {
788 unsigned long long ull = strtoull(cp, pp, 0);
789 if (c == 'x')
790 return ull;
791 c = **pp;
792 if (!isdigit(c) && c != '.')
793 return ull;
794 /* else: it may be a floating number. Examples:
795 * 009.123 (*pp points to '9')
796 * 000.123 (*pp points to '.')
797 * fall through to strtod.
798 */
799 }
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000800 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200801 return strtod(cp, pp);
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000802}
803
Glenn L McGrath545106f2002-11-11 06:21:00 +0000804/* -------- working with variables (set/get/copy/etc) -------- */
805
Mike Frysinger10a11e22005-09-27 02:23:02 +0000806static xhash *iamarray(var *v)
807{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000808 var *a = v;
809
810 while (a->type & VF_CHILD)
811 a = a->x.parent;
812
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000813 if (!(a->type & VF_ARRAY)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000814 a->type |= VF_ARRAY;
815 a->x.array = hash_init();
816 }
817 return a->x.array;
818}
819
Mike Frysinger10a11e22005-09-27 02:23:02 +0000820static void clear_array(xhash *array)
821{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000822 unsigned i;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000823 hash_item *hi, *thi;
824
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000825 for (i = 0; i < array->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000826 hi = array->items[i];
827 while (hi) {
828 thi = hi;
829 hi = hi->next;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000830 free(thi->data.v.string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000831 free(thi);
832 }
833 array->items[i] = NULL;
834 }
835 array->glen = array->nel = 0;
836}
837
838/* clear a variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000839static var *clrvar(var *v)
840{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000841 if (!(v->type & VF_FSTR))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000842 free(v->string);
843
844 v->type &= VF_DONTTOUCH;
845 v->type |= VF_DIRTY;
846 v->string = NULL;
847 return v;
848}
849
850/* assign string value to variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000851static var *setvar_p(var *v, char *value)
852{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000853 clrvar(v);
854 v->string = value;
855 handle_special(v);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000856 return v;
857}
858
859/* same as setvar_p but make a copy of string */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000860static var *setvar_s(var *v, const char *value)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000861{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000862 return setvar_p(v, (value && *value) ? xstrdup(value) : NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000863}
864
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100865/* same as setvar_s but sets USER flag */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000866static var *setvar_u(var *v, const char *value)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000867{
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100868 v = setvar_s(v, value);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000869 v->type |= VF_USER;
870 return v;
871}
872
873/* set array element to user string */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000874static void setari_u(var *a, int idx, const char *s)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000875{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000876 var *v;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000877
Denys Vlasenko7bb346f2009-10-06 22:09:50 +0200878 v = findvar(iamarray(a), itoa(idx));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000879 setvar_u(v, s);
880}
881
882/* assign numeric value to variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000883static var *setvar_i(var *v, double value)
884{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000885 clrvar(v);
886 v->type |= VF_NUMBER;
887 v->number = value;
888 handle_special(v);
889 return v;
890}
891
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000892static const char *getvar_s(var *v)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000893{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000894 /* if v is numeric and has no cached string, convert it to string */
895 if ((v->type & (VF_NUMBER | VF_CACHED)) == VF_NUMBER) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000896 fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[CONVFMT]), v->number, TRUE);
897 v->string = xstrdup(g_buf);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000898 v->type |= VF_CACHED;
899 }
900 return (v->string == NULL) ? "" : v->string;
901}
902
Mike Frysinger10a11e22005-09-27 02:23:02 +0000903static double getvar_i(var *v)
904{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000905 char *s;
906
907 if ((v->type & (VF_NUMBER | VF_CACHED)) == 0) {
908 v->number = 0;
909 s = v->string;
910 if (s && *s) {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200911 debug_printf_eval("getvar_i: '%s'->", s);
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000912 v->number = my_strtod(&s);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200913 debug_printf_eval("%f (s:'%s')\n", v->number, s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000914 if (v->type & VF_USER) {
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100915 s = skip_spaces(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000916 if (*s != '\0')
917 v->type &= ~VF_USER;
918 }
919 } else {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200920 debug_printf_eval("getvar_i: '%s'->zero\n", s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000921 v->type &= ~VF_USER;
922 }
923 v->type |= VF_CACHED;
924 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200925 debug_printf_eval("getvar_i: %f\n", v->number);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000926 return v->number;
927}
928
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000929/* Used for operands of bitwise ops */
930static unsigned long getvar_i_int(var *v)
931{
932 double d = getvar_i(v);
933
934 /* Casting doubles to longs is undefined for values outside
935 * of target type range. Try to widen it as much as possible */
936 if (d >= 0)
937 return (unsigned long)d;
Denis Vlasenko665eaff2008-09-05 04:59:02 +0000938 /* Why? Think about d == -4294967295.0 (assuming 32bit longs) */
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000939 return - (long) (unsigned long) (-d);
940}
941
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000942static var *copyvar(var *dest, const var *src)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000943{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000944 if (dest != src) {
945 clrvar(dest);
Denis Vlasenko629563b2007-02-24 17:05:52 +0000946 dest->type |= (src->type & ~(VF_DONTTOUCH | VF_FSTR));
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200947 debug_printf_eval("copyvar: number:%f string:'%s'\n", src->number, src->string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000948 dest->number = src->number;
949 if (src->string)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000950 dest->string = xstrdup(src->string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000951 }
952 handle_special(dest);
953 return dest;
954}
955
Mike Frysinger10a11e22005-09-27 02:23:02 +0000956static var *incvar(var *v)
957{
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100958 return setvar_i(v, getvar_i(v) + 1.0);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000959}
960
961/* return true if v is number or numeric string */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000962static int is_numeric(var *v)
963{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000964 getvar_i(v);
965 return ((v->type ^ VF_DIRTY) & (VF_NUMBER | VF_USER | VF_DIRTY));
966}
967
968/* return 1 when value of v corresponds to true, 0 otherwise */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000969static int istrue(var *v)
970{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000971 if (is_numeric(v))
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100972 return (v->number != 0);
973 return (v->string && v->string[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000974}
975
Eric Andersenaff114c2004-04-14 17:51:38 +0000976/* temporary variables allocator. Last allocated should be first freed */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000977static var *nvalloc(int n)
978{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000979 nvblock *pb = NULL;
980 var *v, *r;
981 int size;
982
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000983 while (g_cb) {
984 pb = g_cb;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100985 if ((g_cb->pos - g_cb->nv) + n <= g_cb->size)
986 break;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000987 g_cb = g_cb->next;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000988 }
989
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000990 if (!g_cb) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000991 size = (n <= MINNVBLOCK) ? MINNVBLOCK : n;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000992 g_cb = xzalloc(sizeof(nvblock) + size * sizeof(var));
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000993 g_cb->size = size;
994 g_cb->pos = g_cb->nv;
995 g_cb->prev = pb;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000996 /*g_cb->next = NULL; - xzalloc did it */
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100997 if (pb)
998 pb->next = g_cb;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000999 }
1000
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001001 v = r = g_cb->pos;
1002 g_cb->pos += n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001003
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001004 while (v < g_cb->pos) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001005 v->type = 0;
1006 v->string = NULL;
1007 v++;
1008 }
1009
1010 return r;
1011}
1012
Mike Frysinger10a11e22005-09-27 02:23:02 +00001013static void nvfree(var *v)
1014{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001015 var *p;
1016
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001017 if (v < g_cb->nv || v >= g_cb->pos)
1018 syntax_error(EMSG_INTERNAL_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001019
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001020 for (p = v; p < g_cb->pos; p++) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001021 if ((p->type & (VF_ARRAY | VF_CHILD)) == VF_ARRAY) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001022 clear_array(iamarray(p));
1023 free(p->x.array->items);
1024 free(p->x.array);
1025 }
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001026 if (p->type & VF_WALK) {
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001027 walker_list *n;
1028 walker_list *w = p->x.walker;
1029 debug_printf_walker("nvfree: freeing walker @%p\n", &p->x.walker);
1030 p->x.walker = NULL;
1031 while (w) {
1032 n = w->prev;
1033 debug_printf_walker(" free(%p)\n", w);
1034 free(w);
1035 w = n;
1036 }
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001037 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001038 clrvar(p);
1039 }
1040
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001041 g_cb->pos = v;
1042 while (g_cb->prev && g_cb->pos == g_cb->nv) {
1043 g_cb = g_cb->prev;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001044 }
1045}
1046
1047/* ------- awk program text parsing ------- */
1048
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001049/* Parse next token pointed by global pos, place results into global ttt.
Glenn L McGrath545106f2002-11-11 06:21:00 +00001050 * If token isn't expected, give away. Return token class
1051 */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001052static uint32_t next_token(uint32_t expected)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001053{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001054#define concat_inserted (G.next_token__concat_inserted)
1055#define save_tclass (G.next_token__save_tclass)
1056#define save_info (G.next_token__save_info)
1057/* Initialized to TC_OPTERM: */
1058#define ltclass (G.next_token__ltclass)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001059
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001060 char *p, *s;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001061 const char *tl;
1062 uint32_t tc;
1063 const uint32_t *ti;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001064
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001065 if (t_rollback) {
1066 t_rollback = FALSE;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001067 } else if (concat_inserted) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001068 concat_inserted = FALSE;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001069 t_tclass = save_tclass;
1070 t_info = save_info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001071 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001072 p = g_pos;
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001073 readnext:
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +01001074 p = skip_spaces(p);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001075 g_lineno = t_lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001076 if (*p == '#')
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001077 while (*p != '\n' && *p != '\0')
1078 p++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001079
1080 if (*p == '\n')
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001081 t_lineno++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001082
1083 if (*p == '\0') {
1084 tc = TC_EOF;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001085 debug_printf_parse("%s: token found: TC_EOF\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001086 } else if (*p == '\"') {
1087 /* it's a string */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001088 t_string = s = ++p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001089 while (*p != '\"') {
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02001090 char *pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001091 if (*p == '\0' || *p == '\n')
1092 syntax_error(EMSG_UNEXP_EOS);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02001093 pp = p;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001094 *s++ = nextchar(&pp);
1095 p = pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001096 }
1097 p++;
1098 *s = '\0';
1099 tc = TC_STRING;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001100 debug_printf_parse("%s: token found:'%s' TC_STRING\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001101 } else if ((expected & TC_REGEXP) && *p == '/') {
1102 /* it's regexp */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001103 t_string = s = ++p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001104 while (*p != '/') {
1105 if (*p == '\0' || *p == '\n')
1106 syntax_error(EMSG_UNEXP_EOS);
Denis Vlasenkod9b5ab82007-05-18 07:30:43 +00001107 *s = *p++;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001108 if (*s++ == '\\') {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001109 char *pp = p;
1110 s[-1] = bb_process_escape_sequence((const char **)&pp);
1111 if (*p == '\\')
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001112 *s++ = '\\';
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001113 if (pp == p)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001114 *s++ = *p++;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001115 else
1116 p = pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001117 }
1118 }
1119 p++;
1120 *s = '\0';
1121 tc = TC_REGEXP;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001122 debug_printf_parse("%s: token found:'%s' TC_REGEXP\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001123
1124 } else if (*p == '.' || isdigit(*p)) {
1125 /* it's a number */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001126 char *pp = p;
1127 t_double = my_strtod(&pp);
1128 p = pp;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001129 if (*p == '.')
Glenn L McGrath545106f2002-11-11 06:21:00 +00001130 syntax_error(EMSG_UNEXP_TOKEN);
1131 tc = TC_NUMBER;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001132 debug_printf_parse("%s: token found:%f TC_NUMBER\n", __func__, t_double);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001133 } else {
1134 /* search for something known */
1135 tl = tokenlist;
1136 tc = 0x00000001;
1137 ti = tokeninfo;
1138 while (*tl) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001139 int l = (unsigned char) *tl++;
1140 if (l == (unsigned char) NTCC) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001141 tc <<= 1;
1142 continue;
1143 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001144 /* if token class is expected,
1145 * token matches,
1146 * and it's not a longer word,
Glenn L McGrath545106f2002-11-11 06:21:00 +00001147 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001148 if ((tc & (expected | TC_WORD | TC_NEWLINE))
Denys Vlasenko28458c62010-10-05 16:49:03 +02001149 && strncmp(p, tl, l) == 0
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001150 && !((tc & TC_WORD) && isalnum_(p[l]))
1151 ) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001152 /* then this is what we are looking for */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001153 t_info = *ti;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001154 debug_printf_parse("%s: token found:'%.*s' t_info:%x\n", __func__, l, p, t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001155 p += l;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001156 goto token_found;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001157 }
1158 ti++;
1159 tl += l;
1160 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001161 /* not a known token */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001162
Denys Vlasenko28458c62010-10-05 16:49:03 +02001163 /* is it a name? (var/array/function) */
1164 if (!isalnum_(*p))
1165 syntax_error(EMSG_UNEXP_TOKEN); /* no */
1166 /* yes */
1167 t_string = --p;
1168 while (isalnum_(*++p)) {
1169 p[-1] = *p;
1170 }
1171 p[-1] = '\0';
1172 tc = TC_VARIABLE;
1173 /* also consume whitespace between functionname and bracket */
1174 if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY))
1175 p = skip_spaces(p);
1176 if (*p == '(') {
1177 tc = TC_FUNCTION;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001178 debug_printf_parse("%s: token found:'%s' TC_FUNCTION\n", __func__, t_string);
Denys Vlasenko28458c62010-10-05 16:49:03 +02001179 } else {
1180 if (*p == '[') {
1181 p++;
1182 tc = TC_ARRAY;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001183 debug_printf_parse("%s: token found:'%s' TC_ARRAY\n", __func__, t_string);
1184 } else
1185 debug_printf_parse("%s: token found:'%s' TC_VARIABLE\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001186 }
1187 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001188 token_found:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001189 g_pos = p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001190
1191 /* skipping newlines in some cases */
1192 if ((ltclass & TC_NOTERM) && (tc & TC_NEWLINE))
1193 goto readnext;
1194
1195 /* insert concatenation operator when needed */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001196 if ((ltclass & TC_CONCAT1) && (tc & TC_CONCAT2) && (expected & TC_BINOP)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001197 concat_inserted = TRUE;
1198 save_tclass = tc;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001199 save_info = t_info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001200 tc = TC_BINOP;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001201 t_info = OC_CONCAT | SS | P(35);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001202 }
1203
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001204 t_tclass = tc;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001205 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001206 ltclass = t_tclass;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001207
1208 /* Are we ready for this? */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001209 if (!(ltclass & expected)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001210 syntax_error((ltclass & (TC_NEWLINE | TC_EOF)) ?
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001211 EMSG_UNEXP_EOS : EMSG_UNEXP_TOKEN);
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001212 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001213
1214 return ltclass;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001215#undef concat_inserted
1216#undef save_tclass
1217#undef save_info
1218#undef ltclass
Glenn L McGrath545106f2002-11-11 06:21:00 +00001219}
1220
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001221static void rollback_token(void)
1222{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001223 t_rollback = TRUE;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001224}
Glenn L McGrath545106f2002-11-11 06:21:00 +00001225
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001226static node *new_node(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001227{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001228 node *n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001229
Denis Vlasenko4cccc032006-12-22 18:37:07 +00001230 n = xzalloc(sizeof(node));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001231 n->info = info;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001232 n->lineno = g_lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001233 return n;
1234}
1235
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001236static void mk_re_node(const char *s, node *n, regex_t *re)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001237{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001238 n->info = OC_REGEXP;
1239 n->l.re = re;
1240 n->r.ire = re + 1;
1241 xregcomp(re, s, REG_EXTENDED);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001242 xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001243}
1244
Mike Frysinger10a11e22005-09-27 02:23:02 +00001245static node *condition(void)
1246{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001247 next_token(TC_SEQSTART);
1248 return parse_expr(TC_SEQTERM);
1249}
1250
1251/* parse expression terminated by given argument, return ptr
1252 * to built subtree. Terminator is eaten by parse_expr */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001253static node *parse_expr(uint32_t iexp)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001254{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001255 node sn;
1256 node *cn = &sn;
1257 node *vn, *glptr;
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001258 uint32_t tc, xtc;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001259 var *v;
1260
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001261 debug_printf_parse("%s(%x)\n", __func__, iexp);
1262
Glenn L McGrath545106f2002-11-11 06:21:00 +00001263 sn.info = PRIMASK;
1264 sn.r.n = glptr = NULL;
1265 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp;
1266
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001267 while (!((tc = next_token(xtc)) & iexp)) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001268
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001269 if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001270 /* input redirection (<) attached to glptr node */
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001271 debug_printf_parse("%s: input redir\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001272 cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37));
Glenn L McGrath4bded582004-02-22 11:55:09 +00001273 cn->a.n = glptr;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001274 xtc = TC_OPERAND | TC_UOPPRE;
1275 glptr = NULL;
1276
1277 } else if (tc & (TC_BINOP | TC_UOPPOST)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001278 debug_printf_parse("%s: TC_BINOP | TC_UOPPOST\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001279 /* for binary and postfix-unary operators, jump back over
1280 * previous operators with higher priority */
1281 vn = cn;
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001282 while (((t_info & PRIMASK) > (vn->a.n->info & PRIMASK2))
1283 || ((t_info == vn->info) && ((t_info & OPCLSMASK) == OC_COLON))
1284 ) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001285 vn = vn->a.n;
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001286 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001287 if ((t_info & OPCLSMASK) == OC_TERNARY)
1288 t_info += P(6);
1289 cn = vn->a.n->r.n = new_node(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001290 cn->a.n = vn->a.n;
1291 if (tc & TC_BINOP) {
1292 cn->l.n = vn;
1293 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001294 if ((t_info & OPCLSMASK) == OC_PGETLINE) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001295 /* it's a pipe */
1296 next_token(TC_GETLINE);
1297 /* give maximum priority to this pipe */
1298 cn->info &= ~PRIMASK;
1299 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1300 }
1301 } else {
1302 cn->r.n = vn;
1303 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1304 }
1305 vn->a.n = cn;
1306
1307 } else {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001308 debug_printf_parse("%s: other\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001309 /* for operands and prefix-unary operators, attach them
1310 * to last node */
1311 vn = cn;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001312 cn = vn->r.n = new_node(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001313 cn->a.n = vn;
1314 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
1315 if (tc & (TC_OPERAND | TC_REGEXP)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001316 debug_printf_parse("%s: TC_OPERAND | TC_REGEXP\n", __func__);
Rob Landleyed830e82005-06-07 02:43:52 +00001317 xtc = TC_UOPPRE | TC_UOPPOST | TC_BINOP | TC_OPERAND | iexp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001318 /* one should be very careful with switch on tclass -
Glenn L McGrath545106f2002-11-11 06:21:00 +00001319 * only simple tclasses should be used! */
1320 switch (tc) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00001321 case TC_VARIABLE:
1322 case TC_ARRAY:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001323 debug_printf_parse("%s: TC_VARIABLE | TC_ARRAY\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001324 cn->info = OC_VAR;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001325 v = hash_search(ahash, t_string);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001326 if (v != NULL) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001327 cn->info = OC_FNARG;
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001328 cn->l.aidx = v->x.aidx;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001329 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001330 cn->l.v = newvar(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001331 }
1332 if (tc & TC_ARRAY) {
1333 cn->info |= xS;
1334 cn->r.n = parse_expr(TC_ARRTERM);
1335 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001336 break;
Mike Frysingerde2b9382005-09-27 03:18:00 +00001337
Denis Vlasenkof782f522007-01-01 23:51:30 +00001338 case TC_NUMBER:
1339 case TC_STRING:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001340 debug_printf_parse("%s: TC_NUMBER | TC_STRING\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001341 cn->info = OC_VAR;
Rob Landley9ffd4232006-05-21 18:30:35 +00001342 v = cn->l.v = xzalloc(sizeof(var));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001343 if (tc & TC_NUMBER)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001344 setvar_i(v, t_double);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001345 else
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001346 setvar_s(v, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001347 break;
1348
Denis Vlasenkof782f522007-01-01 23:51:30 +00001349 case TC_REGEXP:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001350 debug_printf_parse("%s: TC_REGEXP\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001351 mk_re_node(t_string, cn, xzalloc(sizeof(regex_t)*2));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001352 break;
1353
Denis Vlasenkof782f522007-01-01 23:51:30 +00001354 case TC_FUNCTION:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001355 debug_printf_parse("%s: TC_FUNCTION\n", __func__);
Mike Frysingerde2b9382005-09-27 03:18:00 +00001356 cn->info = OC_FUNC;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001357 cn->r.f = newfunc(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001358 cn->l.n = condition();
1359 break;
1360
Denis Vlasenkof782f522007-01-01 23:51:30 +00001361 case TC_SEQSTART:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001362 debug_printf_parse("%s: TC_SEQSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001363 cn = vn->r.n = parse_expr(TC_SEQTERM);
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001364 if (!cn)
1365 syntax_error("Empty sequence");
Glenn L McGrath545106f2002-11-11 06:21:00 +00001366 cn->a.n = vn;
1367 break;
1368
Denis Vlasenkof782f522007-01-01 23:51:30 +00001369 case TC_GETLINE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001370 debug_printf_parse("%s: TC_GETLINE\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001371 glptr = cn;
1372 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1373 break;
1374
Denis Vlasenkof782f522007-01-01 23:51:30 +00001375 case TC_BUILTIN:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001376 debug_printf_parse("%s: TC_BUILTIN\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001377 cn->l.n = condition();
1378 break;
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001379
1380 case TC_LENGTH:
1381 debug_printf_parse("%s: TC_LENGTH\n", __func__);
1382 next_token(TC_SEQSTART | TC_OPTERM | TC_GRPTERM);
1383 rollback_token();
1384 if (t_tclass & TC_SEQSTART) {
1385 /* It was a "(" token. Handle just like TC_BUILTIN */
1386 cn->l.n = condition();
1387 }
1388 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001389 }
1390 }
1391 }
1392 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001393
1394 debug_printf_parse("%s() returns %p\n", __func__, sn.r.n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001395 return sn.r.n;
1396}
1397
1398/* add node to chain. Return ptr to alloc'd node */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001399static node *chain_node(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001400{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001401 node *n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001402
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001403 if (!seq->first)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001404 seq->first = seq->last = new_node(0);
1405
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001406 if (seq->programname != g_progname) {
1407 seq->programname = g_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001408 n = chain_node(OC_NEWSOURCE);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001409 n->l.new_progname = xstrdup(g_progname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001410 }
1411
1412 n = seq->last;
1413 n->info = info;
1414 seq->last = n->a.n = new_node(OC_DONE);
1415
1416 return n;
1417}
1418
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001419static void chain_expr(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001420{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001421 node *n;
1422
1423 n = chain_node(info);
1424 n->l.n = parse_expr(TC_OPTERM | TC_GRPTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001425 if (t_tclass & TC_GRPTERM)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001426 rollback_token();
1427}
1428
Mike Frysinger10a11e22005-09-27 02:23:02 +00001429static node *chain_loop(node *nn)
1430{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001431 node *n, *n2, *save_brk, *save_cont;
1432
1433 save_brk = break_ptr;
1434 save_cont = continue_ptr;
1435
1436 n = chain_node(OC_BR | Vx);
1437 continue_ptr = new_node(OC_EXEC);
1438 break_ptr = new_node(OC_EXEC);
1439 chain_group();
1440 n2 = chain_node(OC_EXEC | Vx);
1441 n2->l.n = nn;
1442 n2->a.n = n;
1443 continue_ptr->a.n = n2;
1444 break_ptr->a.n = n->r.n = seq->last;
1445
1446 continue_ptr = save_cont;
1447 break_ptr = save_brk;
1448
1449 return n;
1450}
1451
1452/* parse group and attach it to chain */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001453static void chain_group(void)
1454{
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001455 uint32_t c;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001456 node *n, *n2, *n3;
1457
1458 do {
1459 c = next_token(TC_GRPSEQ);
1460 } while (c & TC_NEWLINE);
1461
1462 if (c & TC_GRPSTART) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001463 debug_printf_parse("%s: TC_GRPSTART\n", __func__);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001464 while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001465 debug_printf_parse("%s: !TC_GRPTERM\n", __func__);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001466 if (t_tclass & TC_NEWLINE)
1467 continue;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001468 rollback_token();
1469 chain_group();
1470 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001471 debug_printf_parse("%s: TC_GRPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001472 } else if (c & (TC_OPSEQ | TC_OPTERM)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001473 debug_printf_parse("%s: TC_OPSEQ | TC_OPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001474 rollback_token();
1475 chain_expr(OC_EXEC | Vx);
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001476 } else {
1477 /* TC_STATEMNT */
1478 debug_printf_parse("%s: TC_STATEMNT(?)\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001479 switch (t_info & OPCLSMASK) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001480 case ST_IF:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001481 debug_printf_parse("%s: ST_IF\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001482 n = chain_node(OC_BR | Vx);
1483 n->l.n = condition();
1484 chain_group();
1485 n2 = chain_node(OC_EXEC);
1486 n->r.n = seq->last;
1487 if (next_token(TC_GRPSEQ | TC_GRPTERM | TC_ELSE) == TC_ELSE) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001488 chain_group();
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001489 n2->a.n = seq->last;
1490 } else {
1491 rollback_token();
1492 }
1493 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001494
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001495 case ST_WHILE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001496 debug_printf_parse("%s: ST_WHILE\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001497 n2 = condition();
1498 n = chain_loop(NULL);
1499 n->l.n = n2;
1500 break;
1501
1502 case ST_DO:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001503 debug_printf_parse("%s: ST_DO\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001504 n2 = chain_node(OC_EXEC);
1505 n = chain_loop(NULL);
1506 n2->a.n = n->a.n;
1507 next_token(TC_WHILE);
1508 n->l.n = condition();
1509 break;
1510
1511 case ST_FOR:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001512 debug_printf_parse("%s: ST_FOR\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001513 next_token(TC_SEQSTART);
1514 n2 = parse_expr(TC_SEMICOL | TC_SEQTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001515 if (t_tclass & TC_SEQTERM) { /* for-in */
Brian Foley61d59972016-10-15 14:45:40 +01001516 if (!n2 || (n2->info & OPCLSMASK) != OC_IN)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001517 syntax_error(EMSG_UNEXP_TOKEN);
1518 n = chain_node(OC_WALKINIT | VV);
1519 n->l.n = n2->l.n;
1520 n->r.n = n2->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001521 n = chain_loop(NULL);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001522 n->info = OC_WALKNEXT | Vx;
1523 n->l.n = n2->l.n;
1524 } else { /* for (;;) */
1525 n = chain_node(OC_EXEC | Vx);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001526 n->l.n = n2;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001527 n2 = parse_expr(TC_SEMICOL);
1528 n3 = parse_expr(TC_SEQTERM);
1529 n = chain_loop(n3);
1530 n->l.n = n2;
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001531 if (!n2)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001532 n->info = OC_EXEC;
1533 }
1534 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001535
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001536 case OC_PRINT:
1537 case OC_PRINTF:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001538 debug_printf_parse("%s: OC_PRINT[F]\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001539 n = chain_node(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001540 n->l.n = parse_expr(TC_OPTERM | TC_OUTRDR | TC_GRPTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001541 if (t_tclass & TC_OUTRDR) {
1542 n->info |= t_info;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001543 n->r.n = parse_expr(TC_OPTERM | TC_GRPTERM);
1544 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001545 if (t_tclass & TC_GRPTERM)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001546 rollback_token();
1547 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001548
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001549 case OC_BREAK:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001550 debug_printf_parse("%s: OC_BREAK\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001551 n = chain_node(OC_EXEC);
1552 n->a.n = break_ptr;
Denys Vlasenko5f8daef2014-06-26 16:40:28 +02001553 chain_expr(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001554 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001555
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001556 case OC_CONTINUE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001557 debug_printf_parse("%s: OC_CONTINUE\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001558 n = chain_node(OC_EXEC);
1559 n->a.n = continue_ptr;
Denys Vlasenko5f8daef2014-06-26 16:40:28 +02001560 chain_expr(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001561 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001562
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001563 /* delete, next, nextfile, return, exit */
1564 default:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001565 debug_printf_parse("%s: default\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001566 chain_expr(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001567 }
1568 }
1569}
1570
Mike Frysinger10a11e22005-09-27 02:23:02 +00001571static void parse_program(char *p)
1572{
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001573 uint32_t tclass;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001574 node *cn;
1575 func *f;
1576 var *v;
1577
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001578 g_pos = p;
1579 t_lineno = 1;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001580 while ((tclass = next_token(TC_EOF | TC_OPSEQ | TC_GRPSTART |
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001581 TC_OPTERM | TC_BEGIN | TC_END | TC_FUNCDECL)) != TC_EOF) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001582
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001583 if (tclass & TC_OPTERM) {
1584 debug_printf_parse("%s: TC_OPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001585 continue;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001586 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001587
1588 seq = &mainseq;
1589 if (tclass & TC_BEGIN) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001590 debug_printf_parse("%s: TC_BEGIN\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001591 seq = &beginseq;
1592 chain_group();
Glenn L McGrath545106f2002-11-11 06:21:00 +00001593 } else if (tclass & TC_END) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001594 debug_printf_parse("%s: TC_END\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001595 seq = &endseq;
1596 chain_group();
Glenn L McGrath545106f2002-11-11 06:21:00 +00001597 } else if (tclass & TC_FUNCDECL) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001598 debug_printf_parse("%s: TC_FUNCDECL\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001599 next_token(TC_FUNCTION);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001600 g_pos++;
1601 f = newfunc(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001602 f->body.first = NULL;
1603 f->nargs = 0;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001604 while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001605 v = findvar(ahash, t_string);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001606 v->x.aidx = f->nargs++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001607
1608 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
1609 break;
1610 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001611 seq = &f->body;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001612 chain_group();
1613 clear_array(ahash);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001614 } else if (tclass & TC_OPSEQ) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001615 debug_printf_parse("%s: TC_OPSEQ\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001616 rollback_token();
1617 cn = chain_node(OC_TEST);
1618 cn->l.n = parse_expr(TC_OPTERM | TC_EOF | TC_GRPSTART);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001619 if (t_tclass & TC_GRPSTART) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001620 debug_printf_parse("%s: TC_GRPSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001621 rollback_token();
1622 chain_group();
1623 } else {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001624 debug_printf_parse("%s: !TC_GRPSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001625 chain_node(OC_PRINT);
1626 }
1627 cn->r.n = mainseq.last;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001628 } else /* if (tclass & TC_GRPSTART) */ {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001629 debug_printf_parse("%s: TC_GRPSTART(?)\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001630 rollback_token();
1631 chain_group();
1632 }
1633 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001634 debug_printf_parse("%s: TC_EOF\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001635}
1636
1637
1638/* -------- program execution part -------- */
1639
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001640static node *mk_splitter(const char *s, tsplitter *spl)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001641{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001642 regex_t *re, *ire;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001643 node *n;
1644
1645 re = &spl->re[0];
1646 ire = &spl->re[1];
1647 n = &spl->n;
Denis Vlasenko890ac9d2006-10-07 15:16:19 +00001648 if ((n->info & OPCLSMASK) == OC_REGEXP) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001649 regfree(re);
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001650 regfree(ire); // TODO: nuke ire, use re+1?
Glenn L McGrath545106f2002-11-11 06:21:00 +00001651 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001652 if (s[0] && s[1]) { /* strlen(s) > 1 */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001653 mk_re_node(s, n, re);
1654 } else {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001655 n->info = (uint32_t) s[0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00001656 }
1657
1658 return n;
1659}
1660
1661/* use node as a regular expression. Supplied with node ptr and regex_t
Eric Andersenaff114c2004-04-14 17:51:38 +00001662 * storage space. Return ptr to regex (if result points to preg, it should
Glenn L McGrath545106f2002-11-11 06:21:00 +00001663 * be later regfree'd manually
1664 */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001665static regex_t *as_regex(node *op, regex_t *preg)
1666{
Denis Vlasenko7a676642009-03-15 22:20:31 +00001667 int cflags;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001668 var *v;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001669 const char *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001670
1671 if ((op->info & OPCLSMASK) == OC_REGEXP) {
1672 return icase ? op->r.ire : op->l.re;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001673 }
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001674 v = nvalloc(1);
1675 s = getvar_s(evaluate(op, v));
Denis Vlasenko7a676642009-03-15 22:20:31 +00001676
1677 cflags = icase ? REG_EXTENDED | REG_ICASE : REG_EXTENDED;
1678 /* Testcase where REG_EXTENDED fails (unpaired '{'):
1679 * echo Hi | awk 'gsub("@(samp|code|file)\{","");'
1680 * gawk 3.1.5 eats this. We revert to ~REG_EXTENDED
1681 * (maybe gsub is not supposed to use REG_EXTENDED?).
1682 */
1683 if (regcomp(preg, s, cflags)) {
1684 cflags &= ~REG_EXTENDED;
1685 xregcomp(preg, s, cflags);
1686 }
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001687 nvfree(v);
1688 return preg;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001689}
1690
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001691/* gradually increasing buffer.
1692 * note that we reallocate even if n == old_size,
1693 * and thus there is at least one extra allocated byte.
1694 */
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001695static char* qrealloc(char *b, int n, int *size)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001696{
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001697 if (!b || n >= *size) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001698 *size = n + (n>>1) + 80;
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001699 b = xrealloc(b, *size);
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001700 }
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001701 return b;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001702}
1703
1704/* resize field storage space */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001705static void fsrealloc(int size)
1706{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001707 int i;
1708
1709 if (size >= maxfields) {
1710 i = maxfields;
1711 maxfields = size + 16;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001712 Fields = xrealloc(Fields, maxfields * sizeof(Fields[0]));
Denis Vlasenkof782f522007-01-01 23:51:30 +00001713 for (; i < maxfields; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001714 Fields[i].type = VF_SPECIAL;
1715 Fields[i].string = NULL;
1716 }
1717 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001718 /* if size < nfields, clear extra field variables */
1719 for (i = size; i < nfields; i++) {
1720 clrvar(Fields + i);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001721 }
1722 nfields = size;
1723}
1724
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001725static int awk_split(const char *s, node *spl, char **slist)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001726{
Denys Vlasenko28458c62010-10-05 16:49:03 +02001727 int l, n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001728 char c[4];
1729 char *s1;
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001730 regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough...
Glenn L McGrath545106f2002-11-11 06:21:00 +00001731
1732 /* in worst case, each char would be a separate field */
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001733 *slist = s1 = xzalloc(strlen(s) * 2 + 3);
1734 strcpy(s1, s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001735
1736 c[0] = c[1] = (char)spl->info;
1737 c[2] = c[3] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001738 if (*getvar_s(intvar[RS]) == '\0')
1739 c[2] = '\n';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001740
Denys Vlasenko28458c62010-10-05 16:49:03 +02001741 n = 0;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001742 if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
1743 if (!*s)
1744 return n; /* "": zero fields */
1745 n++; /* at least one field will be there */
1746 do {
1747 l = strcspn(s, c+2); /* len till next NUL or \n */
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001748 if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0
1749 && pmatch[0].rm_so <= l
1750 ) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001751 l = pmatch[0].rm_so;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001752 if (pmatch[0].rm_eo == 0) {
1753 l++;
1754 pmatch[0].rm_eo++;
1755 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001756 n++; /* we saw yet another delimiter */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001757 } else {
1758 pmatch[0].rm_eo = l;
Denys Vlasenkoe4244232009-05-18 23:50:03 +02001759 if (s[l])
1760 pmatch[0].rm_eo++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001761 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001762 memcpy(s1, s, l);
Denis Vlasenko67b5eeb2009-04-12 13:54:13 +00001763 /* make sure we remove *all* of the separator chars */
Denys Vlasenkoe4244232009-05-18 23:50:03 +02001764 do {
1765 s1[l] = '\0';
1766 } while (++l < pmatch[0].rm_eo);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001767 nextword(&s1);
1768 s += pmatch[0].rm_eo;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001769 } while (*s);
1770 return n;
1771 }
1772 if (c[0] == '\0') { /* null split */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001773 while (*s) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001774 *s1++ = *s++;
1775 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001776 n++;
1777 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001778 return n;
1779 }
1780 if (c[0] != ' ') { /* single-character split */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001781 if (icase) {
1782 c[0] = toupper(c[0]);
1783 c[1] = tolower(c[1]);
1784 }
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001785 if (*s1)
1786 n++;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001787 while ((s1 = strpbrk(s1, c)) != NULL) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001788 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001789 n++;
1790 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001791 return n;
1792 }
1793 /* space split */
1794 while (*s) {
1795 s = skip_whitespace(s);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001796 if (!*s)
1797 break;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001798 n++;
1799 while (*s && !isspace(*s))
1800 *s1++ = *s++;
1801 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001802 }
1803 return n;
1804}
1805
Mike Frysinger10a11e22005-09-27 02:23:02 +00001806static void split_f0(void)
1807{
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001808/* static char *fstrings; */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001809#define fstrings (G.split_f0__fstrings)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001810
Glenn L McGrath545106f2002-11-11 06:21:00 +00001811 int i, n;
1812 char *s;
1813
1814 if (is_f0_split)
1815 return;
1816
1817 is_f0_split = TRUE;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001818 free(fstrings);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001819 fsrealloc(0);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001820 n = awk_split(getvar_s(intvar[F0]), &fsplitter.n, &fstrings);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001821 fsrealloc(n);
1822 s = fstrings;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001823 for (i = 0; i < n; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001824 Fields[i].string = nextword(&s);
1825 Fields[i].type |= (VF_FSTR | VF_USER | VF_DIRTY);
1826 }
1827
1828 /* set NF manually to avoid side effects */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001829 clrvar(intvar[NF]);
1830 intvar[NF]->type = VF_NUMBER | VF_SPECIAL;
1831 intvar[NF]->number = nfields;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001832#undef fstrings
Glenn L McGrath545106f2002-11-11 06:21:00 +00001833}
1834
1835/* perform additional actions when some internal variables changed */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001836static void handle_special(var *v)
1837{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001838 int n;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001839 char *b;
1840 const char *sep, *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001841 int sl, l, len, i, bsize;
1842
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001843 if (!(v->type & VF_SPECIAL))
Glenn L McGrath545106f2002-11-11 06:21:00 +00001844 return;
1845
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001846 if (v == intvar[NF]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001847 n = (int)getvar_i(v);
1848 fsrealloc(n);
1849
1850 /* recalculate $0 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001851 sep = getvar_s(intvar[OFS]);
Rob Landleya3896512006-05-07 20:20:34 +00001852 sl = strlen(sep);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001853 b = NULL;
1854 len = 0;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001855 for (i = 0; i < n; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001856 s = getvar_s(&Fields[i]);
Rob Landleya3896512006-05-07 20:20:34 +00001857 l = strlen(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001858 if (b) {
1859 memcpy(b+len, sep, sl);
1860 len += sl;
1861 }
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001862 b = qrealloc(b, len+l+sl, &bsize);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001863 memcpy(b+len, s, l);
1864 len += l;
1865 }
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001866 if (b)
1867 b[len] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001868 setvar_p(intvar[F0], b);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001869 is_f0_split = TRUE;
1870
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001871 } else if (v == intvar[F0]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001872 is_f0_split = FALSE;
1873
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001874 } else if (v == intvar[FS]) {
Denys Vlasenkodf8066a2012-07-11 01:27:15 +02001875 /*
1876 * The POSIX-2008 standard says that changing FS should have no effect on the
1877 * current input line, but only on the next one. The language is:
1878 *
1879 * > Before the first reference to a field in the record is evaluated, the record
1880 * > shall be split into fields, according to the rules in Regular Expressions,
1881 * > using the value of FS that was current at the time the record was read.
1882 *
1883 * So, split up current line before assignment to FS:
1884 */
1885 split_f0();
1886
Glenn L McGrath545106f2002-11-11 06:21:00 +00001887 mk_splitter(getvar_s(v), &fsplitter);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001888 } else if (v == intvar[RS]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001889 mk_splitter(getvar_s(v), &rsplitter);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001890 } else if (v == intvar[IGNORECASE]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001891 icase = istrue(v);
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001892 } else { /* $n */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001893 n = getvar_i(intvar[NF]);
1894 setvar_i(intvar[NF], n > v-Fields ? n : v-Fields+1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001895 /* right here v is invalid. Just to note... */
1896 }
1897}
1898
1899/* step through func/builtin/etc arguments */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001900static node *nextarg(node **pn)
1901{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001902 node *n;
1903
1904 n = *pn;
1905 if (n && (n->info & OPCLSMASK) == OC_COMMA) {
1906 *pn = n->r.n;
1907 n = n->l.n;
1908 } else {
1909 *pn = NULL;
1910 }
1911 return n;
1912}
1913
Mike Frysinger10a11e22005-09-27 02:23:02 +00001914static void hashwalk_init(var *v, xhash *array)
1915{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001916 hash_item *hi;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00001917 unsigned i;
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001918 walker_list *w;
1919 walker_list *prev_walker;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001920
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001921 if (v->type & VF_WALK) {
1922 prev_walker = v->x.walker;
1923 } else {
1924 v->type |= VF_WALK;
1925 prev_walker = NULL;
1926 }
1927 debug_printf_walker("hashwalk_init: prev_walker:%p\n", prev_walker);
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001928
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001929 w = v->x.walker = xzalloc(sizeof(*w) + array->glen + 1); /* why + 1? */
1930 debug_printf_walker(" walker@%p=%p\n", &v->x.walker, w);
1931 w->cur = w->end = w->wbuf;
1932 w->prev = prev_walker;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001933 for (i = 0; i < array->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001934 hi = array->items[i];
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001935 while (hi) {
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001936 strcpy(w->end, hi->name);
1937 nextword(&w->end);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001938 hi = hi->next;
1939 }
1940 }
1941}
1942
Mike Frysinger10a11e22005-09-27 02:23:02 +00001943static int hashwalk_next(var *v)
1944{
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001945 walker_list *w = v->x.walker;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001946
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001947 if (w->cur >= w->end) {
1948 walker_list *prev_walker = w->prev;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001949
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001950 debug_printf_walker("end of iteration, free(walker@%p:%p), prev_walker:%p\n", &v->x.walker, w, prev_walker);
1951 free(w);
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001952 v->x.walker = prev_walker;
1953 return FALSE;
1954 }
1955
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001956 setvar_s(v, nextword(&w->cur));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001957 return TRUE;
1958}
1959
1960/* evaluate node, return 1 when result is true, 0 otherwise */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001961static int ptest(node *pattern)
1962{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001963 /* ptest__v is "static": to save stack space? */
1964 return istrue(evaluate(pattern, &G.ptest__v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001965}
1966
1967/* read next record from stream rsm into a variable v */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001968static int awk_getline(rstream *rsm, var *v)
1969{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001970 char *b;
1971 regmatch_t pmatch[2];
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001972 int size, a, p, pp = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001973 int fd, so, eo, r, rp;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001974 char c, *m, *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001975
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02001976 debug_printf_eval("entered %s()\n", __func__);
1977
Glenn L McGrath545106f2002-11-11 06:21:00 +00001978 /* we're using our own buffer since we need access to accumulating
1979 * characters
1980 */
1981 fd = fileno(rsm->F);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001982 m = rsm->buffer;
1983 a = rsm->adv;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001984 p = rsm->pos;
1985 size = rsm->size;
1986 c = (char) rsplitter.n.info;
1987 rp = 0;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001988
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001989 if (!m)
1990 m = qrealloc(m, 256, &size);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001991
Glenn L McGrath545106f2002-11-11 06:21:00 +00001992 do {
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001993 b = m + a;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001994 so = eo = p;
1995 r = 1;
1996 if (p > 0) {
1997 if ((rsplitter.n.info & OPCLSMASK) == OC_REGEXP) {
1998 if (regexec(icase ? rsplitter.n.r.ire : rsplitter.n.l.re,
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001999 b, 1, pmatch, 0) == 0) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002000 so = pmatch[0].rm_so;
2001 eo = pmatch[0].rm_eo;
2002 if (b[eo] != '\0')
2003 break;
2004 }
2005 } else if (c != '\0') {
2006 s = strchr(b+pp, c);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002007 if (!s)
2008 s = memchr(b+pp, '\0', p - pp);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002009 if (s) {
2010 so = eo = s-b;
2011 eo++;
2012 break;
2013 }
2014 } else {
2015 while (b[rp] == '\n')
2016 rp++;
2017 s = strstr(b+rp, "\n\n");
2018 if (s) {
2019 so = eo = s-b;
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002020 while (b[eo] == '\n')
2021 eo++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002022 if (b[eo] != '\0')
2023 break;
2024 }
2025 }
2026 }
2027
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002028 if (a > 0) {
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002029 memmove(m, m+a, p+1);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002030 b = m;
2031 a = 0;
2032 }
2033
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002034 m = qrealloc(m, a+p+128, &size);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002035 b = m + a;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002036 pp = p;
2037 p += safe_read(fd, b+p, size-p-1);
2038 if (p < pp) {
2039 p = 0;
2040 r = 0;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002041 setvar_i(intvar[ERRNO], errno);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002042 }
2043 b[p] = '\0';
2044
2045 } while (p > pp);
2046
2047 if (p == 0) {
2048 r--;
2049 } else {
2050 c = b[so]; b[so] = '\0';
2051 setvar_s(v, b+rp);
2052 v->type |= VF_USER;
2053 b[so] = c;
2054 c = b[eo]; b[eo] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002055 setvar_s(intvar[RT], b+so);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002056 b[eo] = c;
2057 }
2058
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002059 rsm->buffer = m;
2060 rsm->adv = a + eo;
2061 rsm->pos = p - eo;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002062 rsm->size = size;
2063
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002064 debug_printf_eval("returning from %s(): %d\n", __func__, r);
2065
Glenn L McGrath545106f2002-11-11 06:21:00 +00002066 return r;
2067}
2068
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00002069static int fmt_num(char *b, int size, const char *format, double n, int int_as_int)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002070{
Denis Vlasenkof782f522007-01-01 23:51:30 +00002071 int r = 0;
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00002072 char c;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002073 const char *s = format;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002074
Denys Vlasenko1390a012013-07-20 21:23:01 +02002075 if (int_as_int && n == (long long)n) {
2076 r = snprintf(b, size, "%lld", (long long)n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002077 } else {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002078 do { c = *s; } while (c && *++s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002079 if (strchr("diouxX", c)) {
2080 r = snprintf(b, size, format, (int)n);
2081 } else if (strchr("eEfgG", c)) {
2082 r = snprintf(b, size, format, n);
2083 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002084 syntax_error(EMSG_INV_FMT);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002085 }
2086 }
2087 return r;
2088}
2089
Glenn L McGrath545106f2002-11-11 06:21:00 +00002090/* formatted output into an allocated buffer, return ptr to buffer */
Mike Frysinger10a11e22005-09-27 02:23:02 +00002091static char *awk_printf(node *n)
2092{
Glenn L McGrath545106f2002-11-11 06:21:00 +00002093 char *b = NULL;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002094 char *fmt, *s, *f;
2095 const char *s1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002096 int i, j, incr, bsize;
2097 char c, c1;
2098 var *v, *arg;
2099
2100 v = nvalloc(1);
Rob Landleyd921b2e2006-08-03 15:41:12 +00002101 fmt = f = xstrdup(getvar_s(evaluate(nextarg(&n), v)));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002102
2103 i = 0;
2104 while (*f) {
2105 s = f;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002106 while (*f && (*f != '%' || *++f == '%'))
Glenn L McGrath545106f2002-11-11 06:21:00 +00002107 f++;
Denis Vlasenko389f9d52007-05-09 21:57:23 +00002108 while (*f && !isalpha(*f)) {
2109 if (*f == '*')
2110 syntax_error("%*x formats are not supported");
Glenn L McGrath545106f2002-11-11 06:21:00 +00002111 f++;
Denis Vlasenko389f9d52007-05-09 21:57:23 +00002112 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002113
2114 incr = (f - s) + MAXVARFMT;
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002115 b = qrealloc(b, incr + i, &bsize);
Denis Vlasenkof782f522007-01-01 23:51:30 +00002116 c = *f;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002117 if (c != '\0')
2118 f++;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002119 c1 = *f;
2120 *f = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00002121 arg = evaluate(nextarg(&n), v);
2122
2123 j = i;
2124 if (c == 'c' || !c) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002125 i += sprintf(b+i, s, is_numeric(arg) ?
2126 (char)getvar_i(arg) : *getvar_s(arg));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002127 } else if (c == 's') {
Denis Vlasenko92758142006-10-03 19:56:34 +00002128 s1 = getvar_s(arg);
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002129 b = qrealloc(b, incr+i+strlen(s1), &bsize);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002130 i += sprintf(b+i, s, s1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002131 } else {
2132 i += fmt_num(b+i, incr, s, getvar_i(arg), FALSE);
2133 }
2134 *f = c1;
2135
2136 /* if there was an error while sprintf, return value is negative */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002137 if (i < j)
2138 i = j;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002139 }
2140
Glenn L McGrath545106f2002-11-11 06:21:00 +00002141 free(fmt);
2142 nvfree(v);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002143 b = xrealloc(b, i + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002144 b[i] = '\0';
2145 return b;
2146}
2147
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002148/* Common substitution routine.
2149 * Replace (nm)'th substring of (src) that matches (rn) with (repl),
2150 * store result into (dest), return number of substitutions.
2151 * If nm = 0, replace all matches.
2152 * If src or dst is NULL, use $0.
2153 * If subexp != 0, enable subexpression matching (\1-\9).
Glenn L McGrath545106f2002-11-11 06:21:00 +00002154 */
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002155static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int subexp)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002156{
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002157 char *resbuf;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002158 const char *sp;
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002159 int match_no, residx, replen, resbufsize;
2160 int regexec_flags;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002161 regmatch_t pmatch[10];
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002162 regex_t sreg, *regex;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002163
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002164 resbuf = NULL;
2165 residx = 0;
2166 match_no = 0;
2167 regexec_flags = 0;
2168 regex = as_regex(rn, &sreg);
2169 sp = getvar_s(src ? src : intvar[F0]);
2170 replen = strlen(repl);
2171 while (regexec(regex, sp, 10, pmatch, regexec_flags) == 0) {
2172 int so = pmatch[0].rm_so;
2173 int eo = pmatch[0].rm_eo;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002174
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002175 //bb_error_msg("match %u: [%u,%u] '%s'%p", match_no+1, so, eo, sp,sp);
2176 resbuf = qrealloc(resbuf, residx + eo + replen, &resbufsize);
2177 memcpy(resbuf + residx, sp, eo);
2178 residx += eo;
2179 if (++match_no >= nm) {
2180 const char *s;
2181 int nbs;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002182
Glenn L McGrath545106f2002-11-11 06:21:00 +00002183 /* replace */
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002184 residx -= (eo - so);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002185 nbs = 0;
2186 for (s = repl; *s; s++) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002187 char c = resbuf[residx++] = *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002188 if (c == '\\') {
2189 nbs++;
2190 continue;
2191 }
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002192 if (c == '&' || (subexp && c >= '0' && c <= '9')) {
2193 int j;
2194 residx -= ((nbs + 3) >> 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002195 j = 0;
2196 if (c != '&') {
2197 j = c - '0';
2198 nbs++;
2199 }
2200 if (nbs % 2) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002201 resbuf[residx++] = c;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002202 } else {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002203 int n = pmatch[j].rm_eo - pmatch[j].rm_so;
2204 resbuf = qrealloc(resbuf, residx + replen + n, &resbufsize);
2205 memcpy(resbuf + residx, sp + pmatch[j].rm_so, n);
2206 residx += n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002207 }
2208 }
2209 nbs = 0;
2210 }
2211 }
2212
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002213 regexec_flags = REG_NOTBOL;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002214 sp += eo;
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002215 if (match_no == nm)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002216 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002217 if (eo == so) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002218 /* Empty match (e.g. "b*" will match anywhere).
2219 * Advance by one char. */
2220//BUG (bug 1333):
2221//gsub(/\<b*/,"") on "abc" will reach this point, advance to "bc"
2222//... and will erroneously match "b" even though it is NOT at the word start.
2223//we need REG_NOTBOW but it does not exist...
Denys Vlasenko7379cd12010-04-04 01:48:12 +02002224//TODO: if EXTRA_COMPAT=y, use GNU matching and re_search,
2225//it should be able to do it correctly.
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002226 /* Subtle: this is safe only because
2227 * qrealloc allocated at least one extra byte */
2228 resbuf[residx] = *sp;
2229 if (*sp == '\0')
2230 goto ret;
2231 sp++;
2232 residx++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002233 }
2234 }
2235
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002236 resbuf = qrealloc(resbuf, residx + strlen(sp), &resbufsize);
2237 strcpy(resbuf + residx, sp);
2238 ret:
2239 //bb_error_msg("end sp:'%s'%p", sp,sp);
2240 setvar_p(dest ? dest : intvar[F0], resbuf);
2241 if (regex == &sreg)
2242 regfree(regex);
2243 return match_no;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002244}
2245
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002246static NOINLINE int do_mktime(const char *ds)
2247{
2248 struct tm then;
2249 int count;
2250
2251 /*memset(&then, 0, sizeof(then)); - not needed */
2252 then.tm_isdst = -1; /* default is unknown */
2253
2254 /* manpage of mktime says these fields are ints,
2255 * so we can sscanf stuff directly into them */
2256 count = sscanf(ds, "%u %u %u %u %u %u %d",
2257 &then.tm_year, &then.tm_mon, &then.tm_mday,
2258 &then.tm_hour, &then.tm_min, &then.tm_sec,
2259 &then.tm_isdst);
2260
2261 if (count < 6
2262 || (unsigned)then.tm_mon < 1
2263 || (unsigned)then.tm_year < 1900
2264 ) {
2265 return -1;
2266 }
2267
2268 then.tm_mon -= 1;
Denys Vlasenkobc3e9472009-09-21 04:16:00 +02002269 then.tm_year -= 1900;
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002270
2271 return mktime(&then);
2272}
2273
2274static NOINLINE var *exec_builtin(node *op, var *res)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002275{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002276#define tspl (G.exec_builtin__tspl)
2277
Glenn L McGrath545106f2002-11-11 06:21:00 +00002278 var *tv;
2279 node *an[4];
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002280 var *av[4];
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002281 const char *as[4];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002282 regmatch_t pmatch[2];
2283 regex_t sreg, *re;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002284 node *spl;
Mike Frysingerf87b3e32005-09-27 04:16:22 +00002285 uint32_t isr, info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002286 int nargs;
2287 time_t tt;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002288 int i, l, ll, n;
2289
2290 tv = nvalloc(4);
2291 isr = info = op->info;
2292 op = op->l.n;
2293
2294 av[2] = av[3] = NULL;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002295 for (i = 0; i < 4 && op; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002296 an[i] = nextarg(&op);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002297 if (isr & 0x09000000)
2298 av[i] = evaluate(an[i], &tv[i]);
2299 if (isr & 0x08000000)
2300 as[i] = getvar_s(av[i]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002301 isr >>= 1;
2302 }
2303
2304 nargs = i;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00002305 if ((uint32_t)nargs < (info >> 30))
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002306 syntax_error(EMSG_TOO_FEW_ARGS);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002307
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02002308 info &= OPNMASK;
2309 switch (info) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002310
Denis Vlasenkof782f522007-01-01 23:51:30 +00002311 case B_a2:
Rob Landleyd8205b32010-10-24 03:27:22 +02002312 if (ENABLE_FEATURE_AWK_LIBM)
2313 setvar_i(res, atan2(getvar_i(av[0]), getvar_i(av[1])));
2314 else
2315 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002316 break;
2317
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002318 case B_sp: {
2319 char *s, *s1;
2320
Glenn L McGrath545106f2002-11-11 06:21:00 +00002321 if (nargs > 2) {
2322 spl = (an[2]->info & OPCLSMASK) == OC_REGEXP ?
2323 an[2] : mk_splitter(getvar_s(evaluate(an[2], &tv[2])), &tspl);
2324 } else {
2325 spl = &fsplitter.n;
2326 }
2327
2328 n = awk_split(as[0], spl, &s);
2329 s1 = s;
2330 clear_array(iamarray(av[1]));
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002331 for (i = 1; i <= n; i++)
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002332 setari_u(av[1], i, nextword(&s));
2333 free(s1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002334 setvar_i(res, n);
2335 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002336 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002337
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002338 case B_ss: {
2339 char *s;
2340
Rob Landleya3896512006-05-07 20:20:34 +00002341 l = strlen(as[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002342 i = getvar_i(av[1]) - 1;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002343 if (i > l)
2344 i = l;
2345 if (i < 0)
2346 i = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002347 n = (nargs > 2) ? getvar_i(av[2]) : l-i;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002348 if (n < 0)
2349 n = 0;
Denis Vlasenko8ae5b282008-07-02 22:47:49 +00002350 s = xstrndup(as[0]+i, n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002351 setvar_p(res, s);
2352 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002353 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +00002354
Denis Vlasenko7cbcd1c2008-08-28 23:16:58 +00002355 /* Bitwise ops must assume that operands are unsigned. GNU Awk 3.1.5:
2356 * awk '{ print or(-1,1) }' gives "4.29497e+09", not "-2.xxxe+09" */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002357 case B_an:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002358 setvar_i(res, getvar_i_int(av[0]) & getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002359 break;
Denis Vlasenkof7996f32007-01-11 17:20:00 +00002360
Denis Vlasenkof782f522007-01-01 23:51:30 +00002361 case B_co:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002362 setvar_i(res, ~getvar_i_int(av[0]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002363 break;
2364
Denis Vlasenkof782f522007-01-01 23:51:30 +00002365 case B_ls:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002366 setvar_i(res, getvar_i_int(av[0]) << getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002367 break;
2368
Denis Vlasenkof782f522007-01-01 23:51:30 +00002369 case B_or:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002370 setvar_i(res, getvar_i_int(av[0]) | getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002371 break;
2372
Denis Vlasenkof782f522007-01-01 23:51:30 +00002373 case B_rs:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002374 setvar_i(res, getvar_i_int(av[0]) >> getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002375 break;
2376
Denis Vlasenkof782f522007-01-01 23:51:30 +00002377 case B_xo:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002378 setvar_i(res, getvar_i_int(av[0]) ^ getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002379 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002380
Denis Vlasenkof782f522007-01-01 23:51:30 +00002381 case B_lo:
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002382 case B_up: {
2383 char *s, *s1;
Rob Landleyd921b2e2006-08-03 15:41:12 +00002384 s1 = s = xstrdup(as[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002385 while (*s1) {
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02002386 //*s1 = (info == B_up) ? toupper(*s1) : tolower(*s1);
2387 if ((unsigned char)((*s1 | 0x20) - 'a') <= ('z' - 'a'))
2388 *s1 = (info == B_up) ? (*s1 & 0xdf) : (*s1 | 0x20);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002389 s1++;
2390 }
2391 setvar_p(res, s);
2392 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002393 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002394
Denis Vlasenkof782f522007-01-01 23:51:30 +00002395 case B_ix:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002396 n = 0;
Rob Landleya3896512006-05-07 20:20:34 +00002397 ll = strlen(as[1]);
2398 l = strlen(as[0]) - ll;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002399 if (ll > 0 && l >= 0) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002400 if (!icase) {
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002401 char *s = strstr(as[0], as[1]);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002402 if (s)
2403 n = (s - as[0]) + 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002404 } else {
2405 /* this piece of code is terribly slow and
2406 * really should be rewritten
2407 */
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002408 for (i = 0; i <= l; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002409 if (strncasecmp(as[0]+i, as[1], ll) == 0) {
2410 n = i+1;
2411 break;
2412 }
2413 }
2414 }
2415 }
2416 setvar_i(res, n);
2417 break;
2418
Denis Vlasenkof782f522007-01-01 23:51:30 +00002419 case B_ti:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002420 if (nargs > 1)
2421 tt = getvar_i(av[1]);
2422 else
2423 time(&tt);
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002424 //s = (nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y";
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002425 i = strftime(g_buf, MAXVARFMT,
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002426 ((nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"),
2427 localtime(&tt));
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002428 g_buf[i] = '\0';
2429 setvar_s(res, g_buf);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002430 break;
2431
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002432 case B_mt:
2433 setvar_i(res, do_mktime(as[0]));
2434 break;
2435
Denis Vlasenkof782f522007-01-01 23:51:30 +00002436 case B_ma:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002437 re = as_regex(an[1], &sreg);
2438 n = regexec(re, as[0], 1, pmatch, 0);
2439 if (n == 0) {
2440 pmatch[0].rm_so++;
2441 pmatch[0].rm_eo++;
2442 } else {
2443 pmatch[0].rm_so = 0;
2444 pmatch[0].rm_eo = -1;
2445 }
2446 setvar_i(newvar("RSTART"), pmatch[0].rm_so);
2447 setvar_i(newvar("RLENGTH"), pmatch[0].rm_eo - pmatch[0].rm_so);
2448 setvar_i(res, pmatch[0].rm_so);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002449 if (re == &sreg)
2450 regfree(re);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002451 break;
2452
Denis Vlasenkof782f522007-01-01 23:51:30 +00002453 case B_ge:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002454 awk_sub(an[0], as[1], getvar_i(av[2]), av[3], res, TRUE);
2455 break;
2456
Denis Vlasenkof782f522007-01-01 23:51:30 +00002457 case B_gs:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002458 setvar_i(res, awk_sub(an[0], as[1], 0, av[2], av[2], FALSE));
2459 break;
2460
Denis Vlasenkof782f522007-01-01 23:51:30 +00002461 case B_su:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002462 setvar_i(res, awk_sub(an[0], as[1], 1, av[2], av[2], FALSE));
2463 break;
2464 }
2465
2466 nvfree(tv);
2467 return res;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002468#undef tspl
Glenn L McGrath545106f2002-11-11 06:21:00 +00002469}
2470
2471/*
2472 * Evaluate node - the heart of the program. Supplied with subtree
2473 * and place where to store result. returns ptr to result.
2474 */
2475#define XC(n) ((n) >> 8)
2476
Mike Frysinger10a11e22005-09-27 02:23:02 +00002477static var *evaluate(node *op, var *res)
2478{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002479/* This procedure is recursive so we should count every byte */
2480#define fnargs (G.evaluate__fnargs)
2481/* seed is initialized to 1 */
2482#define seed (G.evaluate__seed)
Denys Vlasenkofb132e42010-10-29 11:46:52 +02002483#define sreg (G.evaluate__sreg)
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002484
Glenn L McGrath545106f2002-11-11 06:21:00 +00002485 var *v1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002486
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002487 if (!op)
Glenn L McGrath545106f2002-11-11 06:21:00 +00002488 return setvar_s(res, NULL);
2489
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002490 debug_printf_eval("entered %s()\n", __func__);
2491
Glenn L McGrath545106f2002-11-11 06:21:00 +00002492 v1 = nvalloc(2);
2493
2494 while (op) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002495 struct {
2496 var *v;
2497 const char *s;
2498 } L = L; /* for compiler */
2499 struct {
2500 var *v;
2501 const char *s;
2502 } R = R;
2503 double L_d = L_d;
2504 uint32_t opinfo;
2505 int opn;
2506 node *op1;
2507
Glenn L McGrath545106f2002-11-11 06:21:00 +00002508 opinfo = op->info;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002509 opn = (opinfo & OPNMASK);
2510 g_lineno = op->lineno;
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002511 op1 = op->l.n;
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002512 debug_printf_eval("opinfo:%08x opn:%08x\n", opinfo, opn);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002513
Mike Frysingerde2b9382005-09-27 03:18:00 +00002514 /* execute inevitable things */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002515 if (opinfo & OF_RES1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002516 L.v = evaluate(op1, v1);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002517 if (opinfo & OF_RES2)
2518 R.v = evaluate(op->r.n, v1+1);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002519 if (opinfo & OF_STR1) {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002520 L.s = getvar_s(L.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002521 debug_printf_eval("L.s:'%s'\n", L.s);
2522 }
2523 if (opinfo & OF_STR2) {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002524 R.s = getvar_s(R.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002525 debug_printf_eval("R.s:'%s'\n", R.s);
2526 }
2527 if (opinfo & OF_NUM1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002528 L_d = getvar_i(L.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002529 debug_printf_eval("L_d:%f\n", L_d);
2530 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002531
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002532 debug_printf_eval("switch(0x%x)\n", XC(opinfo & OPCLSMASK));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002533 switch (XC(opinfo & OPCLSMASK)) {
2534
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002535 /* -- iterative node type -- */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002536
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002537 /* test pattern */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002538 case XC( OC_TEST ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002539 if ((op1->info & OPCLSMASK) == OC_COMMA) {
2540 /* it's range pattern */
2541 if ((opinfo & OF_CHECKED) || ptest(op1->l.n)) {
2542 op->info |= OF_CHECKED;
2543 if (ptest(op1->r.n))
2544 op->info &= ~OF_CHECKED;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002545 op = op->a.n;
2546 } else {
2547 op = op->r.n;
2548 }
2549 } else {
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002550 op = ptest(op1) ? op->a.n : op->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002551 }
2552 break;
2553
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002554 /* just evaluate an expression, also used as unconditional jump */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002555 case XC( OC_EXEC ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002556 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002557
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002558 /* branch, used in if-else and various loops */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002559 case XC( OC_BR ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002560 op = istrue(L.v) ? op->a.n : op->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002561 break;
2562
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002563 /* initialize for-in loop */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002564 case XC( OC_WALKINIT ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002565 hashwalk_init(L.v, iamarray(R.v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002566 break;
2567
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002568 /* get next array item */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002569 case XC( OC_WALKNEXT ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002570 op = hashwalk_next(L.v) ? op->a.n : op->r.n;
2571 break;
2572
Denis Vlasenkof782f522007-01-01 23:51:30 +00002573 case XC( OC_PRINT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002574 case XC( OC_PRINTF ): {
2575 FILE *F = stdout;
2576
Mike Frysingerde2b9382005-09-27 03:18:00 +00002577 if (op->r.n) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002578 rstream *rsm = newfile(R.s);
2579 if (!rsm->F) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002580 if (opn == '|') {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002581 rsm->F = popen(R.s, "w");
2582 if (rsm->F == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002583 bb_perror_msg_and_die("popen");
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002584 rsm->is_pipe = 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002585 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002586 rsm->F = xfopen(R.s, opn=='w' ? "w" : "a");
Glenn L McGrath545106f2002-11-11 06:21:00 +00002587 }
2588 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002589 F = rsm->F;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002590 }
2591
2592 if ((opinfo & OPCLSMASK) == OC_PRINT) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002593 if (!op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002594 fputs(getvar_s(intvar[F0]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002595 } else {
2596 while (op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002597 var *v = evaluate(nextarg(&op1), v1);
2598 if (v->type & VF_NUMBER) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002599 fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[OFMT]),
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002600 getvar_i(v), TRUE);
2601 fputs(g_buf, F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002602 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002603 fputs(getvar_s(v), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002604 }
2605
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002606 if (op1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002607 fputs(getvar_s(intvar[OFS]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002608 }
2609 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002610 fputs(getvar_s(intvar[ORS]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002611
2612 } else { /* OC_PRINTF */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002613 char *s = awk_printf(op1);
2614 fputs(s, F);
2615 free(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002616 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002617 fflush(F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002618 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002619 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002620
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002621 case XC( OC_DELETE ): {
2622 uint32_t info = op1->info & OPCLSMASK;
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002623 var *v;
2624
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002625 if (info == OC_VAR) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002626 v = op1->l.v;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002627 } else if (info == OC_FNARG) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002628 v = &fnargs[op1->l.aidx];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002629 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002630 syntax_error(EMSG_NOT_ARRAY);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002631 }
2632
Mike Frysingerde2b9382005-09-27 03:18:00 +00002633 if (op1->r.n) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002634 const char *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002635 clrvar(L.v);
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002636 s = getvar_s(evaluate(op1->r.n, v1));
2637 hash_remove(iamarray(v), s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002638 } else {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002639 clear_array(iamarray(v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002640 }
2641 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002642 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002643
Denis Vlasenkof782f522007-01-01 23:51:30 +00002644 case XC( OC_NEWSOURCE ):
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002645 g_progname = op->l.new_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002646 break;
2647
Denis Vlasenkof782f522007-01-01 23:51:30 +00002648 case XC( OC_RETURN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002649 copyvar(res, L.v);
2650 break;
2651
Denis Vlasenkof782f522007-01-01 23:51:30 +00002652 case XC( OC_NEXTFILE ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002653 nextfile = TRUE;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002654 case XC( OC_NEXT ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002655 nextrec = TRUE;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002656 case XC( OC_DONE ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002657 clrvar(res);
2658 break;
2659
Denis Vlasenkof782f522007-01-01 23:51:30 +00002660 case XC( OC_EXIT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002661 awk_exit(L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002662
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002663 /* -- recursive node type -- */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002664
Denis Vlasenkof782f522007-01-01 23:51:30 +00002665 case XC( OC_VAR ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002666 L.v = op->l.v;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002667 if (L.v == intvar[NF])
Glenn L McGrath545106f2002-11-11 06:21:00 +00002668 split_f0();
2669 goto v_cont;
2670
Denis Vlasenkof782f522007-01-01 23:51:30 +00002671 case XC( OC_FNARG ):
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002672 L.v = &fnargs[op->l.aidx];
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002673 v_cont:
2674 res = op->r.n ? findvar(iamarray(L.v), R.s) : L.v;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002675 break;
2676
Denis Vlasenkof782f522007-01-01 23:51:30 +00002677 case XC( OC_IN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002678 setvar_i(res, hash_search(iamarray(R.v), L.s) ? 1 : 0);
2679 break;
2680
Denis Vlasenkof782f522007-01-01 23:51:30 +00002681 case XC( OC_REGEXP ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002682 op1 = op;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002683 L.s = getvar_s(intvar[F0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002684 goto re_cont;
2685
Denis Vlasenkof782f522007-01-01 23:51:30 +00002686 case XC( OC_MATCH ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002687 op1 = op->r.n;
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002688 re_cont:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002689 {
2690 regex_t *re = as_regex(op1, &sreg);
2691 int i = regexec(re, L.s, 0, NULL, 0);
2692 if (re == &sreg)
2693 regfree(re);
2694 setvar_i(res, (i == 0) ^ (opn == '!'));
2695 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002696 break;
2697
Denis Vlasenkof782f522007-01-01 23:51:30 +00002698 case XC( OC_MOVE ):
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002699 debug_printf_eval("MOVE\n");
Mike Frysingerde2b9382005-09-27 03:18:00 +00002700 /* if source is a temporary string, jusk relink it to dest */
Denys Vlasenko12847742009-11-30 01:15:04 +01002701//Disabled: if R.v is numeric but happens to have cached R.v->string,
2702//then L.v ends up being a string, which is wrong
2703// if (R.v == v1+1 && R.v->string) {
2704// res = setvar_p(L.v, R.v->string);
2705// R.v->string = NULL;
2706// } else {
Mike Frysingerde2b9382005-09-27 03:18:00 +00002707 res = copyvar(L.v, R.v);
Denys Vlasenko12847742009-11-30 01:15:04 +01002708// }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002709 break;
2710
Denis Vlasenkof782f522007-01-01 23:51:30 +00002711 case XC( OC_TERNARY ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002712 if ((op->r.n->info & OPCLSMASK) != OC_COLON)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002713 syntax_error(EMSG_POSSIBLE_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002714 res = evaluate(istrue(L.v) ? op->r.n->l.n : op->r.n->r.n, res);
2715 break;
2716
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002717 case XC( OC_FUNC ): {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002718 var *vbeg, *v;
2719 const char *sv_progname;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002720
Bernhard Reutner-Fischerb79a0fe2013-03-06 21:01:05 +01002721 /* The body might be empty, still has to eval the args */
Bernhard Reutner-Fischera060a1a2013-07-31 15:29:20 +02002722 if (!op->r.n->info && !op->r.f->body.first)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002723 syntax_error(EMSG_UNDEF_FUNC);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002724
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002725 vbeg = v = nvalloc(op->r.f->nargs + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002726 while (op1) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002727 var *arg = evaluate(nextarg(&op1), v1);
2728 copyvar(v, arg);
2729 v->type |= VF_CHILD;
2730 v->x.parent = arg;
2731 if (++v - vbeg >= op->r.f->nargs)
Glenn L McGrath545106f2002-11-11 06:21:00 +00002732 break;
2733 }
2734
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002735 v = fnargs;
2736 fnargs = vbeg;
2737 sv_progname = g_progname;
2738
2739 res = evaluate(op->r.f->body.first, res);
2740
2741 g_progname = sv_progname;
2742 nvfree(fnargs);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002743 fnargs = v;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002744
Glenn L McGrath545106f2002-11-11 06:21:00 +00002745 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002746 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002747
Denis Vlasenkof782f522007-01-01 23:51:30 +00002748 case XC( OC_GETLINE ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002749 case XC( OC_PGETLINE ): {
2750 rstream *rsm;
2751 int i;
2752
Mike Frysingerde2b9382005-09-27 03:18:00 +00002753 if (op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002754 rsm = newfile(L.s);
2755 if (!rsm->F) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002756 if ((opinfo & OPCLSMASK) == OC_PGETLINE) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002757 rsm->F = popen(L.s, "r");
2758 rsm->is_pipe = TRUE;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002759 } else {
Denys Vlasenkofb132e42010-10-29 11:46:52 +02002760 rsm->F = fopen_for_read(L.s); /* not xfopen! */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002761 }
2762 }
2763 } else {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002764 if (!iF)
2765 iF = next_input_file();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002766 rsm = iF;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002767 }
2768
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02002769 if (!rsm || !rsm->F) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002770 setvar_i(intvar[ERRNO], errno);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002771 setvar_i(res, -1);
2772 break;
2773 }
2774
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002775 if (!op->r.n)
2776 R.v = intvar[F0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002777
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002778 i = awk_getline(rsm, R.v);
2779 if (i > 0 && !op1) {
2780 incvar(intvar[FNR]);
2781 incvar(intvar[NR]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002782 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002783 setvar_i(res, i);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002784 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002785 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002786
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002787 /* simple builtins */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002788 case XC( OC_FBLTIN ): {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002789 double R_d = R_d; /* for compiler */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002790
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002791 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002792 case F_in:
Denys Vlasenko1390a012013-07-20 21:23:01 +02002793 R_d = (long long)L_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002794 break;
2795
Denis Vlasenkof782f522007-01-01 23:51:30 +00002796 case F_rn:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002797 R_d = (double)rand() / (double)RAND_MAX;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002798 break;
Rob Landleyd8205b32010-10-24 03:27:22 +02002799
Denis Vlasenkof782f522007-01-01 23:51:30 +00002800 case F_co:
Rob Landleyd8205b32010-10-24 03:27:22 +02002801 if (ENABLE_FEATURE_AWK_LIBM) {
2802 R_d = cos(L_d);
2803 break;
2804 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002805
Denis Vlasenkof782f522007-01-01 23:51:30 +00002806 case F_ex:
Rob Landleyd8205b32010-10-24 03:27:22 +02002807 if (ENABLE_FEATURE_AWK_LIBM) {
2808 R_d = exp(L_d);
2809 break;
2810 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002811
Denis Vlasenkof782f522007-01-01 23:51:30 +00002812 case F_lg:
Rob Landleyd8205b32010-10-24 03:27:22 +02002813 if (ENABLE_FEATURE_AWK_LIBM) {
2814 R_d = log(L_d);
2815 break;
2816 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002817
Denis Vlasenkof782f522007-01-01 23:51:30 +00002818 case F_si:
Rob Landleyd8205b32010-10-24 03:27:22 +02002819 if (ENABLE_FEATURE_AWK_LIBM) {
2820 R_d = sin(L_d);
2821 break;
2822 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002823
Denis Vlasenkof782f522007-01-01 23:51:30 +00002824 case F_sq:
Rob Landleyd8205b32010-10-24 03:27:22 +02002825 if (ENABLE_FEATURE_AWK_LIBM) {
2826 R_d = sqrt(L_d);
2827 break;
2828 }
2829
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002830 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002831 break;
Rob Landleyd8205b32010-10-24 03:27:22 +02002832
Denis Vlasenkof782f522007-01-01 23:51:30 +00002833 case F_sr:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002834 R_d = (double)seed;
2835 seed = op1 ? (unsigned)L_d : (unsigned)time(NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002836 srand(seed);
2837 break;
2838
Denis Vlasenkof782f522007-01-01 23:51:30 +00002839 case F_ti:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002840 R_d = time(NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002841 break;
2842
Denis Vlasenkof782f522007-01-01 23:51:30 +00002843 case F_le:
Denys Vlasenko7985bc12013-10-12 04:51:54 +02002844 debug_printf_eval("length: L.s:'%s'\n", L.s);
2845 if (!op1) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002846 L.s = getvar_s(intvar[F0]);
Denys Vlasenko7985bc12013-10-12 04:51:54 +02002847 debug_printf_eval("length: L.s='%s'\n", L.s);
2848 }
2849 else if (L.v->type & VF_ARRAY) {
2850 R_d = L.v->x.array->nel;
2851 debug_printf_eval("length: array_len:%d\n", L.v->x.array->nel);
2852 break;
2853 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002854 R_d = strlen(L.s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002855 break;
2856
Denis Vlasenkof782f522007-01-01 23:51:30 +00002857 case F_sy:
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002858 fflush_all();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002859 R_d = (ENABLE_FEATURE_ALLOW_EXEC && L.s && *L.s)
Denis Vlasenko249fabf2006-12-19 00:29:22 +00002860 ? (system(L.s) >> 8) : 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002861 break;
2862
Denis Vlasenkof782f522007-01-01 23:51:30 +00002863 case F_ff:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002864 if (!op1) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002865 fflush(stdout);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002866 } else if (L.s && *L.s) {
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002867 rstream *rsm = newfile(L.s);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002868 fflush(rsm->F);
2869 } else {
2870 fflush_all();
Glenn L McGrath545106f2002-11-11 06:21:00 +00002871 }
2872 break;
2873
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002874 case F_cl: {
2875 rstream *rsm;
2876 int err = 0;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002877 rsm = (rstream *)hash_search(fdhash, L.s);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002878 debug_printf_eval("OC_FBLTIN F_cl rsm:%p\n", rsm);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002879 if (rsm) {
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002880 debug_printf_eval("OC_FBLTIN F_cl "
2881 "rsm->is_pipe:%d, ->F:%p\n",
2882 rsm->is_pipe, rsm->F);
2883 /* Can be NULL if open failed. Example:
2884 * getline line <"doesnt_exist";
2885 * close("doesnt_exist"); <--- here rsm->F is NULL
2886 */
2887 if (rsm->F)
2888 err = rsm->is_pipe ? pclose(rsm->F) : fclose(rsm->F);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002889 free(rsm->buffer);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002890 hash_remove(fdhash, L.s);
2891 }
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002892 if (err)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002893 setvar_i(intvar[ERRNO], errno);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002894 R_d = (double)err;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002895 break;
2896 }
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002897 } /* switch */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002898 setvar_i(res, R_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002899 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002900 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002901
Denis Vlasenkof782f522007-01-01 23:51:30 +00002902 case XC( OC_BUILTIN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002903 res = exec_builtin(op, res);
2904 break;
2905
Denis Vlasenkof782f522007-01-01 23:51:30 +00002906 case XC( OC_SPRINTF ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002907 setvar_p(res, awk_printf(op1));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002908 break;
2909
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002910 case XC( OC_UNARY ): {
2911 double Ld, R_d;
2912
2913 Ld = R_d = getvar_i(R.v);
Mike Frysingerde2b9382005-09-27 03:18:00 +00002914 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002915 case 'P':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002916 Ld = ++R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002917 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002918 case 'p':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002919 R_d++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002920 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002921 case 'M':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002922 Ld = --R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002923 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002924 case 'm':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002925 R_d--;
2926 r_op_change:
2927 setvar_i(R.v, R_d);
2928 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002929 case '!':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002930 Ld = !istrue(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002931 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002932 case '-':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002933 Ld = -R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002934 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002935 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002936 setvar_i(res, Ld);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002937 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002938 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002939
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002940 case XC( OC_FIELD ): {
2941 int i = (int)getvar_i(R.v);
2942 if (i == 0) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002943 res = intvar[F0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002944 } else {
2945 split_f0();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002946 if (i > nfields)
2947 fsrealloc(i);
2948 res = &Fields[i - 1];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002949 }
2950 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002951 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002952
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002953 /* concatenation (" ") and index joining (",") */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002954 case XC( OC_CONCAT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002955 case XC( OC_COMMA ): {
2956 const char *sep = "";
2957 if ((opinfo & OPCLSMASK) == OC_COMMA)
2958 sep = getvar_s(intvar[SUBSEP]);
2959 setvar_p(res, xasprintf("%s%s%s", L.s, sep, R.s));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002960 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002961 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002962
Denis Vlasenkof782f522007-01-01 23:51:30 +00002963 case XC( OC_LAND ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002964 setvar_i(res, istrue(L.v) ? ptest(op->r.n) : 0);
2965 break;
2966
Denis Vlasenkof782f522007-01-01 23:51:30 +00002967 case XC( OC_LOR ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002968 setvar_i(res, istrue(L.v) ? 1 : ptest(op->r.n));
2969 break;
2970
Denis Vlasenkof782f522007-01-01 23:51:30 +00002971 case XC( OC_BINARY ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002972 case XC( OC_REPLACE ): {
2973 double R_d = getvar_i(R.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002974 debug_printf_eval("BINARY/REPLACE: R_d:%f opn:%c\n", R_d, opn);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002975 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002976 case '+':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002977 L_d += R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002978 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002979 case '-':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002980 L_d -= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002981 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002982 case '*':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002983 L_d *= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002984 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002985 case '/':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002986 if (R_d == 0)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002987 syntax_error(EMSG_DIV_BY_ZERO);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002988 L_d /= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002989 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002990 case '&':
Rob Landleyd8205b32010-10-24 03:27:22 +02002991 if (ENABLE_FEATURE_AWK_LIBM)
2992 L_d = pow(L_d, R_d);
2993 else
2994 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002995 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002996 case '%':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002997 if (R_d == 0)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002998 syntax_error(EMSG_DIV_BY_ZERO);
Denys Vlasenko1390a012013-07-20 21:23:01 +02002999 L_d -= (long long)(L_d / R_d) * R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003000 break;
3001 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02003002 debug_printf_eval("BINARY/REPLACE result:%f\n", L_d);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003003 res = setvar_i(((opinfo & OPCLSMASK) == OC_BINARY) ? res : L.v, L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003004 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003005 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003006
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003007 case XC( OC_COMPARE ): {
3008 int i = i; /* for compiler */
3009 double Ld;
3010
Glenn L McGrath545106f2002-11-11 06:21:00 +00003011 if (is_numeric(L.v) && is_numeric(R.v)) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003012 Ld = getvar_i(L.v) - getvar_i(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003013 } else {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01003014 const char *l = getvar_s(L.v);
3015 const char *r = getvar_s(R.v);
3016 Ld = icase ? strcasecmp(l, r) : strcmp(l, r);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003017 }
3018 switch (opn & 0xfe) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00003019 case 0:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003020 i = (Ld > 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003021 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003022 case 2:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003023 i = (Ld >= 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003024 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003025 case 4:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003026 i = (Ld == 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003027 break;
3028 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003029 setvar_i(res, (i == 0) ^ (opn & 1));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003030 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003031 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003032
Denis Vlasenkof782f522007-01-01 23:51:30 +00003033 default:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003034 syntax_error(EMSG_POSSIBLE_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003035 }
3036 if ((opinfo & OPCLSMASK) <= SHIFT_TIL_THIS)
3037 op = op->a.n;
3038 if ((opinfo & OPCLSMASK) >= RECUR_FROM_THIS)
3039 break;
3040 if (nextrec)
3041 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003042 } /* while (op) */
3043
Glenn L McGrath545106f2002-11-11 06:21:00 +00003044 nvfree(v1);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003045 debug_printf_eval("returning from %s(): %p\n", __func__, res);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003046 return res;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003047#undef fnargs
3048#undef seed
3049#undef sreg
Glenn L McGrath545106f2002-11-11 06:21:00 +00003050}
3051
3052
3053/* -------- main & co. -------- */
3054
Mike Frysinger10a11e22005-09-27 02:23:02 +00003055static int awk_exit(int r)
3056{
Denis Vlasenkof782f522007-01-01 23:51:30 +00003057 var tv;
3058 unsigned i;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003059 hash_item *hi;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003060
Denis Vlasenkof782f522007-01-01 23:51:30 +00003061 zero_out_var(&tv);
3062
3063 if (!exiting) {
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003064 exiting = TRUE;
Glenn L McGrathca29ffc2004-09-24 09:24:27 +00003065 nextrec = FALSE;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003066 evaluate(endseq.first, &tv);
3067 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003068
3069 /* waiting for children */
Denis Vlasenkof782f522007-01-01 23:51:30 +00003070 for (i = 0; i < fdhash->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003071 hi = fdhash->items[i];
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00003072 while (hi) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003073 if (hi->data.rs.F && hi->data.rs.is_pipe)
3074 pclose(hi->data.rs.F);
3075 hi = hi->next;
3076 }
3077 }
3078
3079 exit(r);
3080}
3081
3082/* if expr looks like "var=value", perform assignment and return 1,
3083 * otherwise return 0 */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00003084static int is_assignment(const char *expr)
Mike Frysinger10a11e22005-09-27 02:23:02 +00003085{
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003086 char *exprc, *val;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003087
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003088 if (!isalnum_(*expr) || (val = strchr(expr, '=')) == NULL) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003089 return FALSE;
3090 }
3091
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003092 exprc = xstrdup(expr);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003093 val = exprc + (val - expr);
3094 *val++ = '\0';
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003095
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003096 unescape_string_in_place(val);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003097 setvar_u(newvar(exprc), val);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003098 free(exprc);
3099 return TRUE;
3100}
3101
3102/* switch to next input file */
Mike Frysinger10a11e22005-09-27 02:23:02 +00003103static rstream *next_input_file(void)
3104{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003105#define rsm (G.next_input_file__rsm)
3106#define files_happen (G.next_input_file__files_happen)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003107
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003108 FILE *F;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00003109 const char *fname, *ind;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003110
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003111 if (rsm.F)
3112 fclose(rsm.F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003113 rsm.F = NULL;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003114 rsm.pos = rsm.adv = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003115
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003116 for (;;) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003117 if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003118 if (files_happen)
3119 return NULL;
3120 fname = "-";
3121 F = stdin;
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003122 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003123 }
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003124 ind = getvar_s(incvar(intvar[ARGIND]));
3125 fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind));
3126 if (fname && *fname && !is_assignment(fname)) {
3127 F = xfopen_stdin(fname);
3128 break;
3129 }
3130 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003131
3132 files_happen = TRUE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003133 setvar_s(intvar[FILENAME], fname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003134 rsm.F = F;
3135 return &rsm;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003136#undef rsm
3137#undef files_happen
Glenn L McGrath545106f2002-11-11 06:21:00 +00003138}
3139
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00003140int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +00003141int awk_main(int argc, char **argv)
Mike Frysinger10a11e22005-09-27 02:23:02 +00003142{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00003143 unsigned opt;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02003144 char *opt_F;
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003145 llist_t *list_v = NULL;
3146 llist_t *list_f = NULL;
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003147#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
3148 llist_t *list_e = NULL;
3149#endif
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003150 int i, j;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003151 var *v;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003152 var tv;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003153 char **envp;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003154 char *vnames = (char *)vNames; /* cheat */
3155 char *vvalues = (char *)vValues;
3156
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003157 INIT_G();
3158
Denis Vlasenko150f4022007-01-13 21:06:21 +00003159 /* Undo busybox.c, or else strtod may eat ','! This breaks parsing:
Denis Vlasenko6dc6ebb2007-01-01 23:53:12 +00003160 * $1,$2 == '$1,' '$2', NOT '$1' ',' '$2' */
3161 if (ENABLE_LOCALE_SUPPORT)
3162 setlocale(LC_NUMERIC, "C");
3163
Denis Vlasenkof782f522007-01-01 23:51:30 +00003164 zero_out_var(&tv);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003165
3166 /* allocate global buffer */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003167 g_buf = xmalloc(MAXVARFMT + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003168
3169 vhash = hash_init();
3170 ahash = hash_init();
3171 fdhash = hash_init();
3172 fnhash = hash_init();
3173
3174 /* initialize variables */
Denis Vlasenkof782f522007-01-01 23:51:30 +00003175 for (i = 0; *vnames; i++) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003176 intvar[i] = v = newvar(nextword(&vnames));
Denis Vlasenkof782f522007-01-01 23:51:30 +00003177 if (*vvalues != '\377')
3178 setvar_s(v, nextword(&vvalues));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003179 else
3180 setvar_i(v, 0);
3181
Denis Vlasenkof782f522007-01-01 23:51:30 +00003182 if (*vnames == '*') {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003183 v->type |= VF_SPECIAL;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003184 vnames++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003185 }
3186 }
3187
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003188 handle_special(intvar[FS]);
3189 handle_special(intvar[RS]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003190
Denis Vlasenkof782f522007-01-01 23:51:30 +00003191 newfile("/dev/stdin")->F = stdin;
3192 newfile("/dev/stdout")->F = stdout;
3193 newfile("/dev/stderr")->F = stderr;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003194
Denis Vlasenkof71d9162007-05-03 22:57:56 +00003195 /* Huh, people report that sometimes environ is NULL. Oh well. */
3196 if (environ) for (envp = environ; *envp; envp++) {
Denis Vlasenkob78c7822007-07-18 18:31:11 +00003197 /* environ is writable, thus we don't strdup it needlessly */
3198 char *s = *envp;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003199 char *s1 = strchr(s, '=');
3200 if (s1) {
Denis Vlasenkob78c7822007-07-18 18:31:11 +00003201 *s1 = '\0';
3202 /* Both findvar and setvar_u take const char*
3203 * as 2nd arg -> environment is not trashed */
3204 setvar_u(findvar(iamarray(intvar[ENVIRON]), s), s1 + 1);
3205 *s1 = '=';
Eric Andersen67776be2004-07-30 23:52:08 +00003206 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003207 }
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003208 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 +00003209 argv += optind;
3210 argc -= optind;
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003211 if (opt & OPT_W)
3212 bb_error_msg("warning: option -W is ignored");
3213 if (opt & OPT_F) {
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003214 unescape_string_in_place(opt_F);
3215 setvar_s(intvar[FS], opt_F);
3216 }
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003217 while (list_v) {
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003218 if (!is_assignment(llist_pop(&list_v)))
Denis Vlasenkobe644a82007-03-10 17:22:14 +00003219 bb_show_usage();
3220 }
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003221 while (list_f) {
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003222 char *s = NULL;
3223 FILE *from_file;
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003224
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003225 g_progname = llist_pop(&list_f);
3226 from_file = xfopen_stdin(g_progname);
3227 /* one byte is reserved for some trick in next_token */
3228 for (i = j = 1; j > 0; i += j) {
3229 s = xrealloc(s, i + 4096);
3230 j = fread(s + i, 1, 4094, from_file);
3231 }
3232 s[i] = '\0';
3233 fclose(from_file);
3234 parse_program(s + 1);
3235 free(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003236 }
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003237 g_progname = "cmd. line";
3238#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003239 while (list_e) {
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003240 parse_program(llist_pop(&list_e));
3241 }
3242#endif
3243 if (!(opt & (OPT_f | OPT_e))) {
3244 if (!*argv)
3245 bb_show_usage();
3246 parse_program(*argv++);
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003247 argc--;
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003248 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003249
Glenn L McGrath545106f2002-11-11 06:21:00 +00003250 /* fill in ARGV array */
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003251 setvar_i(intvar[ARGC], argc + 1);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003252 setari_u(intvar[ARGV], 0, "awk");
Denis Vlasenkof782f522007-01-01 23:51:30 +00003253 i = 0;
3254 while (*argv)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003255 setari_u(intvar[ARGV], ++i, *argv++);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003256
3257 evaluate(beginseq.first, &tv);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003258 if (!mainseq.first && !endseq.first)
Glenn L McGrath545106f2002-11-11 06:21:00 +00003259 awk_exit(EXIT_SUCCESS);
3260
3261 /* input file could already be opened in BEGIN block */
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003262 if (!iF)
3263 iF = next_input_file();
Glenn L McGrath545106f2002-11-11 06:21:00 +00003264
3265 /* passing through input files */
3266 while (iF) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003267 nextfile = FALSE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003268 setvar_i(intvar[FNR], 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003269
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003270 while ((i = awk_getline(iF, intvar[F0])) > 0) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003271 nextrec = FALSE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003272 incvar(intvar[NR]);
3273 incvar(intvar[FNR]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003274 evaluate(mainseq.first, &tv);
3275
3276 if (nextfile)
3277 break;
3278 }
3279
Denis Vlasenkof782f522007-01-01 23:51:30 +00003280 if (i < 0)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003281 syntax_error(strerror(errno));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003282
3283 iF = next_input_file();
Glenn L McGrath545106f2002-11-11 06:21:00 +00003284 }
3285
Glenn L McGrath545106f2002-11-11 06:21:00 +00003286 awk_exit(EXIT_SUCCESS);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003287 /*return 0;*/
Glenn L McGrath545106f2002-11-11 06:21:00 +00003288}