blob: d0269b9f43bd340d2eb5ce7fe6625fe7b9ca1ce8 [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
11//config: bool "awk"
12//config: default y
13//config: help
14//config: Awk is used as a pattern scanning and processing language. This is
15//config: the BusyBox implementation of that programming language.
16//config:
17//config:config FEATURE_AWK_LIBM
18//config: bool "Enable math functions (requires libm)"
19//config: default y
20//config: depends on AWK
21//config: help
22//config: Enable math functions of the Awk programming language.
23//config: NOTE: This will require libm to be present for linking.
24//config:
25//config:config FEATURE_AWK_GNU_EXTENSIONS
26//config: bool "Enable a few GNU extensions"
27//config: default y
28//config: depends on AWK
29//config: help
30//config: Enable a few features from gawk:
31//config: * command line option -e AWK_PROGRAM
32//config: * simultaneous use of -f and -e on the command line.
33//config: This enables the use of awk library files.
34//config: Ex: awk -f mylib.awk -e '{print myfunction($1);}' ...
35
36//applet:IF_AWK(APPLET_NOEXEC(awk, awk, BB_DIR_USR_BIN, BB_SUID_DROP, awk))
37
38//kbuild:lib-$(CONFIG_AWK) += awk.o
39
Pere Orga6a3e01d2011-04-01 22:56:30 +020040//usage:#define awk_trivial_usage
41//usage: "[OPTIONS] [AWK_PROGRAM] [FILE]..."
42//usage:#define awk_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020043//usage: " -v VAR=VAL Set variable"
Pere Orga6a3e01d2011-04-01 22:56:30 +020044//usage: "\n -F SEP Use SEP as field separator"
45//usage: "\n -f FILE Read program from FILE"
Sven-Göran Berghf200f732013-11-12 14:18:25 +010046//usage: IF_FEATURE_AWK_GNU_EXTENSIONS(
47//usage: "\n -e AWK_PROGRAM"
48//usage: )
Pere Orga6a3e01d2011-04-01 22:56:30 +020049
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000050#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000051#include "xregex.h"
52#include <math.h>
Glenn L McGrath545106f2002-11-11 06:21:00 +000053
Denis Vlasenko99912ca2007-04-10 15:43:37 +000054/* This is a NOEXEC applet. Be very careful! */
55
Glenn L McGrath545106f2002-11-11 06:21:00 +000056
Denys Vlasenkoda62b092010-03-11 12:13:18 +010057/* If you comment out one of these below, it will be #defined later
58 * to perform debug printfs to stderr: */
59#define debug_printf_walker(...) do {} while (0)
Denys Vlasenkod527e0c2010-10-05 13:22:11 +020060#define debug_printf_eval(...) do {} while (0)
Denys Vlasenko7b46d112011-09-11 00:30:56 +020061#define debug_printf_parse(...) do {} while (0)
Denys Vlasenkoda62b092010-03-11 12:13:18 +010062
63#ifndef debug_printf_walker
64# define debug_printf_walker(...) (fprintf(stderr, __VA_ARGS__))
65#endif
Denys Vlasenkod527e0c2010-10-05 13:22:11 +020066#ifndef debug_printf_eval
67# define debug_printf_eval(...) (fprintf(stderr, __VA_ARGS__))
68#endif
Denys Vlasenko7b46d112011-09-11 00:30:56 +020069#ifndef debug_printf_parse
70# define debug_printf_parse(...) (fprintf(stderr, __VA_ARGS__))
71#endif
Denys Vlasenkoda62b092010-03-11 12:13:18 +010072
73
Sven-Göran Berghf200f732013-11-12 14:18:25 +010074#define OPTSTR_AWK \
Denys Vlasenko237bedd2016-07-06 21:58:02 +020075 "F:v:*f:*" \
76 IF_FEATURE_AWK_GNU_EXTENSIONS("e:*") \
Sven-Göran Berghf200f732013-11-12 14:18:25 +010077 "W:"
Sven-Göran Berghf200f732013-11-12 14:18:25 +010078enum {
79 OPTBIT_F, /* define field separator */
80 OPTBIT_v, /* define variable */
81 OPTBIT_f, /* pull in awk program from file */
82 IF_FEATURE_AWK_GNU_EXTENSIONS(OPTBIT_e,) /* -e AWK_PROGRAM */
83 OPTBIT_W, /* -W ignored */
84 OPT_F = 1 << OPTBIT_F,
85 OPT_v = 1 << OPTBIT_v,
86 OPT_f = 1 << OPTBIT_f,
87 OPT_e = IF_FEATURE_AWK_GNU_EXTENSIONS((1 << OPTBIT_e)) + 0,
88 OPT_W = 1 << OPTBIT_W
89};
Denys Vlasenkoda62b092010-03-11 12:13:18 +010090
Denis Vlasenko629563b2007-02-24 17:05:52 +000091#define MAXVARFMT 240
92#define MINNVBLOCK 64
Glenn L McGrath545106f2002-11-11 06:21:00 +000093
94/* variable flags */
Denis Vlasenko629563b2007-02-24 17:05:52 +000095#define VF_NUMBER 0x0001 /* 1 = primary type is number */
96#define VF_ARRAY 0x0002 /* 1 = it's an array */
Glenn L McGrath545106f2002-11-11 06:21:00 +000097
Denis Vlasenko629563b2007-02-24 17:05:52 +000098#define VF_CACHED 0x0100 /* 1 = num/str value has cached str/num eq */
99#define VF_USER 0x0200 /* 1 = user input (may be numeric string) */
100#define VF_SPECIAL 0x0400 /* 1 = requires extra handling when changed */
101#define VF_WALK 0x0800 /* 1 = variable has alloc'd x.walker list */
102#define VF_FSTR 0x1000 /* 1 = var::string points to fstring buffer */
103#define VF_CHILD 0x2000 /* 1 = function arg; x.parent points to source */
104#define VF_DIRTY 0x4000 /* 1 = variable was set explicitly */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000105
106/* these flags are static, don't change them when value is changed */
Denis Vlasenko629563b2007-02-24 17:05:52 +0000107#define VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000108
Denys Vlasenkoda62b092010-03-11 12:13:18 +0100109typedef struct walker_list {
110 char *end;
111 char *cur;
112 struct walker_list *prev;
113 char wbuf[1];
114} walker_list;
115
Glenn L McGrath545106f2002-11-11 06:21:00 +0000116/* Variable */
117typedef struct var_s {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000118 unsigned type; /* flags */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000119 double number;
120 char *string;
121 union {
Denis Vlasenko629563b2007-02-24 17:05:52 +0000122 int aidx; /* func arg idx (for compilation stage) */
123 struct xhash_s *array; /* array ptr */
124 struct var_s *parent; /* for func args, ptr to actual parameter */
Denys Vlasenkoda62b092010-03-11 12:13:18 +0100125 walker_list *walker; /* list of array elements (for..in) */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000126 } x;
127} var;
128
129/* Node chain (pattern-action chain, BEGIN, END, function bodies) */
130typedef struct chain_s {
131 struct node_s *first;
132 struct node_s *last;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000133 const char *programname;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000134} chain;
135
136/* Function */
137typedef struct func_s {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000138 unsigned nargs;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000139 struct chain_s body;
140} func;
141
142/* I/O stream */
143typedef struct rstream_s {
144 FILE *F;
145 char *buffer;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +0000146 int adv;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000147 int size;
148 int pos;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000149 smallint is_pipe;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000150} rstream;
151
152typedef struct hash_item_s {
153 union {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000154 struct var_s v; /* variable/array hash */
155 struct rstream_s rs; /* redirect streams hash */
156 struct func_s f; /* functions hash */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000157 } data;
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000158 struct hash_item_s *next; /* next in chain */
159 char name[1]; /* really it's longer */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000160} hash_item;
161
162typedef struct xhash_s {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000163 unsigned nel; /* num of elements */
164 unsigned csize; /* current hash size */
165 unsigned nprime; /* next hash size in PRIMES[] */
166 unsigned glen; /* summary length of item names */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000167 struct hash_item_s **items;
168} xhash;
169
170/* Tree node */
171typedef struct node_s {
Mike Frysingerf87b3e32005-09-27 04:16:22 +0000172 uint32_t info;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000173 unsigned lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000174 union {
175 struct node_s *n;
176 var *v;
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100177 int aidx;
178 char *new_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000179 regex_t *re;
180 } l;
181 union {
182 struct node_s *n;
183 regex_t *ire;
184 func *f;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000185 } r;
186 union {
187 struct node_s *n;
188 } a;
189} node;
190
191/* Block of temporary variables */
192typedef struct nvblock_s {
193 int size;
194 var *pos;
195 struct nvblock_s *prev;
196 struct nvblock_s *next;
Denys Vlasenkod069e532009-09-09 23:12:10 +0200197 var nv[];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000198} nvblock;
199
200typedef struct tsplitter_s {
201 node n;
202 regex_t re[2];
203} tsplitter;
204
205/* simple token classes */
206/* Order and hex values are very important!!! See next_token() */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200207#define TC_SEQSTART (1 << 0) /* ( */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000208#define TC_SEQTERM (1 << 1) /* ) */
209#define TC_REGEXP (1 << 2) /* /.../ */
210#define TC_OUTRDR (1 << 3) /* | > >> */
211#define TC_UOPPOST (1 << 4) /* unary postfix operator */
212#define TC_UOPPRE1 (1 << 5) /* unary prefix operator */
213#define TC_BINOPX (1 << 6) /* two-opnd operator */
214#define TC_IN (1 << 7)
215#define TC_COMMA (1 << 8)
216#define TC_PIPE (1 << 9) /* input redirection pipe */
217#define TC_UOPPRE2 (1 << 10) /* unary prefix operator */
218#define TC_ARRTERM (1 << 11) /* ] */
219#define TC_GRPSTART (1 << 12) /* { */
220#define TC_GRPTERM (1 << 13) /* } */
221#define TC_SEMICOL (1 << 14)
222#define TC_NEWLINE (1 << 15)
223#define TC_STATX (1 << 16) /* ctl statement (for, next...) */
224#define TC_WHILE (1 << 17)
225#define TC_ELSE (1 << 18)
226#define TC_BUILTIN (1 << 19)
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200227/* This costs ~50 bytes of code.
228 * A separate class to support deprecated "length" form. If we don't need that
229 * (i.e. if we demand that only "length()" with () is valid), then TC_LENGTH
230 * can be merged with TC_BUILTIN:
231 */
232#define TC_LENGTH (1 << 20)
233#define TC_GETLINE (1 << 21)
234#define TC_FUNCDECL (1 << 22) /* `function' `func' */
235#define TC_BEGIN (1 << 23)
236#define TC_END (1 << 24)
237#define TC_EOF (1 << 25)
238#define TC_VARIABLE (1 << 26)
239#define TC_ARRAY (1 << 27)
240#define TC_FUNCTION (1 << 28)
241#define TC_STRING (1 << 29)
242#define TC_NUMBER (1 << 30)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000243
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000244#define TC_UOPPRE (TC_UOPPRE1 | TC_UOPPRE2)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000245
246/* combined token classes */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000247#define TC_BINOP (TC_BINOPX | TC_COMMA | TC_PIPE | TC_IN)
Denys Vlasenko1390a012013-07-20 21:23:01 +0200248//#define TC_UNARYOP (TC_UOPPRE | TC_UOPPOST)
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000249#define TC_OPERAND (TC_VARIABLE | TC_ARRAY | TC_FUNCTION \
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200250 | TC_BUILTIN | TC_LENGTH | TC_GETLINE \
251 | TC_SEQSTART | TC_STRING | TC_NUMBER)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000252
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000253#define TC_STATEMNT (TC_STATX | TC_WHILE)
254#define TC_OPTERM (TC_SEMICOL | TC_NEWLINE)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000255
256/* word tokens, cannot mean something else if not expected */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200257#define TC_WORD (TC_IN | TC_STATEMNT | TC_ELSE \
258 | TC_BUILTIN | TC_LENGTH | TC_GETLINE \
259 | TC_FUNCDECL | TC_BEGIN | TC_END)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000260
261/* discard newlines after these */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000262#define TC_NOTERM (TC_COMMA | TC_GRPSTART | TC_GRPTERM \
263 | TC_BINOP | TC_OPTERM)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000264
265/* what can expression begin with */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000266#define TC_OPSEQ (TC_OPERAND | TC_UOPPRE | TC_REGEXP)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000267/* what can group begin with */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000268#define TC_GRPSEQ (TC_OPSEQ | TC_OPTERM | TC_STATEMNT | TC_GRPSTART)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000269
270/* if previous token class is CONCAT1 and next is CONCAT2, concatenation */
271/* operator is inserted between them */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000272#define TC_CONCAT1 (TC_VARIABLE | TC_ARRTERM | TC_SEQTERM \
273 | TC_STRING | TC_NUMBER | TC_UOPPOST)
274#define TC_CONCAT2 (TC_OPERAND | TC_UOPPRE)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000275
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000276#define OF_RES1 0x010000
277#define OF_RES2 0x020000
278#define OF_STR1 0x040000
279#define OF_STR2 0x080000
280#define OF_NUM1 0x100000
281#define OF_CHECKED 0x200000
Glenn L McGrath545106f2002-11-11 06:21:00 +0000282
283/* combined operator flags */
284#define xx 0
285#define xV OF_RES2
286#define xS (OF_RES2 | OF_STR2)
287#define Vx OF_RES1
288#define VV (OF_RES1 | OF_RES2)
289#define Nx (OF_RES1 | OF_NUM1)
290#define NV (OF_RES1 | OF_NUM1 | OF_RES2)
291#define Sx (OF_RES1 | OF_STR1)
292#define SV (OF_RES1 | OF_STR1 | OF_RES2)
293#define SS (OF_RES1 | OF_STR1 | OF_RES2 | OF_STR2)
294
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000295#define OPCLSMASK 0xFF00
296#define OPNMASK 0x007F
Glenn L McGrath545106f2002-11-11 06:21:00 +0000297
298/* operator priority is a highest byte (even: r->l, odd: l->r grouping)
299 * For builtins it has different meaning: n n s3 s2 s1 v3 v2 v1,
300 * n - min. number of args, vN - resolve Nth arg to var, sN - resolve to string
301 */
Denys Vlasenko202a1b92011-09-10 04:51:09 +0200302#undef P
303#undef PRIMASK
304#undef PRIMASK2
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000305#define P(x) (x << 24)
306#define PRIMASK 0x7F000000
307#define PRIMASK2 0x7E000000
Glenn L McGrath545106f2002-11-11 06:21:00 +0000308
309/* Operation classes */
310
311#define SHIFT_TIL_THIS 0x0600
312#define RECUR_FROM_THIS 0x1000
313
314enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000315 OC_DELETE = 0x0100, OC_EXEC = 0x0200, OC_NEWSOURCE = 0x0300,
316 OC_PRINT = 0x0400, OC_PRINTF = 0x0500, OC_WALKINIT = 0x0600,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000317
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000318 OC_BR = 0x0700, OC_BREAK = 0x0800, OC_CONTINUE = 0x0900,
319 OC_EXIT = 0x0a00, OC_NEXT = 0x0b00, OC_NEXTFILE = 0x0c00,
320 OC_TEST = 0x0d00, OC_WALKNEXT = 0x0e00,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000321
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000322 OC_BINARY = 0x1000, OC_BUILTIN = 0x1100, OC_COLON = 0x1200,
323 OC_COMMA = 0x1300, OC_COMPARE = 0x1400, OC_CONCAT = 0x1500,
324 OC_FBLTIN = 0x1600, OC_FIELD = 0x1700, OC_FNARG = 0x1800,
325 OC_FUNC = 0x1900, OC_GETLINE = 0x1a00, OC_IN = 0x1b00,
326 OC_LAND = 0x1c00, OC_LOR = 0x1d00, OC_MATCH = 0x1e00,
327 OC_MOVE = 0x1f00, OC_PGETLINE = 0x2000, OC_REGEXP = 0x2100,
328 OC_REPLACE = 0x2200, OC_RETURN = 0x2300, OC_SPRINTF = 0x2400,
329 OC_TERNARY = 0x2500, OC_UNARY = 0x2600, OC_VAR = 0x2700,
330 OC_DONE = 0x2800,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000331
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000332 ST_IF = 0x3000, ST_DO = 0x3100, ST_FOR = 0x3200,
333 ST_WHILE = 0x3300
Glenn L McGrath545106f2002-11-11 06:21:00 +0000334};
335
336/* simple builtins */
337enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000338 F_in, F_rn, F_co, F_ex, F_lg, F_si, F_sq, F_sr,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000339 F_ti, F_le, F_sy, F_ff, F_cl
340};
341
342/* builtins */
343enum {
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +0200344 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 +0000345 B_ge, B_gs, B_su,
346 B_an, B_co, B_ls, B_or, B_rs, B_xo,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000347};
348
349/* tokens and their corresponding info values */
350
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200351#define NTC "\377" /* switch to next token class (tc<<1) */
352#define NTCC '\377'
Glenn L McGrath545106f2002-11-11 06:21:00 +0000353
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000354static const char tokenlist[] ALIGN1 =
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200355 "\1(" NTC /* TC_SEQSTART */
356 "\1)" NTC /* TC_SEQTERM */
357 "\1/" NTC /* TC_REGEXP */
358 "\2>>" "\1>" "\1|" NTC /* TC_OUTRDR */
359 "\2++" "\2--" NTC /* TC_UOPPOST */
360 "\2++" "\2--" "\1$" NTC /* TC_UOPPRE1 */
361 "\2==" "\1=" "\2+=" "\2-=" /* TC_BINOPX */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200362 "\2*=" "\2/=" "\2%=" "\2^="
363 "\1+" "\1-" "\3**=" "\2**"
364 "\1/" "\1%" "\1^" "\1*"
365 "\2!=" "\2>=" "\2<=" "\1>"
366 "\1<" "\2!~" "\1~" "\2&&"
367 "\2||" "\1?" "\1:" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200368 "\2in" NTC /* TC_IN */
369 "\1," NTC /* TC_COMMA */
370 "\1|" NTC /* TC_PIPE */
371 "\1+" "\1-" "\1!" NTC /* TC_UOPPRE2 */
372 "\1]" NTC /* TC_ARRTERM */
373 "\1{" NTC /* TC_GRPSTART */
374 "\1}" NTC /* TC_GRPTERM */
375 "\1;" NTC /* TC_SEMICOL */
376 "\1\n" NTC /* TC_NEWLINE */
377 "\2if" "\2do" "\3for" "\5break" /* TC_STATX */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200378 "\10continue" "\6delete" "\5print"
379 "\6printf" "\4next" "\10nextfile"
380 "\6return" "\4exit" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200381 "\5while" NTC /* TC_WHILE */
382 "\4else" NTC /* TC_ELSE */
383 "\3and" "\5compl" "\6lshift" "\2or" /* TC_BUILTIN */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200384 "\6rshift" "\3xor"
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200385 "\5close" "\6system" "\6fflush" "\5atan2"
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200386 "\3cos" "\3exp" "\3int" "\3log"
387 "\4rand" "\3sin" "\4sqrt" "\5srand"
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200388 "\6gensub" "\4gsub" "\5index" /* "\6length" was here */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200389 "\5match" "\5split" "\7sprintf" "\3sub"
390 "\6substr" "\7systime" "\10strftime" "\6mktime"
391 "\7tolower" "\7toupper" NTC
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200392 "\6length" NTC /* TC_LENGTH */
393 "\7getline" NTC /* TC_GETLINE */
394 "\4func" "\10function" NTC /* TC_FUNCDECL */
395 "\5BEGIN" NTC /* TC_BEGIN */
396 "\3END" /* TC_END */
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200397 /* compiler adds trailing "\0" */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000398 ;
399
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200400#define OC_B OC_BUILTIN
401
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000402static const uint32_t tokeninfo[] = {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000403 0,
404 0,
405 OC_REGEXP,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200406 xS|'a', xS|'w', xS|'|',
407 OC_UNARY|xV|P(9)|'p', OC_UNARY|xV|P(9)|'m',
408 OC_UNARY|xV|P(9)|'P', OC_UNARY|xV|P(9)|'M', OC_FIELD|xV|P(5),
409 OC_COMPARE|VV|P(39)|5, OC_MOVE|VV|P(74), OC_REPLACE|NV|P(74)|'+', OC_REPLACE|NV|P(74)|'-',
410 OC_REPLACE|NV|P(74)|'*', OC_REPLACE|NV|P(74)|'/', OC_REPLACE|NV|P(74)|'%', OC_REPLACE|NV|P(74)|'&',
411 OC_BINARY|NV|P(29)|'+', OC_BINARY|NV|P(29)|'-', OC_REPLACE|NV|P(74)|'&', OC_BINARY|NV|P(15)|'&',
412 OC_BINARY|NV|P(25)|'/', OC_BINARY|NV|P(25)|'%', OC_BINARY|NV|P(15)|'&', OC_BINARY|NV|P(25)|'*',
413 OC_COMPARE|VV|P(39)|4, OC_COMPARE|VV|P(39)|3, OC_COMPARE|VV|P(39)|0, OC_COMPARE|VV|P(39)|1,
414 OC_COMPARE|VV|P(39)|2, OC_MATCH|Sx|P(45)|'!', OC_MATCH|Sx|P(45)|'~', OC_LAND|Vx|P(55),
415 OC_LOR|Vx|P(59), OC_TERNARY|Vx|P(64)|'?', OC_COLON|xx|P(67)|':',
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200416 OC_IN|SV|P(49), /* TC_IN */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000417 OC_COMMA|SS|P(80),
418 OC_PGETLINE|SV|P(37),
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200419 OC_UNARY|xV|P(19)|'+', OC_UNARY|xV|P(19)|'-', OC_UNARY|xV|P(19)|'!',
420 0, /* ] */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000421 0,
422 0,
423 0,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200424 0, /* \n */
425 ST_IF, ST_DO, ST_FOR, OC_BREAK,
426 OC_CONTINUE, OC_DELETE|Vx, OC_PRINT,
427 OC_PRINTF, OC_NEXT, OC_NEXTFILE,
428 OC_RETURN|Vx, OC_EXIT|Nx,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000429 ST_WHILE,
Denys Vlasenko6a0d7492010-10-23 21:02:15 +0200430 0, /* else */
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000431 OC_B|B_an|P(0x83), OC_B|B_co|P(0x41), OC_B|B_ls|P(0x83), OC_B|B_or|P(0x83),
432 OC_B|B_rs|P(0x83), OC_B|B_xo|P(0x83),
Glenn L McGrath545106f2002-11-11 06:21:00 +0000433 OC_FBLTIN|Sx|F_cl, OC_FBLTIN|Sx|F_sy, OC_FBLTIN|Sx|F_ff, OC_B|B_a2|P(0x83),
434 OC_FBLTIN|Nx|F_co, OC_FBLTIN|Nx|F_ex, OC_FBLTIN|Nx|F_in, OC_FBLTIN|Nx|F_lg,
435 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 +0200436 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 +0000437 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 +0200438 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 +0000439 OC_B|B_lo|P(0x49), OC_B|B_up|P(0x49),
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200440 OC_FBLTIN|Sx|F_le, /* TC_LENGTH */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000441 OC_GETLINE|SV|P(0),
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200442 0, 0,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000443 0,
Denys Vlasenko28b00ce2015-10-02 02:41:39 +0200444 0 /* TC_END */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000445};
446
447/* internal variable names and their initial values */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000448/* asterisk marks SPECIAL vars; $ is just no-named Field0 */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000449enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000450 CONVFMT, OFMT, FS, OFS,
Denis Vlasenkof782f522007-01-01 23:51:30 +0000451 ORS, RS, RT, FILENAME,
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000452 SUBSEP, F0, ARGIND, ARGC,
453 ARGV, ERRNO, FNR, NR,
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200454 NF, IGNORECASE, ENVIRON, NUM_INTERNAL_VARS
Glenn L McGrath545106f2002-11-11 06:21:00 +0000455};
456
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000457static const char vNames[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000458 "CONVFMT\0" "OFMT\0" "FS\0*" "OFS\0"
459 "ORS\0" "RS\0*" "RT\0" "FILENAME\0"
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000460 "SUBSEP\0" "$\0*" "ARGIND\0" "ARGC\0"
461 "ARGV\0" "ERRNO\0" "FNR\0" "NR\0"
462 "NF\0*" "IGNORECASE\0*" "ENVIRON\0" "\0";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000463
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000464static const char vValues[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000465 "%.6g\0" "%.6g\0" " \0" " \0"
466 "\n\0" "\n\0" "\0" "\0"
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000467 "\034\0" "\0" "\377";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000468
469/* hash size may grow to these values */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000470#define FIRST_PRIME 61
471static const uint16_t PRIMES[] ALIGN2 = { 251, 1021, 4093, 16381, 65521 };
Glenn L McGrath545106f2002-11-11 06:21:00 +0000472
Glenn L McGrath545106f2002-11-11 06:21:00 +0000473
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000474/* Globals. Split in two parts so that first one is addressed
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000475 * with (mostly short) negative offsets.
476 * NB: it's unsafe to put members of type "double"
477 * into globals2 (gcc may fail to align them).
478 */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000479struct globals {
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000480 double t_double;
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000481 chain beginseq, mainseq, endseq;
482 chain *seq;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000483 node *break_ptr, *continue_ptr;
484 rstream *iF;
485 xhash *vhash, *ahash, *fdhash, *fnhash;
486 const char *g_progname;
487 int g_lineno;
488 int nfields;
489 int maxfields; /* used in fsrealloc() only */
490 var *Fields;
491 nvblock *g_cb;
492 char *g_pos;
493 char *g_buf;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000494 smallint icase;
495 smallint exiting;
496 smallint nextrec;
497 smallint nextfile;
498 smallint is_f0_split;
Denys Vlasenko7b46d112011-09-11 00:30:56 +0200499 smallint t_rollback;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000500};
501struct globals2 {
502 uint32_t t_info; /* often used */
503 uint32_t t_tclass;
504 char *t_string;
505 int t_lineno;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000506
507 var *intvar[NUM_INTERNAL_VARS]; /* often used */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000508
509 /* former statics from various functions */
510 char *split_f0__fstrings;
511
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000512 uint32_t next_token__save_tclass;
513 uint32_t next_token__save_info;
514 uint32_t next_token__ltclass;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000515 smallint next_token__concat_inserted;
516
517 smallint next_input_file__files_happen;
518 rstream next_input_file__rsm;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000519
520 var *evaluate__fnargs;
521 unsigned evaluate__seed;
522 regex_t evaluate__sreg;
523
524 var ptest__v;
525
526 tsplitter exec_builtin__tspl;
527
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000528 /* biggest and least used members go last */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000529 tsplitter fsplitter, rsplitter;
Denys Vlasenko3dbc5a92010-02-05 14:54:22 +0100530};
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000531#define G1 (ptr_to_globals[-1])
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000532#define G (*(struct globals2 *)ptr_to_globals)
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000533/* For debug. nm --size-sort awk.o | grep -vi ' [tr] ' */
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000534/*char G1size[sizeof(G1)]; - 0x74 */
535/*char Gsize[sizeof(G)]; - 0x1c4 */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000536/* Trying to keep most of members accessible with short offsets: */
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000537/*char Gofs_seed[offsetof(struct globals2, evaluate__seed)]; - 0x90 */
538#define t_double (G1.t_double )
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000539#define beginseq (G1.beginseq )
540#define mainseq (G1.mainseq )
541#define endseq (G1.endseq )
542#define seq (G1.seq )
543#define break_ptr (G1.break_ptr )
544#define continue_ptr (G1.continue_ptr)
545#define iF (G1.iF )
546#define vhash (G1.vhash )
547#define ahash (G1.ahash )
548#define fdhash (G1.fdhash )
549#define fnhash (G1.fnhash )
550#define g_progname (G1.g_progname )
551#define g_lineno (G1.g_lineno )
552#define nfields (G1.nfields )
553#define maxfields (G1.maxfields )
554#define Fields (G1.Fields )
555#define g_cb (G1.g_cb )
556#define g_pos (G1.g_pos )
557#define g_buf (G1.g_buf )
558#define icase (G1.icase )
559#define exiting (G1.exiting )
560#define nextrec (G1.nextrec )
561#define nextfile (G1.nextfile )
562#define is_f0_split (G1.is_f0_split )
Denys Vlasenko7b46d112011-09-11 00:30:56 +0200563#define t_rollback (G1.t_rollback )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000564#define t_info (G.t_info )
565#define t_tclass (G.t_tclass )
566#define t_string (G.t_string )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000567#define t_lineno (G.t_lineno )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000568#define intvar (G.intvar )
569#define fsplitter (G.fsplitter )
570#define rsplitter (G.rsplitter )
571#define INIT_G() do { \
Denys Vlasenko90a99042009-09-06 02:36:23 +0200572 SET_PTR_TO_GLOBALS((char*)xzalloc(sizeof(G1)+sizeof(G)) + sizeof(G1)); \
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000573 G.next_token__ltclass = TC_OPTERM; \
574 G.evaluate__seed = 1; \
575} while (0)
576
Glenn L McGrath545106f2002-11-11 06:21:00 +0000577
578/* function prototypes */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000579static void handle_special(var *);
Mike Frysingerf87b3e32005-09-27 04:16:22 +0000580static node *parse_expr(uint32_t);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000581static void chain_group(void);
582static var *evaluate(node *, var *);
583static rstream *next_input_file(void);
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000584static int fmt_num(char *, int, const char *, double, int);
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000585static int awk_exit(int) NORETURN;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000586
587/* ---- error handling ---- */
588
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000589static const char EMSG_INTERNAL_ERROR[] ALIGN1 = "Internal error";
590static const char EMSG_UNEXP_EOS[] ALIGN1 = "Unexpected end of string";
591static const char EMSG_UNEXP_TOKEN[] ALIGN1 = "Unexpected token";
592static const char EMSG_DIV_BY_ZERO[] ALIGN1 = "Division by zero";
593static const char EMSG_INV_FMT[] ALIGN1 = "Invalid format specifier";
594static const char EMSG_TOO_FEW_ARGS[] ALIGN1 = "Too few arguments for builtin";
595static const char EMSG_NOT_ARRAY[] ALIGN1 = "Not an array";
596static const char EMSG_POSSIBLE_ERROR[] ALIGN1 = "Possible syntax error";
597static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function";
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000598static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000599
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100600static void zero_out_var(var *vp)
Denis Vlasenkof782f522007-01-01 23:51:30 +0000601{
602 memset(vp, 0, sizeof(*vp));
603}
604
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000605static void syntax_error(const char *message) NORETURN;
606static void syntax_error(const char *message)
Glenn L McGrathd4036f82002-11-28 09:30:40 +0000607{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000608 bb_error_msg_and_die("%s:%i: %s", g_progname, g_lineno, message);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000609}
610
Glenn L McGrath545106f2002-11-11 06:21:00 +0000611/* ---- hash stuff ---- */
612
Denis Vlasenkof782f522007-01-01 23:51:30 +0000613static unsigned hashidx(const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000614{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000615 unsigned idx = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000616
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100617 while (*name)
618 idx = *name++ + (idx << 6) - idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000619 return idx;
620}
621
622/* create new hash */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000623static xhash *hash_init(void)
624{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000625 xhash *newhash;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000626
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100627 newhash = xzalloc(sizeof(*newhash));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000628 newhash->csize = FIRST_PRIME;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100629 newhash->items = xzalloc(FIRST_PRIME * sizeof(newhash->items[0]));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000630
631 return newhash;
632}
633
634/* find item in hash, return ptr to data, NULL if not found */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000635static void *hash_search(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000636{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000637 hash_item *hi;
638
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100639 hi = hash->items[hashidx(name) % hash->csize];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000640 while (hi) {
641 if (strcmp(hi->name, name) == 0)
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100642 return &hi->data;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000643 hi = hi->next;
644 }
645 return NULL;
646}
647
648/* grow hash if it becomes too big */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000649static void hash_rebuild(xhash *hash)
650{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000651 unsigned newsize, i, idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000652 hash_item **newitems, *hi, *thi;
653
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000654 if (hash->nprime == ARRAY_SIZE(PRIMES))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000655 return;
656
657 newsize = PRIMES[hash->nprime++];
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100658 newitems = xzalloc(newsize * sizeof(newitems[0]));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000659
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000660 for (i = 0; i < hash->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000661 hi = hash->items[i];
662 while (hi) {
663 thi = hi;
664 hi = thi->next;
665 idx = hashidx(thi->name) % newsize;
666 thi->next = newitems[idx];
667 newitems[idx] = thi;
668 }
669 }
670
671 free(hash->items);
672 hash->csize = newsize;
673 hash->items = newitems;
674}
675
676/* find item in hash, add it if necessary. Return ptr to data */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000677static void *hash_find(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000678{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000679 hash_item *hi;
Denis Vlasenkof782f522007-01-01 23:51:30 +0000680 unsigned idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000681 int l;
682
683 hi = hash_search(hash, name);
Denis Vlasenkob78c7822007-07-18 18:31:11 +0000684 if (!hi) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000685 if (++hash->nel / hash->csize > 10)
686 hash_rebuild(hash);
687
Rob Landleya3896512006-05-07 20:20:34 +0000688 l = strlen(name) + 1;
Denis Vlasenko7a676642009-03-15 22:20:31 +0000689 hi = xzalloc(sizeof(*hi) + l);
690 strcpy(hi->name, name);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000691
692 idx = hashidx(name) % hash->csize;
693 hi->next = hash->items[idx];
694 hash->items[idx] = hi;
695 hash->glen += l;
696 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100697 return &hi->data;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000698}
699
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000700#define findvar(hash, name) ((var*) hash_find((hash), (name)))
701#define newvar(name) ((var*) hash_find(vhash, (name)))
702#define newfile(name) ((rstream*)hash_find(fdhash, (name)))
703#define newfunc(name) ((func*) hash_find(fnhash, (name)))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000704
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000705static void hash_remove(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000706{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000707 hash_item *hi, **phi;
708
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100709 phi = &hash->items[hashidx(name) % hash->csize];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000710 while (*phi) {
711 hi = *phi;
712 if (strcmp(hi->name, name) == 0) {
Rob Landleya3896512006-05-07 20:20:34 +0000713 hash->glen -= (strlen(name) + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000714 hash->nel--;
715 *phi = hi->next;
716 free(hi);
717 break;
718 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100719 phi = &hi->next;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000720 }
721}
722
723/* ------ some useful functions ------ */
724
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100725static char *skip_spaces(char *p)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000726{
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000727 while (1) {
728 if (*p == '\\' && p[1] == '\n') {
729 p++;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000730 t_lineno++;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000731 } else if (*p != ' ' && *p != '\t') {
732 break;
733 }
Mike Frysingerde2b9382005-09-27 03:18:00 +0000734 p++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000735 }
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100736 return p;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000737}
738
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100739/* returns old *s, advances *s past word and terminating NUL */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000740static char *nextword(char **s)
741{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000742 char *p = *s;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100743 while (*(*s)++ != '\0')
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100744 continue;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000745 return p;
746}
747
Mike Frysinger10a11e22005-09-27 02:23:02 +0000748static char nextchar(char **s)
749{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000750 char c, *pps;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000751
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100752 c = *(*s)++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000753 pps = *s;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100754 if (c == '\\')
755 c = bb_process_escape_sequence((const char**)s);
Denys Vlasenkoea664dd2012-06-22 18:41:01 +0200756 /* Example awk statement:
757 * s = "abc\"def"
758 * we must treat \" as "
759 */
Denys Vlasenko2b299fe2010-10-24 01:58:04 +0200760 if (c == '\\' && *s == pps) { /* unrecognized \z? */
761 c = *(*s); /* yes, fetch z */
762 if (c)
763 (*s)++; /* advance unless z = NUL */
764 }
Glenn L McGrath545106f2002-11-11 06:21:00 +0000765 return c;
766}
767
Denys Vlasenkoea664dd2012-06-22 18:41:01 +0200768/* TODO: merge with strcpy_and_process_escape_sequences()?
769 */
770static void unescape_string_in_place(char *s1)
771{
772 char *s = s1;
773 while ((*s1 = nextchar(&s)) != '\0')
774 s1++;
775}
776
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000777static ALWAYS_INLINE int isalnum_(int c)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000778{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000779 return (isalnum(c) || c == '_');
780}
781
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000782static double my_strtod(char **pp)
783{
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200784 char *cp = *pp;
Rob Landleyd8205b32010-10-24 03:27:22 +0200785 if (ENABLE_DESKTOP && cp[0] == '0') {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200786 /* Might be hex or octal integer: 0x123abc or 07777 */
787 char c = (cp[1] | 0x20);
788 if (c == 'x' || isdigit(cp[1])) {
789 unsigned long long ull = strtoull(cp, pp, 0);
790 if (c == 'x')
791 return ull;
792 c = **pp;
793 if (!isdigit(c) && c != '.')
794 return ull;
795 /* else: it may be a floating number. Examples:
796 * 009.123 (*pp points to '9')
797 * 000.123 (*pp points to '.')
798 * fall through to strtod.
799 */
800 }
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000801 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200802 return strtod(cp, pp);
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000803}
804
Glenn L McGrath545106f2002-11-11 06:21:00 +0000805/* -------- working with variables (set/get/copy/etc) -------- */
806
Mike Frysinger10a11e22005-09-27 02:23:02 +0000807static xhash *iamarray(var *v)
808{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000809 var *a = v;
810
811 while (a->type & VF_CHILD)
812 a = a->x.parent;
813
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000814 if (!(a->type & VF_ARRAY)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000815 a->type |= VF_ARRAY;
816 a->x.array = hash_init();
817 }
818 return a->x.array;
819}
820
Mike Frysinger10a11e22005-09-27 02:23:02 +0000821static void clear_array(xhash *array)
822{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000823 unsigned i;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000824 hash_item *hi, *thi;
825
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000826 for (i = 0; i < array->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000827 hi = array->items[i];
828 while (hi) {
829 thi = hi;
830 hi = hi->next;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000831 free(thi->data.v.string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000832 free(thi);
833 }
834 array->items[i] = NULL;
835 }
836 array->glen = array->nel = 0;
837}
838
839/* clear a variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000840static var *clrvar(var *v)
841{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000842 if (!(v->type & VF_FSTR))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000843 free(v->string);
844
845 v->type &= VF_DONTTOUCH;
846 v->type |= VF_DIRTY;
847 v->string = NULL;
848 return v;
849}
850
851/* assign string value to variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000852static var *setvar_p(var *v, char *value)
853{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000854 clrvar(v);
855 v->string = value;
856 handle_special(v);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000857 return v;
858}
859
860/* same as setvar_p but make a copy of string */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000861static var *setvar_s(var *v, const char *value)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000862{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000863 return setvar_p(v, (value && *value) ? xstrdup(value) : NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000864}
865
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100866/* same as setvar_s but sets USER flag */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000867static var *setvar_u(var *v, const char *value)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000868{
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100869 v = setvar_s(v, value);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000870 v->type |= VF_USER;
871 return v;
872}
873
874/* set array element to user string */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000875static void setari_u(var *a, int idx, const char *s)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000876{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000877 var *v;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000878
Denys Vlasenko7bb346f2009-10-06 22:09:50 +0200879 v = findvar(iamarray(a), itoa(idx));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000880 setvar_u(v, s);
881}
882
883/* assign numeric value to variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000884static var *setvar_i(var *v, double value)
885{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000886 clrvar(v);
887 v->type |= VF_NUMBER;
888 v->number = value;
889 handle_special(v);
890 return v;
891}
892
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000893static const char *getvar_s(var *v)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000894{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000895 /* if v is numeric and has no cached string, convert it to string */
896 if ((v->type & (VF_NUMBER | VF_CACHED)) == VF_NUMBER) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000897 fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[CONVFMT]), v->number, TRUE);
898 v->string = xstrdup(g_buf);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000899 v->type |= VF_CACHED;
900 }
901 return (v->string == NULL) ? "" : v->string;
902}
903
Mike Frysinger10a11e22005-09-27 02:23:02 +0000904static double getvar_i(var *v)
905{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000906 char *s;
907
908 if ((v->type & (VF_NUMBER | VF_CACHED)) == 0) {
909 v->number = 0;
910 s = v->string;
911 if (s && *s) {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200912 debug_printf_eval("getvar_i: '%s'->", s);
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000913 v->number = my_strtod(&s);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200914 debug_printf_eval("%f (s:'%s')\n", v->number, s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000915 if (v->type & VF_USER) {
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100916 s = skip_spaces(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000917 if (*s != '\0')
918 v->type &= ~VF_USER;
919 }
920 } else {
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200921 debug_printf_eval("getvar_i: '%s'->zero\n", s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000922 v->type &= ~VF_USER;
923 }
924 v->type |= VF_CACHED;
925 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200926 debug_printf_eval("getvar_i: %f\n", v->number);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000927 return v->number;
928}
929
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000930/* Used for operands of bitwise ops */
931static unsigned long getvar_i_int(var *v)
932{
933 double d = getvar_i(v);
934
935 /* Casting doubles to longs is undefined for values outside
936 * of target type range. Try to widen it as much as possible */
937 if (d >= 0)
938 return (unsigned long)d;
Denis Vlasenko665eaff2008-09-05 04:59:02 +0000939 /* Why? Think about d == -4294967295.0 (assuming 32bit longs) */
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000940 return - (long) (unsigned long) (-d);
941}
942
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000943static var *copyvar(var *dest, const var *src)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000944{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000945 if (dest != src) {
946 clrvar(dest);
Denis Vlasenko629563b2007-02-24 17:05:52 +0000947 dest->type |= (src->type & ~(VF_DONTTOUCH | VF_FSTR));
Denys Vlasenkod527e0c2010-10-05 13:22:11 +0200948 debug_printf_eval("copyvar: number:%f string:'%s'\n", src->number, src->string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000949 dest->number = src->number;
950 if (src->string)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000951 dest->string = xstrdup(src->string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000952 }
953 handle_special(dest);
954 return dest;
955}
956
Mike Frysinger10a11e22005-09-27 02:23:02 +0000957static var *incvar(var *v)
958{
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100959 return setvar_i(v, getvar_i(v) + 1.0);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000960}
961
962/* return true if v is number or numeric string */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000963static int is_numeric(var *v)
964{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000965 getvar_i(v);
966 return ((v->type ^ VF_DIRTY) & (VF_NUMBER | VF_USER | VF_DIRTY));
967}
968
969/* return 1 when value of v corresponds to true, 0 otherwise */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000970static int istrue(var *v)
971{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000972 if (is_numeric(v))
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100973 return (v->number != 0);
974 return (v->string && v->string[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000975}
976
Eric Andersenaff114c2004-04-14 17:51:38 +0000977/* temporary variables allocator. Last allocated should be first freed */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000978static var *nvalloc(int n)
979{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000980 nvblock *pb = NULL;
981 var *v, *r;
982 int size;
983
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000984 while (g_cb) {
985 pb = g_cb;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100986 if ((g_cb->pos - g_cb->nv) + n <= g_cb->size)
987 break;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000988 g_cb = g_cb->next;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000989 }
990
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000991 if (!g_cb) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000992 size = (n <= MINNVBLOCK) ? MINNVBLOCK : n;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000993 g_cb = xzalloc(sizeof(nvblock) + size * sizeof(var));
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000994 g_cb->size = size;
995 g_cb->pos = g_cb->nv;
996 g_cb->prev = pb;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000997 /*g_cb->next = NULL; - xzalloc did it */
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100998 if (pb)
999 pb->next = g_cb;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001000 }
1001
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001002 v = r = g_cb->pos;
1003 g_cb->pos += n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001004
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001005 while (v < g_cb->pos) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001006 v->type = 0;
1007 v->string = NULL;
1008 v++;
1009 }
1010
1011 return r;
1012}
1013
Mike Frysinger10a11e22005-09-27 02:23:02 +00001014static void nvfree(var *v)
1015{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001016 var *p;
1017
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001018 if (v < g_cb->nv || v >= g_cb->pos)
1019 syntax_error(EMSG_INTERNAL_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001020
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001021 for (p = v; p < g_cb->pos; p++) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001022 if ((p->type & (VF_ARRAY | VF_CHILD)) == VF_ARRAY) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001023 clear_array(iamarray(p));
1024 free(p->x.array->items);
1025 free(p->x.array);
1026 }
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001027 if (p->type & VF_WALK) {
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001028 walker_list *n;
1029 walker_list *w = p->x.walker;
1030 debug_printf_walker("nvfree: freeing walker @%p\n", &p->x.walker);
1031 p->x.walker = NULL;
1032 while (w) {
1033 n = w->prev;
1034 debug_printf_walker(" free(%p)\n", w);
1035 free(w);
1036 w = n;
1037 }
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001038 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001039 clrvar(p);
1040 }
1041
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001042 g_cb->pos = v;
1043 while (g_cb->prev && g_cb->pos == g_cb->nv) {
1044 g_cb = g_cb->prev;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001045 }
1046}
1047
1048/* ------- awk program text parsing ------- */
1049
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001050/* Parse next token pointed by global pos, place results into global ttt.
Glenn L McGrath545106f2002-11-11 06:21:00 +00001051 * If token isn't expected, give away. Return token class
1052 */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001053static uint32_t next_token(uint32_t expected)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001054{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001055#define concat_inserted (G.next_token__concat_inserted)
1056#define save_tclass (G.next_token__save_tclass)
1057#define save_info (G.next_token__save_info)
1058/* Initialized to TC_OPTERM: */
1059#define ltclass (G.next_token__ltclass)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001060
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001061 char *p, *s;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001062 const char *tl;
1063 uint32_t tc;
1064 const uint32_t *ti;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001065
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001066 if (t_rollback) {
1067 t_rollback = FALSE;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001068 } else if (concat_inserted) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001069 concat_inserted = FALSE;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001070 t_tclass = save_tclass;
1071 t_info = save_info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001072 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001073 p = g_pos;
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001074 readnext:
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +01001075 p = skip_spaces(p);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001076 g_lineno = t_lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001077 if (*p == '#')
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001078 while (*p != '\n' && *p != '\0')
1079 p++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001080
1081 if (*p == '\n')
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001082 t_lineno++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001083
1084 if (*p == '\0') {
1085 tc = TC_EOF;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001086 debug_printf_parse("%s: token found: TC_EOF\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001087 } else if (*p == '\"') {
1088 /* it's a string */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001089 t_string = s = ++p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001090 while (*p != '\"') {
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02001091 char *pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001092 if (*p == '\0' || *p == '\n')
1093 syntax_error(EMSG_UNEXP_EOS);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02001094 pp = p;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001095 *s++ = nextchar(&pp);
1096 p = pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001097 }
1098 p++;
1099 *s = '\0';
1100 tc = TC_STRING;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001101 debug_printf_parse("%s: token found:'%s' TC_STRING\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001102 } else if ((expected & TC_REGEXP) && *p == '/') {
1103 /* it's regexp */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001104 t_string = s = ++p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001105 while (*p != '/') {
1106 if (*p == '\0' || *p == '\n')
1107 syntax_error(EMSG_UNEXP_EOS);
Denis Vlasenkod9b5ab82007-05-18 07:30:43 +00001108 *s = *p++;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001109 if (*s++ == '\\') {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001110 char *pp = p;
1111 s[-1] = bb_process_escape_sequence((const char **)&pp);
1112 if (*p == '\\')
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001113 *s++ = '\\';
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001114 if (pp == p)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001115 *s++ = *p++;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001116 else
1117 p = pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001118 }
1119 }
1120 p++;
1121 *s = '\0';
1122 tc = TC_REGEXP;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001123 debug_printf_parse("%s: token found:'%s' TC_REGEXP\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001124
1125 } else if (*p == '.' || isdigit(*p)) {
1126 /* it's a number */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001127 char *pp = p;
1128 t_double = my_strtod(&pp);
1129 p = pp;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001130 if (*p == '.')
Glenn L McGrath545106f2002-11-11 06:21:00 +00001131 syntax_error(EMSG_UNEXP_TOKEN);
1132 tc = TC_NUMBER;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001133 debug_printf_parse("%s: token found:%f TC_NUMBER\n", __func__, t_double);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001134 } else {
1135 /* search for something known */
1136 tl = tokenlist;
1137 tc = 0x00000001;
1138 ti = tokeninfo;
1139 while (*tl) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001140 int l = (unsigned char) *tl++;
1141 if (l == (unsigned char) NTCC) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001142 tc <<= 1;
1143 continue;
1144 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001145 /* if token class is expected,
1146 * token matches,
1147 * and it's not a longer word,
Glenn L McGrath545106f2002-11-11 06:21:00 +00001148 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001149 if ((tc & (expected | TC_WORD | TC_NEWLINE))
Denys Vlasenko28458c62010-10-05 16:49:03 +02001150 && strncmp(p, tl, l) == 0
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001151 && !((tc & TC_WORD) && isalnum_(p[l]))
1152 ) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001153 /* then this is what we are looking for */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001154 t_info = *ti;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001155 debug_printf_parse("%s: token found:'%.*s' t_info:%x\n", __func__, l, p, t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001156 p += l;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001157 goto token_found;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001158 }
1159 ti++;
1160 tl += l;
1161 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001162 /* not a known token */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001163
Denys Vlasenko28458c62010-10-05 16:49:03 +02001164 /* is it a name? (var/array/function) */
1165 if (!isalnum_(*p))
1166 syntax_error(EMSG_UNEXP_TOKEN); /* no */
1167 /* yes */
1168 t_string = --p;
1169 while (isalnum_(*++p)) {
1170 p[-1] = *p;
1171 }
1172 p[-1] = '\0';
1173 tc = TC_VARIABLE;
1174 /* also consume whitespace between functionname and bracket */
1175 if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY))
1176 p = skip_spaces(p);
1177 if (*p == '(') {
1178 tc = TC_FUNCTION;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001179 debug_printf_parse("%s: token found:'%s' TC_FUNCTION\n", __func__, t_string);
Denys Vlasenko28458c62010-10-05 16:49:03 +02001180 } else {
1181 if (*p == '[') {
1182 p++;
1183 tc = TC_ARRAY;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001184 debug_printf_parse("%s: token found:'%s' TC_ARRAY\n", __func__, t_string);
1185 } else
1186 debug_printf_parse("%s: token found:'%s' TC_VARIABLE\n", __func__, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001187 }
1188 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001189 token_found:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001190 g_pos = p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001191
1192 /* skipping newlines in some cases */
1193 if ((ltclass & TC_NOTERM) && (tc & TC_NEWLINE))
1194 goto readnext;
1195
1196 /* insert concatenation operator when needed */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001197 if ((ltclass & TC_CONCAT1) && (tc & TC_CONCAT2) && (expected & TC_BINOP)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001198 concat_inserted = TRUE;
1199 save_tclass = tc;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001200 save_info = t_info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001201 tc = TC_BINOP;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001202 t_info = OC_CONCAT | SS | P(35);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001203 }
1204
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001205 t_tclass = tc;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001206 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001207 ltclass = t_tclass;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001208
1209 /* Are we ready for this? */
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001210 if (!(ltclass & expected)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001211 syntax_error((ltclass & (TC_NEWLINE | TC_EOF)) ?
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001212 EMSG_UNEXP_EOS : EMSG_UNEXP_TOKEN);
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001213 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001214
1215 return ltclass;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001216#undef concat_inserted
1217#undef save_tclass
1218#undef save_info
1219#undef ltclass
Glenn L McGrath545106f2002-11-11 06:21:00 +00001220}
1221
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001222static void rollback_token(void)
1223{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001224 t_rollback = TRUE;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001225}
Glenn L McGrath545106f2002-11-11 06:21:00 +00001226
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001227static node *new_node(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001228{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001229 node *n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001230
Denis Vlasenko4cccc032006-12-22 18:37:07 +00001231 n = xzalloc(sizeof(node));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001232 n->info = info;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001233 n->lineno = g_lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001234 return n;
1235}
1236
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001237static void mk_re_node(const char *s, node *n, regex_t *re)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001238{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001239 n->info = OC_REGEXP;
1240 n->l.re = re;
1241 n->r.ire = re + 1;
1242 xregcomp(re, s, REG_EXTENDED);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001243 xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001244}
1245
Mike Frysinger10a11e22005-09-27 02:23:02 +00001246static node *condition(void)
1247{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001248 next_token(TC_SEQSTART);
1249 return parse_expr(TC_SEQTERM);
1250}
1251
1252/* parse expression terminated by given argument, return ptr
1253 * to built subtree. Terminator is eaten by parse_expr */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001254static node *parse_expr(uint32_t iexp)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001255{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001256 node sn;
1257 node *cn = &sn;
1258 node *vn, *glptr;
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001259 uint32_t tc, xtc;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001260 var *v;
1261
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001262 debug_printf_parse("%s(%x)\n", __func__, iexp);
1263
Glenn L McGrath545106f2002-11-11 06:21:00 +00001264 sn.info = PRIMASK;
1265 sn.r.n = glptr = NULL;
1266 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp;
1267
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001268 while (!((tc = next_token(xtc)) & iexp)) {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001269
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001270 if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001271 /* input redirection (<) attached to glptr node */
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001272 debug_printf_parse("%s: input redir\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001273 cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37));
Glenn L McGrath4bded582004-02-22 11:55:09 +00001274 cn->a.n = glptr;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001275 xtc = TC_OPERAND | TC_UOPPRE;
1276 glptr = NULL;
1277
1278 } else if (tc & (TC_BINOP | TC_UOPPOST)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001279 debug_printf_parse("%s: TC_BINOP | TC_UOPPOST\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001280 /* for binary and postfix-unary operators, jump back over
1281 * previous operators with higher priority */
1282 vn = cn;
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001283 while (((t_info & PRIMASK) > (vn->a.n->info & PRIMASK2))
1284 || ((t_info == vn->info) && ((t_info & OPCLSMASK) == OC_COLON))
1285 ) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001286 vn = vn->a.n;
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001287 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001288 if ((t_info & OPCLSMASK) == OC_TERNARY)
1289 t_info += P(6);
1290 cn = vn->a.n->r.n = new_node(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001291 cn->a.n = vn->a.n;
1292 if (tc & TC_BINOP) {
1293 cn->l.n = vn;
1294 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001295 if ((t_info & OPCLSMASK) == OC_PGETLINE) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001296 /* it's a pipe */
1297 next_token(TC_GETLINE);
1298 /* give maximum priority to this pipe */
1299 cn->info &= ~PRIMASK;
1300 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1301 }
1302 } else {
1303 cn->r.n = vn;
1304 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1305 }
1306 vn->a.n = cn;
1307
1308 } else {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001309 debug_printf_parse("%s: other\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001310 /* for operands and prefix-unary operators, attach them
1311 * to last node */
1312 vn = cn;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001313 cn = vn->r.n = new_node(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001314 cn->a.n = vn;
1315 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
1316 if (tc & (TC_OPERAND | TC_REGEXP)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001317 debug_printf_parse("%s: TC_OPERAND | TC_REGEXP\n", __func__);
Rob Landleyed830e82005-06-07 02:43:52 +00001318 xtc = TC_UOPPRE | TC_UOPPOST | TC_BINOP | TC_OPERAND | iexp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001319 /* one should be very careful with switch on tclass -
Glenn L McGrath545106f2002-11-11 06:21:00 +00001320 * only simple tclasses should be used! */
1321 switch (tc) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00001322 case TC_VARIABLE:
1323 case TC_ARRAY:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001324 debug_printf_parse("%s: TC_VARIABLE | TC_ARRAY\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001325 cn->info = OC_VAR;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001326 v = hash_search(ahash, t_string);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001327 if (v != NULL) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001328 cn->info = OC_FNARG;
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001329 cn->l.aidx = v->x.aidx;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001330 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001331 cn->l.v = newvar(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001332 }
1333 if (tc & TC_ARRAY) {
1334 cn->info |= xS;
1335 cn->r.n = parse_expr(TC_ARRTERM);
1336 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001337 break;
Mike Frysingerde2b9382005-09-27 03:18:00 +00001338
Denis Vlasenkof782f522007-01-01 23:51:30 +00001339 case TC_NUMBER:
1340 case TC_STRING:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001341 debug_printf_parse("%s: TC_NUMBER | TC_STRING\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001342 cn->info = OC_VAR;
Rob Landley9ffd4232006-05-21 18:30:35 +00001343 v = cn->l.v = xzalloc(sizeof(var));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001344 if (tc & TC_NUMBER)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001345 setvar_i(v, t_double);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001346 else
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001347 setvar_s(v, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001348 break;
1349
Denis Vlasenkof782f522007-01-01 23:51:30 +00001350 case TC_REGEXP:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001351 debug_printf_parse("%s: TC_REGEXP\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001352 mk_re_node(t_string, cn, xzalloc(sizeof(regex_t)*2));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001353 break;
1354
Denis Vlasenkof782f522007-01-01 23:51:30 +00001355 case TC_FUNCTION:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001356 debug_printf_parse("%s: TC_FUNCTION\n", __func__);
Mike Frysingerde2b9382005-09-27 03:18:00 +00001357 cn->info = OC_FUNC;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001358 cn->r.f = newfunc(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001359 cn->l.n = condition();
1360 break;
1361
Denis Vlasenkof782f522007-01-01 23:51:30 +00001362 case TC_SEQSTART:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001363 debug_printf_parse("%s: TC_SEQSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001364 cn = vn->r.n = parse_expr(TC_SEQTERM);
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001365 if (!cn)
1366 syntax_error("Empty sequence");
Glenn L McGrath545106f2002-11-11 06:21:00 +00001367 cn->a.n = vn;
1368 break;
1369
Denis Vlasenkof782f522007-01-01 23:51:30 +00001370 case TC_GETLINE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001371 debug_printf_parse("%s: TC_GETLINE\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001372 glptr = cn;
1373 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1374 break;
1375
Denis Vlasenkof782f522007-01-01 23:51:30 +00001376 case TC_BUILTIN:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001377 debug_printf_parse("%s: TC_BUILTIN\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001378 cn->l.n = condition();
1379 break;
Denys Vlasenko28b00ce2015-10-02 02:41:39 +02001380
1381 case TC_LENGTH:
1382 debug_printf_parse("%s: TC_LENGTH\n", __func__);
1383 next_token(TC_SEQSTART | TC_OPTERM | TC_GRPTERM);
1384 rollback_token();
1385 if (t_tclass & TC_SEQSTART) {
1386 /* It was a "(" token. Handle just like TC_BUILTIN */
1387 cn->l.n = condition();
1388 }
1389 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001390 }
1391 }
1392 }
1393 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001394
1395 debug_printf_parse("%s() returns %p\n", __func__, sn.r.n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001396 return sn.r.n;
1397}
1398
1399/* add node to chain. Return ptr to alloc'd node */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001400static node *chain_node(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001401{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001402 node *n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001403
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001404 if (!seq->first)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001405 seq->first = seq->last = new_node(0);
1406
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001407 if (seq->programname != g_progname) {
1408 seq->programname = g_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001409 n = chain_node(OC_NEWSOURCE);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001410 n->l.new_progname = xstrdup(g_progname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001411 }
1412
1413 n = seq->last;
1414 n->info = info;
1415 seq->last = n->a.n = new_node(OC_DONE);
1416
1417 return n;
1418}
1419
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001420static void chain_expr(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001421{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001422 node *n;
1423
1424 n = chain_node(info);
1425 n->l.n = parse_expr(TC_OPTERM | TC_GRPTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001426 if (t_tclass & TC_GRPTERM)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001427 rollback_token();
1428}
1429
Mike Frysinger10a11e22005-09-27 02:23:02 +00001430static node *chain_loop(node *nn)
1431{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001432 node *n, *n2, *save_brk, *save_cont;
1433
1434 save_brk = break_ptr;
1435 save_cont = continue_ptr;
1436
1437 n = chain_node(OC_BR | Vx);
1438 continue_ptr = new_node(OC_EXEC);
1439 break_ptr = new_node(OC_EXEC);
1440 chain_group();
1441 n2 = chain_node(OC_EXEC | Vx);
1442 n2->l.n = nn;
1443 n2->a.n = n;
1444 continue_ptr->a.n = n2;
1445 break_ptr->a.n = n->r.n = seq->last;
1446
1447 continue_ptr = save_cont;
1448 break_ptr = save_brk;
1449
1450 return n;
1451}
1452
1453/* parse group and attach it to chain */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001454static void chain_group(void)
1455{
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001456 uint32_t c;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001457 node *n, *n2, *n3;
1458
1459 do {
1460 c = next_token(TC_GRPSEQ);
1461 } while (c & TC_NEWLINE);
1462
1463 if (c & TC_GRPSTART) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001464 debug_printf_parse("%s: TC_GRPSTART\n", __func__);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001465 while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001466 debug_printf_parse("%s: !TC_GRPTERM\n", __func__);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001467 if (t_tclass & TC_NEWLINE)
1468 continue;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001469 rollback_token();
1470 chain_group();
1471 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001472 debug_printf_parse("%s: TC_GRPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001473 } else if (c & (TC_OPSEQ | TC_OPTERM)) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001474 debug_printf_parse("%s: TC_OPSEQ | TC_OPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001475 rollback_token();
1476 chain_expr(OC_EXEC | Vx);
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001477 } else {
1478 /* TC_STATEMNT */
1479 debug_printf_parse("%s: TC_STATEMNT(?)\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001480 switch (t_info & OPCLSMASK) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001481 case ST_IF:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001482 debug_printf_parse("%s: ST_IF\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001483 n = chain_node(OC_BR | Vx);
1484 n->l.n = condition();
1485 chain_group();
1486 n2 = chain_node(OC_EXEC);
1487 n->r.n = seq->last;
1488 if (next_token(TC_GRPSEQ | TC_GRPTERM | TC_ELSE) == TC_ELSE) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001489 chain_group();
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001490 n2->a.n = seq->last;
1491 } else {
1492 rollback_token();
1493 }
1494 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001495
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001496 case ST_WHILE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001497 debug_printf_parse("%s: ST_WHILE\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001498 n2 = condition();
1499 n = chain_loop(NULL);
1500 n->l.n = n2;
1501 break;
1502
1503 case ST_DO:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001504 debug_printf_parse("%s: ST_DO\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001505 n2 = chain_node(OC_EXEC);
1506 n = chain_loop(NULL);
1507 n2->a.n = n->a.n;
1508 next_token(TC_WHILE);
1509 n->l.n = condition();
1510 break;
1511
1512 case ST_FOR:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001513 debug_printf_parse("%s: ST_FOR\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001514 next_token(TC_SEQSTART);
1515 n2 = parse_expr(TC_SEMICOL | TC_SEQTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001516 if (t_tclass & TC_SEQTERM) { /* for-in */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001517 if ((n2->info & OPCLSMASK) != OC_IN)
1518 syntax_error(EMSG_UNEXP_TOKEN);
1519 n = chain_node(OC_WALKINIT | VV);
1520 n->l.n = n2->l.n;
1521 n->r.n = n2->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001522 n = chain_loop(NULL);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001523 n->info = OC_WALKNEXT | Vx;
1524 n->l.n = n2->l.n;
1525 } else { /* for (;;) */
1526 n = chain_node(OC_EXEC | Vx);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001527 n->l.n = n2;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001528 n2 = parse_expr(TC_SEMICOL);
1529 n3 = parse_expr(TC_SEQTERM);
1530 n = chain_loop(n3);
1531 n->l.n = n2;
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001532 if (!n2)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001533 n->info = OC_EXEC;
1534 }
1535 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001536
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001537 case OC_PRINT:
1538 case OC_PRINTF:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001539 debug_printf_parse("%s: OC_PRINT[F]\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001540 n = chain_node(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001541 n->l.n = parse_expr(TC_OPTERM | TC_OUTRDR | TC_GRPTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001542 if (t_tclass & TC_OUTRDR) {
1543 n->info |= t_info;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001544 n->r.n = parse_expr(TC_OPTERM | TC_GRPTERM);
1545 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001546 if (t_tclass & TC_GRPTERM)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001547 rollback_token();
1548 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001549
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001550 case OC_BREAK:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001551 debug_printf_parse("%s: OC_BREAK\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001552 n = chain_node(OC_EXEC);
1553 n->a.n = break_ptr;
Denys Vlasenko5f8daef2014-06-26 16:40:28 +02001554 chain_expr(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001555 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001556
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001557 case OC_CONTINUE:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001558 debug_printf_parse("%s: OC_CONTINUE\n", __func__);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001559 n = chain_node(OC_EXEC);
1560 n->a.n = continue_ptr;
Denys Vlasenko5f8daef2014-06-26 16:40:28 +02001561 chain_expr(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001562 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001563
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001564 /* delete, next, nextfile, return, exit */
1565 default:
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001566 debug_printf_parse("%s: default\n", __func__);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001567 chain_expr(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001568 }
1569 }
1570}
1571
Mike Frysinger10a11e22005-09-27 02:23:02 +00001572static void parse_program(char *p)
1573{
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001574 uint32_t tclass;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001575 node *cn;
1576 func *f;
1577 var *v;
1578
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001579 g_pos = p;
1580 t_lineno = 1;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001581 while ((tclass = next_token(TC_EOF | TC_OPSEQ | TC_GRPSTART |
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001582 TC_OPTERM | TC_BEGIN | TC_END | TC_FUNCDECL)) != TC_EOF) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001583
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001584 if (tclass & TC_OPTERM) {
1585 debug_printf_parse("%s: TC_OPTERM\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001586 continue;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001587 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001588
1589 seq = &mainseq;
1590 if (tclass & TC_BEGIN) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001591 debug_printf_parse("%s: TC_BEGIN\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001592 seq = &beginseq;
1593 chain_group();
Glenn L McGrath545106f2002-11-11 06:21:00 +00001594 } else if (tclass & TC_END) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001595 debug_printf_parse("%s: TC_END\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001596 seq = &endseq;
1597 chain_group();
Glenn L McGrath545106f2002-11-11 06:21:00 +00001598 } else if (tclass & TC_FUNCDECL) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001599 debug_printf_parse("%s: TC_FUNCDECL\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001600 next_token(TC_FUNCTION);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001601 g_pos++;
1602 f = newfunc(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001603 f->body.first = NULL;
1604 f->nargs = 0;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001605 while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001606 v = findvar(ahash, t_string);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001607 v->x.aidx = f->nargs++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001608
1609 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
1610 break;
1611 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001612 seq = &f->body;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001613 chain_group();
1614 clear_array(ahash);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001615 } else if (tclass & TC_OPSEQ) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001616 debug_printf_parse("%s: TC_OPSEQ\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001617 rollback_token();
1618 cn = chain_node(OC_TEST);
1619 cn->l.n = parse_expr(TC_OPTERM | TC_EOF | TC_GRPSTART);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001620 if (t_tclass & TC_GRPSTART) {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001621 debug_printf_parse("%s: TC_GRPSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001622 rollback_token();
1623 chain_group();
1624 } else {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001625 debug_printf_parse("%s: !TC_GRPSTART\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001626 chain_node(OC_PRINT);
1627 }
1628 cn->r.n = mainseq.last;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001629 } else /* if (tclass & TC_GRPSTART) */ {
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001630 debug_printf_parse("%s: TC_GRPSTART(?)\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001631 rollback_token();
1632 chain_group();
1633 }
1634 }
Denys Vlasenko7b46d112011-09-11 00:30:56 +02001635 debug_printf_parse("%s: TC_EOF\n", __func__);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001636}
1637
1638
1639/* -------- program execution part -------- */
1640
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001641static node *mk_splitter(const char *s, tsplitter *spl)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001642{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001643 regex_t *re, *ire;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001644 node *n;
1645
1646 re = &spl->re[0];
1647 ire = &spl->re[1];
1648 n = &spl->n;
Denis Vlasenko890ac9d2006-10-07 15:16:19 +00001649 if ((n->info & OPCLSMASK) == OC_REGEXP) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001650 regfree(re);
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001651 regfree(ire); // TODO: nuke ire, use re+1?
Glenn L McGrath545106f2002-11-11 06:21:00 +00001652 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001653 if (s[0] && s[1]) { /* strlen(s) > 1 */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001654 mk_re_node(s, n, re);
1655 } else {
Denys Vlasenko28458c62010-10-05 16:49:03 +02001656 n->info = (uint32_t) s[0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00001657 }
1658
1659 return n;
1660}
1661
1662/* use node as a regular expression. Supplied with node ptr and regex_t
Eric Andersenaff114c2004-04-14 17:51:38 +00001663 * storage space. Return ptr to regex (if result points to preg, it should
Glenn L McGrath545106f2002-11-11 06:21:00 +00001664 * be later regfree'd manually
1665 */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001666static regex_t *as_regex(node *op, regex_t *preg)
1667{
Denis Vlasenko7a676642009-03-15 22:20:31 +00001668 int cflags;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001669 var *v;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001670 const char *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001671
1672 if ((op->info & OPCLSMASK) == OC_REGEXP) {
1673 return icase ? op->r.ire : op->l.re;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001674 }
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001675 v = nvalloc(1);
1676 s = getvar_s(evaluate(op, v));
Denis Vlasenko7a676642009-03-15 22:20:31 +00001677
1678 cflags = icase ? REG_EXTENDED | REG_ICASE : REG_EXTENDED;
1679 /* Testcase where REG_EXTENDED fails (unpaired '{'):
1680 * echo Hi | awk 'gsub("@(samp|code|file)\{","");'
1681 * gawk 3.1.5 eats this. We revert to ~REG_EXTENDED
1682 * (maybe gsub is not supposed to use REG_EXTENDED?).
1683 */
1684 if (regcomp(preg, s, cflags)) {
1685 cflags &= ~REG_EXTENDED;
1686 xregcomp(preg, s, cflags);
1687 }
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001688 nvfree(v);
1689 return preg;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001690}
1691
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001692/* gradually increasing buffer.
1693 * note that we reallocate even if n == old_size,
1694 * and thus there is at least one extra allocated byte.
1695 */
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001696static char* qrealloc(char *b, int n, int *size)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001697{
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001698 if (!b || n >= *size) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001699 *size = n + (n>>1) + 80;
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001700 b = xrealloc(b, *size);
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001701 }
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001702 return b;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001703}
1704
1705/* resize field storage space */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001706static void fsrealloc(int size)
1707{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001708 int i;
1709
1710 if (size >= maxfields) {
1711 i = maxfields;
1712 maxfields = size + 16;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001713 Fields = xrealloc(Fields, maxfields * sizeof(Fields[0]));
Denis Vlasenkof782f522007-01-01 23:51:30 +00001714 for (; i < maxfields; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001715 Fields[i].type = VF_SPECIAL;
1716 Fields[i].string = NULL;
1717 }
1718 }
Denys Vlasenko28458c62010-10-05 16:49:03 +02001719 /* if size < nfields, clear extra field variables */
1720 for (i = size; i < nfields; i++) {
1721 clrvar(Fields + i);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001722 }
1723 nfields = size;
1724}
1725
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001726static int awk_split(const char *s, node *spl, char **slist)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001727{
Denys Vlasenko28458c62010-10-05 16:49:03 +02001728 int l, n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001729 char c[4];
1730 char *s1;
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001731 regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough...
Glenn L McGrath545106f2002-11-11 06:21:00 +00001732
1733 /* in worst case, each char would be a separate field */
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001734 *slist = s1 = xzalloc(strlen(s) * 2 + 3);
1735 strcpy(s1, s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001736
1737 c[0] = c[1] = (char)spl->info;
1738 c[2] = c[3] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001739 if (*getvar_s(intvar[RS]) == '\0')
1740 c[2] = '\n';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001741
Denys Vlasenko28458c62010-10-05 16:49:03 +02001742 n = 0;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001743 if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
1744 if (!*s)
1745 return n; /* "": zero fields */
1746 n++; /* at least one field will be there */
1747 do {
1748 l = strcspn(s, c+2); /* len till next NUL or \n */
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001749 if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0
1750 && pmatch[0].rm_so <= l
1751 ) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001752 l = pmatch[0].rm_so;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001753 if (pmatch[0].rm_eo == 0) {
1754 l++;
1755 pmatch[0].rm_eo++;
1756 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001757 n++; /* we saw yet another delimiter */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001758 } else {
1759 pmatch[0].rm_eo = l;
Denys Vlasenkoe4244232009-05-18 23:50:03 +02001760 if (s[l])
1761 pmatch[0].rm_eo++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001762 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001763 memcpy(s1, s, l);
Denis Vlasenko67b5eeb2009-04-12 13:54:13 +00001764 /* make sure we remove *all* of the separator chars */
Denys Vlasenkoe4244232009-05-18 23:50:03 +02001765 do {
1766 s1[l] = '\0';
1767 } while (++l < pmatch[0].rm_eo);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001768 nextword(&s1);
1769 s += pmatch[0].rm_eo;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001770 } while (*s);
1771 return n;
1772 }
1773 if (c[0] == '\0') { /* null split */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001774 while (*s) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001775 *s1++ = *s++;
1776 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001777 n++;
1778 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001779 return n;
1780 }
1781 if (c[0] != ' ') { /* single-character split */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001782 if (icase) {
1783 c[0] = toupper(c[0]);
1784 c[1] = tolower(c[1]);
1785 }
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001786 if (*s1)
1787 n++;
Denys Vlasenko28458c62010-10-05 16:49:03 +02001788 while ((s1 = strpbrk(s1, c)) != NULL) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001789 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001790 n++;
1791 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001792 return n;
1793 }
1794 /* space split */
1795 while (*s) {
1796 s = skip_whitespace(s);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001797 if (!*s)
1798 break;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001799 n++;
1800 while (*s && !isspace(*s))
1801 *s1++ = *s++;
1802 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001803 }
1804 return n;
1805}
1806
Mike Frysinger10a11e22005-09-27 02:23:02 +00001807static void split_f0(void)
1808{
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001809/* static char *fstrings; */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001810#define fstrings (G.split_f0__fstrings)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001811
Glenn L McGrath545106f2002-11-11 06:21:00 +00001812 int i, n;
1813 char *s;
1814
1815 if (is_f0_split)
1816 return;
1817
1818 is_f0_split = TRUE;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001819 free(fstrings);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001820 fsrealloc(0);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001821 n = awk_split(getvar_s(intvar[F0]), &fsplitter.n, &fstrings);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001822 fsrealloc(n);
1823 s = fstrings;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001824 for (i = 0; i < n; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001825 Fields[i].string = nextword(&s);
1826 Fields[i].type |= (VF_FSTR | VF_USER | VF_DIRTY);
1827 }
1828
1829 /* set NF manually to avoid side effects */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001830 clrvar(intvar[NF]);
1831 intvar[NF]->type = VF_NUMBER | VF_SPECIAL;
1832 intvar[NF]->number = nfields;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001833#undef fstrings
Glenn L McGrath545106f2002-11-11 06:21:00 +00001834}
1835
1836/* perform additional actions when some internal variables changed */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001837static void handle_special(var *v)
1838{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001839 int n;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001840 char *b;
1841 const char *sep, *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001842 int sl, l, len, i, bsize;
1843
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001844 if (!(v->type & VF_SPECIAL))
Glenn L McGrath545106f2002-11-11 06:21:00 +00001845 return;
1846
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001847 if (v == intvar[NF]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001848 n = (int)getvar_i(v);
1849 fsrealloc(n);
1850
1851 /* recalculate $0 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001852 sep = getvar_s(intvar[OFS]);
Rob Landleya3896512006-05-07 20:20:34 +00001853 sl = strlen(sep);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001854 b = NULL;
1855 len = 0;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001856 for (i = 0; i < n; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001857 s = getvar_s(&Fields[i]);
Rob Landleya3896512006-05-07 20:20:34 +00001858 l = strlen(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001859 if (b) {
1860 memcpy(b+len, sep, sl);
1861 len += sl;
1862 }
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001863 b = qrealloc(b, len+l+sl, &bsize);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001864 memcpy(b+len, s, l);
1865 len += l;
1866 }
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001867 if (b)
1868 b[len] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001869 setvar_p(intvar[F0], b);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001870 is_f0_split = TRUE;
1871
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001872 } else if (v == intvar[F0]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001873 is_f0_split = FALSE;
1874
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001875 } else if (v == intvar[FS]) {
Denys Vlasenkodf8066a2012-07-11 01:27:15 +02001876 /*
1877 * The POSIX-2008 standard says that changing FS should have no effect on the
1878 * current input line, but only on the next one. The language is:
1879 *
1880 * > Before the first reference to a field in the record is evaluated, the record
1881 * > shall be split into fields, according to the rules in Regular Expressions,
1882 * > using the value of FS that was current at the time the record was read.
1883 *
1884 * So, split up current line before assignment to FS:
1885 */
1886 split_f0();
1887
Glenn L McGrath545106f2002-11-11 06:21:00 +00001888 mk_splitter(getvar_s(v), &fsplitter);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001889 } else if (v == intvar[RS]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001890 mk_splitter(getvar_s(v), &rsplitter);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001891 } else if (v == intvar[IGNORECASE]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001892 icase = istrue(v);
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001893 } else { /* $n */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001894 n = getvar_i(intvar[NF]);
1895 setvar_i(intvar[NF], n > v-Fields ? n : v-Fields+1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001896 /* right here v is invalid. Just to note... */
1897 }
1898}
1899
1900/* step through func/builtin/etc arguments */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001901static node *nextarg(node **pn)
1902{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001903 node *n;
1904
1905 n = *pn;
1906 if (n && (n->info & OPCLSMASK) == OC_COMMA) {
1907 *pn = n->r.n;
1908 n = n->l.n;
1909 } else {
1910 *pn = NULL;
1911 }
1912 return n;
1913}
1914
Mike Frysinger10a11e22005-09-27 02:23:02 +00001915static void hashwalk_init(var *v, xhash *array)
1916{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001917 hash_item *hi;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00001918 unsigned i;
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001919 walker_list *w;
1920 walker_list *prev_walker;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001921
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001922 if (v->type & VF_WALK) {
1923 prev_walker = v->x.walker;
1924 } else {
1925 v->type |= VF_WALK;
1926 prev_walker = NULL;
1927 }
1928 debug_printf_walker("hashwalk_init: prev_walker:%p\n", prev_walker);
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001929
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001930 w = v->x.walker = xzalloc(sizeof(*w) + array->glen + 1); /* why + 1? */
1931 debug_printf_walker(" walker@%p=%p\n", &v->x.walker, w);
1932 w->cur = w->end = w->wbuf;
1933 w->prev = prev_walker;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001934 for (i = 0; i < array->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001935 hi = array->items[i];
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001936 while (hi) {
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001937 strcpy(w->end, hi->name);
1938 nextword(&w->end);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001939 hi = hi->next;
1940 }
1941 }
1942}
1943
Mike Frysinger10a11e22005-09-27 02:23:02 +00001944static int hashwalk_next(var *v)
1945{
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001946 walker_list *w = v->x.walker;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001947
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001948 if (w->cur >= w->end) {
1949 walker_list *prev_walker = w->prev;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001950
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001951 debug_printf_walker("end of iteration, free(walker@%p:%p), prev_walker:%p\n", &v->x.walker, w, prev_walker);
1952 free(w);
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001953 v->x.walker = prev_walker;
1954 return FALSE;
1955 }
1956
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001957 setvar_s(v, nextword(&w->cur));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001958 return TRUE;
1959}
1960
1961/* evaluate node, return 1 when result is true, 0 otherwise */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001962static int ptest(node *pattern)
1963{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001964 /* ptest__v is "static": to save stack space? */
1965 return istrue(evaluate(pattern, &G.ptest__v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001966}
1967
1968/* read next record from stream rsm into a variable v */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001969static int awk_getline(rstream *rsm, var *v)
1970{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001971 char *b;
1972 regmatch_t pmatch[2];
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001973 int size, a, p, pp = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001974 int fd, so, eo, r, rp;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001975 char c, *m, *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001976
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02001977 debug_printf_eval("entered %s()\n", __func__);
1978
Glenn L McGrath545106f2002-11-11 06:21:00 +00001979 /* we're using our own buffer since we need access to accumulating
1980 * characters
1981 */
1982 fd = fileno(rsm->F);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001983 m = rsm->buffer;
1984 a = rsm->adv;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001985 p = rsm->pos;
1986 size = rsm->size;
1987 c = (char) rsplitter.n.info;
1988 rp = 0;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001989
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001990 if (!m)
1991 m = qrealloc(m, 256, &size);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001992
Glenn L McGrath545106f2002-11-11 06:21:00 +00001993 do {
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001994 b = m + a;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001995 so = eo = p;
1996 r = 1;
1997 if (p > 0) {
1998 if ((rsplitter.n.info & OPCLSMASK) == OC_REGEXP) {
1999 if (regexec(icase ? rsplitter.n.r.ire : rsplitter.n.l.re,
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002000 b, 1, pmatch, 0) == 0) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002001 so = pmatch[0].rm_so;
2002 eo = pmatch[0].rm_eo;
2003 if (b[eo] != '\0')
2004 break;
2005 }
2006 } else if (c != '\0') {
2007 s = strchr(b+pp, c);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002008 if (!s)
2009 s = memchr(b+pp, '\0', p - pp);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002010 if (s) {
2011 so = eo = s-b;
2012 eo++;
2013 break;
2014 }
2015 } else {
2016 while (b[rp] == '\n')
2017 rp++;
2018 s = strstr(b+rp, "\n\n");
2019 if (s) {
2020 so = eo = s-b;
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002021 while (b[eo] == '\n')
2022 eo++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002023 if (b[eo] != '\0')
2024 break;
2025 }
2026 }
2027 }
2028
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002029 if (a > 0) {
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002030 memmove(m, m+a, p+1);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002031 b = m;
2032 a = 0;
2033 }
2034
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002035 m = qrealloc(m, a+p+128, &size);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002036 b = m + a;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002037 pp = p;
2038 p += safe_read(fd, b+p, size-p-1);
2039 if (p < pp) {
2040 p = 0;
2041 r = 0;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002042 setvar_i(intvar[ERRNO], errno);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002043 }
2044 b[p] = '\0';
2045
2046 } while (p > pp);
2047
2048 if (p == 0) {
2049 r--;
2050 } else {
2051 c = b[so]; b[so] = '\0';
2052 setvar_s(v, b+rp);
2053 v->type |= VF_USER;
2054 b[so] = c;
2055 c = b[eo]; b[eo] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002056 setvar_s(intvar[RT], b+so);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002057 b[eo] = c;
2058 }
2059
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002060 rsm->buffer = m;
2061 rsm->adv = a + eo;
2062 rsm->pos = p - eo;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002063 rsm->size = size;
2064
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002065 debug_printf_eval("returning from %s(): %d\n", __func__, r);
2066
Glenn L McGrath545106f2002-11-11 06:21:00 +00002067 return r;
2068}
2069
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00002070static int fmt_num(char *b, int size, const char *format, double n, int int_as_int)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002071{
Denis Vlasenkof782f522007-01-01 23:51:30 +00002072 int r = 0;
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00002073 char c;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002074 const char *s = format;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002075
Denys Vlasenko1390a012013-07-20 21:23:01 +02002076 if (int_as_int && n == (long long)n) {
2077 r = snprintf(b, size, "%lld", (long long)n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002078 } else {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002079 do { c = *s; } while (c && *++s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002080 if (strchr("diouxX", c)) {
2081 r = snprintf(b, size, format, (int)n);
2082 } else if (strchr("eEfgG", c)) {
2083 r = snprintf(b, size, format, n);
2084 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002085 syntax_error(EMSG_INV_FMT);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002086 }
2087 }
2088 return r;
2089}
2090
Glenn L McGrath545106f2002-11-11 06:21:00 +00002091/* formatted output into an allocated buffer, return ptr to buffer */
Mike Frysinger10a11e22005-09-27 02:23:02 +00002092static char *awk_printf(node *n)
2093{
Glenn L McGrath545106f2002-11-11 06:21:00 +00002094 char *b = NULL;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002095 char *fmt, *s, *f;
2096 const char *s1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002097 int i, j, incr, bsize;
2098 char c, c1;
2099 var *v, *arg;
2100
2101 v = nvalloc(1);
Rob Landleyd921b2e2006-08-03 15:41:12 +00002102 fmt = f = xstrdup(getvar_s(evaluate(nextarg(&n), v)));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002103
2104 i = 0;
2105 while (*f) {
2106 s = f;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002107 while (*f && (*f != '%' || *++f == '%'))
Glenn L McGrath545106f2002-11-11 06:21:00 +00002108 f++;
Denis Vlasenko389f9d52007-05-09 21:57:23 +00002109 while (*f && !isalpha(*f)) {
2110 if (*f == '*')
2111 syntax_error("%*x formats are not supported");
Glenn L McGrath545106f2002-11-11 06:21:00 +00002112 f++;
Denis Vlasenko389f9d52007-05-09 21:57:23 +00002113 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002114
2115 incr = (f - s) + MAXVARFMT;
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002116 b = qrealloc(b, incr + i, &bsize);
Denis Vlasenkof782f522007-01-01 23:51:30 +00002117 c = *f;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002118 if (c != '\0')
2119 f++;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002120 c1 = *f;
2121 *f = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00002122 arg = evaluate(nextarg(&n), v);
2123
2124 j = i;
2125 if (c == 'c' || !c) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002126 i += sprintf(b+i, s, is_numeric(arg) ?
2127 (char)getvar_i(arg) : *getvar_s(arg));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002128 } else if (c == 's') {
Denis Vlasenko92758142006-10-03 19:56:34 +00002129 s1 = getvar_s(arg);
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01002130 b = qrealloc(b, incr+i+strlen(s1), &bsize);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002131 i += sprintf(b+i, s, s1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002132 } else {
2133 i += fmt_num(b+i, incr, s, getvar_i(arg), FALSE);
2134 }
2135 *f = c1;
2136
2137 /* if there was an error while sprintf, return value is negative */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002138 if (i < j)
2139 i = j;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002140 }
2141
Glenn L McGrath545106f2002-11-11 06:21:00 +00002142 free(fmt);
2143 nvfree(v);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002144 b = xrealloc(b, i + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002145 b[i] = '\0';
2146 return b;
2147}
2148
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002149/* Common substitution routine.
2150 * Replace (nm)'th substring of (src) that matches (rn) with (repl),
2151 * store result into (dest), return number of substitutions.
2152 * If nm = 0, replace all matches.
2153 * If src or dst is NULL, use $0.
2154 * If subexp != 0, enable subexpression matching (\1-\9).
Glenn L McGrath545106f2002-11-11 06:21:00 +00002155 */
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002156static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int subexp)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002157{
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002158 char *resbuf;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002159 const char *sp;
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002160 int match_no, residx, replen, resbufsize;
2161 int regexec_flags;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002162 regmatch_t pmatch[10];
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002163 regex_t sreg, *regex;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002164
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002165 resbuf = NULL;
2166 residx = 0;
2167 match_no = 0;
2168 regexec_flags = 0;
2169 regex = as_regex(rn, &sreg);
2170 sp = getvar_s(src ? src : intvar[F0]);
2171 replen = strlen(repl);
2172 while (regexec(regex, sp, 10, pmatch, regexec_flags) == 0) {
2173 int so = pmatch[0].rm_so;
2174 int eo = pmatch[0].rm_eo;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002175
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002176 //bb_error_msg("match %u: [%u,%u] '%s'%p", match_no+1, so, eo, sp,sp);
2177 resbuf = qrealloc(resbuf, residx + eo + replen, &resbufsize);
2178 memcpy(resbuf + residx, sp, eo);
2179 residx += eo;
2180 if (++match_no >= nm) {
2181 const char *s;
2182 int nbs;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002183
Glenn L McGrath545106f2002-11-11 06:21:00 +00002184 /* replace */
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002185 residx -= (eo - so);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002186 nbs = 0;
2187 for (s = repl; *s; s++) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002188 char c = resbuf[residx++] = *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002189 if (c == '\\') {
2190 nbs++;
2191 continue;
2192 }
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002193 if (c == '&' || (subexp && c >= '0' && c <= '9')) {
2194 int j;
2195 residx -= ((nbs + 3) >> 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002196 j = 0;
2197 if (c != '&') {
2198 j = c - '0';
2199 nbs++;
2200 }
2201 if (nbs % 2) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002202 resbuf[residx++] = c;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002203 } else {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002204 int n = pmatch[j].rm_eo - pmatch[j].rm_so;
2205 resbuf = qrealloc(resbuf, residx + replen + n, &resbufsize);
2206 memcpy(resbuf + residx, sp + pmatch[j].rm_so, n);
2207 residx += n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002208 }
2209 }
2210 nbs = 0;
2211 }
2212 }
2213
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002214 regexec_flags = REG_NOTBOL;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002215 sp += eo;
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002216 if (match_no == nm)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002217 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002218 if (eo == so) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002219 /* Empty match (e.g. "b*" will match anywhere).
2220 * Advance by one char. */
2221//BUG (bug 1333):
2222//gsub(/\<b*/,"") on "abc" will reach this point, advance to "bc"
2223//... and will erroneously match "b" even though it is NOT at the word start.
2224//we need REG_NOTBOW but it does not exist...
Denys Vlasenko7379cd12010-04-04 01:48:12 +02002225//TODO: if EXTRA_COMPAT=y, use GNU matching and re_search,
2226//it should be able to do it correctly.
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002227 /* Subtle: this is safe only because
2228 * qrealloc allocated at least one extra byte */
2229 resbuf[residx] = *sp;
2230 if (*sp == '\0')
2231 goto ret;
2232 sp++;
2233 residx++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002234 }
2235 }
2236
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002237 resbuf = qrealloc(resbuf, residx + strlen(sp), &resbufsize);
2238 strcpy(resbuf + residx, sp);
2239 ret:
2240 //bb_error_msg("end sp:'%s'%p", sp,sp);
2241 setvar_p(dest ? dest : intvar[F0], resbuf);
2242 if (regex == &sreg)
2243 regfree(regex);
2244 return match_no;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002245}
2246
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002247static NOINLINE int do_mktime(const char *ds)
2248{
2249 struct tm then;
2250 int count;
2251
2252 /*memset(&then, 0, sizeof(then)); - not needed */
2253 then.tm_isdst = -1; /* default is unknown */
2254
2255 /* manpage of mktime says these fields are ints,
2256 * so we can sscanf stuff directly into them */
2257 count = sscanf(ds, "%u %u %u %u %u %u %d",
2258 &then.tm_year, &then.tm_mon, &then.tm_mday,
2259 &then.tm_hour, &then.tm_min, &then.tm_sec,
2260 &then.tm_isdst);
2261
2262 if (count < 6
2263 || (unsigned)then.tm_mon < 1
2264 || (unsigned)then.tm_year < 1900
2265 ) {
2266 return -1;
2267 }
2268
2269 then.tm_mon -= 1;
Denys Vlasenkobc3e9472009-09-21 04:16:00 +02002270 then.tm_year -= 1900;
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002271
2272 return mktime(&then);
2273}
2274
2275static NOINLINE var *exec_builtin(node *op, var *res)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002276{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002277#define tspl (G.exec_builtin__tspl)
2278
Glenn L McGrath545106f2002-11-11 06:21:00 +00002279 var *tv;
2280 node *an[4];
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002281 var *av[4];
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002282 const char *as[4];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002283 regmatch_t pmatch[2];
2284 regex_t sreg, *re;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002285 node *spl;
Mike Frysingerf87b3e32005-09-27 04:16:22 +00002286 uint32_t isr, info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002287 int nargs;
2288 time_t tt;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002289 int i, l, ll, n;
2290
2291 tv = nvalloc(4);
2292 isr = info = op->info;
2293 op = op->l.n;
2294
2295 av[2] = av[3] = NULL;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002296 for (i = 0; i < 4 && op; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002297 an[i] = nextarg(&op);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002298 if (isr & 0x09000000)
2299 av[i] = evaluate(an[i], &tv[i]);
2300 if (isr & 0x08000000)
2301 as[i] = getvar_s(av[i]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002302 isr >>= 1;
2303 }
2304
2305 nargs = i;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00002306 if ((uint32_t)nargs < (info >> 30))
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002307 syntax_error(EMSG_TOO_FEW_ARGS);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002308
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02002309 info &= OPNMASK;
2310 switch (info) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002311
Denis Vlasenkof782f522007-01-01 23:51:30 +00002312 case B_a2:
Rob Landleyd8205b32010-10-24 03:27:22 +02002313 if (ENABLE_FEATURE_AWK_LIBM)
2314 setvar_i(res, atan2(getvar_i(av[0]), getvar_i(av[1])));
2315 else
2316 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002317 break;
2318
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002319 case B_sp: {
2320 char *s, *s1;
2321
Glenn L McGrath545106f2002-11-11 06:21:00 +00002322 if (nargs > 2) {
2323 spl = (an[2]->info & OPCLSMASK) == OC_REGEXP ?
2324 an[2] : mk_splitter(getvar_s(evaluate(an[2], &tv[2])), &tspl);
2325 } else {
2326 spl = &fsplitter.n;
2327 }
2328
2329 n = awk_split(as[0], spl, &s);
2330 s1 = s;
2331 clear_array(iamarray(av[1]));
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002332 for (i = 1; i <= n; i++)
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002333 setari_u(av[1], i, nextword(&s));
2334 free(s1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002335 setvar_i(res, n);
2336 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002337 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002338
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002339 case B_ss: {
2340 char *s;
2341
Rob Landleya3896512006-05-07 20:20:34 +00002342 l = strlen(as[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002343 i = getvar_i(av[1]) - 1;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002344 if (i > l)
2345 i = l;
2346 if (i < 0)
2347 i = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002348 n = (nargs > 2) ? getvar_i(av[2]) : l-i;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002349 if (n < 0)
2350 n = 0;
Denis Vlasenko8ae5b282008-07-02 22:47:49 +00002351 s = xstrndup(as[0]+i, n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002352 setvar_p(res, s);
2353 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002354 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +00002355
Denis Vlasenko7cbcd1c2008-08-28 23:16:58 +00002356 /* Bitwise ops must assume that operands are unsigned. GNU Awk 3.1.5:
2357 * awk '{ print or(-1,1) }' gives "4.29497e+09", not "-2.xxxe+09" */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002358 case B_an:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002359 setvar_i(res, getvar_i_int(av[0]) & getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002360 break;
Denis Vlasenkof7996f32007-01-11 17:20:00 +00002361
Denis Vlasenkof782f522007-01-01 23:51:30 +00002362 case B_co:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002363 setvar_i(res, ~getvar_i_int(av[0]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002364 break;
2365
Denis Vlasenkof782f522007-01-01 23:51:30 +00002366 case B_ls:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002367 setvar_i(res, getvar_i_int(av[0]) << getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002368 break;
2369
Denis Vlasenkof782f522007-01-01 23:51:30 +00002370 case B_or:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002371 setvar_i(res, getvar_i_int(av[0]) | getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002372 break;
2373
Denis Vlasenkof782f522007-01-01 23:51:30 +00002374 case B_rs:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002375 setvar_i(res, getvar_i_int(av[0]) >> getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002376 break;
2377
Denis Vlasenkof782f522007-01-01 23:51:30 +00002378 case B_xo:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002379 setvar_i(res, getvar_i_int(av[0]) ^ getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002380 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002381
Denis Vlasenkof782f522007-01-01 23:51:30 +00002382 case B_lo:
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002383 case B_up: {
2384 char *s, *s1;
Rob Landleyd921b2e2006-08-03 15:41:12 +00002385 s1 = s = xstrdup(as[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002386 while (*s1) {
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02002387 //*s1 = (info == B_up) ? toupper(*s1) : tolower(*s1);
2388 if ((unsigned char)((*s1 | 0x20) - 'a') <= ('z' - 'a'))
2389 *s1 = (info == B_up) ? (*s1 & 0xdf) : (*s1 | 0x20);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002390 s1++;
2391 }
2392 setvar_p(res, s);
2393 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002394 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002395
Denis Vlasenkof782f522007-01-01 23:51:30 +00002396 case B_ix:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002397 n = 0;
Rob Landleya3896512006-05-07 20:20:34 +00002398 ll = strlen(as[1]);
2399 l = strlen(as[0]) - ll;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002400 if (ll > 0 && l >= 0) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002401 if (!icase) {
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002402 char *s = strstr(as[0], as[1]);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002403 if (s)
2404 n = (s - as[0]) + 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002405 } else {
2406 /* this piece of code is terribly slow and
2407 * really should be rewritten
2408 */
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002409 for (i = 0; i <= l; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002410 if (strncasecmp(as[0]+i, as[1], ll) == 0) {
2411 n = i+1;
2412 break;
2413 }
2414 }
2415 }
2416 }
2417 setvar_i(res, n);
2418 break;
2419
Denis Vlasenkof782f522007-01-01 23:51:30 +00002420 case B_ti:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002421 if (nargs > 1)
2422 tt = getvar_i(av[1]);
2423 else
2424 time(&tt);
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002425 //s = (nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y";
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002426 i = strftime(g_buf, MAXVARFMT,
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002427 ((nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"),
2428 localtime(&tt));
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002429 g_buf[i] = '\0';
2430 setvar_s(res, g_buf);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002431 break;
2432
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002433 case B_mt:
2434 setvar_i(res, do_mktime(as[0]));
2435 break;
2436
Denis Vlasenkof782f522007-01-01 23:51:30 +00002437 case B_ma:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002438 re = as_regex(an[1], &sreg);
2439 n = regexec(re, as[0], 1, pmatch, 0);
2440 if (n == 0) {
2441 pmatch[0].rm_so++;
2442 pmatch[0].rm_eo++;
2443 } else {
2444 pmatch[0].rm_so = 0;
2445 pmatch[0].rm_eo = -1;
2446 }
2447 setvar_i(newvar("RSTART"), pmatch[0].rm_so);
2448 setvar_i(newvar("RLENGTH"), pmatch[0].rm_eo - pmatch[0].rm_so);
2449 setvar_i(res, pmatch[0].rm_so);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002450 if (re == &sreg)
2451 regfree(re);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002452 break;
2453
Denis Vlasenkof782f522007-01-01 23:51:30 +00002454 case B_ge:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002455 awk_sub(an[0], as[1], getvar_i(av[2]), av[3], res, TRUE);
2456 break;
2457
Denis Vlasenkof782f522007-01-01 23:51:30 +00002458 case B_gs:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002459 setvar_i(res, awk_sub(an[0], as[1], 0, av[2], av[2], FALSE));
2460 break;
2461
Denis Vlasenkof782f522007-01-01 23:51:30 +00002462 case B_su:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002463 setvar_i(res, awk_sub(an[0], as[1], 1, av[2], av[2], FALSE));
2464 break;
2465 }
2466
2467 nvfree(tv);
2468 return res;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002469#undef tspl
Glenn L McGrath545106f2002-11-11 06:21:00 +00002470}
2471
2472/*
2473 * Evaluate node - the heart of the program. Supplied with subtree
2474 * and place where to store result. returns ptr to result.
2475 */
2476#define XC(n) ((n) >> 8)
2477
Mike Frysinger10a11e22005-09-27 02:23:02 +00002478static var *evaluate(node *op, var *res)
2479{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002480/* This procedure is recursive so we should count every byte */
2481#define fnargs (G.evaluate__fnargs)
2482/* seed is initialized to 1 */
2483#define seed (G.evaluate__seed)
Denys Vlasenkofb132e42010-10-29 11:46:52 +02002484#define sreg (G.evaluate__sreg)
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002485
Glenn L McGrath545106f2002-11-11 06:21:00 +00002486 var *v1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002487
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002488 if (!op)
Glenn L McGrath545106f2002-11-11 06:21:00 +00002489 return setvar_s(res, NULL);
2490
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002491 debug_printf_eval("entered %s()\n", __func__);
2492
Glenn L McGrath545106f2002-11-11 06:21:00 +00002493 v1 = nvalloc(2);
2494
2495 while (op) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002496 struct {
2497 var *v;
2498 const char *s;
2499 } L = L; /* for compiler */
2500 struct {
2501 var *v;
2502 const char *s;
2503 } R = R;
2504 double L_d = L_d;
2505 uint32_t opinfo;
2506 int opn;
2507 node *op1;
2508
Glenn L McGrath545106f2002-11-11 06:21:00 +00002509 opinfo = op->info;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002510 opn = (opinfo & OPNMASK);
2511 g_lineno = op->lineno;
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002512 op1 = op->l.n;
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002513 debug_printf_eval("opinfo:%08x opn:%08x\n", opinfo, opn);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002514
Mike Frysingerde2b9382005-09-27 03:18:00 +00002515 /* execute inevitable things */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002516 if (opinfo & OF_RES1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002517 L.v = evaluate(op1, v1);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002518 if (opinfo & OF_RES2)
2519 R.v = evaluate(op->r.n, v1+1);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002520 if (opinfo & OF_STR1) {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002521 L.s = getvar_s(L.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002522 debug_printf_eval("L.s:'%s'\n", L.s);
2523 }
2524 if (opinfo & OF_STR2) {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002525 R.s = getvar_s(R.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002526 debug_printf_eval("R.s:'%s'\n", R.s);
2527 }
2528 if (opinfo & OF_NUM1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002529 L_d = getvar_i(L.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002530 debug_printf_eval("L_d:%f\n", L_d);
2531 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002532
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002533 debug_printf_eval("switch(0x%x)\n", XC(opinfo & OPCLSMASK));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002534 switch (XC(opinfo & OPCLSMASK)) {
2535
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002536 /* -- iterative node type -- */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002537
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002538 /* test pattern */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002539 case XC( OC_TEST ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002540 if ((op1->info & OPCLSMASK) == OC_COMMA) {
2541 /* it's range pattern */
2542 if ((opinfo & OF_CHECKED) || ptest(op1->l.n)) {
2543 op->info |= OF_CHECKED;
2544 if (ptest(op1->r.n))
2545 op->info &= ~OF_CHECKED;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002546 op = op->a.n;
2547 } else {
2548 op = op->r.n;
2549 }
2550 } else {
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002551 op = ptest(op1) ? op->a.n : op->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002552 }
2553 break;
2554
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002555 /* just evaluate an expression, also used as unconditional jump */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002556 case XC( OC_EXEC ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002557 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002558
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002559 /* branch, used in if-else and various loops */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002560 case XC( OC_BR ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002561 op = istrue(L.v) ? op->a.n : op->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002562 break;
2563
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002564 /* initialize for-in loop */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002565 case XC( OC_WALKINIT ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002566 hashwalk_init(L.v, iamarray(R.v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002567 break;
2568
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002569 /* get next array item */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002570 case XC( OC_WALKNEXT ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002571 op = hashwalk_next(L.v) ? op->a.n : op->r.n;
2572 break;
2573
Denis Vlasenkof782f522007-01-01 23:51:30 +00002574 case XC( OC_PRINT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002575 case XC( OC_PRINTF ): {
2576 FILE *F = stdout;
2577
Mike Frysingerde2b9382005-09-27 03:18:00 +00002578 if (op->r.n) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002579 rstream *rsm = newfile(R.s);
2580 if (!rsm->F) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002581 if (opn == '|') {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002582 rsm->F = popen(R.s, "w");
2583 if (rsm->F == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002584 bb_perror_msg_and_die("popen");
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002585 rsm->is_pipe = 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002586 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002587 rsm->F = xfopen(R.s, opn=='w' ? "w" : "a");
Glenn L McGrath545106f2002-11-11 06:21:00 +00002588 }
2589 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002590 F = rsm->F;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002591 }
2592
2593 if ((opinfo & OPCLSMASK) == OC_PRINT) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002594 if (!op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002595 fputs(getvar_s(intvar[F0]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002596 } else {
2597 while (op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002598 var *v = evaluate(nextarg(&op1), v1);
2599 if (v->type & VF_NUMBER) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002600 fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[OFMT]),
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002601 getvar_i(v), TRUE);
2602 fputs(g_buf, F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002603 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002604 fputs(getvar_s(v), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002605 }
2606
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002607 if (op1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002608 fputs(getvar_s(intvar[OFS]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002609 }
2610 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002611 fputs(getvar_s(intvar[ORS]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002612
2613 } else { /* OC_PRINTF */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002614 char *s = awk_printf(op1);
2615 fputs(s, F);
2616 free(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002617 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002618 fflush(F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002619 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002620 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002621
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002622 case XC( OC_DELETE ): {
2623 uint32_t info = op1->info & OPCLSMASK;
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002624 var *v;
2625
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002626 if (info == OC_VAR) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002627 v = op1->l.v;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002628 } else if (info == OC_FNARG) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002629 v = &fnargs[op1->l.aidx];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002630 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002631 syntax_error(EMSG_NOT_ARRAY);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002632 }
2633
Mike Frysingerde2b9382005-09-27 03:18:00 +00002634 if (op1->r.n) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002635 const char *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002636 clrvar(L.v);
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002637 s = getvar_s(evaluate(op1->r.n, v1));
2638 hash_remove(iamarray(v), s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002639 } else {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002640 clear_array(iamarray(v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002641 }
2642 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002643 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002644
Denis Vlasenkof782f522007-01-01 23:51:30 +00002645 case XC( OC_NEWSOURCE ):
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002646 g_progname = op->l.new_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002647 break;
2648
Denis Vlasenkof782f522007-01-01 23:51:30 +00002649 case XC( OC_RETURN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002650 copyvar(res, L.v);
2651 break;
2652
Denis Vlasenkof782f522007-01-01 23:51:30 +00002653 case XC( OC_NEXTFILE ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002654 nextfile = TRUE;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002655 case XC( OC_NEXT ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002656 nextrec = TRUE;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002657 case XC( OC_DONE ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002658 clrvar(res);
2659 break;
2660
Denis Vlasenkof782f522007-01-01 23:51:30 +00002661 case XC( OC_EXIT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002662 awk_exit(L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002663
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002664 /* -- recursive node type -- */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002665
Denis Vlasenkof782f522007-01-01 23:51:30 +00002666 case XC( OC_VAR ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002667 L.v = op->l.v;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002668 if (L.v == intvar[NF])
Glenn L McGrath545106f2002-11-11 06:21:00 +00002669 split_f0();
2670 goto v_cont;
2671
Denis Vlasenkof782f522007-01-01 23:51:30 +00002672 case XC( OC_FNARG ):
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002673 L.v = &fnargs[op->l.aidx];
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002674 v_cont:
2675 res = op->r.n ? findvar(iamarray(L.v), R.s) : L.v;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002676 break;
2677
Denis Vlasenkof782f522007-01-01 23:51:30 +00002678 case XC( OC_IN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002679 setvar_i(res, hash_search(iamarray(R.v), L.s) ? 1 : 0);
2680 break;
2681
Denis Vlasenkof782f522007-01-01 23:51:30 +00002682 case XC( OC_REGEXP ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002683 op1 = op;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002684 L.s = getvar_s(intvar[F0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002685 goto re_cont;
2686
Denis Vlasenkof782f522007-01-01 23:51:30 +00002687 case XC( OC_MATCH ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002688 op1 = op->r.n;
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002689 re_cont:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002690 {
2691 regex_t *re = as_regex(op1, &sreg);
2692 int i = regexec(re, L.s, 0, NULL, 0);
2693 if (re == &sreg)
2694 regfree(re);
2695 setvar_i(res, (i == 0) ^ (opn == '!'));
2696 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002697 break;
2698
Denis Vlasenkof782f522007-01-01 23:51:30 +00002699 case XC( OC_MOVE ):
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002700 debug_printf_eval("MOVE\n");
Mike Frysingerde2b9382005-09-27 03:18:00 +00002701 /* if source is a temporary string, jusk relink it to dest */
Denys Vlasenko12847742009-11-30 01:15:04 +01002702//Disabled: if R.v is numeric but happens to have cached R.v->string,
2703//then L.v ends up being a string, which is wrong
2704// if (R.v == v1+1 && R.v->string) {
2705// res = setvar_p(L.v, R.v->string);
2706// R.v->string = NULL;
2707// } else {
Mike Frysingerde2b9382005-09-27 03:18:00 +00002708 res = copyvar(L.v, R.v);
Denys Vlasenko12847742009-11-30 01:15:04 +01002709// }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002710 break;
2711
Denis Vlasenkof782f522007-01-01 23:51:30 +00002712 case XC( OC_TERNARY ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002713 if ((op->r.n->info & OPCLSMASK) != OC_COLON)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002714 syntax_error(EMSG_POSSIBLE_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002715 res = evaluate(istrue(L.v) ? op->r.n->l.n : op->r.n->r.n, res);
2716 break;
2717
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002718 case XC( OC_FUNC ): {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002719 var *vbeg, *v;
2720 const char *sv_progname;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002721
Bernhard Reutner-Fischerb79a0fe2013-03-06 21:01:05 +01002722 /* The body might be empty, still has to eval the args */
Bernhard Reutner-Fischera060a1a2013-07-31 15:29:20 +02002723 if (!op->r.n->info && !op->r.f->body.first)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002724 syntax_error(EMSG_UNDEF_FUNC);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002725
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002726 vbeg = v = nvalloc(op->r.f->nargs + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002727 while (op1) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002728 var *arg = evaluate(nextarg(&op1), v1);
2729 copyvar(v, arg);
2730 v->type |= VF_CHILD;
2731 v->x.parent = arg;
2732 if (++v - vbeg >= op->r.f->nargs)
Glenn L McGrath545106f2002-11-11 06:21:00 +00002733 break;
2734 }
2735
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002736 v = fnargs;
2737 fnargs = vbeg;
2738 sv_progname = g_progname;
2739
2740 res = evaluate(op->r.f->body.first, res);
2741
2742 g_progname = sv_progname;
2743 nvfree(fnargs);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002744 fnargs = v;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002745
Glenn L McGrath545106f2002-11-11 06:21:00 +00002746 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002747 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002748
Denis Vlasenkof782f522007-01-01 23:51:30 +00002749 case XC( OC_GETLINE ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002750 case XC( OC_PGETLINE ): {
2751 rstream *rsm;
2752 int i;
2753
Mike Frysingerde2b9382005-09-27 03:18:00 +00002754 if (op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002755 rsm = newfile(L.s);
2756 if (!rsm->F) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002757 if ((opinfo & OPCLSMASK) == OC_PGETLINE) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002758 rsm->F = popen(L.s, "r");
2759 rsm->is_pipe = TRUE;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002760 } else {
Denys Vlasenkofb132e42010-10-29 11:46:52 +02002761 rsm->F = fopen_for_read(L.s); /* not xfopen! */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002762 }
2763 }
2764 } else {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002765 if (!iF)
2766 iF = next_input_file();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002767 rsm = iF;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002768 }
2769
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02002770 if (!rsm || !rsm->F) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002771 setvar_i(intvar[ERRNO], errno);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002772 setvar_i(res, -1);
2773 break;
2774 }
2775
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002776 if (!op->r.n)
2777 R.v = intvar[F0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002778
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002779 i = awk_getline(rsm, R.v);
2780 if (i > 0 && !op1) {
2781 incvar(intvar[FNR]);
2782 incvar(intvar[NR]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002783 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002784 setvar_i(res, i);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002785 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002786 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002787
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002788 /* simple builtins */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002789 case XC( OC_FBLTIN ): {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002790 double R_d = R_d; /* for compiler */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002791
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002792 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002793 case F_in:
Denys Vlasenko1390a012013-07-20 21:23:01 +02002794 R_d = (long long)L_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002795 break;
2796
Denis Vlasenkof782f522007-01-01 23:51:30 +00002797 case F_rn:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002798 R_d = (double)rand() / (double)RAND_MAX;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002799 break;
Rob Landleyd8205b32010-10-24 03:27:22 +02002800
Denis Vlasenkof782f522007-01-01 23:51:30 +00002801 case F_co:
Rob Landleyd8205b32010-10-24 03:27:22 +02002802 if (ENABLE_FEATURE_AWK_LIBM) {
2803 R_d = cos(L_d);
2804 break;
2805 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002806
Denis Vlasenkof782f522007-01-01 23:51:30 +00002807 case F_ex:
Rob Landleyd8205b32010-10-24 03:27:22 +02002808 if (ENABLE_FEATURE_AWK_LIBM) {
2809 R_d = exp(L_d);
2810 break;
2811 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002812
Denis Vlasenkof782f522007-01-01 23:51:30 +00002813 case F_lg:
Rob Landleyd8205b32010-10-24 03:27:22 +02002814 if (ENABLE_FEATURE_AWK_LIBM) {
2815 R_d = log(L_d);
2816 break;
2817 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002818
Denis Vlasenkof782f522007-01-01 23:51:30 +00002819 case F_si:
Rob Landleyd8205b32010-10-24 03:27:22 +02002820 if (ENABLE_FEATURE_AWK_LIBM) {
2821 R_d = sin(L_d);
2822 break;
2823 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002824
Denis Vlasenkof782f522007-01-01 23:51:30 +00002825 case F_sq:
Rob Landleyd8205b32010-10-24 03:27:22 +02002826 if (ENABLE_FEATURE_AWK_LIBM) {
2827 R_d = sqrt(L_d);
2828 break;
2829 }
2830
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002831 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002832 break;
Rob Landleyd8205b32010-10-24 03:27:22 +02002833
Denis Vlasenkof782f522007-01-01 23:51:30 +00002834 case F_sr:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002835 R_d = (double)seed;
2836 seed = op1 ? (unsigned)L_d : (unsigned)time(NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002837 srand(seed);
2838 break;
2839
Denis Vlasenkof782f522007-01-01 23:51:30 +00002840 case F_ti:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002841 R_d = time(NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002842 break;
2843
Denis Vlasenkof782f522007-01-01 23:51:30 +00002844 case F_le:
Denys Vlasenko7985bc12013-10-12 04:51:54 +02002845 debug_printf_eval("length: L.s:'%s'\n", L.s);
2846 if (!op1) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002847 L.s = getvar_s(intvar[F0]);
Denys Vlasenko7985bc12013-10-12 04:51:54 +02002848 debug_printf_eval("length: L.s='%s'\n", L.s);
2849 }
2850 else if (L.v->type & VF_ARRAY) {
2851 R_d = L.v->x.array->nel;
2852 debug_printf_eval("length: array_len:%d\n", L.v->x.array->nel);
2853 break;
2854 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002855 R_d = strlen(L.s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002856 break;
2857
Denis Vlasenkof782f522007-01-01 23:51:30 +00002858 case F_sy:
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002859 fflush_all();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002860 R_d = (ENABLE_FEATURE_ALLOW_EXEC && L.s && *L.s)
Denis Vlasenko249fabf2006-12-19 00:29:22 +00002861 ? (system(L.s) >> 8) : 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002862 break;
2863
Denis Vlasenkof782f522007-01-01 23:51:30 +00002864 case F_ff:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002865 if (!op1) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002866 fflush(stdout);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002867 } else if (L.s && *L.s) {
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002868 rstream *rsm = newfile(L.s);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002869 fflush(rsm->F);
2870 } else {
2871 fflush_all();
Glenn L McGrath545106f2002-11-11 06:21:00 +00002872 }
2873 break;
2874
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002875 case F_cl: {
2876 rstream *rsm;
2877 int err = 0;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002878 rsm = (rstream *)hash_search(fdhash, L.s);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002879 debug_printf_eval("OC_FBLTIN F_cl rsm:%p\n", rsm);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002880 if (rsm) {
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002881 debug_printf_eval("OC_FBLTIN F_cl "
2882 "rsm->is_pipe:%d, ->F:%p\n",
2883 rsm->is_pipe, rsm->F);
2884 /* Can be NULL if open failed. Example:
2885 * getline line <"doesnt_exist";
2886 * close("doesnt_exist"); <--- here rsm->F is NULL
2887 */
2888 if (rsm->F)
2889 err = rsm->is_pipe ? pclose(rsm->F) : fclose(rsm->F);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002890 free(rsm->buffer);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002891 hash_remove(fdhash, L.s);
2892 }
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002893 if (err)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002894 setvar_i(intvar[ERRNO], errno);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002895 R_d = (double)err;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002896 break;
2897 }
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02002898 } /* switch */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002899 setvar_i(res, R_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002900 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002901 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002902
Denis Vlasenkof782f522007-01-01 23:51:30 +00002903 case XC( OC_BUILTIN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002904 res = exec_builtin(op, res);
2905 break;
2906
Denis Vlasenkof782f522007-01-01 23:51:30 +00002907 case XC( OC_SPRINTF ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002908 setvar_p(res, awk_printf(op1));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002909 break;
2910
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002911 case XC( OC_UNARY ): {
2912 double Ld, R_d;
2913
2914 Ld = R_d = getvar_i(R.v);
Mike Frysingerde2b9382005-09-27 03:18:00 +00002915 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002916 case 'P':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002917 Ld = ++R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002918 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002919 case 'p':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002920 R_d++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002921 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002922 case 'M':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002923 Ld = --R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002924 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002925 case 'm':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002926 R_d--;
2927 r_op_change:
2928 setvar_i(R.v, R_d);
2929 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002930 case '!':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002931 Ld = !istrue(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002932 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002933 case '-':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002934 Ld = -R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002935 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002936 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002937 setvar_i(res, Ld);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002938 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002939 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002940
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002941 case XC( OC_FIELD ): {
2942 int i = (int)getvar_i(R.v);
2943 if (i == 0) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002944 res = intvar[F0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002945 } else {
2946 split_f0();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002947 if (i > nfields)
2948 fsrealloc(i);
2949 res = &Fields[i - 1];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002950 }
2951 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002952 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002953
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002954 /* concatenation (" ") and index joining (",") */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002955 case XC( OC_CONCAT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002956 case XC( OC_COMMA ): {
2957 const char *sep = "";
2958 if ((opinfo & OPCLSMASK) == OC_COMMA)
2959 sep = getvar_s(intvar[SUBSEP]);
2960 setvar_p(res, xasprintf("%s%s%s", L.s, sep, R.s));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002961 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002962 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002963
Denis Vlasenkof782f522007-01-01 23:51:30 +00002964 case XC( OC_LAND ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002965 setvar_i(res, istrue(L.v) ? ptest(op->r.n) : 0);
2966 break;
2967
Denis Vlasenkof782f522007-01-01 23:51:30 +00002968 case XC( OC_LOR ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002969 setvar_i(res, istrue(L.v) ? 1 : ptest(op->r.n));
2970 break;
2971
Denis Vlasenkof782f522007-01-01 23:51:30 +00002972 case XC( OC_BINARY ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002973 case XC( OC_REPLACE ): {
2974 double R_d = getvar_i(R.v);
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02002975 debug_printf_eval("BINARY/REPLACE: R_d:%f opn:%c\n", R_d, opn);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002976 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002977 case '+':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002978 L_d += R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002979 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002980 case '-':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002981 L_d -= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002982 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002983 case '*':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002984 L_d *= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002985 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002986 case '/':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002987 if (R_d == 0)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002988 syntax_error(EMSG_DIV_BY_ZERO);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002989 L_d /= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002990 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002991 case '&':
Rob Landleyd8205b32010-10-24 03:27:22 +02002992 if (ENABLE_FEATURE_AWK_LIBM)
2993 L_d = pow(L_d, R_d);
2994 else
2995 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002996 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002997 case '%':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002998 if (R_d == 0)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002999 syntax_error(EMSG_DIV_BY_ZERO);
Denys Vlasenko1390a012013-07-20 21:23:01 +02003000 L_d -= (long long)(L_d / R_d) * R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003001 break;
3002 }
Denys Vlasenkod527e0c2010-10-05 13:22:11 +02003003 debug_printf_eval("BINARY/REPLACE result:%f\n", L_d);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003004 res = setvar_i(((opinfo & OPCLSMASK) == OC_BINARY) ? res : L.v, L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003005 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003006 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003007
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003008 case XC( OC_COMPARE ): {
3009 int i = i; /* for compiler */
3010 double Ld;
3011
Glenn L McGrath545106f2002-11-11 06:21:00 +00003012 if (is_numeric(L.v) && is_numeric(R.v)) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003013 Ld = getvar_i(L.v) - getvar_i(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003014 } else {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01003015 const char *l = getvar_s(L.v);
3016 const char *r = getvar_s(R.v);
3017 Ld = icase ? strcasecmp(l, r) : strcmp(l, r);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003018 }
3019 switch (opn & 0xfe) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00003020 case 0:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003021 i = (Ld > 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003022 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003023 case 2:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003024 i = (Ld >= 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003025 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003026 case 4:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003027 i = (Ld == 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003028 break;
3029 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003030 setvar_i(res, (i == 0) ^ (opn & 1));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003031 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003032 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003033
Denis Vlasenkof782f522007-01-01 23:51:30 +00003034 default:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003035 syntax_error(EMSG_POSSIBLE_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003036 }
3037 if ((opinfo & OPCLSMASK) <= SHIFT_TIL_THIS)
3038 op = op->a.n;
3039 if ((opinfo & OPCLSMASK) >= RECUR_FROM_THIS)
3040 break;
3041 if (nextrec)
3042 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01003043 } /* while (op) */
3044
Glenn L McGrath545106f2002-11-11 06:21:00 +00003045 nvfree(v1);
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003046 debug_printf_eval("returning from %s(): %p\n", __func__, res);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003047 return res;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003048#undef fnargs
3049#undef seed
3050#undef sreg
Glenn L McGrath545106f2002-11-11 06:21:00 +00003051}
3052
3053
3054/* -------- main & co. -------- */
3055
Mike Frysinger10a11e22005-09-27 02:23:02 +00003056static int awk_exit(int r)
3057{
Denis Vlasenkof782f522007-01-01 23:51:30 +00003058 var tv;
3059 unsigned i;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003060 hash_item *hi;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003061
Denis Vlasenkof782f522007-01-01 23:51:30 +00003062 zero_out_var(&tv);
3063
3064 if (!exiting) {
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003065 exiting = TRUE;
Glenn L McGrathca29ffc2004-09-24 09:24:27 +00003066 nextrec = FALSE;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003067 evaluate(endseq.first, &tv);
3068 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003069
3070 /* waiting for children */
Denis Vlasenkof782f522007-01-01 23:51:30 +00003071 for (i = 0; i < fdhash->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003072 hi = fdhash->items[i];
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00003073 while (hi) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003074 if (hi->data.rs.F && hi->data.rs.is_pipe)
3075 pclose(hi->data.rs.F);
3076 hi = hi->next;
3077 }
3078 }
3079
3080 exit(r);
3081}
3082
3083/* if expr looks like "var=value", perform assignment and return 1,
3084 * otherwise return 0 */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00003085static int is_assignment(const char *expr)
Mike Frysinger10a11e22005-09-27 02:23:02 +00003086{
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003087 char *exprc, *val;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003088
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003089 if (!isalnum_(*expr) || (val = strchr(expr, '=')) == NULL) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003090 return FALSE;
3091 }
3092
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003093 exprc = xstrdup(expr);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003094 val = exprc + (val - expr);
3095 *val++ = '\0';
Denys Vlasenko6a0d7492010-10-23 21:02:15 +02003096
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003097 unescape_string_in_place(val);
Denys Vlasenko2b299fe2010-10-24 01:58:04 +02003098 setvar_u(newvar(exprc), val);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003099 free(exprc);
3100 return TRUE;
3101}
3102
3103/* switch to next input file */
Mike Frysinger10a11e22005-09-27 02:23:02 +00003104static rstream *next_input_file(void)
3105{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003106#define rsm (G.next_input_file__rsm)
3107#define files_happen (G.next_input_file__files_happen)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003108
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003109 FILE *F;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00003110 const char *fname, *ind;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003111
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003112 if (rsm.F)
3113 fclose(rsm.F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003114 rsm.F = NULL;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00003115 rsm.pos = rsm.adv = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003116
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003117 for (;;) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003118 if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003119 if (files_happen)
3120 return NULL;
3121 fname = "-";
3122 F = stdin;
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003123 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003124 }
Denys Vlasenkof65c5f52011-09-07 20:01:39 +02003125 ind = getvar_s(incvar(intvar[ARGIND]));
3126 fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind));
3127 if (fname && *fname && !is_assignment(fname)) {
3128 F = xfopen_stdin(fname);
3129 break;
3130 }
3131 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003132
3133 files_happen = TRUE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003134 setvar_s(intvar[FILENAME], fname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003135 rsm.F = F;
3136 return &rsm;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003137#undef rsm
3138#undef files_happen
Glenn L McGrath545106f2002-11-11 06:21:00 +00003139}
3140
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00003141int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +00003142int awk_main(int argc, char **argv)
Mike Frysinger10a11e22005-09-27 02:23:02 +00003143{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00003144 unsigned opt;
Denys Vlasenko7b46d112011-09-11 00:30:56 +02003145 char *opt_F;
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003146 llist_t *list_v = NULL;
3147 llist_t *list_f = NULL;
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003148#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
3149 llist_t *list_e = NULL;
3150#endif
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003151 int i, j;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003152 var *v;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003153 var tv;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003154 char **envp;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003155 char *vnames = (char *)vNames; /* cheat */
3156 char *vvalues = (char *)vValues;
3157
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003158 INIT_G();
3159
Denis Vlasenko150f4022007-01-13 21:06:21 +00003160 /* Undo busybox.c, or else strtod may eat ','! This breaks parsing:
Denis Vlasenko6dc6ebb2007-01-01 23:53:12 +00003161 * $1,$2 == '$1,' '$2', NOT '$1' ',' '$2' */
3162 if (ENABLE_LOCALE_SUPPORT)
3163 setlocale(LC_NUMERIC, "C");
3164
Denis Vlasenkof782f522007-01-01 23:51:30 +00003165 zero_out_var(&tv);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003166
3167 /* allocate global buffer */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003168 g_buf = xmalloc(MAXVARFMT + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003169
3170 vhash = hash_init();
3171 ahash = hash_init();
3172 fdhash = hash_init();
3173 fnhash = hash_init();
3174
3175 /* initialize variables */
Denis Vlasenkof782f522007-01-01 23:51:30 +00003176 for (i = 0; *vnames; i++) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003177 intvar[i] = v = newvar(nextword(&vnames));
Denis Vlasenkof782f522007-01-01 23:51:30 +00003178 if (*vvalues != '\377')
3179 setvar_s(v, nextword(&vvalues));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003180 else
3181 setvar_i(v, 0);
3182
Denis Vlasenkof782f522007-01-01 23:51:30 +00003183 if (*vnames == '*') {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003184 v->type |= VF_SPECIAL;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003185 vnames++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003186 }
3187 }
3188
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003189 handle_special(intvar[FS]);
3190 handle_special(intvar[RS]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003191
Denis Vlasenkof782f522007-01-01 23:51:30 +00003192 newfile("/dev/stdin")->F = stdin;
3193 newfile("/dev/stdout")->F = stdout;
3194 newfile("/dev/stderr")->F = stderr;
Glenn L McGrath545106f2002-11-11 06:21:00 +00003195
Denis Vlasenkof71d9162007-05-03 22:57:56 +00003196 /* Huh, people report that sometimes environ is NULL. Oh well. */
3197 if (environ) for (envp = environ; *envp; envp++) {
Denis Vlasenkob78c7822007-07-18 18:31:11 +00003198 /* environ is writable, thus we don't strdup it needlessly */
3199 char *s = *envp;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003200 char *s1 = strchr(s, '=');
3201 if (s1) {
Denis Vlasenkob78c7822007-07-18 18:31:11 +00003202 *s1 = '\0';
3203 /* Both findvar and setvar_u take const char*
3204 * as 2nd arg -> environment is not trashed */
3205 setvar_u(findvar(iamarray(intvar[ENVIRON]), s), s1 + 1);
3206 *s1 = '=';
Eric Andersen67776be2004-07-30 23:52:08 +00003207 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003208 }
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003209 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 +00003210 argv += optind;
3211 argc -= optind;
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003212 if (opt & OPT_W)
3213 bb_error_msg("warning: option -W is ignored");
3214 if (opt & OPT_F) {
Denys Vlasenkoea664dd2012-06-22 18:41:01 +02003215 unescape_string_in_place(opt_F);
3216 setvar_s(intvar[FS], opt_F);
3217 }
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003218 while (list_v) {
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003219 if (!is_assignment(llist_pop(&list_v)))
Denis Vlasenkobe644a82007-03-10 17:22:14 +00003220 bb_show_usage();
3221 }
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003222 while (list_f) {
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003223 char *s = NULL;
3224 FILE *from_file;
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003225
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003226 g_progname = llist_pop(&list_f);
3227 from_file = xfopen_stdin(g_progname);
3228 /* one byte is reserved for some trick in next_token */
3229 for (i = j = 1; j > 0; i += j) {
3230 s = xrealloc(s, i + 4096);
3231 j = fread(s + i, 1, 4094, from_file);
3232 }
3233 s[i] = '\0';
3234 fclose(from_file);
3235 parse_program(s + 1);
3236 free(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003237 }
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003238 g_progname = "cmd. line";
3239#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003240 while (list_e) {
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003241 parse_program(llist_pop(&list_e));
3242 }
3243#endif
3244 if (!(opt & (OPT_f | OPT_e))) {
3245 if (!*argv)
3246 bb_show_usage();
3247 parse_program(*argv++);
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003248 argc--;
Sven-Göran Berghf200f732013-11-12 14:18:25 +01003249 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003250
Glenn L McGrath545106f2002-11-11 06:21:00 +00003251 /* fill in ARGV array */
Denys Vlasenkobd0e2212013-11-21 15:09:55 +01003252 setvar_i(intvar[ARGC], argc + 1);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003253 setari_u(intvar[ARGV], 0, "awk");
Denis Vlasenkof782f522007-01-01 23:51:30 +00003254 i = 0;
3255 while (*argv)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003256 setari_u(intvar[ARGV], ++i, *argv++);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003257
3258 evaluate(beginseq.first, &tv);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003259 if (!mainseq.first && !endseq.first)
Glenn L McGrath545106f2002-11-11 06:21:00 +00003260 awk_exit(EXIT_SUCCESS);
3261
3262 /* input file could already be opened in BEGIN block */
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003263 if (!iF)
3264 iF = next_input_file();
Glenn L McGrath545106f2002-11-11 06:21:00 +00003265
3266 /* passing through input files */
3267 while (iF) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003268 nextfile = FALSE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003269 setvar_i(intvar[FNR], 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003270
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003271 while ((i = awk_getline(iF, intvar[F0])) > 0) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003272 nextrec = FALSE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003273 incvar(intvar[NR]);
3274 incvar(intvar[FNR]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003275 evaluate(mainseq.first, &tv);
3276
3277 if (nextfile)
3278 break;
3279 }
3280
Denis Vlasenkof782f522007-01-01 23:51:30 +00003281 if (i < 0)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003282 syntax_error(strerror(errno));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003283
3284 iF = next_input_file();
Glenn L McGrath545106f2002-11-11 06:21:00 +00003285 }
3286
Glenn L McGrath545106f2002-11-11 06:21:00 +00003287 awk_exit(EXIT_SUCCESS);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003288 /*return 0;*/
Glenn L McGrath545106f2002-11-11 06:21:00 +00003289}