blob: f47d1ab027d30d035e76ce3979bb2045d38baafa [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000011#include "xregex.h"
12#include <math.h>
Glenn L McGrath545106f2002-11-11 06:21:00 +000013
Denis Vlasenko99912ca2007-04-10 15:43:37 +000014/* This is a NOEXEC applet. Be very careful! */
15
Glenn L McGrath545106f2002-11-11 06:21:00 +000016
Denys Vlasenkoda62b092010-03-11 12:13:18 +010017/* If you comment out one of these below, it will be #defined later
18 * to perform debug printfs to stderr: */
19#define debug_printf_walker(...) do {} while (0)
20
21#ifndef debug_printf_walker
22# define debug_printf_walker(...) (fprintf(stderr, __VA_ARGS__))
23#endif
24
25
26
Denis Vlasenko629563b2007-02-24 17:05:52 +000027#define MAXVARFMT 240
28#define MINNVBLOCK 64
Glenn L McGrath545106f2002-11-11 06:21:00 +000029
30/* variable flags */
Denis Vlasenko629563b2007-02-24 17:05:52 +000031#define VF_NUMBER 0x0001 /* 1 = primary type is number */
32#define VF_ARRAY 0x0002 /* 1 = it's an array */
Glenn L McGrath545106f2002-11-11 06:21:00 +000033
Denis Vlasenko629563b2007-02-24 17:05:52 +000034#define VF_CACHED 0x0100 /* 1 = num/str value has cached str/num eq */
35#define VF_USER 0x0200 /* 1 = user input (may be numeric string) */
36#define VF_SPECIAL 0x0400 /* 1 = requires extra handling when changed */
37#define VF_WALK 0x0800 /* 1 = variable has alloc'd x.walker list */
38#define VF_FSTR 0x1000 /* 1 = var::string points to fstring buffer */
39#define VF_CHILD 0x2000 /* 1 = function arg; x.parent points to source */
40#define VF_DIRTY 0x4000 /* 1 = variable was set explicitly */
Glenn L McGrath545106f2002-11-11 06:21:00 +000041
42/* these flags are static, don't change them when value is changed */
Denis Vlasenko629563b2007-02-24 17:05:52 +000043#define VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY)
Glenn L McGrath545106f2002-11-11 06:21:00 +000044
Denys Vlasenkoda62b092010-03-11 12:13:18 +010045typedef struct walker_list {
46 char *end;
47 char *cur;
48 struct walker_list *prev;
49 char wbuf[1];
50} walker_list;
51
Glenn L McGrath545106f2002-11-11 06:21:00 +000052/* Variable */
53typedef struct var_s {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +000054 unsigned type; /* flags */
Glenn L McGrath545106f2002-11-11 06:21:00 +000055 double number;
56 char *string;
57 union {
Denis Vlasenko629563b2007-02-24 17:05:52 +000058 int aidx; /* func arg idx (for compilation stage) */
59 struct xhash_s *array; /* array ptr */
60 struct var_s *parent; /* for func args, ptr to actual parameter */
Denys Vlasenkoda62b092010-03-11 12:13:18 +010061 walker_list *walker; /* list of array elements (for..in) */
Glenn L McGrath545106f2002-11-11 06:21:00 +000062 } x;
63} var;
64
65/* Node chain (pattern-action chain, BEGIN, END, function bodies) */
66typedef struct chain_s {
67 struct node_s *first;
68 struct node_s *last;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000069 const char *programname;
Glenn L McGrath545106f2002-11-11 06:21:00 +000070} chain;
71
72/* Function */
73typedef struct func_s {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +000074 unsigned nargs;
Glenn L McGrath545106f2002-11-11 06:21:00 +000075 struct chain_s body;
76} func;
77
78/* I/O stream */
79typedef struct rstream_s {
80 FILE *F;
81 char *buffer;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +000082 int adv;
Glenn L McGrath545106f2002-11-11 06:21:00 +000083 int size;
84 int pos;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +000085 smallint is_pipe;
Glenn L McGrath545106f2002-11-11 06:21:00 +000086} rstream;
87
88typedef struct hash_item_s {
89 union {
Denis Vlasenkoffba9412007-05-17 23:03:35 +000090 struct var_s v; /* variable/array hash */
91 struct rstream_s rs; /* redirect streams hash */
92 struct func_s f; /* functions hash */
Glenn L McGrath545106f2002-11-11 06:21:00 +000093 } data;
Denis Vlasenkoffba9412007-05-17 23:03:35 +000094 struct hash_item_s *next; /* next in chain */
95 char name[1]; /* really it's longer */
Glenn L McGrath545106f2002-11-11 06:21:00 +000096} hash_item;
97
98typedef struct xhash_s {
Denis Vlasenkoffba9412007-05-17 23:03:35 +000099 unsigned nel; /* num of elements */
100 unsigned csize; /* current hash size */
101 unsigned nprime; /* next hash size in PRIMES[] */
102 unsigned glen; /* summary length of item names */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000103 struct hash_item_s **items;
104} xhash;
105
106/* Tree node */
107typedef struct node_s {
Mike Frysingerf87b3e32005-09-27 04:16:22 +0000108 uint32_t info;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000109 unsigned lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000110 union {
111 struct node_s *n;
112 var *v;
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100113 int aidx;
114 char *new_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000115 regex_t *re;
116 } l;
117 union {
118 struct node_s *n;
119 regex_t *ire;
120 func *f;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000121 } r;
122 union {
123 struct node_s *n;
124 } a;
125} node;
126
127/* Block of temporary variables */
128typedef struct nvblock_s {
129 int size;
130 var *pos;
131 struct nvblock_s *prev;
132 struct nvblock_s *next;
Denys Vlasenkod069e532009-09-09 23:12:10 +0200133 var nv[];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000134} nvblock;
135
136typedef struct tsplitter_s {
137 node n;
138 regex_t re[2];
139} tsplitter;
140
141/* simple token classes */
142/* Order and hex values are very important!!! See next_token() */
143#define TC_SEQSTART 1 /* ( */
144#define TC_SEQTERM (1 << 1) /* ) */
145#define TC_REGEXP (1 << 2) /* /.../ */
146#define TC_OUTRDR (1 << 3) /* | > >> */
147#define TC_UOPPOST (1 << 4) /* unary postfix operator */
148#define TC_UOPPRE1 (1 << 5) /* unary prefix operator */
149#define TC_BINOPX (1 << 6) /* two-opnd operator */
150#define TC_IN (1 << 7)
151#define TC_COMMA (1 << 8)
152#define TC_PIPE (1 << 9) /* input redirection pipe */
153#define TC_UOPPRE2 (1 << 10) /* unary prefix operator */
154#define TC_ARRTERM (1 << 11) /* ] */
155#define TC_GRPSTART (1 << 12) /* { */
156#define TC_GRPTERM (1 << 13) /* } */
157#define TC_SEMICOL (1 << 14)
158#define TC_NEWLINE (1 << 15)
159#define TC_STATX (1 << 16) /* ctl statement (for, next...) */
160#define TC_WHILE (1 << 17)
161#define TC_ELSE (1 << 18)
162#define TC_BUILTIN (1 << 19)
163#define TC_GETLINE (1 << 20)
164#define TC_FUNCDECL (1 << 21) /* `function' `func' */
165#define TC_BEGIN (1 << 22)
166#define TC_END (1 << 23)
167#define TC_EOF (1 << 24)
168#define TC_VARIABLE (1 << 25)
169#define TC_ARRAY (1 << 26)
170#define TC_FUNCTION (1 << 27)
171#define TC_STRING (1 << 28)
172#define TC_NUMBER (1 << 29)
173
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000174#define TC_UOPPRE (TC_UOPPRE1 | TC_UOPPRE2)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000175
176/* combined token classes */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000177#define TC_BINOP (TC_BINOPX | TC_COMMA | TC_PIPE | TC_IN)
178#define TC_UNARYOP (TC_UOPPRE | TC_UOPPOST)
179#define TC_OPERAND (TC_VARIABLE | TC_ARRAY | TC_FUNCTION \
180 | TC_BUILTIN | TC_GETLINE | TC_SEQSTART | TC_STRING | TC_NUMBER)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000181
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000182#define TC_STATEMNT (TC_STATX | TC_WHILE)
183#define TC_OPTERM (TC_SEMICOL | TC_NEWLINE)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000184
185/* word tokens, cannot mean something else if not expected */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000186#define TC_WORD (TC_IN | TC_STATEMNT | TC_ELSE | TC_BUILTIN \
187 | TC_GETLINE | TC_FUNCDECL | TC_BEGIN | TC_END)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000188
189/* discard newlines after these */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000190#define TC_NOTERM (TC_COMMA | TC_GRPSTART | TC_GRPTERM \
191 | TC_BINOP | TC_OPTERM)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000192
193/* what can expression begin with */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000194#define TC_OPSEQ (TC_OPERAND | TC_UOPPRE | TC_REGEXP)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000195/* what can group begin with */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000196#define TC_GRPSEQ (TC_OPSEQ | TC_OPTERM | TC_STATEMNT | TC_GRPSTART)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000197
198/* if previous token class is CONCAT1 and next is CONCAT2, concatenation */
199/* operator is inserted between them */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000200#define TC_CONCAT1 (TC_VARIABLE | TC_ARRTERM | TC_SEQTERM \
201 | TC_STRING | TC_NUMBER | TC_UOPPOST)
202#define TC_CONCAT2 (TC_OPERAND | TC_UOPPRE)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000203
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000204#define OF_RES1 0x010000
205#define OF_RES2 0x020000
206#define OF_STR1 0x040000
207#define OF_STR2 0x080000
208#define OF_NUM1 0x100000
209#define OF_CHECKED 0x200000
Glenn L McGrath545106f2002-11-11 06:21:00 +0000210
211/* combined operator flags */
212#define xx 0
213#define xV OF_RES2
214#define xS (OF_RES2 | OF_STR2)
215#define Vx OF_RES1
216#define VV (OF_RES1 | OF_RES2)
217#define Nx (OF_RES1 | OF_NUM1)
218#define NV (OF_RES1 | OF_NUM1 | OF_RES2)
219#define Sx (OF_RES1 | OF_STR1)
220#define SV (OF_RES1 | OF_STR1 | OF_RES2)
221#define SS (OF_RES1 | OF_STR1 | OF_RES2 | OF_STR2)
222
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000223#define OPCLSMASK 0xFF00
224#define OPNMASK 0x007F
Glenn L McGrath545106f2002-11-11 06:21:00 +0000225
226/* operator priority is a highest byte (even: r->l, odd: l->r grouping)
227 * For builtins it has different meaning: n n s3 s2 s1 v3 v2 v1,
228 * n - min. number of args, vN - resolve Nth arg to var, sN - resolve to string
229 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000230#define P(x) (x << 24)
231#define PRIMASK 0x7F000000
232#define PRIMASK2 0x7E000000
Glenn L McGrath545106f2002-11-11 06:21:00 +0000233
234/* Operation classes */
235
236#define SHIFT_TIL_THIS 0x0600
237#define RECUR_FROM_THIS 0x1000
238
239enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000240 OC_DELETE = 0x0100, OC_EXEC = 0x0200, OC_NEWSOURCE = 0x0300,
241 OC_PRINT = 0x0400, OC_PRINTF = 0x0500, OC_WALKINIT = 0x0600,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000242
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000243 OC_BR = 0x0700, OC_BREAK = 0x0800, OC_CONTINUE = 0x0900,
244 OC_EXIT = 0x0a00, OC_NEXT = 0x0b00, OC_NEXTFILE = 0x0c00,
245 OC_TEST = 0x0d00, OC_WALKNEXT = 0x0e00,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000246
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000247 OC_BINARY = 0x1000, OC_BUILTIN = 0x1100, OC_COLON = 0x1200,
248 OC_COMMA = 0x1300, OC_COMPARE = 0x1400, OC_CONCAT = 0x1500,
249 OC_FBLTIN = 0x1600, OC_FIELD = 0x1700, OC_FNARG = 0x1800,
250 OC_FUNC = 0x1900, OC_GETLINE = 0x1a00, OC_IN = 0x1b00,
251 OC_LAND = 0x1c00, OC_LOR = 0x1d00, OC_MATCH = 0x1e00,
252 OC_MOVE = 0x1f00, OC_PGETLINE = 0x2000, OC_REGEXP = 0x2100,
253 OC_REPLACE = 0x2200, OC_RETURN = 0x2300, OC_SPRINTF = 0x2400,
254 OC_TERNARY = 0x2500, OC_UNARY = 0x2600, OC_VAR = 0x2700,
255 OC_DONE = 0x2800,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000256
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000257 ST_IF = 0x3000, ST_DO = 0x3100, ST_FOR = 0x3200,
258 ST_WHILE = 0x3300
Glenn L McGrath545106f2002-11-11 06:21:00 +0000259};
260
261/* simple builtins */
262enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000263 F_in, F_rn, F_co, F_ex, F_lg, F_si, F_sq, F_sr,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000264 F_ti, F_le, F_sy, F_ff, F_cl
265};
266
267/* builtins */
268enum {
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +0200269 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 +0000270 B_ge, B_gs, B_su,
271 B_an, B_co, B_ls, B_or, B_rs, B_xo,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000272};
273
274/* tokens and their corresponding info values */
275
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000276#define NTC "\377" /* switch to next token class (tc<<1) */
277#define NTCC '\377'
Glenn L McGrath545106f2002-11-11 06:21:00 +0000278
279#define OC_B OC_BUILTIN
280
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000281static const char tokenlist[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000282 "\1(" NTC
283 "\1)" NTC
284 "\1/" NTC /* REGEXP */
285 "\2>>" "\1>" "\1|" NTC /* OUTRDR */
286 "\2++" "\2--" NTC /* UOPPOST */
287 "\2++" "\2--" "\1$" NTC /* UOPPRE1 */
288 "\2==" "\1=" "\2+=" "\2-=" /* BINOPX */
289 "\2*=" "\2/=" "\2%=" "\2^="
290 "\1+" "\1-" "\3**=" "\2**"
291 "\1/" "\1%" "\1^" "\1*"
292 "\2!=" "\2>=" "\2<=" "\1>"
293 "\1<" "\2!~" "\1~" "\2&&"
294 "\2||" "\1?" "\1:" NTC
295 "\2in" NTC
296 "\1," NTC
297 "\1|" NTC
298 "\1+" "\1-" "\1!" NTC /* UOPPRE2 */
299 "\1]" NTC
300 "\1{" NTC
301 "\1}" NTC
302 "\1;" NTC
303 "\1\n" NTC
304 "\2if" "\2do" "\3for" "\5break" /* STATX */
305 "\10continue" "\6delete" "\5print"
306 "\6printf" "\4next" "\10nextfile"
307 "\6return" "\4exit" NTC
308 "\5while" NTC
309 "\4else" NTC
Glenn L McGrath545106f2002-11-11 06:21:00 +0000310
Denis Vlasenkof782f522007-01-01 23:51:30 +0000311 "\3and" "\5compl" "\6lshift" "\2or"
312 "\6rshift" "\3xor"
313 "\5close" "\6system" "\6fflush" "\5atan2" /* BUILTIN */
314 "\3cos" "\3exp" "\3int" "\3log"
315 "\4rand" "\3sin" "\4sqrt" "\5srand"
316 "\6gensub" "\4gsub" "\5index" "\6length"
317 "\5match" "\5split" "\7sprintf" "\3sub"
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +0200318 "\6substr" "\7systime" "\10strftime" "\6mktime"
Denis Vlasenkof782f522007-01-01 23:51:30 +0000319 "\7tolower" "\7toupper" NTC
320 "\7getline" NTC
321 "\4func" "\10function" NTC
322 "\5BEGIN" NTC
323 "\3END" "\0"
Glenn L McGrath545106f2002-11-11 06:21:00 +0000324 ;
325
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000326static const uint32_t tokeninfo[] = {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000327 0,
328 0,
329 OC_REGEXP,
Denis Vlasenkof782f522007-01-01 23:51:30 +0000330 xS|'a', xS|'w', xS|'|',
331 OC_UNARY|xV|P(9)|'p', OC_UNARY|xV|P(9)|'m',
332 OC_UNARY|xV|P(9)|'P', OC_UNARY|xV|P(9)|'M',
333 OC_FIELD|xV|P(5),
334 OC_COMPARE|VV|P(39)|5, OC_MOVE|VV|P(74),
335 OC_REPLACE|NV|P(74)|'+', OC_REPLACE|NV|P(74)|'-',
336 OC_REPLACE|NV|P(74)|'*', OC_REPLACE|NV|P(74)|'/',
337 OC_REPLACE|NV|P(74)|'%', OC_REPLACE|NV|P(74)|'&',
338 OC_BINARY|NV|P(29)|'+', OC_BINARY|NV|P(29)|'-',
339 OC_REPLACE|NV|P(74)|'&', OC_BINARY|NV|P(15)|'&',
340 OC_BINARY|NV|P(25)|'/', OC_BINARY|NV|P(25)|'%',
341 OC_BINARY|NV|P(15)|'&', OC_BINARY|NV|P(25)|'*',
342 OC_COMPARE|VV|P(39)|4, OC_COMPARE|VV|P(39)|3,
343 OC_COMPARE|VV|P(39)|0, OC_COMPARE|VV|P(39)|1,
344 OC_COMPARE|VV|P(39)|2, OC_MATCH|Sx|P(45)|'!',
345 OC_MATCH|Sx|P(45)|'~', OC_LAND|Vx|P(55),
346 OC_LOR|Vx|P(59), OC_TERNARY|Vx|P(64)|'?',
347 OC_COLON|xx|P(67)|':',
Glenn L McGrath545106f2002-11-11 06:21:00 +0000348 OC_IN|SV|P(49),
349 OC_COMMA|SS|P(80),
350 OC_PGETLINE|SV|P(37),
Denis Vlasenkof782f522007-01-01 23:51:30 +0000351 OC_UNARY|xV|P(19)|'+', OC_UNARY|xV|P(19)|'-',
352 OC_UNARY|xV|P(19)|'!',
Glenn L McGrath545106f2002-11-11 06:21:00 +0000353 0,
354 0,
355 0,
356 0,
357 0,
Denis Vlasenkof782f522007-01-01 23:51:30 +0000358 ST_IF, ST_DO, ST_FOR, OC_BREAK,
359 OC_CONTINUE, OC_DELETE|Vx, OC_PRINT,
360 OC_PRINTF, OC_NEXT, OC_NEXTFILE,
361 OC_RETURN|Vx, OC_EXIT|Nx,
Glenn L McGrath545106f2002-11-11 06:21:00 +0000362 ST_WHILE,
363 0,
364
Denis Vlasenkoe175ff22006-09-26 17:41:00 +0000365 OC_B|B_an|P(0x83), OC_B|B_co|P(0x41), OC_B|B_ls|P(0x83), OC_B|B_or|P(0x83),
366 OC_B|B_rs|P(0x83), OC_B|B_xo|P(0x83),
Glenn L McGrath545106f2002-11-11 06:21:00 +0000367 OC_FBLTIN|Sx|F_cl, OC_FBLTIN|Sx|F_sy, OC_FBLTIN|Sx|F_ff, OC_B|B_a2|P(0x83),
368 OC_FBLTIN|Nx|F_co, OC_FBLTIN|Nx|F_ex, OC_FBLTIN|Nx|F_in, OC_FBLTIN|Nx|F_lg,
369 OC_FBLTIN|F_rn, OC_FBLTIN|Nx|F_si, OC_FBLTIN|Nx|F_sq, OC_FBLTIN|Nx|F_sr,
370 OC_B|B_ge|P(0xd6), OC_B|B_gs|P(0xb6), OC_B|B_ix|P(0x9b), OC_FBLTIN|Sx|F_le,
371 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 +0200372 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 +0000373 OC_B|B_lo|P(0x49), OC_B|B_up|P(0x49),
374 OC_GETLINE|SV|P(0),
375 0, 0,
376 0,
377 0
378};
379
380/* internal variable names and their initial values */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000381/* asterisk marks SPECIAL vars; $ is just no-named Field0 */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000382enum {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000383 CONVFMT, OFMT, FS, OFS,
Denis Vlasenkof782f522007-01-01 23:51:30 +0000384 ORS, RS, RT, FILENAME,
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000385 SUBSEP, F0, ARGIND, ARGC,
386 ARGV, ERRNO, FNR, NR,
387 NF, IGNORECASE, ENVIRON, NUM_INTERNAL_VARS
Glenn L McGrath545106f2002-11-11 06:21:00 +0000388};
389
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000390static const char vNames[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000391 "CONVFMT\0" "OFMT\0" "FS\0*" "OFS\0"
392 "ORS\0" "RS\0*" "RT\0" "FILENAME\0"
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000393 "SUBSEP\0" "$\0*" "ARGIND\0" "ARGC\0"
394 "ARGV\0" "ERRNO\0" "FNR\0" "NR\0"
395 "NF\0*" "IGNORECASE\0*" "ENVIRON\0" "\0";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000396
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000397static const char vValues[] ALIGN1 =
Denis Vlasenkof782f522007-01-01 23:51:30 +0000398 "%.6g\0" "%.6g\0" " \0" " \0"
399 "\n\0" "\n\0" "\0" "\0"
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +0000400 "\034\0" "\0" "\377";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000401
402/* hash size may grow to these values */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000403#define FIRST_PRIME 61
404static const uint16_t PRIMES[] ALIGN2 = { 251, 1021, 4093, 16381, 65521 };
Glenn L McGrath545106f2002-11-11 06:21:00 +0000405
Glenn L McGrath545106f2002-11-11 06:21:00 +0000406
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000407/* Globals. Split in two parts so that first one is addressed
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000408 * with (mostly short) negative offsets.
409 * NB: it's unsafe to put members of type "double"
410 * into globals2 (gcc may fail to align them).
411 */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000412struct globals {
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000413 double t_double;
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000414 chain beginseq, mainseq, endseq;
415 chain *seq;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000416 node *break_ptr, *continue_ptr;
417 rstream *iF;
418 xhash *vhash, *ahash, *fdhash, *fnhash;
419 const char *g_progname;
420 int g_lineno;
421 int nfields;
422 int maxfields; /* used in fsrealloc() only */
423 var *Fields;
424 nvblock *g_cb;
425 char *g_pos;
426 char *g_buf;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000427 smallint icase;
428 smallint exiting;
429 smallint nextrec;
430 smallint nextfile;
431 smallint is_f0_split;
432};
433struct globals2 {
434 uint32_t t_info; /* often used */
435 uint32_t t_tclass;
436 char *t_string;
437 int t_lineno;
438 int t_rollback;
439
440 var *intvar[NUM_INTERNAL_VARS]; /* often used */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000441
442 /* former statics from various functions */
443 char *split_f0__fstrings;
444
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000445 uint32_t next_token__save_tclass;
446 uint32_t next_token__save_info;
447 uint32_t next_token__ltclass;
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000448 smallint next_token__concat_inserted;
449
450 smallint next_input_file__files_happen;
451 rstream next_input_file__rsm;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000452
453 var *evaluate__fnargs;
454 unsigned evaluate__seed;
455 regex_t evaluate__sreg;
456
457 var ptest__v;
458
459 tsplitter exec_builtin__tspl;
460
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000461 /* biggest and least used members go last */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000462 tsplitter fsplitter, rsplitter;
Denys Vlasenko3dbc5a92010-02-05 14:54:22 +0100463};
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000464#define G1 (ptr_to_globals[-1])
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000465#define G (*(struct globals2 *)ptr_to_globals)
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000466/* For debug. nm --size-sort awk.o | grep -vi ' [tr] ' */
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000467/*char G1size[sizeof(G1)]; - 0x74 */
468/*char Gsize[sizeof(G)]; - 0x1c4 */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000469/* Trying to keep most of members accessible with short offsets: */
Denis Vlasenko9aa5c652009-02-26 11:21:04 +0000470/*char Gofs_seed[offsetof(struct globals2, evaluate__seed)]; - 0x90 */
471#define t_double (G1.t_double )
Denis Vlasenkoe16e6e72007-06-07 13:14:53 +0000472#define beginseq (G1.beginseq )
473#define mainseq (G1.mainseq )
474#define endseq (G1.endseq )
475#define seq (G1.seq )
476#define break_ptr (G1.break_ptr )
477#define continue_ptr (G1.continue_ptr)
478#define iF (G1.iF )
479#define vhash (G1.vhash )
480#define ahash (G1.ahash )
481#define fdhash (G1.fdhash )
482#define fnhash (G1.fnhash )
483#define g_progname (G1.g_progname )
484#define g_lineno (G1.g_lineno )
485#define nfields (G1.nfields )
486#define maxfields (G1.maxfields )
487#define Fields (G1.Fields )
488#define g_cb (G1.g_cb )
489#define g_pos (G1.g_pos )
490#define g_buf (G1.g_buf )
491#define icase (G1.icase )
492#define exiting (G1.exiting )
493#define nextrec (G1.nextrec )
494#define nextfile (G1.nextfile )
495#define is_f0_split (G1.is_f0_split )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000496#define t_info (G.t_info )
497#define t_tclass (G.t_tclass )
498#define t_string (G.t_string )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000499#define t_lineno (G.t_lineno )
500#define t_rollback (G.t_rollback )
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000501#define intvar (G.intvar )
502#define fsplitter (G.fsplitter )
503#define rsplitter (G.rsplitter )
504#define INIT_G() do { \
Denys Vlasenko90a99042009-09-06 02:36:23 +0200505 SET_PTR_TO_GLOBALS((char*)xzalloc(sizeof(G1)+sizeof(G)) + sizeof(G1)); \
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000506 G.next_token__ltclass = TC_OPTERM; \
507 G.evaluate__seed = 1; \
508} while (0)
509
Glenn L McGrath545106f2002-11-11 06:21:00 +0000510
511/* function prototypes */
Glenn L McGrath545106f2002-11-11 06:21:00 +0000512static void handle_special(var *);
Mike Frysingerf87b3e32005-09-27 04:16:22 +0000513static node *parse_expr(uint32_t);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000514static void chain_group(void);
515static var *evaluate(node *, var *);
516static rstream *next_input_file(void);
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000517static int fmt_num(char *, int, const char *, double, int);
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000518static int awk_exit(int) NORETURN;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000519
520/* ---- error handling ---- */
521
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000522static const char EMSG_INTERNAL_ERROR[] ALIGN1 = "Internal error";
523static const char EMSG_UNEXP_EOS[] ALIGN1 = "Unexpected end of string";
524static const char EMSG_UNEXP_TOKEN[] ALIGN1 = "Unexpected token";
525static const char EMSG_DIV_BY_ZERO[] ALIGN1 = "Division by zero";
526static const char EMSG_INV_FMT[] ALIGN1 = "Invalid format specifier";
527static const char EMSG_TOO_FEW_ARGS[] ALIGN1 = "Too few arguments for builtin";
528static const char EMSG_NOT_ARRAY[] ALIGN1 = "Not an array";
529static const char EMSG_POSSIBLE_ERROR[] ALIGN1 = "Possible syntax error";
530static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function";
Denis Vlasenko2d5bd802008-10-24 10:49:49 +0000531#if !ENABLE_FEATURE_AWK_LIBM
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000532static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in";
Glenn L McGrath545106f2002-11-11 06:21:00 +0000533#endif
534
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100535static void zero_out_var(var *vp)
Denis Vlasenkof782f522007-01-01 23:51:30 +0000536{
537 memset(vp, 0, sizeof(*vp));
538}
539
Denis Vlasenkoc7cc5a92009-04-19 01:27:20 +0000540static void syntax_error(const char *message) NORETURN;
541static void syntax_error(const char *message)
Glenn L McGrathd4036f82002-11-28 09:30:40 +0000542{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000543 bb_error_msg_and_die("%s:%i: %s", g_progname, g_lineno, message);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000544}
545
Glenn L McGrath545106f2002-11-11 06:21:00 +0000546/* ---- hash stuff ---- */
547
Denis Vlasenkof782f522007-01-01 23:51:30 +0000548static unsigned hashidx(const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000549{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000550 unsigned idx = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000551
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100552 while (*name)
553 idx = *name++ + (idx << 6) - idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000554 return idx;
555}
556
557/* create new hash */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000558static xhash *hash_init(void)
559{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000560 xhash *newhash;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000561
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100562 newhash = xzalloc(sizeof(*newhash));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000563 newhash->csize = FIRST_PRIME;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100564 newhash->items = xzalloc(FIRST_PRIME * sizeof(newhash->items[0]));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000565
566 return newhash;
567}
568
569/* find item in hash, return ptr to data, NULL if not found */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000570static void *hash_search(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000571{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000572 hash_item *hi;
573
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100574 hi = hash->items[hashidx(name) % hash->csize];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000575 while (hi) {
576 if (strcmp(hi->name, name) == 0)
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100577 return &hi->data;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000578 hi = hi->next;
579 }
580 return NULL;
581}
582
583/* grow hash if it becomes too big */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000584static void hash_rebuild(xhash *hash)
585{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000586 unsigned newsize, i, idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000587 hash_item **newitems, *hi, *thi;
588
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000589 if (hash->nprime == ARRAY_SIZE(PRIMES))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000590 return;
591
592 newsize = PRIMES[hash->nprime++];
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100593 newitems = xzalloc(newsize * sizeof(newitems[0]));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000594
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000595 for (i = 0; i < hash->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000596 hi = hash->items[i];
597 while (hi) {
598 thi = hi;
599 hi = thi->next;
600 idx = hashidx(thi->name) % newsize;
601 thi->next = newitems[idx];
602 newitems[idx] = thi;
603 }
604 }
605
606 free(hash->items);
607 hash->csize = newsize;
608 hash->items = newitems;
609}
610
611/* find item in hash, add it if necessary. Return ptr to data */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000612static void *hash_find(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000613{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000614 hash_item *hi;
Denis Vlasenkof782f522007-01-01 23:51:30 +0000615 unsigned idx;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000616 int l;
617
618 hi = hash_search(hash, name);
Denis Vlasenkob78c7822007-07-18 18:31:11 +0000619 if (!hi) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000620 if (++hash->nel / hash->csize > 10)
621 hash_rebuild(hash);
622
Rob Landleya3896512006-05-07 20:20:34 +0000623 l = strlen(name) + 1;
Denis Vlasenko7a676642009-03-15 22:20:31 +0000624 hi = xzalloc(sizeof(*hi) + l);
625 strcpy(hi->name, name);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000626
627 idx = hashidx(name) % hash->csize;
628 hi->next = hash->items[idx];
629 hash->items[idx] = hi;
630 hash->glen += l;
631 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100632 return &hi->data;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000633}
634
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000635#define findvar(hash, name) ((var*) hash_find((hash), (name)))
636#define newvar(name) ((var*) hash_find(vhash, (name)))
637#define newfile(name) ((rstream*)hash_find(fdhash, (name)))
638#define newfunc(name) ((func*) hash_find(fnhash, (name)))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000639
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000640static void hash_remove(xhash *hash, const char *name)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000641{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000642 hash_item *hi, **phi;
643
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100644 phi = &hash->items[hashidx(name) % hash->csize];
Glenn L McGrath545106f2002-11-11 06:21:00 +0000645 while (*phi) {
646 hi = *phi;
647 if (strcmp(hi->name, name) == 0) {
Rob Landleya3896512006-05-07 20:20:34 +0000648 hash->glen -= (strlen(name) + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000649 hash->nel--;
650 *phi = hi->next;
651 free(hi);
652 break;
653 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +0100654 phi = &hi->next;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000655 }
656}
657
658/* ------ some useful functions ------ */
659
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100660static char *skip_spaces(char *p)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000661{
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000662 while (1) {
663 if (*p == '\\' && p[1] == '\n') {
664 p++;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000665 t_lineno++;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000666 } else if (*p != ' ' && *p != '\t') {
667 break;
668 }
Mike Frysingerde2b9382005-09-27 03:18:00 +0000669 p++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000670 }
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100671 return p;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000672}
673
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100674/* returns old *s, advances *s past word and terminating NUL */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000675static char *nextword(char **s)
676{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000677 char *p = *s;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100678 while (*(*s)++ != '\0')
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100679 continue;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000680 return p;
681}
682
Mike Frysinger10a11e22005-09-27 02:23:02 +0000683static char nextchar(char **s)
684{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000685 char c, *pps;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000686
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100687 c = *(*s)++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000688 pps = *s;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100689 if (c == '\\')
690 c = bb_process_escape_sequence((const char**)s);
691 if (c == '\\' && *s == pps)
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100692 c = *(*s)++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000693 return c;
694}
695
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000696static ALWAYS_INLINE int isalnum_(int c)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000697{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000698 return (isalnum(c) || c == '_');
699}
700
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000701static double my_strtod(char **pp)
702{
703#if ENABLE_DESKTOP
704 if ((*pp)[0] == '0'
705 && ((((*pp)[1] | 0x20) == 'x') || isdigit((*pp)[1]))
706 ) {
707 return strtoull(*pp, pp, 0);
708 }
709#endif
710 return strtod(*pp, pp);
711}
712
Glenn L McGrath545106f2002-11-11 06:21:00 +0000713/* -------- working with variables (set/get/copy/etc) -------- */
714
Mike Frysinger10a11e22005-09-27 02:23:02 +0000715static xhash *iamarray(var *v)
716{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000717 var *a = v;
718
719 while (a->type & VF_CHILD)
720 a = a->x.parent;
721
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000722 if (!(a->type & VF_ARRAY)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000723 a->type |= VF_ARRAY;
724 a->x.array = hash_init();
725 }
726 return a->x.array;
727}
728
Mike Frysinger10a11e22005-09-27 02:23:02 +0000729static void clear_array(xhash *array)
730{
Denis Vlasenkof782f522007-01-01 23:51:30 +0000731 unsigned i;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000732 hash_item *hi, *thi;
733
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000734 for (i = 0; i < array->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000735 hi = array->items[i];
736 while (hi) {
737 thi = hi;
738 hi = hi->next;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000739 free(thi->data.v.string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000740 free(thi);
741 }
742 array->items[i] = NULL;
743 }
744 array->glen = array->nel = 0;
745}
746
747/* clear a variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000748static var *clrvar(var *v)
749{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000750 if (!(v->type & VF_FSTR))
Glenn L McGrath545106f2002-11-11 06:21:00 +0000751 free(v->string);
752
753 v->type &= VF_DONTTOUCH;
754 v->type |= VF_DIRTY;
755 v->string = NULL;
756 return v;
757}
758
759/* assign string value to variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000760static var *setvar_p(var *v, char *value)
761{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000762 clrvar(v);
763 v->string = value;
764 handle_special(v);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000765 return v;
766}
767
768/* same as setvar_p but make a copy of string */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000769static var *setvar_s(var *v, const char *value)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000770{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000771 return setvar_p(v, (value && *value) ? xstrdup(value) : NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000772}
773
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100774/* same as setvar_s but sets USER flag */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000775static var *setvar_u(var *v, const char *value)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000776{
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100777 v = setvar_s(v, value);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000778 v->type |= VF_USER;
779 return v;
780}
781
782/* set array element to user string */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000783static void setari_u(var *a, int idx, const char *s)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000784{
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000785 var *v;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000786
Denys Vlasenko7bb346f2009-10-06 22:09:50 +0200787 v = findvar(iamarray(a), itoa(idx));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000788 setvar_u(v, s);
789}
790
791/* assign numeric value to variable */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000792static var *setvar_i(var *v, double value)
793{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000794 clrvar(v);
795 v->type |= VF_NUMBER;
796 v->number = value;
797 handle_special(v);
798 return v;
799}
800
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000801static const char *getvar_s(var *v)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000802{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000803 /* if v is numeric and has no cached string, convert it to string */
804 if ((v->type & (VF_NUMBER | VF_CACHED)) == VF_NUMBER) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000805 fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[CONVFMT]), v->number, TRUE);
806 v->string = xstrdup(g_buf);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000807 v->type |= VF_CACHED;
808 }
809 return (v->string == NULL) ? "" : v->string;
810}
811
Mike Frysinger10a11e22005-09-27 02:23:02 +0000812static double getvar_i(var *v)
813{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000814 char *s;
815
816 if ((v->type & (VF_NUMBER | VF_CACHED)) == 0) {
817 v->number = 0;
818 s = v->string;
819 if (s && *s) {
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000820 v->number = my_strtod(&s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000821 if (v->type & VF_USER) {
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100822 s = skip_spaces(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000823 if (*s != '\0')
824 v->type &= ~VF_USER;
825 }
826 } else {
827 v->type &= ~VF_USER;
828 }
829 v->type |= VF_CACHED;
830 }
831 return v->number;
832}
833
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000834/* Used for operands of bitwise ops */
835static unsigned long getvar_i_int(var *v)
836{
837 double d = getvar_i(v);
838
839 /* Casting doubles to longs is undefined for values outside
840 * of target type range. Try to widen it as much as possible */
841 if (d >= 0)
842 return (unsigned long)d;
Denis Vlasenko665eaff2008-09-05 04:59:02 +0000843 /* Why? Think about d == -4294967295.0 (assuming 32bit longs) */
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +0000844 return - (long) (unsigned long) (-d);
845}
846
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +0000847static var *copyvar(var *dest, const var *src)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000848{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000849 if (dest != src) {
850 clrvar(dest);
Denis Vlasenko629563b2007-02-24 17:05:52 +0000851 dest->type |= (src->type & ~(VF_DONTTOUCH | VF_FSTR));
Glenn L McGrath545106f2002-11-11 06:21:00 +0000852 dest->number = src->number;
853 if (src->string)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000854 dest->string = xstrdup(src->string);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000855 }
856 handle_special(dest);
857 return dest;
858}
859
Mike Frysinger10a11e22005-09-27 02:23:02 +0000860static var *incvar(var *v)
861{
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100862 return setvar_i(v, getvar_i(v) + 1.0);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000863}
864
865/* return true if v is number or numeric string */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000866static int is_numeric(var *v)
867{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000868 getvar_i(v);
869 return ((v->type ^ VF_DIRTY) & (VF_NUMBER | VF_USER | VF_DIRTY));
870}
871
872/* return 1 when value of v corresponds to true, 0 otherwise */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000873static int istrue(var *v)
874{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000875 if (is_numeric(v))
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100876 return (v->number != 0);
877 return (v->string && v->string[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000878}
879
Eric Andersenaff114c2004-04-14 17:51:38 +0000880/* temporary variables allocator. Last allocated should be first freed */
Mike Frysinger10a11e22005-09-27 02:23:02 +0000881static var *nvalloc(int n)
882{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000883 nvblock *pb = NULL;
884 var *v, *r;
885 int size;
886
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000887 while (g_cb) {
888 pb = g_cb;
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100889 if ((g_cb->pos - g_cb->nv) + n <= g_cb->size)
890 break;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000891 g_cb = g_cb->next;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000892 }
893
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000894 if (!g_cb) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000895 size = (n <= MINNVBLOCK) ? MINNVBLOCK : n;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000896 g_cb = xzalloc(sizeof(nvblock) + size * sizeof(var));
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000897 g_cb->size = size;
898 g_cb->pos = g_cb->nv;
899 g_cb->prev = pb;
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +0000900 /*g_cb->next = NULL; - xzalloc did it */
Denys Vlasenkocdeda162009-11-30 01:14:16 +0100901 if (pb)
902 pb->next = g_cb;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000903 }
904
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000905 v = r = g_cb->pos;
906 g_cb->pos += n;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000907
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000908 while (v < g_cb->pos) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000909 v->type = 0;
910 v->string = NULL;
911 v++;
912 }
913
914 return r;
915}
916
Mike Frysinger10a11e22005-09-27 02:23:02 +0000917static void nvfree(var *v)
918{
Glenn L McGrath545106f2002-11-11 06:21:00 +0000919 var *p;
920
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000921 if (v < g_cb->nv || v >= g_cb->pos)
922 syntax_error(EMSG_INTERNAL_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000923
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000924 for (p = v; p < g_cb->pos; p++) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000925 if ((p->type & (VF_ARRAY | VF_CHILD)) == VF_ARRAY) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000926 clear_array(iamarray(p));
927 free(p->x.array->items);
928 free(p->x.array);
929 }
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100930 if (p->type & VF_WALK) {
Denys Vlasenkoda62b092010-03-11 12:13:18 +0100931 walker_list *n;
932 walker_list *w = p->x.walker;
933 debug_printf_walker("nvfree: freeing walker @%p\n", &p->x.walker);
934 p->x.walker = NULL;
935 while (w) {
936 n = w->prev;
937 debug_printf_walker(" free(%p)\n", w);
938 free(w);
939 w = n;
940 }
Denys Vlasenko3cb60c32010-03-10 19:20:32 +0100941 }
Glenn L McGrath545106f2002-11-11 06:21:00 +0000942 clrvar(p);
943 }
944
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000945 g_cb->pos = v;
946 while (g_cb->prev && g_cb->pos == g_cb->nv) {
947 g_cb = g_cb->prev;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000948 }
949}
950
951/* ------- awk program text parsing ------- */
952
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000953/* Parse next token pointed by global pos, place results into global ttt.
Glenn L McGrath545106f2002-11-11 06:21:00 +0000954 * If token isn't expected, give away. Return token class
955 */
Mike Frysingerf87b3e32005-09-27 04:16:22 +0000956static uint32_t next_token(uint32_t expected)
Mike Frysinger10a11e22005-09-27 02:23:02 +0000957{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000958#define concat_inserted (G.next_token__concat_inserted)
959#define save_tclass (G.next_token__save_tclass)
960#define save_info (G.next_token__save_info)
961/* Initialized to TC_OPTERM: */
962#define ltclass (G.next_token__ltclass)
Glenn L McGrath545106f2002-11-11 06:21:00 +0000963
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100964 char *p, *s;
Denis Vlasenkof782f522007-01-01 23:51:30 +0000965 const char *tl;
966 uint32_t tc;
967 const uint32_t *ti;
968 int l;
969
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000970 if (t_rollback) {
971 t_rollback = FALSE;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000972
973 } else if (concat_inserted) {
Glenn L McGrath545106f2002-11-11 06:21:00 +0000974 concat_inserted = FALSE;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000975 t_tclass = save_tclass;
976 t_info = save_info;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000977
978 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000979 p = g_pos;
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +0000980 readnext:
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +0100981 p = skip_spaces(p);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000982 g_lineno = t_lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000983 if (*p == '#')
Denis Vlasenkoffba9412007-05-17 23:03:35 +0000984 while (*p != '\n' && *p != '\0')
985 p++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000986
987 if (*p == '\n')
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000988 t_lineno++;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000989
990 if (*p == '\0') {
991 tc = TC_EOF;
992
993 } else if (*p == '\"') {
994 /* it's a string */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +0000995 t_string = s = ++p;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000996 while (*p != '\"') {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +0100997 char *pp = p;
Glenn L McGrath545106f2002-11-11 06:21:00 +0000998 if (*p == '\0' || *p == '\n')
999 syntax_error(EMSG_UNEXP_EOS);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001000 *s++ = nextchar(&pp);
1001 p = pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001002 }
1003 p++;
1004 *s = '\0';
1005 tc = TC_STRING;
1006
1007 } else if ((expected & TC_REGEXP) && *p == '/') {
1008 /* it's regexp */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001009 t_string = s = ++p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001010 while (*p != '/') {
1011 if (*p == '\0' || *p == '\n')
1012 syntax_error(EMSG_UNEXP_EOS);
Denis Vlasenkod9b5ab82007-05-18 07:30:43 +00001013 *s = *p++;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001014 if (*s++ == '\\') {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001015 char *pp = p;
1016 s[-1] = bb_process_escape_sequence((const char **)&pp);
1017 if (*p == '\\')
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001018 *s++ = '\\';
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001019 if (pp == p)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001020 *s++ = *p++;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001021 else
1022 p = pp;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001023 }
1024 }
1025 p++;
1026 *s = '\0';
1027 tc = TC_REGEXP;
1028
1029 } else if (*p == '.' || isdigit(*p)) {
1030 /* it's a number */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001031 char *pp = p;
1032 t_double = my_strtod(&pp);
1033 p = pp;
1034 if (*pp == '.')
Glenn L McGrath545106f2002-11-11 06:21:00 +00001035 syntax_error(EMSG_UNEXP_TOKEN);
1036 tc = TC_NUMBER;
1037
1038 } else {
1039 /* search for something known */
1040 tl = tokenlist;
1041 tc = 0x00000001;
1042 ti = tokeninfo;
1043 while (*tl) {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001044 l = *tl++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001045 if (l == NTCC) {
1046 tc <<= 1;
1047 continue;
1048 }
1049 /* if token class is expected, token
1050 * matches and it's not a longer word,
1051 * then this is what we are looking for
1052 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001053 if ((tc & (expected | TC_WORD | TC_NEWLINE))
1054 && *tl == *p && strncmp(p, tl, l) == 0
1055 && !((tc & TC_WORD) && isalnum_(p[l]))
1056 ) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001057 t_info = *ti;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001058 p += l;
1059 break;
1060 }
1061 ti++;
1062 tl += l;
1063 }
1064
Denis Vlasenkof782f522007-01-01 23:51:30 +00001065 if (!*tl) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001066 /* it's a name (var/array/function),
1067 * otherwise it's something wrong
1068 */
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001069 if (!isalnum_(*p))
Glenn L McGrath545106f2002-11-11 06:21:00 +00001070 syntax_error(EMSG_UNEXP_TOKEN);
1071
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001072 t_string = --p;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001073 while (isalnum_(*++p)) {
1074 p[-1] = *p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001075 }
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001076 p[-1] = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001077 tc = TC_VARIABLE;
Bernhard Reutner-Fischerbb204622005-10-17 14:21:06 +00001078 /* also consume whitespace between functionname and bracket */
Alexander Shishkind03cd3b2010-02-25 17:55:40 +02001079 if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY))
Denys Vlasenkob0a57ab2010-03-11 12:44:25 +01001080 p = skip_spaces(p);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001081 if (*p == '(') {
1082 tc = TC_FUNCTION;
1083 } else {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001084 if (*p == '[') {
1085 p++;
1086 tc = TC_ARRAY;
1087 }
1088 }
1089 }
1090 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001091 g_pos = p;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001092
1093 /* skipping newlines in some cases */
1094 if ((ltclass & TC_NOTERM) && (tc & TC_NEWLINE))
1095 goto readnext;
1096
1097 /* insert concatenation operator when needed */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001098 if ((ltclass & TC_CONCAT1) && (tc & TC_CONCAT2) && (expected & TC_BINOP)) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001099 concat_inserted = TRUE;
1100 save_tclass = tc;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001101 save_info = t_info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001102 tc = TC_BINOP;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001103 t_info = OC_CONCAT | SS | P(35);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001104 }
1105
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001106 t_tclass = tc;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001107 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001108 ltclass = t_tclass;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001109
1110 /* Are we ready for this? */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001111 if (!(ltclass & expected))
Glenn L McGrath545106f2002-11-11 06:21:00 +00001112 syntax_error((ltclass & (TC_NEWLINE | TC_EOF)) ?
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001113 EMSG_UNEXP_EOS : EMSG_UNEXP_TOKEN);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001114
1115 return ltclass;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001116#undef concat_inserted
1117#undef save_tclass
1118#undef save_info
1119#undef ltclass
Glenn L McGrath545106f2002-11-11 06:21:00 +00001120}
1121
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001122static void rollback_token(void)
1123{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001124 t_rollback = TRUE;
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001125}
Glenn L McGrath545106f2002-11-11 06:21:00 +00001126
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001127static node *new_node(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001128{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001129 node *n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001130
Denis Vlasenko4cccc032006-12-22 18:37:07 +00001131 n = xzalloc(sizeof(node));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001132 n->info = info;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001133 n->lineno = g_lineno;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001134 return n;
1135}
1136
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001137static void mk_re_node(const char *s, node *n, regex_t *re)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001138{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001139 n->info = OC_REGEXP;
1140 n->l.re = re;
1141 n->r.ire = re + 1;
1142 xregcomp(re, s, REG_EXTENDED);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001143 xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001144}
1145
Mike Frysinger10a11e22005-09-27 02:23:02 +00001146static node *condition(void)
1147{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001148 next_token(TC_SEQSTART);
1149 return parse_expr(TC_SEQTERM);
1150}
1151
1152/* parse expression terminated by given argument, return ptr
1153 * to built subtree. Terminator is eaten by parse_expr */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001154static node *parse_expr(uint32_t iexp)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001155{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001156 node sn;
1157 node *cn = &sn;
1158 node *vn, *glptr;
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001159 uint32_t tc, xtc;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001160 var *v;
1161
1162 sn.info = PRIMASK;
1163 sn.r.n = glptr = NULL;
1164 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp;
1165
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001166 while (!((tc = next_token(xtc)) & iexp)) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001167 if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001168 /* input redirection (<) attached to glptr node */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001169 cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37));
Glenn L McGrath4bded582004-02-22 11:55:09 +00001170 cn->a.n = glptr;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001171 xtc = TC_OPERAND | TC_UOPPRE;
1172 glptr = NULL;
1173
1174 } else if (tc & (TC_BINOP | TC_UOPPOST)) {
1175 /* for binary and postfix-unary operators, jump back over
1176 * previous operators with higher priority */
1177 vn = cn;
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001178 while (((t_info & PRIMASK) > (vn->a.n->info & PRIMASK2))
1179 || ((t_info == vn->info) && ((t_info & OPCLSMASK) == OC_COLON))
1180 ) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001181 vn = vn->a.n;
Denys Vlasenkocdeda162009-11-30 01:14:16 +01001182 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001183 if ((t_info & OPCLSMASK) == OC_TERNARY)
1184 t_info += P(6);
1185 cn = vn->a.n->r.n = new_node(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001186 cn->a.n = vn->a.n;
1187 if (tc & TC_BINOP) {
1188 cn->l.n = vn;
1189 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001190 if ((t_info & OPCLSMASK) == OC_PGETLINE) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001191 /* it's a pipe */
1192 next_token(TC_GETLINE);
1193 /* give maximum priority to this pipe */
1194 cn->info &= ~PRIMASK;
1195 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1196 }
1197 } else {
1198 cn->r.n = vn;
1199 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1200 }
1201 vn->a.n = cn;
1202
1203 } else {
1204 /* for operands and prefix-unary operators, attach them
1205 * to last node */
1206 vn = cn;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001207 cn = vn->r.n = new_node(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001208 cn->a.n = vn;
1209 xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
1210 if (tc & (TC_OPERAND | TC_REGEXP)) {
Rob Landleyed830e82005-06-07 02:43:52 +00001211 xtc = TC_UOPPRE | TC_UOPPOST | TC_BINOP | TC_OPERAND | iexp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001212 /* one should be very careful with switch on tclass -
Glenn L McGrath545106f2002-11-11 06:21:00 +00001213 * only simple tclasses should be used! */
1214 switch (tc) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00001215 case TC_VARIABLE:
1216 case TC_ARRAY:
Glenn L McGrath545106f2002-11-11 06:21:00 +00001217 cn->info = OC_VAR;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001218 v = hash_search(ahash, t_string);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001219 if (v != NULL) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001220 cn->info = OC_FNARG;
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001221 cn->l.aidx = v->x.aidx;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001222 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001223 cn->l.v = newvar(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001224 }
1225 if (tc & TC_ARRAY) {
1226 cn->info |= xS;
1227 cn->r.n = parse_expr(TC_ARRTERM);
1228 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001229 break;
Mike Frysingerde2b9382005-09-27 03:18:00 +00001230
Denis Vlasenkof782f522007-01-01 23:51:30 +00001231 case TC_NUMBER:
1232 case TC_STRING:
Glenn L McGrath545106f2002-11-11 06:21:00 +00001233 cn->info = OC_VAR;
Rob Landley9ffd4232006-05-21 18:30:35 +00001234 v = cn->l.v = xzalloc(sizeof(var));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001235 if (tc & TC_NUMBER)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001236 setvar_i(v, t_double);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001237 else
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001238 setvar_s(v, t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001239 break;
1240
Denis Vlasenkof782f522007-01-01 23:51:30 +00001241 case TC_REGEXP:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001242 mk_re_node(t_string, cn, xzalloc(sizeof(regex_t)*2));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001243 break;
1244
Denis Vlasenkof782f522007-01-01 23:51:30 +00001245 case TC_FUNCTION:
Mike Frysingerde2b9382005-09-27 03:18:00 +00001246 cn->info = OC_FUNC;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001247 cn->r.f = newfunc(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001248 cn->l.n = condition();
1249 break;
1250
Denis Vlasenkof782f522007-01-01 23:51:30 +00001251 case TC_SEQSTART:
Glenn L McGrath545106f2002-11-11 06:21:00 +00001252 cn = vn->r.n = parse_expr(TC_SEQTERM);
1253 cn->a.n = vn;
1254 break;
1255
Denis Vlasenkof782f522007-01-01 23:51:30 +00001256 case TC_GETLINE:
Glenn L McGrath545106f2002-11-11 06:21:00 +00001257 glptr = cn;
1258 xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
1259 break;
1260
Denis Vlasenkof782f522007-01-01 23:51:30 +00001261 case TC_BUILTIN:
Glenn L McGrath545106f2002-11-11 06:21:00 +00001262 cn->l.n = condition();
1263 break;
1264 }
1265 }
1266 }
1267 }
1268 return sn.r.n;
1269}
1270
1271/* add node to chain. Return ptr to alloc'd node */
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001272static node *chain_node(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001273{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001274 node *n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001275
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00001276 if (!seq->first)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001277 seq->first = seq->last = new_node(0);
1278
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001279 if (seq->programname != g_progname) {
1280 seq->programname = g_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001281 n = chain_node(OC_NEWSOURCE);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001282 n->l.new_progname = xstrdup(g_progname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001283 }
1284
1285 n = seq->last;
1286 n->info = info;
1287 seq->last = n->a.n = new_node(OC_DONE);
1288
1289 return n;
1290}
1291
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001292static void chain_expr(uint32_t info)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001293{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001294 node *n;
1295
1296 n = chain_node(info);
1297 n->l.n = parse_expr(TC_OPTERM | TC_GRPTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001298 if (t_tclass & TC_GRPTERM)
Glenn L McGrath545106f2002-11-11 06:21:00 +00001299 rollback_token();
1300}
1301
Mike Frysinger10a11e22005-09-27 02:23:02 +00001302static node *chain_loop(node *nn)
1303{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001304 node *n, *n2, *save_brk, *save_cont;
1305
1306 save_brk = break_ptr;
1307 save_cont = continue_ptr;
1308
1309 n = chain_node(OC_BR | Vx);
1310 continue_ptr = new_node(OC_EXEC);
1311 break_ptr = new_node(OC_EXEC);
1312 chain_group();
1313 n2 = chain_node(OC_EXEC | Vx);
1314 n2->l.n = nn;
1315 n2->a.n = n;
1316 continue_ptr->a.n = n2;
1317 break_ptr->a.n = n->r.n = seq->last;
1318
1319 continue_ptr = save_cont;
1320 break_ptr = save_brk;
1321
1322 return n;
1323}
1324
1325/* parse group and attach it to chain */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001326static void chain_group(void)
1327{
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001328 uint32_t c;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001329 node *n, *n2, *n3;
1330
1331 do {
1332 c = next_token(TC_GRPSEQ);
1333 } while (c & TC_NEWLINE);
1334
1335 if (c & TC_GRPSTART) {
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001336 while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001337 if (t_tclass & TC_NEWLINE)
1338 continue;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001339 rollback_token();
1340 chain_group();
1341 }
1342 } else if (c & (TC_OPSEQ | TC_OPTERM)) {
1343 rollback_token();
1344 chain_expr(OC_EXEC | Vx);
1345 } else { /* TC_STATEMNT */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001346 switch (t_info & OPCLSMASK) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001347 case ST_IF:
1348 n = chain_node(OC_BR | Vx);
1349 n->l.n = condition();
1350 chain_group();
1351 n2 = chain_node(OC_EXEC);
1352 n->r.n = seq->last;
1353 if (next_token(TC_GRPSEQ | TC_GRPTERM | TC_ELSE) == TC_ELSE) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001354 chain_group();
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001355 n2->a.n = seq->last;
1356 } else {
1357 rollback_token();
1358 }
1359 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001360
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001361 case ST_WHILE:
1362 n2 = condition();
1363 n = chain_loop(NULL);
1364 n->l.n = n2;
1365 break;
1366
1367 case ST_DO:
1368 n2 = chain_node(OC_EXEC);
1369 n = chain_loop(NULL);
1370 n2->a.n = n->a.n;
1371 next_token(TC_WHILE);
1372 n->l.n = condition();
1373 break;
1374
1375 case ST_FOR:
1376 next_token(TC_SEQSTART);
1377 n2 = parse_expr(TC_SEMICOL | TC_SEQTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001378 if (t_tclass & TC_SEQTERM) { /* for-in */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001379 if ((n2->info & OPCLSMASK) != OC_IN)
1380 syntax_error(EMSG_UNEXP_TOKEN);
1381 n = chain_node(OC_WALKINIT | VV);
1382 n->l.n = n2->l.n;
1383 n->r.n = n2->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001384 n = chain_loop(NULL);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001385 n->info = OC_WALKNEXT | Vx;
1386 n->l.n = n2->l.n;
1387 } else { /* for (;;) */
1388 n = chain_node(OC_EXEC | Vx);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001389 n->l.n = n2;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001390 n2 = parse_expr(TC_SEMICOL);
1391 n3 = parse_expr(TC_SEQTERM);
1392 n = chain_loop(n3);
1393 n->l.n = n2;
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001394 if (!n2)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001395 n->info = OC_EXEC;
1396 }
1397 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001398
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001399 case OC_PRINT:
1400 case OC_PRINTF:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001401 n = chain_node(t_info);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001402 n->l.n = parse_expr(TC_OPTERM | TC_OUTRDR | TC_GRPTERM);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001403 if (t_tclass & TC_OUTRDR) {
1404 n->info |= t_info;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001405 n->r.n = parse_expr(TC_OPTERM | TC_GRPTERM);
1406 }
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001407 if (t_tclass & TC_GRPTERM)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001408 rollback_token();
1409 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001410
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001411 case OC_BREAK:
1412 n = chain_node(OC_EXEC);
1413 n->a.n = break_ptr;
1414 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001415
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001416 case OC_CONTINUE:
1417 n = chain_node(OC_EXEC);
1418 n->a.n = continue_ptr;
1419 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001420
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001421 /* delete, next, nextfile, return, exit */
1422 default:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001423 chain_expr(t_info);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001424 }
1425 }
1426}
1427
Mike Frysinger10a11e22005-09-27 02:23:02 +00001428static void parse_program(char *p)
1429{
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001430 uint32_t tclass;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001431 node *cn;
1432 func *f;
1433 var *v;
1434
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001435 g_pos = p;
1436 t_lineno = 1;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001437 while ((tclass = next_token(TC_EOF | TC_OPSEQ | TC_GRPSTART |
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001438 TC_OPTERM | TC_BEGIN | TC_END | TC_FUNCDECL)) != TC_EOF) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001439
1440 if (tclass & TC_OPTERM)
1441 continue;
1442
1443 seq = &mainseq;
1444 if (tclass & TC_BEGIN) {
1445 seq = &beginseq;
1446 chain_group();
1447
1448 } else if (tclass & TC_END) {
1449 seq = &endseq;
1450 chain_group();
1451
1452 } else if (tclass & TC_FUNCDECL) {
1453 next_token(TC_FUNCTION);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001454 g_pos++;
1455 f = newfunc(t_string);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001456 f->body.first = NULL;
1457 f->nargs = 0;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001458 while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001459 v = findvar(ahash, t_string);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001460 v->x.aidx = f->nargs++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001461
1462 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
1463 break;
1464 }
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001465 seq = &f->body;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001466 chain_group();
1467 clear_array(ahash);
1468
1469 } else if (tclass & TC_OPSEQ) {
1470 rollback_token();
1471 cn = chain_node(OC_TEST);
1472 cn->l.n = parse_expr(TC_OPTERM | TC_EOF | TC_GRPSTART);
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001473 if (t_tclass & TC_GRPSTART) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001474 rollback_token();
1475 chain_group();
1476 } else {
1477 chain_node(OC_PRINT);
1478 }
1479 cn->r.n = mainseq.last;
1480
1481 } else /* if (tclass & TC_GRPSTART) */ {
1482 rollback_token();
1483 chain_group();
1484 }
1485 }
1486}
1487
1488
1489/* -------- program execution part -------- */
1490
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001491static node *mk_splitter(const char *s, tsplitter *spl)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001492{
"Robert P. J. Day"68229832006-07-01 13:08:46 +00001493 regex_t *re, *ire;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001494 node *n;
1495
1496 re = &spl->re[0];
1497 ire = &spl->re[1];
1498 n = &spl->n;
Denis Vlasenko890ac9d2006-10-07 15:16:19 +00001499 if ((n->info & OPCLSMASK) == OC_REGEXP) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001500 regfree(re);
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001501 regfree(ire); // TODO: nuke ire, use re+1?
Glenn L McGrath545106f2002-11-11 06:21:00 +00001502 }
Rob Landleya3896512006-05-07 20:20:34 +00001503 if (strlen(s) > 1) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001504 mk_re_node(s, n, re);
1505 } else {
Mike Frysingerf87b3e32005-09-27 04:16:22 +00001506 n->info = (uint32_t) *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001507 }
1508
1509 return n;
1510}
1511
1512/* use node as a regular expression. Supplied with node ptr and regex_t
Eric Andersenaff114c2004-04-14 17:51:38 +00001513 * storage space. Return ptr to regex (if result points to preg, it should
Glenn L McGrath545106f2002-11-11 06:21:00 +00001514 * be later regfree'd manually
1515 */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001516static regex_t *as_regex(node *op, regex_t *preg)
1517{
Denis Vlasenko7a676642009-03-15 22:20:31 +00001518 int cflags;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001519 var *v;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001520 const char *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001521
1522 if ((op->info & OPCLSMASK) == OC_REGEXP) {
1523 return icase ? op->r.ire : op->l.re;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001524 }
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001525 v = nvalloc(1);
1526 s = getvar_s(evaluate(op, v));
Denis Vlasenko7a676642009-03-15 22:20:31 +00001527
1528 cflags = icase ? REG_EXTENDED | REG_ICASE : REG_EXTENDED;
1529 /* Testcase where REG_EXTENDED fails (unpaired '{'):
1530 * echo Hi | awk 'gsub("@(samp|code|file)\{","");'
1531 * gawk 3.1.5 eats this. We revert to ~REG_EXTENDED
1532 * (maybe gsub is not supposed to use REG_EXTENDED?).
1533 */
1534 if (regcomp(preg, s, cflags)) {
1535 cflags &= ~REG_EXTENDED;
1536 xregcomp(preg, s, cflags);
1537 }
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001538 nvfree(v);
1539 return preg;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001540}
1541
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001542/* gradually increasing buffer.
1543 * note that we reallocate even if n == old_size,
1544 * and thus there is at least one extra allocated byte.
1545 */
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001546static char* qrealloc(char *b, int n, int *size)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001547{
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001548 if (!b || n >= *size) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001549 *size = n + (n>>1) + 80;
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001550 b = xrealloc(b, *size);
Denis Vlasenkodeeed592008-07-08 05:14:36 +00001551 }
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001552 return b;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001553}
1554
1555/* resize field storage space */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001556static void fsrealloc(int size)
1557{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001558 int i;
1559
1560 if (size >= maxfields) {
1561 i = maxfields;
1562 maxfields = size + 16;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001563 Fields = xrealloc(Fields, maxfields * sizeof(var));
1564 for (; i < maxfields; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001565 Fields[i].type = VF_SPECIAL;
1566 Fields[i].string = NULL;
1567 }
1568 }
1569
1570 if (size < nfields) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001571 for (i = size; i < nfields; i++) {
1572 clrvar(Fields + i);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001573 }
1574 }
1575 nfields = size;
1576}
1577
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001578static int awk_split(const char *s, node *spl, char **slist)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001579{
Denis Vlasenkof782f522007-01-01 23:51:30 +00001580 int l, n = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001581 char c[4];
1582 char *s1;
Denis Vlasenkob78c7822007-07-18 18:31:11 +00001583 regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough...
Glenn L McGrath545106f2002-11-11 06:21:00 +00001584
1585 /* in worst case, each char would be a separate field */
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001586 *slist = s1 = xzalloc(strlen(s) * 2 + 3);
1587 strcpy(s1, s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001588
1589 c[0] = c[1] = (char)spl->info;
1590 c[2] = c[3] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001591 if (*getvar_s(intvar[RS]) == '\0')
1592 c[2] = '\n';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001593
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001594 if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
1595 if (!*s)
1596 return n; /* "": zero fields */
1597 n++; /* at least one field will be there */
1598 do {
1599 l = strcspn(s, c+2); /* len till next NUL or \n */
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001600 if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0
1601 && pmatch[0].rm_so <= l
1602 ) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001603 l = pmatch[0].rm_so;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001604 if (pmatch[0].rm_eo == 0) {
1605 l++;
1606 pmatch[0].rm_eo++;
1607 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001608 n++; /* we saw yet another delimiter */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001609 } else {
1610 pmatch[0].rm_eo = l;
Denys Vlasenkoe4244232009-05-18 23:50:03 +02001611 if (s[l])
1612 pmatch[0].rm_eo++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001613 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001614 memcpy(s1, s, l);
Denis Vlasenko67b5eeb2009-04-12 13:54:13 +00001615 /* make sure we remove *all* of the separator chars */
Denys Vlasenkoe4244232009-05-18 23:50:03 +02001616 do {
1617 s1[l] = '\0';
1618 } while (++l < pmatch[0].rm_eo);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001619 nextword(&s1);
1620 s += pmatch[0].rm_eo;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001621 } while (*s);
1622 return n;
1623 }
1624 if (c[0] == '\0') { /* null split */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001625 while (*s) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001626 *s1++ = *s++;
1627 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001628 n++;
1629 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001630 return n;
1631 }
1632 if (c[0] != ' ') { /* single-character split */
Glenn L McGrath545106f2002-11-11 06:21:00 +00001633 if (icase) {
1634 c[0] = toupper(c[0]);
1635 c[1] = tolower(c[1]);
1636 }
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001637 if (*s1)
1638 n++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001639 while ((s1 = strpbrk(s1, c))) {
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001640 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001641 n++;
1642 }
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001643 return n;
1644 }
1645 /* space split */
1646 while (*s) {
1647 s = skip_whitespace(s);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001648 if (!*s)
1649 break;
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001650 n++;
1651 while (*s && !isspace(*s))
1652 *s1++ = *s++;
1653 *s1++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001654 }
1655 return n;
1656}
1657
Mike Frysinger10a11e22005-09-27 02:23:02 +00001658static void split_f0(void)
1659{
Denis Vlasenkoaf1bd092007-07-18 18:32:25 +00001660/* static char *fstrings; */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001661#define fstrings (G.split_f0__fstrings)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001662
Glenn L McGrath545106f2002-11-11 06:21:00 +00001663 int i, n;
1664 char *s;
1665
1666 if (is_f0_split)
1667 return;
1668
1669 is_f0_split = TRUE;
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001670 free(fstrings);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001671 fsrealloc(0);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001672 n = awk_split(getvar_s(intvar[F0]), &fsplitter.n, &fstrings);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001673 fsrealloc(n);
1674 s = fstrings;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001675 for (i = 0; i < n; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001676 Fields[i].string = nextword(&s);
1677 Fields[i].type |= (VF_FSTR | VF_USER | VF_DIRTY);
1678 }
1679
1680 /* set NF manually to avoid side effects */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001681 clrvar(intvar[NF]);
1682 intvar[NF]->type = VF_NUMBER | VF_SPECIAL;
1683 intvar[NF]->number = nfields;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001684#undef fstrings
Glenn L McGrath545106f2002-11-11 06:21:00 +00001685}
1686
1687/* perform additional actions when some internal variables changed */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001688static void handle_special(var *v)
1689{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001690 int n;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001691 char *b;
1692 const char *sep, *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001693 int sl, l, len, i, bsize;
1694
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001695 if (!(v->type & VF_SPECIAL))
Glenn L McGrath545106f2002-11-11 06:21:00 +00001696 return;
1697
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001698 if (v == intvar[NF]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001699 n = (int)getvar_i(v);
1700 fsrealloc(n);
1701
1702 /* recalculate $0 */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001703 sep = getvar_s(intvar[OFS]);
Rob Landleya3896512006-05-07 20:20:34 +00001704 sl = strlen(sep);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001705 b = NULL;
1706 len = 0;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001707 for (i = 0; i < n; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001708 s = getvar_s(&Fields[i]);
Rob Landleya3896512006-05-07 20:20:34 +00001709 l = strlen(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001710 if (b) {
1711 memcpy(b+len, sep, sl);
1712 len += sl;
1713 }
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001714 b = qrealloc(b, len+l+sl, &bsize);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001715 memcpy(b+len, s, l);
1716 len += l;
1717 }
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001718 if (b)
1719 b[len] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001720 setvar_p(intvar[F0], b);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001721 is_f0_split = TRUE;
1722
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001723 } else if (v == intvar[F0]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001724 is_f0_split = FALSE;
1725
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001726 } else if (v == intvar[FS]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001727 mk_splitter(getvar_s(v), &fsplitter);
1728
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001729 } else if (v == intvar[RS]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001730 mk_splitter(getvar_s(v), &rsplitter);
1731
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001732 } else if (v == intvar[IGNORECASE]) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001733 icase = istrue(v);
1734
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00001735 } else { /* $n */
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001736 n = getvar_i(intvar[NF]);
1737 setvar_i(intvar[NF], n > v-Fields ? n : v-Fields+1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001738 /* right here v is invalid. Just to note... */
1739 }
1740}
1741
1742/* step through func/builtin/etc arguments */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001743static node *nextarg(node **pn)
1744{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001745 node *n;
1746
1747 n = *pn;
1748 if (n && (n->info & OPCLSMASK) == OC_COMMA) {
1749 *pn = n->r.n;
1750 n = n->l.n;
1751 } else {
1752 *pn = NULL;
1753 }
1754 return n;
1755}
1756
Mike Frysinger10a11e22005-09-27 02:23:02 +00001757static void hashwalk_init(var *v, xhash *array)
1758{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001759 hash_item *hi;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00001760 unsigned i;
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001761 walker_list *w;
1762 walker_list *prev_walker;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001763
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001764 if (v->type & VF_WALK) {
1765 prev_walker = v->x.walker;
1766 } else {
1767 v->type |= VF_WALK;
1768 prev_walker = NULL;
1769 }
1770 debug_printf_walker("hashwalk_init: prev_walker:%p\n", prev_walker);
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001771
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001772 w = v->x.walker = xzalloc(sizeof(*w) + array->glen + 1); /* why + 1? */
1773 debug_printf_walker(" walker@%p=%p\n", &v->x.walker, w);
1774 w->cur = w->end = w->wbuf;
1775 w->prev = prev_walker;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001776 for (i = 0; i < array->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001777 hi = array->items[i];
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001778 while (hi) {
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001779 strcpy(w->end, hi->name);
1780 nextword(&w->end);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001781 hi = hi->next;
1782 }
1783 }
1784}
1785
Mike Frysinger10a11e22005-09-27 02:23:02 +00001786static int hashwalk_next(var *v)
1787{
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001788 walker_list *w = v->x.walker;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001789
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001790 if (w->cur >= w->end) {
1791 walker_list *prev_walker = w->prev;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001792
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001793 debug_printf_walker("end of iteration, free(walker@%p:%p), prev_walker:%p\n", &v->x.walker, w, prev_walker);
1794 free(w);
Denys Vlasenko3cb60c32010-03-10 19:20:32 +01001795 v->x.walker = prev_walker;
1796 return FALSE;
1797 }
1798
Denys Vlasenkoda62b092010-03-11 12:13:18 +01001799 setvar_s(v, nextword(&w->cur));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001800 return TRUE;
1801}
1802
1803/* evaluate node, return 1 when result is true, 0 otherwise */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001804static int ptest(node *pattern)
1805{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001806 /* ptest__v is "static": to save stack space? */
1807 return istrue(evaluate(pattern, &G.ptest__v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001808}
1809
1810/* read next record from stream rsm into a variable v */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001811static int awk_getline(rstream *rsm, var *v)
1812{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001813 char *b;
1814 regmatch_t pmatch[2];
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001815 int size, a, p, pp = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001816 int fd, so, eo, r, rp;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001817 char c, *m, *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001818
1819 /* we're using our own buffer since we need access to accumulating
1820 * characters
1821 */
1822 fd = fileno(rsm->F);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001823 m = rsm->buffer;
1824 a = rsm->adv;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001825 p = rsm->pos;
1826 size = rsm->size;
1827 c = (char) rsplitter.n.info;
1828 rp = 0;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001829
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001830 if (!m)
1831 m = qrealloc(m, 256, &size);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001832
Glenn L McGrath545106f2002-11-11 06:21:00 +00001833 do {
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001834 b = m + a;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001835 so = eo = p;
1836 r = 1;
1837 if (p > 0) {
1838 if ((rsplitter.n.info & OPCLSMASK) == OC_REGEXP) {
1839 if (regexec(icase ? rsplitter.n.r.ire : rsplitter.n.l.re,
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001840 b, 1, pmatch, 0) == 0) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00001841 so = pmatch[0].rm_so;
1842 eo = pmatch[0].rm_eo;
1843 if (b[eo] != '\0')
1844 break;
1845 }
1846 } else if (c != '\0') {
1847 s = strchr(b+pp, c);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001848 if (!s)
1849 s = memchr(b+pp, '\0', p - pp);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001850 if (s) {
1851 so = eo = s-b;
1852 eo++;
1853 break;
1854 }
1855 } else {
1856 while (b[rp] == '\n')
1857 rp++;
1858 s = strstr(b+rp, "\n\n");
1859 if (s) {
1860 so = eo = s-b;
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001861 while (b[eo] == '\n')
1862 eo++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001863 if (b[eo] != '\0')
1864 break;
1865 }
1866 }
1867 }
1868
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001869 if (a > 0) {
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001870 memmove(m, m+a, p+1);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001871 b = m;
1872 a = 0;
1873 }
1874
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001875 m = qrealloc(m, a+p+128, &size);
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001876 b = m + a;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001877 pp = p;
1878 p += safe_read(fd, b+p, size-p-1);
1879 if (p < pp) {
1880 p = 0;
1881 r = 0;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001882 setvar_i(intvar[ERRNO], errno);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001883 }
1884 b[p] = '\0';
1885
1886 } while (p > pp);
1887
1888 if (p == 0) {
1889 r--;
1890 } else {
1891 c = b[so]; b[so] = '\0';
1892 setvar_s(v, b+rp);
1893 v->type |= VF_USER;
1894 b[so] = c;
1895 c = b[eo]; b[eo] = '\0';
Denis Vlasenkoffba9412007-05-17 23:03:35 +00001896 setvar_s(intvar[RT], b+so);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001897 b[eo] = c;
1898 }
1899
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00001900 rsm->buffer = m;
1901 rsm->adv = a + eo;
1902 rsm->pos = p - eo;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001903 rsm->size = size;
1904
1905 return r;
1906}
1907
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00001908static int fmt_num(char *b, int size, const char *format, double n, int int_as_int)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001909{
Denis Vlasenkof782f522007-01-01 23:51:30 +00001910 int r = 0;
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00001911 char c;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001912 const char *s = format;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001913
1914 if (int_as_int && n == (int)n) {
1915 r = snprintf(b, size, "%d", (int)n);
1916 } else {
Denis Vlasenkof782f522007-01-01 23:51:30 +00001917 do { c = *s; } while (c && *++s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001918 if (strchr("diouxX", c)) {
1919 r = snprintf(b, size, format, (int)n);
1920 } else if (strchr("eEfgG", c)) {
1921 r = snprintf(b, size, format, n);
1922 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00001923 syntax_error(EMSG_INV_FMT);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001924 }
1925 }
1926 return r;
1927}
1928
Glenn L McGrath545106f2002-11-11 06:21:00 +00001929/* formatted output into an allocated buffer, return ptr to buffer */
Mike Frysinger10a11e22005-09-27 02:23:02 +00001930static char *awk_printf(node *n)
1931{
Glenn L McGrath545106f2002-11-11 06:21:00 +00001932 char *b = NULL;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001933 char *fmt, *s, *f;
1934 const char *s1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001935 int i, j, incr, bsize;
1936 char c, c1;
1937 var *v, *arg;
1938
1939 v = nvalloc(1);
Rob Landleyd921b2e2006-08-03 15:41:12 +00001940 fmt = f = xstrdup(getvar_s(evaluate(nextarg(&n), v)));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001941
1942 i = 0;
1943 while (*f) {
1944 s = f;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001945 while (*f && (*f != '%' || *++f == '%'))
Glenn L McGrath545106f2002-11-11 06:21:00 +00001946 f++;
Denis Vlasenko389f9d52007-05-09 21:57:23 +00001947 while (*f && !isalpha(*f)) {
1948 if (*f == '*')
1949 syntax_error("%*x formats are not supported");
Glenn L McGrath545106f2002-11-11 06:21:00 +00001950 f++;
Denis Vlasenko389f9d52007-05-09 21:57:23 +00001951 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00001952
1953 incr = (f - s) + MAXVARFMT;
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001954 b = qrealloc(b, incr + i, &bsize);
Denis Vlasenkof782f522007-01-01 23:51:30 +00001955 c = *f;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001956 if (c != '\0')
1957 f++;
Denis Vlasenkof782f522007-01-01 23:51:30 +00001958 c1 = *f;
1959 *f = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00001960 arg = evaluate(nextarg(&n), v);
1961
1962 j = i;
1963 if (c == 'c' || !c) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00001964 i += sprintf(b+i, s, is_numeric(arg) ?
1965 (char)getvar_i(arg) : *getvar_s(arg));
Glenn L McGrath545106f2002-11-11 06:21:00 +00001966 } else if (c == 's') {
Denis Vlasenko92758142006-10-03 19:56:34 +00001967 s1 = getvar_s(arg);
Denys Vlasenkoc9955f22010-03-10 19:21:54 +01001968 b = qrealloc(b, incr+i+strlen(s1), &bsize);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001969 i += sprintf(b+i, s, s1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001970 } else {
1971 i += fmt_num(b+i, incr, s, getvar_i(arg), FALSE);
1972 }
1973 *f = c1;
1974
1975 /* if there was an error while sprintf, return value is negative */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01001976 if (i < j)
1977 i = j;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001978 }
1979
Glenn L McGrath545106f2002-11-11 06:21:00 +00001980 free(fmt);
1981 nvfree(v);
Denys Vlasenko7b81db12010-03-12 21:04:47 +01001982 b = xrealloc(b, i + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00001983 b[i] = '\0';
1984 return b;
1985}
1986
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001987/* Common substitution routine.
1988 * Replace (nm)'th substring of (src) that matches (rn) with (repl),
1989 * store result into (dest), return number of substitutions.
1990 * If nm = 0, replace all matches.
1991 * If src or dst is NULL, use $0.
1992 * If subexp != 0, enable subexpression matching (\1-\9).
Glenn L McGrath545106f2002-11-11 06:21:00 +00001993 */
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001994static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int subexp)
Mike Frysinger10a11e22005-09-27 02:23:02 +00001995{
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001996 char *resbuf;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00001997 const char *sp;
Denys Vlasenkofab288c2010-04-04 01:17:30 +02001998 int match_no, residx, replen, resbufsize;
1999 int regexec_flags;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002000 regmatch_t pmatch[10];
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002001 regex_t sreg, *regex;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002002
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002003 resbuf = NULL;
2004 residx = 0;
2005 match_no = 0;
2006 regexec_flags = 0;
2007 regex = as_regex(rn, &sreg);
2008 sp = getvar_s(src ? src : intvar[F0]);
2009 replen = strlen(repl);
2010 while (regexec(regex, sp, 10, pmatch, regexec_flags) == 0) {
2011 int so = pmatch[0].rm_so;
2012 int eo = pmatch[0].rm_eo;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002013
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002014 //bb_error_msg("match %u: [%u,%u] '%s'%p", match_no+1, so, eo, sp,sp);
2015 resbuf = qrealloc(resbuf, residx + eo + replen, &resbufsize);
2016 memcpy(resbuf + residx, sp, eo);
2017 residx += eo;
2018 if (++match_no >= nm) {
2019 const char *s;
2020 int nbs;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002021
Glenn L McGrath545106f2002-11-11 06:21:00 +00002022 /* replace */
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002023 residx -= (eo - so);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002024 nbs = 0;
2025 for (s = repl; *s; s++) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002026 char c = resbuf[residx++] = *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002027 if (c == '\\') {
2028 nbs++;
2029 continue;
2030 }
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002031 if (c == '&' || (subexp && c >= '0' && c <= '9')) {
2032 int j;
2033 residx -= ((nbs + 3) >> 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002034 j = 0;
2035 if (c != '&') {
2036 j = c - '0';
2037 nbs++;
2038 }
2039 if (nbs % 2) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002040 resbuf[residx++] = c;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002041 } else {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002042 int n = pmatch[j].rm_eo - pmatch[j].rm_so;
2043 resbuf = qrealloc(resbuf, residx + replen + n, &resbufsize);
2044 memcpy(resbuf + residx, sp + pmatch[j].rm_so, n);
2045 residx += n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002046 }
2047 }
2048 nbs = 0;
2049 }
2050 }
2051
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002052 regexec_flags = REG_NOTBOL;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002053 sp += eo;
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002054 if (match_no == nm)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002055 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002056 if (eo == so) {
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002057 /* Empty match (e.g. "b*" will match anywhere).
2058 * Advance by one char. */
2059//BUG (bug 1333):
2060//gsub(/\<b*/,"") on "abc" will reach this point, advance to "bc"
2061//... and will erroneously match "b" even though it is NOT at the word start.
2062//we need REG_NOTBOW but it does not exist...
Denys Vlasenko7379cd12010-04-04 01:48:12 +02002063//TODO: if EXTRA_COMPAT=y, use GNU matching and re_search,
2064//it should be able to do it correctly.
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002065 /* Subtle: this is safe only because
2066 * qrealloc allocated at least one extra byte */
2067 resbuf[residx] = *sp;
2068 if (*sp == '\0')
2069 goto ret;
2070 sp++;
2071 residx++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002072 }
2073 }
2074
Denys Vlasenkofab288c2010-04-04 01:17:30 +02002075 resbuf = qrealloc(resbuf, residx + strlen(sp), &resbufsize);
2076 strcpy(resbuf + residx, sp);
2077 ret:
2078 //bb_error_msg("end sp:'%s'%p", sp,sp);
2079 setvar_p(dest ? dest : intvar[F0], resbuf);
2080 if (regex == &sreg)
2081 regfree(regex);
2082 return match_no;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002083}
2084
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002085static NOINLINE int do_mktime(const char *ds)
2086{
2087 struct tm then;
2088 int count;
2089
2090 /*memset(&then, 0, sizeof(then)); - not needed */
2091 then.tm_isdst = -1; /* default is unknown */
2092
2093 /* manpage of mktime says these fields are ints,
2094 * so we can sscanf stuff directly into them */
2095 count = sscanf(ds, "%u %u %u %u %u %u %d",
2096 &then.tm_year, &then.tm_mon, &then.tm_mday,
2097 &then.tm_hour, &then.tm_min, &then.tm_sec,
2098 &then.tm_isdst);
2099
2100 if (count < 6
2101 || (unsigned)then.tm_mon < 1
2102 || (unsigned)then.tm_year < 1900
2103 ) {
2104 return -1;
2105 }
2106
2107 then.tm_mon -= 1;
Denys Vlasenkobc3e9472009-09-21 04:16:00 +02002108 then.tm_year -= 1900;
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002109
2110 return mktime(&then);
2111}
2112
2113static NOINLINE var *exec_builtin(node *op, var *res)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002114{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002115#define tspl (G.exec_builtin__tspl)
2116
Glenn L McGrath545106f2002-11-11 06:21:00 +00002117 var *tv;
2118 node *an[4];
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002119 var *av[4];
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002120 const char *as[4];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002121 regmatch_t pmatch[2];
2122 regex_t sreg, *re;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002123 node *spl;
Mike Frysingerf87b3e32005-09-27 04:16:22 +00002124 uint32_t isr, info;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002125 int nargs;
2126 time_t tt;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002127 int i, l, ll, n;
2128
2129 tv = nvalloc(4);
2130 isr = info = op->info;
2131 op = op->l.n;
2132
2133 av[2] = av[3] = NULL;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002134 for (i = 0; i < 4 && op; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002135 an[i] = nextarg(&op);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002136 if (isr & 0x09000000)
2137 av[i] = evaluate(an[i], &tv[i]);
2138 if (isr & 0x08000000)
2139 as[i] = getvar_s(av[i]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002140 isr >>= 1;
2141 }
2142
2143 nargs = i;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +00002144 if ((uint32_t)nargs < (info >> 30))
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002145 syntax_error(EMSG_TOO_FEW_ARGS);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002146
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02002147 info &= OPNMASK;
2148 switch (info) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002149
Denis Vlasenkof782f522007-01-01 23:51:30 +00002150 case B_a2:
Denis Vlasenko2d5bd802008-10-24 10:49:49 +00002151#if ENABLE_FEATURE_AWK_LIBM
Denis Vlasenko37890e22008-10-21 12:59:34 +00002152 setvar_i(res, atan2(getvar_i(av[0]), getvar_i(av[1])));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002153#else
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002154 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002155#endif
2156 break;
2157
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002158 case B_sp: {
2159 char *s, *s1;
2160
Glenn L McGrath545106f2002-11-11 06:21:00 +00002161 if (nargs > 2) {
2162 spl = (an[2]->info & OPCLSMASK) == OC_REGEXP ?
2163 an[2] : mk_splitter(getvar_s(evaluate(an[2], &tv[2])), &tspl);
2164 } else {
2165 spl = &fsplitter.n;
2166 }
2167
2168 n = awk_split(as[0], spl, &s);
2169 s1 = s;
2170 clear_array(iamarray(av[1]));
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002171 for (i = 1; i <= n; i++)
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002172 setari_u(av[1], i, nextword(&s));
2173 free(s1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002174 setvar_i(res, n);
2175 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002176 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002177
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002178 case B_ss: {
2179 char *s;
2180
Rob Landleya3896512006-05-07 20:20:34 +00002181 l = strlen(as[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002182 i = getvar_i(av[1]) - 1;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002183 if (i > l)
2184 i = l;
2185 if (i < 0)
2186 i = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002187 n = (nargs > 2) ? getvar_i(av[2]) : l-i;
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002188 if (n < 0)
2189 n = 0;
Denis Vlasenko8ae5b282008-07-02 22:47:49 +00002190 s = xstrndup(as[0]+i, n);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002191 setvar_p(res, s);
2192 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002193 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +00002194
Denis Vlasenko7cbcd1c2008-08-28 23:16:58 +00002195 /* Bitwise ops must assume that operands are unsigned. GNU Awk 3.1.5:
2196 * awk '{ print or(-1,1) }' gives "4.29497e+09", not "-2.xxxe+09" */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002197 case B_an:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002198 setvar_i(res, getvar_i_int(av[0]) & getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002199 break;
Denis Vlasenkof7996f32007-01-11 17:20:00 +00002200
Denis Vlasenkof782f522007-01-01 23:51:30 +00002201 case B_co:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002202 setvar_i(res, ~getvar_i_int(av[0]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002203 break;
2204
Denis Vlasenkof782f522007-01-01 23:51:30 +00002205 case B_ls:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002206 setvar_i(res, getvar_i_int(av[0]) << getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002207 break;
2208
Denis Vlasenkof782f522007-01-01 23:51:30 +00002209 case B_or:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002210 setvar_i(res, getvar_i_int(av[0]) | getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002211 break;
2212
Denis Vlasenkof782f522007-01-01 23:51:30 +00002213 case B_rs:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002214 setvar_i(res, getvar_i_int(av[0]) >> getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002215 break;
2216
Denis Vlasenkof782f522007-01-01 23:51:30 +00002217 case B_xo:
Denis Vlasenkoa2e1eea2008-09-02 09:00:23 +00002218 setvar_i(res, getvar_i_int(av[0]) ^ getvar_i_int(av[1]));
Denis Vlasenkoe175ff22006-09-26 17:41:00 +00002219 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002220
Denis Vlasenkof782f522007-01-01 23:51:30 +00002221 case B_lo:
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002222 case B_up: {
2223 char *s, *s1;
Rob Landleyd921b2e2006-08-03 15:41:12 +00002224 s1 = s = xstrdup(as[0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002225 while (*s1) {
Denys Vlasenko56b3eec2009-10-23 13:03:59 +02002226 //*s1 = (info == B_up) ? toupper(*s1) : tolower(*s1);
2227 if ((unsigned char)((*s1 | 0x20) - 'a') <= ('z' - 'a'))
2228 *s1 = (info == B_up) ? (*s1 & 0xdf) : (*s1 | 0x20);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002229 s1++;
2230 }
2231 setvar_p(res, s);
2232 break;
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002233 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002234
Denis Vlasenkof782f522007-01-01 23:51:30 +00002235 case B_ix:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002236 n = 0;
Rob Landleya3896512006-05-07 20:20:34 +00002237 ll = strlen(as[1]);
2238 l = strlen(as[0]) - ll;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002239 if (ll > 0 && l >= 0) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002240 if (!icase) {
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002241 char *s = strstr(as[0], as[1]);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002242 if (s)
2243 n = (s - as[0]) + 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002244 } else {
2245 /* this piece of code is terribly slow and
2246 * really should be rewritten
2247 */
Denys Vlasenko39fe4d12010-03-12 16:57:06 +01002248 for (i = 0; i <= l; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002249 if (strncasecmp(as[0]+i, as[1], ll) == 0) {
2250 n = i+1;
2251 break;
2252 }
2253 }
2254 }
2255 }
2256 setvar_i(res, n);
2257 break;
2258
Denis Vlasenkof782f522007-01-01 23:51:30 +00002259 case B_ti:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002260 if (nargs > 1)
2261 tt = getvar_i(av[1]);
2262 else
2263 time(&tt);
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002264 //s = (nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y";
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002265 i = strftime(g_buf, MAXVARFMT,
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002266 ((nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"),
2267 localtime(&tt));
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002268 g_buf[i] = '\0';
2269 setvar_s(res, g_buf);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002270 break;
2271
Leonid Lisovskiy46a0be52009-09-21 04:08:08 +02002272 case B_mt:
2273 setvar_i(res, do_mktime(as[0]));
2274 break;
2275
Denis Vlasenkof782f522007-01-01 23:51:30 +00002276 case B_ma:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002277 re = as_regex(an[1], &sreg);
2278 n = regexec(re, as[0], 1, pmatch, 0);
2279 if (n == 0) {
2280 pmatch[0].rm_so++;
2281 pmatch[0].rm_eo++;
2282 } else {
2283 pmatch[0].rm_so = 0;
2284 pmatch[0].rm_eo = -1;
2285 }
2286 setvar_i(newvar("RSTART"), pmatch[0].rm_so);
2287 setvar_i(newvar("RLENGTH"), pmatch[0].rm_eo - pmatch[0].rm_so);
2288 setvar_i(res, pmatch[0].rm_so);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002289 if (re == &sreg)
2290 regfree(re);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002291 break;
2292
Denis Vlasenkof782f522007-01-01 23:51:30 +00002293 case B_ge:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002294 awk_sub(an[0], as[1], getvar_i(av[2]), av[3], res, TRUE);
2295 break;
2296
Denis Vlasenkof782f522007-01-01 23:51:30 +00002297 case B_gs:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002298 setvar_i(res, awk_sub(an[0], as[1], 0, av[2], av[2], FALSE));
2299 break;
2300
Denis Vlasenkof782f522007-01-01 23:51:30 +00002301 case B_su:
Glenn L McGrath545106f2002-11-11 06:21:00 +00002302 setvar_i(res, awk_sub(an[0], as[1], 1, av[2], av[2], FALSE));
2303 break;
2304 }
2305
2306 nvfree(tv);
2307 return res;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002308#undef tspl
Glenn L McGrath545106f2002-11-11 06:21:00 +00002309}
2310
2311/*
2312 * Evaluate node - the heart of the program. Supplied with subtree
2313 * and place where to store result. returns ptr to result.
2314 */
2315#define XC(n) ((n) >> 8)
2316
Mike Frysinger10a11e22005-09-27 02:23:02 +00002317static var *evaluate(node *op, var *res)
2318{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002319/* This procedure is recursive so we should count every byte */
2320#define fnargs (G.evaluate__fnargs)
2321/* seed is initialized to 1 */
2322#define seed (G.evaluate__seed)
2323#define sreg (G.evaluate__sreg)
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002324
Glenn L McGrath545106f2002-11-11 06:21:00 +00002325 var *v1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002326
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002327 if (!op)
Glenn L McGrath545106f2002-11-11 06:21:00 +00002328 return setvar_s(res, NULL);
2329
2330 v1 = nvalloc(2);
2331
2332 while (op) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002333 struct {
2334 var *v;
2335 const char *s;
2336 } L = L; /* for compiler */
2337 struct {
2338 var *v;
2339 const char *s;
2340 } R = R;
2341 double L_d = L_d;
2342 uint32_t opinfo;
2343 int opn;
2344 node *op1;
2345
Glenn L McGrath545106f2002-11-11 06:21:00 +00002346 opinfo = op->info;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002347 opn = (opinfo & OPNMASK);
2348 g_lineno = op->lineno;
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002349 op1 = op->l.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002350
Mike Frysingerde2b9382005-09-27 03:18:00 +00002351 /* execute inevitable things */
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002352 if (opinfo & OF_RES1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002353 L.v = evaluate(op1, v1);
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002354 if (opinfo & OF_RES2)
2355 R.v = evaluate(op->r.n, v1+1);
2356 if (opinfo & OF_STR1)
2357 L.s = getvar_s(L.v);
2358 if (opinfo & OF_STR2)
2359 R.s = getvar_s(R.v);
2360 if (opinfo & OF_NUM1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002361 L_d = getvar_i(L.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002362
2363 switch (XC(opinfo & OPCLSMASK)) {
2364
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002365 /* -- iterative node type -- */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002366
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002367 /* test pattern */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002368 case XC( OC_TEST ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002369 if ((op1->info & OPCLSMASK) == OC_COMMA) {
2370 /* it's range pattern */
2371 if ((opinfo & OF_CHECKED) || ptest(op1->l.n)) {
2372 op->info |= OF_CHECKED;
2373 if (ptest(op1->r.n))
2374 op->info &= ~OF_CHECKED;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002375 op = op->a.n;
2376 } else {
2377 op = op->r.n;
2378 }
2379 } else {
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002380 op = ptest(op1) ? op->a.n : op->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002381 }
2382 break;
2383
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002384 /* just evaluate an expression, also used as unconditional jump */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002385 case XC( OC_EXEC ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002386 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002387
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002388 /* branch, used in if-else and various loops */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002389 case XC( OC_BR ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002390 op = istrue(L.v) ? op->a.n : op->r.n;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002391 break;
2392
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002393 /* initialize for-in loop */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002394 case XC( OC_WALKINIT ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002395 hashwalk_init(L.v, iamarray(R.v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002396 break;
2397
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002398 /* get next array item */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002399 case XC( OC_WALKNEXT ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002400 op = hashwalk_next(L.v) ? op->a.n : op->r.n;
2401 break;
2402
Denis Vlasenkof782f522007-01-01 23:51:30 +00002403 case XC( OC_PRINT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002404 case XC( OC_PRINTF ): {
2405 FILE *F = stdout;
2406
Mike Frysingerde2b9382005-09-27 03:18:00 +00002407 if (op->r.n) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002408 rstream *rsm = newfile(R.s);
2409 if (!rsm->F) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002410 if (opn == '|') {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002411 rsm->F = popen(R.s, "w");
2412 if (rsm->F == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002413 bb_perror_msg_and_die("popen");
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002414 rsm->is_pipe = 1;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002415 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002416 rsm->F = xfopen(R.s, opn=='w' ? "w" : "a");
Glenn L McGrath545106f2002-11-11 06:21:00 +00002417 }
2418 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002419 F = rsm->F;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002420 }
2421
2422 if ((opinfo & OPCLSMASK) == OC_PRINT) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002423 if (!op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002424 fputs(getvar_s(intvar[F0]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002425 } else {
2426 while (op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002427 var *v = evaluate(nextarg(&op1), v1);
2428 if (v->type & VF_NUMBER) {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002429 fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[OFMT]),
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002430 getvar_i(v), TRUE);
2431 fputs(g_buf, F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002432 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002433 fputs(getvar_s(v), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002434 }
2435
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002436 if (op1)
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002437 fputs(getvar_s(intvar[OFS]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002438 }
2439 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002440 fputs(getvar_s(intvar[ORS]), F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002441
2442 } else { /* OC_PRINTF */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002443 char *s = awk_printf(op1);
2444 fputs(s, F);
2445 free(s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002446 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002447 fflush(F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002448 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002449 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002450
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002451 case XC( OC_DELETE ): {
2452 uint32_t info = op1->info & OPCLSMASK;
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002453 var *v;
2454
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002455 if (info == OC_VAR) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002456 v = op1->l.v;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002457 } else if (info == OC_FNARG) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002458 v = &fnargs[op1->l.aidx];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002459 } else {
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002460 syntax_error(EMSG_NOT_ARRAY);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002461 }
2462
Mike Frysingerde2b9382005-09-27 03:18:00 +00002463 if (op1->r.n) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002464 const char *s;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002465 clrvar(L.v);
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002466 s = getvar_s(evaluate(op1->r.n, v1));
2467 hash_remove(iamarray(v), s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002468 } else {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002469 clear_array(iamarray(v));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002470 }
2471 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002472 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002473
Denis Vlasenkof782f522007-01-01 23:51:30 +00002474 case XC( OC_NEWSOURCE ):
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002475 g_progname = op->l.new_progname;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002476 break;
2477
Denis Vlasenkof782f522007-01-01 23:51:30 +00002478 case XC( OC_RETURN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002479 copyvar(res, L.v);
2480 break;
2481
Denis Vlasenkof782f522007-01-01 23:51:30 +00002482 case XC( OC_NEXTFILE ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002483 nextfile = TRUE;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002484 case XC( OC_NEXT ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002485 nextrec = TRUE;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002486 case XC( OC_DONE ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002487 clrvar(res);
2488 break;
2489
Denis Vlasenkof782f522007-01-01 23:51:30 +00002490 case XC( OC_EXIT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002491 awk_exit(L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002492
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002493 /* -- recursive node type -- */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002494
Denis Vlasenkof782f522007-01-01 23:51:30 +00002495 case XC( OC_VAR ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002496 L.v = op->l.v;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002497 if (L.v == intvar[NF])
Glenn L McGrath545106f2002-11-11 06:21:00 +00002498 split_f0();
2499 goto v_cont;
2500
Denis Vlasenkof782f522007-01-01 23:51:30 +00002501 case XC( OC_FNARG ):
Denys Vlasenko7b81db12010-03-12 21:04:47 +01002502 L.v = &fnargs[op->l.aidx];
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002503 v_cont:
2504 res = op->r.n ? findvar(iamarray(L.v), R.s) : L.v;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002505 break;
2506
Denis Vlasenkof782f522007-01-01 23:51:30 +00002507 case XC( OC_IN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002508 setvar_i(res, hash_search(iamarray(R.v), L.s) ? 1 : 0);
2509 break;
2510
Denis Vlasenkof782f522007-01-01 23:51:30 +00002511 case XC( OC_REGEXP ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002512 op1 = op;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002513 L.s = getvar_s(intvar[F0]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002514 goto re_cont;
2515
Denis Vlasenkof782f522007-01-01 23:51:30 +00002516 case XC( OC_MATCH ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002517 op1 = op->r.n;
Denis Vlasenkoe1d3e032007-01-01 23:53:52 +00002518 re_cont:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002519 {
2520 regex_t *re = as_regex(op1, &sreg);
2521 int i = regexec(re, L.s, 0, NULL, 0);
2522 if (re == &sreg)
2523 regfree(re);
2524 setvar_i(res, (i == 0) ^ (opn == '!'));
2525 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002526 break;
2527
Denis Vlasenkof782f522007-01-01 23:51:30 +00002528 case XC( OC_MOVE ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002529 /* if source is a temporary string, jusk relink it to dest */
Denys Vlasenko12847742009-11-30 01:15:04 +01002530//Disabled: if R.v is numeric but happens to have cached R.v->string,
2531//then L.v ends up being a string, which is wrong
2532// if (R.v == v1+1 && R.v->string) {
2533// res = setvar_p(L.v, R.v->string);
2534// R.v->string = NULL;
2535// } else {
Mike Frysingerde2b9382005-09-27 03:18:00 +00002536 res = copyvar(L.v, R.v);
Denys Vlasenko12847742009-11-30 01:15:04 +01002537// }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002538 break;
2539
Denis Vlasenkof782f522007-01-01 23:51:30 +00002540 case XC( OC_TERNARY ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002541 if ((op->r.n->info & OPCLSMASK) != OC_COLON)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002542 syntax_error(EMSG_POSSIBLE_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002543 res = evaluate(istrue(L.v) ? op->r.n->l.n : op->r.n->r.n, res);
2544 break;
2545
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002546 case XC( OC_FUNC ): {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002547 var *vbeg, *v;
2548 const char *sv_progname;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002549
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002550 if (!op->r.f->body.first)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002551 syntax_error(EMSG_UNDEF_FUNC);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002552
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002553 vbeg = v = nvalloc(op->r.f->nargs + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002554 while (op1) {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002555 var *arg = evaluate(nextarg(&op1), v1);
2556 copyvar(v, arg);
2557 v->type |= VF_CHILD;
2558 v->x.parent = arg;
2559 if (++v - vbeg >= op->r.f->nargs)
Glenn L McGrath545106f2002-11-11 06:21:00 +00002560 break;
2561 }
2562
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002563 v = fnargs;
2564 fnargs = vbeg;
2565 sv_progname = g_progname;
2566
2567 res = evaluate(op->r.f->body.first, res);
2568
2569 g_progname = sv_progname;
2570 nvfree(fnargs);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002571 fnargs = v;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002572
Glenn L McGrath545106f2002-11-11 06:21:00 +00002573 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002574 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002575
Denis Vlasenkof782f522007-01-01 23:51:30 +00002576 case XC( OC_GETLINE ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002577 case XC( OC_PGETLINE ): {
2578 rstream *rsm;
2579 int i;
2580
Mike Frysingerde2b9382005-09-27 03:18:00 +00002581 if (op1) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002582 rsm = newfile(L.s);
2583 if (!rsm->F) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002584 if ((opinfo & OPCLSMASK) == OC_PGETLINE) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002585 rsm->F = popen(L.s, "r");
2586 rsm->is_pipe = TRUE;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002587 } else {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002588 rsm->F = fopen_for_read(L.s); /* not xfopen! */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002589 }
2590 }
2591 } else {
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002592 if (!iF)
2593 iF = next_input_file();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002594 rsm = iF;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002595 }
2596
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002597 if (!rsm->F) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002598 setvar_i(intvar[ERRNO], errno);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002599 setvar_i(res, -1);
2600 break;
2601 }
2602
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002603 if (!op->r.n)
2604 R.v = intvar[F0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002605
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002606 i = awk_getline(rsm, R.v);
2607 if (i > 0 && !op1) {
2608 incvar(intvar[FNR]);
2609 incvar(intvar[NR]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002610 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002611 setvar_i(res, i);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002612 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002613 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002614
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002615 /* simple builtins */
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002616 case XC( OC_FBLTIN ): {
2617 int i;
2618 rstream *rsm;
2619 double R_d = R_d; /* for compiler */
Glenn L McGrath545106f2002-11-11 06:21:00 +00002620
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002621 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002622 case F_in:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002623 R_d = (int)L_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002624 break;
2625
Denis Vlasenkof782f522007-01-01 23:51:30 +00002626 case F_rn:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002627 R_d = (double)rand() / (double)RAND_MAX;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002628 break;
Denis Vlasenko2d5bd802008-10-24 10:49:49 +00002629#if ENABLE_FEATURE_AWK_LIBM
Denis Vlasenkof782f522007-01-01 23:51:30 +00002630 case F_co:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002631 R_d = cos(L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002632 break;
2633
Denis Vlasenkof782f522007-01-01 23:51:30 +00002634 case F_ex:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002635 R_d = exp(L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002636 break;
2637
Denis Vlasenkof782f522007-01-01 23:51:30 +00002638 case F_lg:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002639 R_d = log(L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002640 break;
2641
Denis Vlasenkof782f522007-01-01 23:51:30 +00002642 case F_si:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002643 R_d = sin(L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002644 break;
2645
Denis Vlasenkof782f522007-01-01 23:51:30 +00002646 case F_sq:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002647 R_d = sqrt(L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002648 break;
2649#else
Denis Vlasenkof782f522007-01-01 23:51:30 +00002650 case F_co:
2651 case F_ex:
2652 case F_lg:
2653 case F_si:
2654 case F_sq:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002655 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002656 break;
2657#endif
Denis Vlasenkof782f522007-01-01 23:51:30 +00002658 case F_sr:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002659 R_d = (double)seed;
2660 seed = op1 ? (unsigned)L_d : (unsigned)time(NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002661 srand(seed);
2662 break;
2663
Denis Vlasenkof782f522007-01-01 23:51:30 +00002664 case F_ti:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002665 R_d = time(NULL);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002666 break;
2667
Denis Vlasenkof782f522007-01-01 23:51:30 +00002668 case F_le:
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002669 if (!op1)
2670 L.s = getvar_s(intvar[F0]);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002671 R_d = strlen(L.s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002672 break;
2673
Denis Vlasenkof782f522007-01-01 23:51:30 +00002674 case F_sy:
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002675 fflush_all();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002676 R_d = (ENABLE_FEATURE_ALLOW_EXEC && L.s && *L.s)
Denis Vlasenko249fabf2006-12-19 00:29:22 +00002677 ? (system(L.s) >> 8) : 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002678 break;
2679
Denis Vlasenkof782f522007-01-01 23:51:30 +00002680 case F_ff:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002681 if (!op1) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002682 fflush(stdout);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002683 } else if (L.s && *L.s) {
2684 rsm = newfile(L.s);
2685 fflush(rsm->F);
2686 } else {
2687 fflush_all();
Glenn L McGrath545106f2002-11-11 06:21:00 +00002688 }
2689 break;
2690
Denis Vlasenkof782f522007-01-01 23:51:30 +00002691 case F_cl:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002692 i = 0;
2693 rsm = (rstream *)hash_search(fdhash, L.s);
2694 if (rsm) {
2695 i = rsm->is_pipe ? pclose(rsm->F) : fclose(rsm->F);
2696 free(rsm->buffer);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002697 hash_remove(fdhash, L.s);
2698 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002699 if (i != 0)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002700 setvar_i(intvar[ERRNO], errno);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002701 R_d = (double)i;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002702 break;
2703 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002704 setvar_i(res, R_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002705 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002706 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002707
Denis Vlasenkof782f522007-01-01 23:51:30 +00002708 case XC( OC_BUILTIN ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002709 res = exec_builtin(op, res);
2710 break;
2711
Denis Vlasenkof782f522007-01-01 23:51:30 +00002712 case XC( OC_SPRINTF ):
Mike Frysingerde2b9382005-09-27 03:18:00 +00002713 setvar_p(res, awk_printf(op1));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002714 break;
2715
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002716 case XC( OC_UNARY ): {
2717 double Ld, R_d;
2718
2719 Ld = R_d = getvar_i(R.v);
Mike Frysingerde2b9382005-09-27 03:18:00 +00002720 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002721 case 'P':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002722 Ld = ++R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002723 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002724 case 'p':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002725 R_d++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002726 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002727 case 'M':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002728 Ld = --R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002729 goto r_op_change;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002730 case 'm':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002731 R_d--;
2732 r_op_change:
2733 setvar_i(R.v, R_d);
2734 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002735 case '!':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002736 Ld = !istrue(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002737 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002738 case '-':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002739 Ld = -R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002740 break;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002741 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002742 setvar_i(res, Ld);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002743 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002744 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002745
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002746 case XC( OC_FIELD ): {
2747 int i = (int)getvar_i(R.v);
2748 if (i == 0) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002749 res = intvar[F0];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002750 } else {
2751 split_f0();
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002752 if (i > nfields)
2753 fsrealloc(i);
2754 res = &Fields[i - 1];
Glenn L McGrath545106f2002-11-11 06:21:00 +00002755 }
2756 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002757 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002758
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002759 /* concatenation (" ") and index joining (",") */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002760 case XC( OC_CONCAT ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002761 case XC( OC_COMMA ): {
2762 const char *sep = "";
2763 if ((opinfo & OPCLSMASK) == OC_COMMA)
2764 sep = getvar_s(intvar[SUBSEP]);
2765 setvar_p(res, xasprintf("%s%s%s", L.s, sep, R.s));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002766 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002767 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002768
Denis Vlasenkof782f522007-01-01 23:51:30 +00002769 case XC( OC_LAND ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002770 setvar_i(res, istrue(L.v) ? ptest(op->r.n) : 0);
2771 break;
2772
Denis Vlasenkof782f522007-01-01 23:51:30 +00002773 case XC( OC_LOR ):
Glenn L McGrath545106f2002-11-11 06:21:00 +00002774 setvar_i(res, istrue(L.v) ? 1 : ptest(op->r.n));
2775 break;
2776
Denis Vlasenkof782f522007-01-01 23:51:30 +00002777 case XC( OC_BINARY ):
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002778 case XC( OC_REPLACE ): {
2779 double R_d = getvar_i(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002780 switch (opn) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002781 case '+':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002782 L_d += R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002783 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002784 case '-':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002785 L_d -= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002786 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002787 case '*':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002788 L_d *= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002789 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002790 case '/':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002791 if (R_d == 0)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002792 syntax_error(EMSG_DIV_BY_ZERO);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002793 L_d /= R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002794 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002795 case '&':
Denis Vlasenko2d5bd802008-10-24 10:49:49 +00002796#if ENABLE_FEATURE_AWK_LIBM
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002797 L_d = pow(L_d, R_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002798#else
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002799 syntax_error(EMSG_NO_MATH);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002800#endif
2801 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002802 case '%':
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002803 if (R_d == 0)
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002804 syntax_error(EMSG_DIV_BY_ZERO);
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002805 L_d -= (int)(L_d / R_d) * R_d;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002806 break;
2807 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002808 res = setvar_i(((opinfo & OPCLSMASK) == OC_BINARY) ? res : L.v, L_d);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002809 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002810 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002811
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002812 case XC( OC_COMPARE ): {
2813 int i = i; /* for compiler */
2814 double Ld;
2815
Glenn L McGrath545106f2002-11-11 06:21:00 +00002816 if (is_numeric(L.v) && is_numeric(R.v)) {
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002817 Ld = getvar_i(L.v) - getvar_i(R.v);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002818 } else {
Denys Vlasenkof9782ff2010-03-12 21:32:13 +01002819 const char *l = getvar_s(L.v);
2820 const char *r = getvar_s(R.v);
2821 Ld = icase ? strcasecmp(l, r) : strcmp(l, r);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002822 }
2823 switch (opn & 0xfe) {
Denis Vlasenkof782f522007-01-01 23:51:30 +00002824 case 0:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002825 i = (Ld > 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002826 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002827 case 2:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002828 i = (Ld >= 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002829 break;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002830 case 4:
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002831 i = (Ld == 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002832 break;
2833 }
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002834 setvar_i(res, (i == 0) ^ (opn & 1));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002835 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002836 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002837
Denis Vlasenkof782f522007-01-01 23:51:30 +00002838 default:
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002839 syntax_error(EMSG_POSSIBLE_ERROR);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002840 }
2841 if ((opinfo & OPCLSMASK) <= SHIFT_TIL_THIS)
2842 op = op->a.n;
2843 if ((opinfo & OPCLSMASK) >= RECUR_FROM_THIS)
2844 break;
2845 if (nextrec)
2846 break;
Denys Vlasenkoc6ba9972010-03-12 21:05:09 +01002847 } /* while (op) */
2848
Glenn L McGrath545106f2002-11-11 06:21:00 +00002849 nvfree(v1);
2850 return res;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002851#undef fnargs
2852#undef seed
2853#undef sreg
Glenn L McGrath545106f2002-11-11 06:21:00 +00002854}
2855
2856
2857/* -------- main & co. -------- */
2858
Mike Frysinger10a11e22005-09-27 02:23:02 +00002859static int awk_exit(int r)
2860{
Denis Vlasenkof782f522007-01-01 23:51:30 +00002861 var tv;
2862 unsigned i;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002863 hash_item *hi;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002864
Denis Vlasenkof782f522007-01-01 23:51:30 +00002865 zero_out_var(&tv);
2866
2867 if (!exiting) {
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002868 exiting = TRUE;
Glenn L McGrathca29ffc2004-09-24 09:24:27 +00002869 nextrec = FALSE;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002870 evaluate(endseq.first, &tv);
2871 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00002872
2873 /* waiting for children */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002874 for (i = 0; i < fdhash->csize; i++) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002875 hi = fdhash->items[i];
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00002876 while (hi) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002877 if (hi->data.rs.F && hi->data.rs.is_pipe)
2878 pclose(hi->data.rs.F);
2879 hi = hi->next;
2880 }
2881 }
2882
2883 exit(r);
2884}
2885
2886/* if expr looks like "var=value", perform assignment and return 1,
2887 * otherwise return 0 */
"Vladimir N. Oleynik"5cf9a032005-10-19 09:21:51 +00002888static int is_assignment(const char *expr)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002889{
Glenn L McGrath545106f2002-11-11 06:21:00 +00002890 char *exprc, *s, *s0, *s1;
2891
Rob Landleyd921b2e2006-08-03 15:41:12 +00002892 exprc = xstrdup(expr);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002893 if (!isalnum_(*exprc) || (s = strchr(exprc, '=')) == NULL) {
2894 free(exprc);
2895 return FALSE;
2896 }
2897
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002898 *s++ = '\0';
Glenn L McGrath545106f2002-11-11 06:21:00 +00002899 s0 = s1 = s;
2900 while (*s)
Denys Vlasenko6ebdf7a2010-03-11 12:41:55 +01002901 *s1++ = nextchar(&s);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002902
2903 *s1 = '\0';
2904 setvar_u(newvar(exprc), s0);
2905 free(exprc);
2906 return TRUE;
2907}
2908
2909/* switch to next input file */
Mike Frysinger10a11e22005-09-27 02:23:02 +00002910static rstream *next_input_file(void)
2911{
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002912#define rsm (G.next_input_file__rsm)
2913#define files_happen (G.next_input_file__files_happen)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002914
Glenn L McGrath545106f2002-11-11 06:21:00 +00002915 FILE *F = NULL;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +00002916 const char *fname, *ind;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002917
Denys Vlasenkocdeda162009-11-30 01:14:16 +01002918 if (rsm.F)
2919 fclose(rsm.F);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002920 rsm.F = NULL;
Glenn L McGrath00ed36f2003-10-30 13:36:39 +00002921 rsm.pos = rsm.adv = 0;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002922
2923 do {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002924 if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002925 if (files_happen)
2926 return NULL;
2927 fname = "-";
2928 F = stdin;
2929 } else {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002930 ind = getvar_s(incvar(intvar[ARGIND]));
2931 fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002932 if (fname && *fname && !is_assignment(fname))
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00002933 F = xfopen_stdin(fname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002934 }
2935 } while (!F);
2936
2937 files_happen = TRUE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002938 setvar_s(intvar[FILENAME], fname);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002939 rsm.F = F;
2940 return &rsm;
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002941#undef rsm
2942#undef files_happen
Glenn L McGrath545106f2002-11-11 06:21:00 +00002943}
2944
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00002945int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +00002946int awk_main(int argc, char **argv)
Mike Frysinger10a11e22005-09-27 02:23:02 +00002947{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00002948 unsigned opt;
Denis Vlasenkobe644a82007-03-10 17:22:14 +00002949 char *opt_F, *opt_W;
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00002950 llist_t *list_v = NULL;
2951 llist_t *list_f = NULL;
2952 int i, j;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002953 var *v;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002954 var tv;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002955 char **envp;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002956 char *vnames = (char *)vNames; /* cheat */
2957 char *vvalues = (char *)vValues;
2958
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002959 INIT_G();
2960
Denis Vlasenko150f4022007-01-13 21:06:21 +00002961 /* Undo busybox.c, or else strtod may eat ','! This breaks parsing:
Denis Vlasenko6dc6ebb2007-01-01 23:53:12 +00002962 * $1,$2 == '$1,' '$2', NOT '$1' ',' '$2' */
2963 if (ENABLE_LOCALE_SUPPORT)
2964 setlocale(LC_NUMERIC, "C");
2965
Denis Vlasenkof782f522007-01-01 23:51:30 +00002966 zero_out_var(&tv);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002967
2968 /* allocate global buffer */
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00002969 g_buf = xmalloc(MAXVARFMT + 1);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002970
2971 vhash = hash_init();
2972 ahash = hash_init();
2973 fdhash = hash_init();
2974 fnhash = hash_init();
2975
2976 /* initialize variables */
Denis Vlasenkof782f522007-01-01 23:51:30 +00002977 for (i = 0; *vnames; i++) {
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002978 intvar[i] = v = newvar(nextword(&vnames));
Denis Vlasenkof782f522007-01-01 23:51:30 +00002979 if (*vvalues != '\377')
2980 setvar_s(v, nextword(&vvalues));
Glenn L McGrath545106f2002-11-11 06:21:00 +00002981 else
2982 setvar_i(v, 0);
2983
Denis Vlasenkof782f522007-01-01 23:51:30 +00002984 if (*vnames == '*') {
Glenn L McGrath545106f2002-11-11 06:21:00 +00002985 v->type |= VF_SPECIAL;
Denis Vlasenkof782f522007-01-01 23:51:30 +00002986 vnames++;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002987 }
2988 }
2989
Denis Vlasenkoffba9412007-05-17 23:03:35 +00002990 handle_special(intvar[FS]);
2991 handle_special(intvar[RS]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00002992
Denis Vlasenkof782f522007-01-01 23:51:30 +00002993 newfile("/dev/stdin")->F = stdin;
2994 newfile("/dev/stdout")->F = stdout;
2995 newfile("/dev/stderr")->F = stderr;
Glenn L McGrath545106f2002-11-11 06:21:00 +00002996
Denis Vlasenkof71d9162007-05-03 22:57:56 +00002997 /* Huh, people report that sometimes environ is NULL. Oh well. */
2998 if (environ) for (envp = environ; *envp; envp++) {
Denis Vlasenkob78c7822007-07-18 18:31:11 +00002999 /* environ is writable, thus we don't strdup it needlessly */
3000 char *s = *envp;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003001 char *s1 = strchr(s, '=');
3002 if (s1) {
Denis Vlasenkob78c7822007-07-18 18:31:11 +00003003 *s1 = '\0';
3004 /* Both findvar and setvar_u take const char*
3005 * as 2nd arg -> environment is not trashed */
3006 setvar_u(findvar(iamarray(intvar[ENVIRON]), s), s1 + 1);
3007 *s1 = '=';
Eric Andersen67776be2004-07-30 23:52:08 +00003008 }
Glenn L McGrath545106f2002-11-11 06:21:00 +00003009 }
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003010 opt_complementary = "v::f::"; /* -v and -f can occur multiple times */
3011 opt = getopt32(argv, "F:v:f:W:", &opt_F, &list_v, &list_f, &opt_W);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003012 argv += optind;
3013 argc -= optind;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003014 if (opt & 0x1)
3015 setvar_s(intvar[FS], opt_F); // -F
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003016 while (list_v) { /* -v */
3017 if (!is_assignment(llist_pop(&list_v)))
Denis Vlasenkobe644a82007-03-10 17:22:14 +00003018 bb_show_usage();
3019 }
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003020 if (list_f) { /* -f */
3021 do {
3022 char *s = NULL;
3023 FILE *from_file;
3024
3025 g_progname = llist_pop(&list_f);
3026 from_file = xfopen_stdin(g_progname);
3027 /* one byte is reserved for some trick in next_token */
Denis Vlasenkof782f522007-01-01 23:51:30 +00003028 for (i = j = 1; j > 0; i += j) {
3029 s = xrealloc(s, i + 4096);
3030 j = fread(s + i, 1, 4094, from_file);
Denis Vlasenko099efbf2006-09-22 09:02:30 +00003031 }
Denis Vlasenko3bb2bbd2008-07-01 01:57:36 +00003032 s[i] = '\0';
3033 fclose(from_file);
3034 parse_program(s + 1);
3035 free(s);
3036 } while (list_f);
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +00003037 argc++;
Denis Vlasenkof782f522007-01-01 23:51:30 +00003038 } else { // no -f: take program from 1st parameter
3039 if (!argc)
3040 bb_show_usage();
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003041 g_progname = "cmd. line";
Denis Vlasenkof782f522007-01-01 23:51:30 +00003042 parse_program(*argv++);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003043 }
Denis Vlasenko099efbf2006-09-22 09:02:30 +00003044 if (opt & 0x8) // -W
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00003045 bb_error_msg("warning: unrecognized option '-W %s' ignored", opt_W);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003046
Glenn L McGrath545106f2002-11-11 06:21:00 +00003047 /* fill in ARGV array */
Denis Vlasenko41d5ebe2009-01-25 01:00:15 +00003048 setvar_i(intvar[ARGC], argc);
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003049 setari_u(intvar[ARGV], 0, "awk");
Denis Vlasenkof782f522007-01-01 23:51:30 +00003050 i = 0;
3051 while (*argv)
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003052 setari_u(intvar[ARGV], ++i, *argv++);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003053
3054 evaluate(beginseq.first, &tv);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003055 if (!mainseq.first && !endseq.first)
Glenn L McGrath545106f2002-11-11 06:21:00 +00003056 awk_exit(EXIT_SUCCESS);
3057
3058 /* input file could already be opened in BEGIN block */
Denys Vlasenkocdeda162009-11-30 01:14:16 +01003059 if (!iF)
3060 iF = next_input_file();
Glenn L McGrath545106f2002-11-11 06:21:00 +00003061
3062 /* passing through input files */
3063 while (iF) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003064 nextfile = FALSE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003065 setvar_i(intvar[FNR], 0);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003066
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003067 while ((i = awk_getline(iF, intvar[F0])) > 0) {
Glenn L McGrath545106f2002-11-11 06:21:00 +00003068 nextrec = FALSE;
Denis Vlasenkoffba9412007-05-17 23:03:35 +00003069 incvar(intvar[NR]);
3070 incvar(intvar[FNR]);
Glenn L McGrath545106f2002-11-11 06:21:00 +00003071 evaluate(mainseq.first, &tv);
3072
3073 if (nextfile)
3074 break;
3075 }
3076
Denis Vlasenkof782f522007-01-01 23:51:30 +00003077 if (i < 0)
Denis Vlasenkoae5a8aa2007-06-06 17:01:00 +00003078 syntax_error(strerror(errno));
Glenn L McGrath545106f2002-11-11 06:21:00 +00003079
3080 iF = next_input_file();
Glenn L McGrath545106f2002-11-11 06:21:00 +00003081 }
3082
Glenn L McGrath545106f2002-11-11 06:21:00 +00003083 awk_exit(EXIT_SUCCESS);
Denis Vlasenkof782f522007-01-01 23:51:30 +00003084 /*return 0;*/
Glenn L McGrath545106f2002-11-11 06:21:00 +00003085}