Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1 | /* 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 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 11 | #include "xregex.h" |
| 12 | #include <math.h> |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 14 | /* This is a NOEXEC applet. Be very careful! */ |
| 15 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 16 | |
Denys Vlasenko | da62b09 | 2010-03-11 12:13:18 +0100 | [diff] [blame] | 17 | /* 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 Vlasenko | 629563b | 2007-02-24 17:05:52 +0000 | [diff] [blame] | 27 | #define MAXVARFMT 240 |
| 28 | #define MINNVBLOCK 64 |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 29 | |
| 30 | /* variable flags */ |
Denis Vlasenko | 629563b | 2007-02-24 17:05:52 +0000 | [diff] [blame] | 31 | #define VF_NUMBER 0x0001 /* 1 = primary type is number */ |
| 32 | #define VF_ARRAY 0x0002 /* 1 = it's an array */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | 629563b | 2007-02-24 17:05:52 +0000 | [diff] [blame] | 34 | #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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 41 | |
| 42 | /* these flags are static, don't change them when value is changed */ |
Denis Vlasenko | 629563b | 2007-02-24 17:05:52 +0000 | [diff] [blame] | 43 | #define VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 44 | |
Denys Vlasenko | da62b09 | 2010-03-11 12:13:18 +0100 | [diff] [blame] | 45 | typedef struct walker_list { |
| 46 | char *end; |
| 47 | char *cur; |
| 48 | struct walker_list *prev; |
| 49 | char wbuf[1]; |
| 50 | } walker_list; |
| 51 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 52 | /* Variable */ |
| 53 | typedef struct var_s { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 54 | unsigned type; /* flags */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 55 | double number; |
| 56 | char *string; |
| 57 | union { |
Denis Vlasenko | 629563b | 2007-02-24 17:05:52 +0000 | [diff] [blame] | 58 | 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 Vlasenko | da62b09 | 2010-03-11 12:13:18 +0100 | [diff] [blame] | 61 | walker_list *walker; /* list of array elements (for..in) */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 62 | } x; |
| 63 | } var; |
| 64 | |
| 65 | /* Node chain (pattern-action chain, BEGIN, END, function bodies) */ |
| 66 | typedef struct chain_s { |
| 67 | struct node_s *first; |
| 68 | struct node_s *last; |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 69 | const char *programname; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 70 | } chain; |
| 71 | |
| 72 | /* Function */ |
| 73 | typedef struct func_s { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 74 | unsigned nargs; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 75 | struct chain_s body; |
| 76 | } func; |
| 77 | |
| 78 | /* I/O stream */ |
| 79 | typedef struct rstream_s { |
| 80 | FILE *F; |
| 81 | char *buffer; |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 82 | int adv; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 83 | int size; |
| 84 | int pos; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 85 | smallint is_pipe; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 86 | } rstream; |
| 87 | |
| 88 | typedef struct hash_item_s { |
| 89 | union { |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 90 | struct var_s v; /* variable/array hash */ |
| 91 | struct rstream_s rs; /* redirect streams hash */ |
| 92 | struct func_s f; /* functions hash */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 93 | } data; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 94 | struct hash_item_s *next; /* next in chain */ |
| 95 | char name[1]; /* really it's longer */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 96 | } hash_item; |
| 97 | |
| 98 | typedef struct xhash_s { |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 99 | 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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 103 | struct hash_item_s **items; |
| 104 | } xhash; |
| 105 | |
| 106 | /* Tree node */ |
| 107 | typedef struct node_s { |
Mike Frysinger | f87b3e3 | 2005-09-27 04:16:22 +0000 | [diff] [blame] | 108 | uint32_t info; |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 109 | unsigned lineno; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 110 | union { |
| 111 | struct node_s *n; |
| 112 | var *v; |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 113 | int aidx; |
| 114 | char *new_progname; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 115 | regex_t *re; |
| 116 | } l; |
| 117 | union { |
| 118 | struct node_s *n; |
| 119 | regex_t *ire; |
| 120 | func *f; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 121 | } r; |
| 122 | union { |
| 123 | struct node_s *n; |
| 124 | } a; |
| 125 | } node; |
| 126 | |
| 127 | /* Block of temporary variables */ |
| 128 | typedef struct nvblock_s { |
| 129 | int size; |
| 130 | var *pos; |
| 131 | struct nvblock_s *prev; |
| 132 | struct nvblock_s *next; |
Denys Vlasenko | d069e53 | 2009-09-09 23:12:10 +0200 | [diff] [blame] | 133 | var nv[]; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 134 | } nvblock; |
| 135 | |
| 136 | typedef 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 Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 174 | #define TC_UOPPRE (TC_UOPPRE1 | TC_UOPPRE2) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 175 | |
| 176 | /* combined token classes */ |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 177 | #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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 181 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 182 | #define TC_STATEMNT (TC_STATX | TC_WHILE) |
| 183 | #define TC_OPTERM (TC_SEMICOL | TC_NEWLINE) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 184 | |
| 185 | /* word tokens, cannot mean something else if not expected */ |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 186 | #define TC_WORD (TC_IN | TC_STATEMNT | TC_ELSE | TC_BUILTIN \ |
| 187 | | TC_GETLINE | TC_FUNCDECL | TC_BEGIN | TC_END) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 188 | |
| 189 | /* discard newlines after these */ |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 190 | #define TC_NOTERM (TC_COMMA | TC_GRPSTART | TC_GRPTERM \ |
| 191 | | TC_BINOP | TC_OPTERM) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 192 | |
| 193 | /* what can expression begin with */ |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 194 | #define TC_OPSEQ (TC_OPERAND | TC_UOPPRE | TC_REGEXP) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 195 | /* what can group begin with */ |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 196 | #define TC_GRPSEQ (TC_OPSEQ | TC_OPTERM | TC_STATEMNT | TC_GRPSTART) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 197 | |
| 198 | /* if previous token class is CONCAT1 and next is CONCAT2, concatenation */ |
| 199 | /* operator is inserted between them */ |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 200 | #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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 203 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 204 | #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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 210 | |
| 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 Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 223 | #define OPCLSMASK 0xFF00 |
| 224 | #define OPNMASK 0x007F |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 225 | |
| 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 Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 230 | #define P(x) (x << 24) |
| 231 | #define PRIMASK 0x7F000000 |
| 232 | #define PRIMASK2 0x7E000000 |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 233 | |
| 234 | /* Operation classes */ |
| 235 | |
| 236 | #define SHIFT_TIL_THIS 0x0600 |
| 237 | #define RECUR_FROM_THIS 0x1000 |
| 238 | |
| 239 | enum { |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 240 | OC_DELETE = 0x0100, OC_EXEC = 0x0200, OC_NEWSOURCE = 0x0300, |
| 241 | OC_PRINT = 0x0400, OC_PRINTF = 0x0500, OC_WALKINIT = 0x0600, |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 242 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 243 | 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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 246 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 247 | 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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 256 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 257 | ST_IF = 0x3000, ST_DO = 0x3100, ST_FOR = 0x3200, |
| 258 | ST_WHILE = 0x3300 |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | /* simple builtins */ |
| 262 | enum { |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 263 | F_in, F_rn, F_co, F_ex, F_lg, F_si, F_sq, F_sr, |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 264 | F_ti, F_le, F_sy, F_ff, F_cl |
| 265 | }; |
| 266 | |
| 267 | /* builtins */ |
| 268 | enum { |
Leonid Lisovskiy | 46a0be5 | 2009-09-21 04:08:08 +0200 | [diff] [blame] | 269 | B_a2, B_ix, B_ma, B_sp, B_ss, B_ti, B_mt, B_lo, B_up, |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 270 | B_ge, B_gs, B_su, |
| 271 | B_an, B_co, B_ls, B_or, B_rs, B_xo, |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | /* tokens and their corresponding info values */ |
| 275 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 276 | #define NTC "\377" /* switch to next token class (tc<<1) */ |
| 277 | #define NTCC '\377' |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 278 | |
| 279 | #define OC_B OC_BUILTIN |
| 280 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 281 | static const char tokenlist[] ALIGN1 = |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 282 | "\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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 310 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 311 | "\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 Lisovskiy | 46a0be5 | 2009-09-21 04:08:08 +0200 | [diff] [blame] | 318 | "\6substr" "\7systime" "\10strftime" "\6mktime" |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 319 | "\7tolower" "\7toupper" NTC |
| 320 | "\7getline" NTC |
| 321 | "\4func" "\10function" NTC |
| 322 | "\5BEGIN" NTC |
| 323 | "\3END" "\0" |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 324 | ; |
| 325 | |
"Vladimir N. Oleynik" | 5cf9a03 | 2005-10-19 09:21:51 +0000 | [diff] [blame] | 326 | static const uint32_t tokeninfo[] = { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 327 | 0, |
| 328 | 0, |
| 329 | OC_REGEXP, |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 330 | 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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 348 | OC_IN|SV|P(49), |
| 349 | OC_COMMA|SS|P(80), |
| 350 | OC_PGETLINE|SV|P(37), |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 351 | OC_UNARY|xV|P(19)|'+', OC_UNARY|xV|P(19)|'-', |
| 352 | OC_UNARY|xV|P(19)|'!', |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 353 | 0, |
| 354 | 0, |
| 355 | 0, |
| 356 | 0, |
| 357 | 0, |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 358 | 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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 362 | ST_WHILE, |
| 363 | 0, |
| 364 | |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 365 | 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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 367 | 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 Lisovskiy | 46a0be5 | 2009-09-21 04:08:08 +0200 | [diff] [blame] | 372 | OC_B|B_ss|P(0x8f), OC_FBLTIN|F_ti, OC_B|B_ti|P(0x0b), OC_B|B_mt|P(0x0b), |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 373 | 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 Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 381 | /* asterisk marks SPECIAL vars; $ is just no-named Field0 */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 382 | enum { |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 383 | CONVFMT, OFMT, FS, OFS, |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 384 | ORS, RS, RT, FILENAME, |
Denis Vlasenko | 41d5ebe | 2009-01-25 01:00:15 +0000 | [diff] [blame] | 385 | SUBSEP, F0, ARGIND, ARGC, |
| 386 | ARGV, ERRNO, FNR, NR, |
| 387 | NF, IGNORECASE, ENVIRON, NUM_INTERNAL_VARS |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 388 | }; |
| 389 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 390 | static const char vNames[] ALIGN1 = |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 391 | "CONVFMT\0" "OFMT\0" "FS\0*" "OFS\0" |
| 392 | "ORS\0" "RS\0*" "RT\0" "FILENAME\0" |
Denis Vlasenko | 41d5ebe | 2009-01-25 01:00:15 +0000 | [diff] [blame] | 393 | "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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 396 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 397 | static const char vValues[] ALIGN1 = |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 398 | "%.6g\0" "%.6g\0" " \0" " \0" |
| 399 | "\n\0" "\n\0" "\0" "\0" |
Denis Vlasenko | 41d5ebe | 2009-01-25 01:00:15 +0000 | [diff] [blame] | 400 | "\034\0" "\0" "\377"; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 401 | |
| 402 | /* hash size may grow to these values */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 403 | #define FIRST_PRIME 61 |
| 404 | static const uint16_t PRIMES[] ALIGN2 = { 251, 1021, 4093, 16381, 65521 }; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 405 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 406 | |
Denis Vlasenko | e16e6e7 | 2007-06-07 13:14:53 +0000 | [diff] [blame] | 407 | /* Globals. Split in two parts so that first one is addressed |
Denis Vlasenko | 9aa5c65 | 2009-02-26 11:21:04 +0000 | [diff] [blame] | 408 | * 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 Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 412 | struct globals { |
Denis Vlasenko | 9aa5c65 | 2009-02-26 11:21:04 +0000 | [diff] [blame] | 413 | double t_double; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 414 | chain beginseq, mainseq, endseq; |
| 415 | chain *seq; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 416 | 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 Vlasenko | e16e6e7 | 2007-06-07 13:14:53 +0000 | [diff] [blame] | 427 | smallint icase; |
| 428 | smallint exiting; |
| 429 | smallint nextrec; |
| 430 | smallint nextfile; |
| 431 | smallint is_f0_split; |
| 432 | }; |
| 433 | struct 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 Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 441 | |
| 442 | /* former statics from various functions */ |
| 443 | char *split_f0__fstrings; |
| 444 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 445 | uint32_t next_token__save_tclass; |
| 446 | uint32_t next_token__save_info; |
| 447 | uint32_t next_token__ltclass; |
Denis Vlasenko | e16e6e7 | 2007-06-07 13:14:53 +0000 | [diff] [blame] | 448 | smallint next_token__concat_inserted; |
| 449 | |
| 450 | smallint next_input_file__files_happen; |
| 451 | rstream next_input_file__rsm; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 452 | |
| 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 Vlasenko | e16e6e7 | 2007-06-07 13:14:53 +0000 | [diff] [blame] | 461 | /* biggest and least used members go last */ |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 462 | tsplitter fsplitter, rsplitter; |
Denys Vlasenko | 3dbc5a9 | 2010-02-05 14:54:22 +0100 | [diff] [blame] | 463 | }; |
Denis Vlasenko | e16e6e7 | 2007-06-07 13:14:53 +0000 | [diff] [blame] | 464 | #define G1 (ptr_to_globals[-1]) |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 465 | #define G (*(struct globals2 *)ptr_to_globals) |
Denis Vlasenko | e16e6e7 | 2007-06-07 13:14:53 +0000 | [diff] [blame] | 466 | /* For debug. nm --size-sort awk.o | grep -vi ' [tr] ' */ |
Denis Vlasenko | 9aa5c65 | 2009-02-26 11:21:04 +0000 | [diff] [blame] | 467 | /*char G1size[sizeof(G1)]; - 0x74 */ |
| 468 | /*char Gsize[sizeof(G)]; - 0x1c4 */ |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 469 | /* Trying to keep most of members accessible with short offsets: */ |
Denis Vlasenko | 9aa5c65 | 2009-02-26 11:21:04 +0000 | [diff] [blame] | 470 | /*char Gofs_seed[offsetof(struct globals2, evaluate__seed)]; - 0x90 */ |
| 471 | #define t_double (G1.t_double ) |
Denis Vlasenko | e16e6e7 | 2007-06-07 13:14:53 +0000 | [diff] [blame] | 472 | #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 Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 496 | #define t_info (G.t_info ) |
| 497 | #define t_tclass (G.t_tclass ) |
| 498 | #define t_string (G.t_string ) |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 499 | #define t_lineno (G.t_lineno ) |
| 500 | #define t_rollback (G.t_rollback ) |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 501 | #define intvar (G.intvar ) |
| 502 | #define fsplitter (G.fsplitter ) |
| 503 | #define rsplitter (G.rsplitter ) |
| 504 | #define INIT_G() do { \ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 505 | SET_PTR_TO_GLOBALS((char*)xzalloc(sizeof(G1)+sizeof(G)) + sizeof(G1)); \ |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 506 | G.next_token__ltclass = TC_OPTERM; \ |
| 507 | G.evaluate__seed = 1; \ |
| 508 | } while (0) |
| 509 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 510 | |
| 511 | /* function prototypes */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 512 | static void handle_special(var *); |
Mike Frysinger | f87b3e3 | 2005-09-27 04:16:22 +0000 | [diff] [blame] | 513 | static node *parse_expr(uint32_t); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 514 | static void chain_group(void); |
| 515 | static var *evaluate(node *, var *); |
| 516 | static rstream *next_input_file(void); |
"Vladimir N. Oleynik" | 5cf9a03 | 2005-10-19 09:21:51 +0000 | [diff] [blame] | 517 | static int fmt_num(char *, int, const char *, double, int); |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 518 | static int awk_exit(int) NORETURN; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 519 | |
| 520 | /* ---- error handling ---- */ |
| 521 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 522 | static const char EMSG_INTERNAL_ERROR[] ALIGN1 = "Internal error"; |
| 523 | static const char EMSG_UNEXP_EOS[] ALIGN1 = "Unexpected end of string"; |
| 524 | static const char EMSG_UNEXP_TOKEN[] ALIGN1 = "Unexpected token"; |
| 525 | static const char EMSG_DIV_BY_ZERO[] ALIGN1 = "Division by zero"; |
| 526 | static const char EMSG_INV_FMT[] ALIGN1 = "Invalid format specifier"; |
| 527 | static const char EMSG_TOO_FEW_ARGS[] ALIGN1 = "Too few arguments for builtin"; |
| 528 | static const char EMSG_NOT_ARRAY[] ALIGN1 = "Not an array"; |
| 529 | static const char EMSG_POSSIBLE_ERROR[] ALIGN1 = "Possible syntax error"; |
| 530 | static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function"; |
Denis Vlasenko | 2d5bd80 | 2008-10-24 10:49:49 +0000 | [diff] [blame] | 531 | #if !ENABLE_FEATURE_AWK_LIBM |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 532 | static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in"; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 533 | #endif |
| 534 | |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 535 | static void zero_out_var(var *vp) |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 536 | { |
| 537 | memset(vp, 0, sizeof(*vp)); |
| 538 | } |
| 539 | |
Denis Vlasenko | c7cc5a9 | 2009-04-19 01:27:20 +0000 | [diff] [blame] | 540 | static void syntax_error(const char *message) NORETURN; |
| 541 | static void syntax_error(const char *message) |
Glenn L McGrath | d4036f8 | 2002-11-28 09:30:40 +0000 | [diff] [blame] | 542 | { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 543 | bb_error_msg_and_die("%s:%i: %s", g_progname, g_lineno, message); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 546 | /* ---- hash stuff ---- */ |
| 547 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 548 | static unsigned hashidx(const char *name) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 549 | { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 550 | unsigned idx = 0; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 551 | |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 552 | while (*name) |
| 553 | idx = *name++ + (idx << 6) - idx; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 554 | return idx; |
| 555 | } |
| 556 | |
| 557 | /* create new hash */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 558 | static xhash *hash_init(void) |
| 559 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 560 | xhash *newhash; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 561 | |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 562 | newhash = xzalloc(sizeof(*newhash)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 563 | newhash->csize = FIRST_PRIME; |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 564 | newhash->items = xzalloc(FIRST_PRIME * sizeof(newhash->items[0])); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 565 | |
| 566 | return newhash; |
| 567 | } |
| 568 | |
| 569 | /* find item in hash, return ptr to data, NULL if not found */ |
"Vladimir N. Oleynik" | 5cf9a03 | 2005-10-19 09:21:51 +0000 | [diff] [blame] | 570 | static void *hash_search(xhash *hash, const char *name) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 571 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 572 | hash_item *hi; |
| 573 | |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 574 | hi = hash->items[hashidx(name) % hash->csize]; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 575 | while (hi) { |
| 576 | if (strcmp(hi->name, name) == 0) |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 577 | return &hi->data; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 578 | hi = hi->next; |
| 579 | } |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | /* grow hash if it becomes too big */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 584 | static void hash_rebuild(xhash *hash) |
| 585 | { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 586 | unsigned newsize, i, idx; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 587 | hash_item **newitems, *hi, *thi; |
| 588 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 589 | if (hash->nprime == ARRAY_SIZE(PRIMES)) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 590 | return; |
| 591 | |
| 592 | newsize = PRIMES[hash->nprime++]; |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 593 | newitems = xzalloc(newsize * sizeof(newitems[0])); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 594 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 595 | for (i = 0; i < hash->csize; i++) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 596 | 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" | 5cf9a03 | 2005-10-19 09:21:51 +0000 | [diff] [blame] | 612 | static void *hash_find(xhash *hash, const char *name) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 613 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 614 | hash_item *hi; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 615 | unsigned idx; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 616 | int l; |
| 617 | |
| 618 | hi = hash_search(hash, name); |
Denis Vlasenko | b78c782 | 2007-07-18 18:31:11 +0000 | [diff] [blame] | 619 | if (!hi) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 620 | if (++hash->nel / hash->csize > 10) |
| 621 | hash_rebuild(hash); |
| 622 | |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 623 | l = strlen(name) + 1; |
Denis Vlasenko | 7a67664 | 2009-03-15 22:20:31 +0000 | [diff] [blame] | 624 | hi = xzalloc(sizeof(*hi) + l); |
| 625 | strcpy(hi->name, name); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 626 | |
| 627 | idx = hashidx(name) % hash->csize; |
| 628 | hi->next = hash->items[idx]; |
| 629 | hash->items[idx] = hi; |
| 630 | hash->glen += l; |
| 631 | } |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 632 | return &hi->data; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 635 | #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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 639 | |
"Vladimir N. Oleynik" | 5cf9a03 | 2005-10-19 09:21:51 +0000 | [diff] [blame] | 640 | static void hash_remove(xhash *hash, const char *name) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 641 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 642 | hash_item *hi, **phi; |
| 643 | |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 644 | phi = &hash->items[hashidx(name) % hash->csize]; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 645 | while (*phi) { |
| 646 | hi = *phi; |
| 647 | if (strcmp(hi->name, name) == 0) { |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 648 | hash->glen -= (strlen(name) + 1); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 649 | hash->nel--; |
| 650 | *phi = hi->next; |
| 651 | free(hi); |
| 652 | break; |
| 653 | } |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 654 | phi = &hi->next; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
| 658 | /* ------ some useful functions ------ */ |
| 659 | |
Denys Vlasenko | b0a57ab | 2010-03-11 12:44:25 +0100 | [diff] [blame] | 660 | static char *skip_spaces(char *p) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 661 | { |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 662 | while (1) { |
| 663 | if (*p == '\\' && p[1] == '\n') { |
| 664 | p++; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 665 | t_lineno++; |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 666 | } else if (*p != ' ' && *p != '\t') { |
| 667 | break; |
| 668 | } |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 669 | p++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 670 | } |
Denys Vlasenko | b0a57ab | 2010-03-11 12:44:25 +0100 | [diff] [blame] | 671 | return p; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 674 | /* returns old *s, advances *s past word and terminating NUL */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 675 | static char *nextword(char **s) |
| 676 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 677 | char *p = *s; |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 678 | while (*(*s)++ != '\0') |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 679 | continue; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 680 | return p; |
| 681 | } |
| 682 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 683 | static char nextchar(char **s) |
| 684 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 685 | char c, *pps; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 686 | |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 687 | c = *(*s)++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 688 | pps = *s; |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 689 | if (c == '\\') |
| 690 | c = bb_process_escape_sequence((const char**)s); |
| 691 | if (c == '\\' && *s == pps) |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 692 | c = *(*s)++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 693 | return c; |
| 694 | } |
| 695 | |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 696 | static ALWAYS_INLINE int isalnum_(int c) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 697 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 698 | return (isalnum(c) || c == '_'); |
| 699 | } |
| 700 | |
Denis Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 701 | static 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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 713 | /* -------- working with variables (set/get/copy/etc) -------- */ |
| 714 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 715 | static xhash *iamarray(var *v) |
| 716 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 717 | var *a = v; |
| 718 | |
| 719 | while (a->type & VF_CHILD) |
| 720 | a = a->x.parent; |
| 721 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 722 | if (!(a->type & VF_ARRAY)) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 723 | a->type |= VF_ARRAY; |
| 724 | a->x.array = hash_init(); |
| 725 | } |
| 726 | return a->x.array; |
| 727 | } |
| 728 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 729 | static void clear_array(xhash *array) |
| 730 | { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 731 | unsigned i; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 732 | hash_item *hi, *thi; |
| 733 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 734 | for (i = 0; i < array->csize; i++) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 735 | hi = array->items[i]; |
| 736 | while (hi) { |
| 737 | thi = hi; |
| 738 | hi = hi->next; |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 739 | free(thi->data.v.string); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 740 | free(thi); |
| 741 | } |
| 742 | array->items[i] = NULL; |
| 743 | } |
| 744 | array->glen = array->nel = 0; |
| 745 | } |
| 746 | |
| 747 | /* clear a variable */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 748 | static var *clrvar(var *v) |
| 749 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 750 | if (!(v->type & VF_FSTR)) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 751 | 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 Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 760 | static var *setvar_p(var *v, char *value) |
| 761 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 762 | clrvar(v); |
| 763 | v->string = value; |
| 764 | handle_special(v); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 765 | return v; |
| 766 | } |
| 767 | |
| 768 | /* same as setvar_p but make a copy of string */ |
"Vladimir N. Oleynik" | 5cf9a03 | 2005-10-19 09:21:51 +0000 | [diff] [blame] | 769 | static var *setvar_s(var *v, const char *value) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 770 | { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 771 | return setvar_p(v, (value && *value) ? xstrdup(value) : NULL); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 774 | /* same as setvar_s but sets USER flag */ |
"Vladimir N. Oleynik" | 5cf9a03 | 2005-10-19 09:21:51 +0000 | [diff] [blame] | 775 | static var *setvar_u(var *v, const char *value) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 776 | { |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 777 | v = setvar_s(v, value); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 778 | v->type |= VF_USER; |
| 779 | return v; |
| 780 | } |
| 781 | |
| 782 | /* set array element to user string */ |
"Vladimir N. Oleynik" | 5cf9a03 | 2005-10-19 09:21:51 +0000 | [diff] [blame] | 783 | static void setari_u(var *a, int idx, const char *s) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 784 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 785 | var *v; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 786 | |
Denys Vlasenko | 7bb346f | 2009-10-06 22:09:50 +0200 | [diff] [blame] | 787 | v = findvar(iamarray(a), itoa(idx)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 788 | setvar_u(v, s); |
| 789 | } |
| 790 | |
| 791 | /* assign numeric value to variable */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 792 | static var *setvar_i(var *v, double value) |
| 793 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 794 | clrvar(v); |
| 795 | v->type |= VF_NUMBER; |
| 796 | v->number = value; |
| 797 | handle_special(v); |
| 798 | return v; |
| 799 | } |
| 800 | |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 801 | static const char *getvar_s(var *v) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 802 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 803 | /* if v is numeric and has no cached string, convert it to string */ |
| 804 | if ((v->type & (VF_NUMBER | VF_CACHED)) == VF_NUMBER) { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 805 | fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[CONVFMT]), v->number, TRUE); |
| 806 | v->string = xstrdup(g_buf); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 807 | v->type |= VF_CACHED; |
| 808 | } |
| 809 | return (v->string == NULL) ? "" : v->string; |
| 810 | } |
| 811 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 812 | static double getvar_i(var *v) |
| 813 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 814 | 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 Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 820 | v->number = my_strtod(&s); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 821 | if (v->type & VF_USER) { |
Denys Vlasenko | b0a57ab | 2010-03-11 12:44:25 +0100 | [diff] [blame] | 822 | s = skip_spaces(s); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 823 | 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 Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 834 | /* Used for operands of bitwise ops */ |
| 835 | static 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 Vlasenko | 665eaff | 2008-09-05 04:59:02 +0000 | [diff] [blame] | 843 | /* Why? Think about d == -4294967295.0 (assuming 32bit longs) */ |
Denis Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 844 | return - (long) (unsigned long) (-d); |
| 845 | } |
| 846 | |
"Vladimir N. Oleynik" | 5cf9a03 | 2005-10-19 09:21:51 +0000 | [diff] [blame] | 847 | static var *copyvar(var *dest, const var *src) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 848 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 849 | if (dest != src) { |
| 850 | clrvar(dest); |
Denis Vlasenko | 629563b | 2007-02-24 17:05:52 +0000 | [diff] [blame] | 851 | dest->type |= (src->type & ~(VF_DONTTOUCH | VF_FSTR)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 852 | dest->number = src->number; |
| 853 | if (src->string) |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 854 | dest->string = xstrdup(src->string); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 855 | } |
| 856 | handle_special(dest); |
| 857 | return dest; |
| 858 | } |
| 859 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 860 | static var *incvar(var *v) |
| 861 | { |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 862 | return setvar_i(v, getvar_i(v) + 1.0); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /* return true if v is number or numeric string */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 866 | static int is_numeric(var *v) |
| 867 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 868 | 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 Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 873 | static int istrue(var *v) |
| 874 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 875 | if (is_numeric(v)) |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 876 | return (v->number != 0); |
| 877 | return (v->string && v->string[0]); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 880 | /* temporary variables allocator. Last allocated should be first freed */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 881 | static var *nvalloc(int n) |
| 882 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 883 | nvblock *pb = NULL; |
| 884 | var *v, *r; |
| 885 | int size; |
| 886 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 887 | while (g_cb) { |
| 888 | pb = g_cb; |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 889 | if ((g_cb->pos - g_cb->nv) + n <= g_cb->size) |
| 890 | break; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 891 | g_cb = g_cb->next; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 894 | if (!g_cb) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 895 | size = (n <= MINNVBLOCK) ? MINNVBLOCK : n; |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 896 | g_cb = xzalloc(sizeof(nvblock) + size * sizeof(var)); |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 897 | g_cb->size = size; |
| 898 | g_cb->pos = g_cb->nv; |
| 899 | g_cb->prev = pb; |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 900 | /*g_cb->next = NULL; - xzalloc did it */ |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 901 | if (pb) |
| 902 | pb->next = g_cb; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 905 | v = r = g_cb->pos; |
| 906 | g_cb->pos += n; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 907 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 908 | while (v < g_cb->pos) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 909 | v->type = 0; |
| 910 | v->string = NULL; |
| 911 | v++; |
| 912 | } |
| 913 | |
| 914 | return r; |
| 915 | } |
| 916 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 917 | static void nvfree(var *v) |
| 918 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 919 | var *p; |
| 920 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 921 | if (v < g_cb->nv || v >= g_cb->pos) |
| 922 | syntax_error(EMSG_INTERNAL_ERROR); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 923 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 924 | for (p = v; p < g_cb->pos; p++) { |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 925 | if ((p->type & (VF_ARRAY | VF_CHILD)) == VF_ARRAY) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 926 | clear_array(iamarray(p)); |
| 927 | free(p->x.array->items); |
| 928 | free(p->x.array); |
| 929 | } |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 930 | if (p->type & VF_WALK) { |
Denys Vlasenko | da62b09 | 2010-03-11 12:13:18 +0100 | [diff] [blame] | 931 | 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 Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 941 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 942 | clrvar(p); |
| 943 | } |
| 944 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 945 | g_cb->pos = v; |
| 946 | while (g_cb->prev && g_cb->pos == g_cb->nv) { |
| 947 | g_cb = g_cb->prev; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
| 951 | /* ------- awk program text parsing ------- */ |
| 952 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 953 | /* Parse next token pointed by global pos, place results into global ttt. |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 954 | * If token isn't expected, give away. Return token class |
| 955 | */ |
Mike Frysinger | f87b3e3 | 2005-09-27 04:16:22 +0000 | [diff] [blame] | 956 | static uint32_t next_token(uint32_t expected) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 957 | { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 958 | #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 McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 963 | |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 964 | char *p, *s; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 965 | const char *tl; |
| 966 | uint32_t tc; |
| 967 | const uint32_t *ti; |
| 968 | int l; |
| 969 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 970 | if (t_rollback) { |
| 971 | t_rollback = FALSE; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 972 | |
| 973 | } else if (concat_inserted) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 974 | concat_inserted = FALSE; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 975 | t_tclass = save_tclass; |
| 976 | t_info = save_info; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 977 | |
| 978 | } else { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 979 | p = g_pos; |
Denis Vlasenko | e1d3e03 | 2007-01-01 23:53:52 +0000 | [diff] [blame] | 980 | readnext: |
Denys Vlasenko | b0a57ab | 2010-03-11 12:44:25 +0100 | [diff] [blame] | 981 | p = skip_spaces(p); |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 982 | g_lineno = t_lineno; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 983 | if (*p == '#') |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 984 | while (*p != '\n' && *p != '\0') |
| 985 | p++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 986 | |
| 987 | if (*p == '\n') |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 988 | t_lineno++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 989 | |
| 990 | if (*p == '\0') { |
| 991 | tc = TC_EOF; |
| 992 | |
| 993 | } else if (*p == '\"') { |
| 994 | /* it's a string */ |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 995 | t_string = s = ++p; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 996 | while (*p != '\"') { |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 997 | char *pp = p; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 998 | if (*p == '\0' || *p == '\n') |
| 999 | syntax_error(EMSG_UNEXP_EOS); |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1000 | *s++ = nextchar(&pp); |
| 1001 | p = pp; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1002 | } |
| 1003 | p++; |
| 1004 | *s = '\0'; |
| 1005 | tc = TC_STRING; |
| 1006 | |
| 1007 | } else if ((expected & TC_REGEXP) && *p == '/') { |
| 1008 | /* it's regexp */ |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1009 | t_string = s = ++p; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1010 | while (*p != '/') { |
| 1011 | if (*p == '\0' || *p == '\n') |
| 1012 | syntax_error(EMSG_UNEXP_EOS); |
Denis Vlasenko | d9b5ab8 | 2007-05-18 07:30:43 +0000 | [diff] [blame] | 1013 | *s = *p++; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1014 | if (*s++ == '\\') { |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1015 | char *pp = p; |
| 1016 | s[-1] = bb_process_escape_sequence((const char **)&pp); |
| 1017 | if (*p == '\\') |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1018 | *s++ = '\\'; |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1019 | if (pp == p) |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1020 | *s++ = *p++; |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1021 | else |
| 1022 | p = pp; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | p++; |
| 1026 | *s = '\0'; |
| 1027 | tc = TC_REGEXP; |
| 1028 | |
| 1029 | } else if (*p == '.' || isdigit(*p)) { |
| 1030 | /* it's a number */ |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1031 | char *pp = p; |
| 1032 | t_double = my_strtod(&pp); |
| 1033 | p = pp; |
| 1034 | if (*pp == '.') |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1035 | 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 Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1044 | l = *tl++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1045 | 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 Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1053 | if ((tc & (expected | TC_WORD | TC_NEWLINE)) |
| 1054 | && *tl == *p && strncmp(p, tl, l) == 0 |
| 1055 | && !((tc & TC_WORD) && isalnum_(p[l])) |
| 1056 | ) { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1057 | t_info = *ti; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1058 | p += l; |
| 1059 | break; |
| 1060 | } |
| 1061 | ti++; |
| 1062 | tl += l; |
| 1063 | } |
| 1064 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1065 | if (!*tl) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1066 | /* it's a name (var/array/function), |
| 1067 | * otherwise it's something wrong |
| 1068 | */ |
Denis Vlasenko | e1d3e03 | 2007-01-01 23:53:52 +0000 | [diff] [blame] | 1069 | if (!isalnum_(*p)) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1070 | syntax_error(EMSG_UNEXP_TOKEN); |
| 1071 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1072 | t_string = --p; |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1073 | while (isalnum_(*++p)) { |
| 1074 | p[-1] = *p; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1075 | } |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1076 | p[-1] = '\0'; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1077 | tc = TC_VARIABLE; |
Bernhard Reutner-Fischer | bb20462 | 2005-10-17 14:21:06 +0000 | [diff] [blame] | 1078 | /* also consume whitespace between functionname and bracket */ |
Alexander Shishkin | d03cd3b | 2010-02-25 17:55:40 +0200 | [diff] [blame] | 1079 | if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY)) |
Denys Vlasenko | b0a57ab | 2010-03-11 12:44:25 +0100 | [diff] [blame] | 1080 | p = skip_spaces(p); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1081 | if (*p == '(') { |
| 1082 | tc = TC_FUNCTION; |
| 1083 | } else { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1084 | if (*p == '[') { |
| 1085 | p++; |
| 1086 | tc = TC_ARRAY; |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | } |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1091 | g_pos = p; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1092 | |
| 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 Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1098 | if ((ltclass & TC_CONCAT1) && (tc & TC_CONCAT2) && (expected & TC_BINOP)) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1099 | concat_inserted = TRUE; |
| 1100 | save_tclass = tc; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1101 | save_info = t_info; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1102 | tc = TC_BINOP; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1103 | t_info = OC_CONCAT | SS | P(35); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1106 | t_tclass = tc; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1107 | } |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1108 | ltclass = t_tclass; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1109 | |
| 1110 | /* Are we ready for this? */ |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1111 | if (!(ltclass & expected)) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1112 | syntax_error((ltclass & (TC_NEWLINE | TC_EOF)) ? |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1113 | EMSG_UNEXP_EOS : EMSG_UNEXP_TOKEN); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1114 | |
| 1115 | return ltclass; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1116 | #undef concat_inserted |
| 1117 | #undef save_tclass |
| 1118 | #undef save_info |
| 1119 | #undef ltclass |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 1122 | static void rollback_token(void) |
| 1123 | { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1124 | t_rollback = TRUE; |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 1125 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1126 | |
Mike Frysinger | f87b3e3 | 2005-09-27 04:16:22 +0000 | [diff] [blame] | 1127 | static node *new_node(uint32_t info) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1128 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1129 | node *n; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1130 | |
Denis Vlasenko | 4cccc03 | 2006-12-22 18:37:07 +0000 | [diff] [blame] | 1131 | n = xzalloc(sizeof(node)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1132 | n->info = info; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1133 | n->lineno = g_lineno; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1134 | return n; |
| 1135 | } |
| 1136 | |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 1137 | static node *mk_re_node(const char *s, node *n, regex_t *re) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1138 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1139 | n->info = OC_REGEXP; |
| 1140 | n->l.re = re; |
| 1141 | n->r.ire = re + 1; |
| 1142 | xregcomp(re, s, REG_EXTENDED); |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1143 | xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1144 | |
| 1145 | return n; |
| 1146 | } |
| 1147 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1148 | static node *condition(void) |
| 1149 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1150 | next_token(TC_SEQSTART); |
| 1151 | return parse_expr(TC_SEQTERM); |
| 1152 | } |
| 1153 | |
| 1154 | /* parse expression terminated by given argument, return ptr |
| 1155 | * to built subtree. Terminator is eaten by parse_expr */ |
Mike Frysinger | f87b3e3 | 2005-09-27 04:16:22 +0000 | [diff] [blame] | 1156 | static node *parse_expr(uint32_t iexp) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1157 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1158 | node sn; |
| 1159 | node *cn = &sn; |
| 1160 | node *vn, *glptr; |
Mike Frysinger | f87b3e3 | 2005-09-27 04:16:22 +0000 | [diff] [blame] | 1161 | uint32_t tc, xtc; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1162 | var *v; |
| 1163 | |
| 1164 | sn.info = PRIMASK; |
| 1165 | sn.r.n = glptr = NULL; |
| 1166 | xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp; |
| 1167 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 1168 | while (!((tc = next_token(xtc)) & iexp)) { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1169 | if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1170 | /* input redirection (<) attached to glptr node */ |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1171 | cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37)); |
Glenn L McGrath | 4bded58 | 2004-02-22 11:55:09 +0000 | [diff] [blame] | 1172 | cn->a.n = glptr; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1173 | xtc = TC_OPERAND | TC_UOPPRE; |
| 1174 | glptr = NULL; |
| 1175 | |
| 1176 | } else if (tc & (TC_BINOP | TC_UOPPOST)) { |
| 1177 | /* for binary and postfix-unary operators, jump back over |
| 1178 | * previous operators with higher priority */ |
| 1179 | vn = cn; |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 1180 | while (((t_info & PRIMASK) > (vn->a.n->info & PRIMASK2)) |
| 1181 | || ((t_info == vn->info) && ((t_info & OPCLSMASK) == OC_COLON)) |
| 1182 | ) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1183 | vn = vn->a.n; |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 1184 | } |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1185 | if ((t_info & OPCLSMASK) == OC_TERNARY) |
| 1186 | t_info += P(6); |
| 1187 | cn = vn->a.n->r.n = new_node(t_info); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1188 | cn->a.n = vn->a.n; |
| 1189 | if (tc & TC_BINOP) { |
| 1190 | cn->l.n = vn; |
| 1191 | xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1192 | if ((t_info & OPCLSMASK) == OC_PGETLINE) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1193 | /* it's a pipe */ |
| 1194 | next_token(TC_GETLINE); |
| 1195 | /* give maximum priority to this pipe */ |
| 1196 | cn->info &= ~PRIMASK; |
| 1197 | xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp; |
| 1198 | } |
| 1199 | } else { |
| 1200 | cn->r.n = vn; |
| 1201 | xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp; |
| 1202 | } |
| 1203 | vn->a.n = cn; |
| 1204 | |
| 1205 | } else { |
| 1206 | /* for operands and prefix-unary operators, attach them |
| 1207 | * to last node */ |
| 1208 | vn = cn; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1209 | cn = vn->r.n = new_node(t_info); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1210 | cn->a.n = vn; |
| 1211 | xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP; |
| 1212 | if (tc & (TC_OPERAND | TC_REGEXP)) { |
Rob Landley | ed830e8 | 2005-06-07 02:43:52 +0000 | [diff] [blame] | 1213 | xtc = TC_UOPPRE | TC_UOPPOST | TC_BINOP | TC_OPERAND | iexp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1214 | /* one should be very careful with switch on tclass - |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1215 | * only simple tclasses should be used! */ |
| 1216 | switch (tc) { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1217 | case TC_VARIABLE: |
| 1218 | case TC_ARRAY: |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1219 | cn->info = OC_VAR; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1220 | v = hash_search(ahash, t_string); |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1221 | if (v != NULL) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1222 | cn->info = OC_FNARG; |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 1223 | cn->l.aidx = v->x.aidx; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1224 | } else { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1225 | cn->l.v = newvar(t_string); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1226 | } |
| 1227 | if (tc & TC_ARRAY) { |
| 1228 | cn->info |= xS; |
| 1229 | cn->r.n = parse_expr(TC_ARRTERM); |
| 1230 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1231 | break; |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 1232 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1233 | case TC_NUMBER: |
| 1234 | case TC_STRING: |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1235 | cn->info = OC_VAR; |
Rob Landley | 9ffd423 | 2006-05-21 18:30:35 +0000 | [diff] [blame] | 1236 | v = cn->l.v = xzalloc(sizeof(var)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1237 | if (tc & TC_NUMBER) |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1238 | setvar_i(v, t_double); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1239 | else |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1240 | setvar_s(v, t_string); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1241 | break; |
| 1242 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1243 | case TC_REGEXP: |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1244 | mk_re_node(t_string, cn, xzalloc(sizeof(regex_t)*2)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1245 | break; |
| 1246 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1247 | case TC_FUNCTION: |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 1248 | cn->info = OC_FUNC; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1249 | cn->r.f = newfunc(t_string); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1250 | cn->l.n = condition(); |
| 1251 | break; |
| 1252 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1253 | case TC_SEQSTART: |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1254 | cn = vn->r.n = parse_expr(TC_SEQTERM); |
| 1255 | cn->a.n = vn; |
| 1256 | break; |
| 1257 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1258 | case TC_GETLINE: |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1259 | glptr = cn; |
| 1260 | xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp; |
| 1261 | break; |
| 1262 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1263 | case TC_BUILTIN: |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1264 | cn->l.n = condition(); |
| 1265 | break; |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | return sn.r.n; |
| 1271 | } |
| 1272 | |
| 1273 | /* add node to chain. Return ptr to alloc'd node */ |
Mike Frysinger | f87b3e3 | 2005-09-27 04:16:22 +0000 | [diff] [blame] | 1274 | static node *chain_node(uint32_t info) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1275 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1276 | node *n; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1277 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 1278 | if (!seq->first) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1279 | seq->first = seq->last = new_node(0); |
| 1280 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1281 | if (seq->programname != g_progname) { |
| 1282 | seq->programname = g_progname; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1283 | n = chain_node(OC_NEWSOURCE); |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 1284 | n->l.new_progname = xstrdup(g_progname); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | n = seq->last; |
| 1288 | n->info = info; |
| 1289 | seq->last = n->a.n = new_node(OC_DONE); |
| 1290 | |
| 1291 | return n; |
| 1292 | } |
| 1293 | |
Mike Frysinger | f87b3e3 | 2005-09-27 04:16:22 +0000 | [diff] [blame] | 1294 | static void chain_expr(uint32_t info) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1295 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1296 | node *n; |
| 1297 | |
| 1298 | n = chain_node(info); |
| 1299 | n->l.n = parse_expr(TC_OPTERM | TC_GRPTERM); |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1300 | if (t_tclass & TC_GRPTERM) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1301 | rollback_token(); |
| 1302 | } |
| 1303 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1304 | static node *chain_loop(node *nn) |
| 1305 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1306 | node *n, *n2, *save_brk, *save_cont; |
| 1307 | |
| 1308 | save_brk = break_ptr; |
| 1309 | save_cont = continue_ptr; |
| 1310 | |
| 1311 | n = chain_node(OC_BR | Vx); |
| 1312 | continue_ptr = new_node(OC_EXEC); |
| 1313 | break_ptr = new_node(OC_EXEC); |
| 1314 | chain_group(); |
| 1315 | n2 = chain_node(OC_EXEC | Vx); |
| 1316 | n2->l.n = nn; |
| 1317 | n2->a.n = n; |
| 1318 | continue_ptr->a.n = n2; |
| 1319 | break_ptr->a.n = n->r.n = seq->last; |
| 1320 | |
| 1321 | continue_ptr = save_cont; |
| 1322 | break_ptr = save_brk; |
| 1323 | |
| 1324 | return n; |
| 1325 | } |
| 1326 | |
| 1327 | /* parse group and attach it to chain */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1328 | static void chain_group(void) |
| 1329 | { |
Mike Frysinger | f87b3e3 | 2005-09-27 04:16:22 +0000 | [diff] [blame] | 1330 | uint32_t c; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1331 | node *n, *n2, *n3; |
| 1332 | |
| 1333 | do { |
| 1334 | c = next_token(TC_GRPSEQ); |
| 1335 | } while (c & TC_NEWLINE); |
| 1336 | |
| 1337 | if (c & TC_GRPSTART) { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 1338 | while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) { |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1339 | if (t_tclass & TC_NEWLINE) |
| 1340 | continue; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1341 | rollback_token(); |
| 1342 | chain_group(); |
| 1343 | } |
| 1344 | } else if (c & (TC_OPSEQ | TC_OPTERM)) { |
| 1345 | rollback_token(); |
| 1346 | chain_expr(OC_EXEC | Vx); |
| 1347 | } else { /* TC_STATEMNT */ |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1348 | switch (t_info & OPCLSMASK) { |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1349 | case ST_IF: |
| 1350 | n = chain_node(OC_BR | Vx); |
| 1351 | n->l.n = condition(); |
| 1352 | chain_group(); |
| 1353 | n2 = chain_node(OC_EXEC); |
| 1354 | n->r.n = seq->last; |
| 1355 | if (next_token(TC_GRPSEQ | TC_GRPTERM | TC_ELSE) == TC_ELSE) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1356 | chain_group(); |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1357 | n2->a.n = seq->last; |
| 1358 | } else { |
| 1359 | rollback_token(); |
| 1360 | } |
| 1361 | break; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1362 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1363 | case ST_WHILE: |
| 1364 | n2 = condition(); |
| 1365 | n = chain_loop(NULL); |
| 1366 | n->l.n = n2; |
| 1367 | break; |
| 1368 | |
| 1369 | case ST_DO: |
| 1370 | n2 = chain_node(OC_EXEC); |
| 1371 | n = chain_loop(NULL); |
| 1372 | n2->a.n = n->a.n; |
| 1373 | next_token(TC_WHILE); |
| 1374 | n->l.n = condition(); |
| 1375 | break; |
| 1376 | |
| 1377 | case ST_FOR: |
| 1378 | next_token(TC_SEQSTART); |
| 1379 | n2 = parse_expr(TC_SEMICOL | TC_SEQTERM); |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1380 | if (t_tclass & TC_SEQTERM) { /* for-in */ |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1381 | if ((n2->info & OPCLSMASK) != OC_IN) |
| 1382 | syntax_error(EMSG_UNEXP_TOKEN); |
| 1383 | n = chain_node(OC_WALKINIT | VV); |
| 1384 | n->l.n = n2->l.n; |
| 1385 | n->r.n = n2->r.n; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1386 | n = chain_loop(NULL); |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1387 | n->info = OC_WALKNEXT | Vx; |
| 1388 | n->l.n = n2->l.n; |
| 1389 | } else { /* for (;;) */ |
| 1390 | n = chain_node(OC_EXEC | Vx); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1391 | n->l.n = n2; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1392 | n2 = parse_expr(TC_SEMICOL); |
| 1393 | n3 = parse_expr(TC_SEQTERM); |
| 1394 | n = chain_loop(n3); |
| 1395 | n->l.n = n2; |
Denis Vlasenko | b78c782 | 2007-07-18 18:31:11 +0000 | [diff] [blame] | 1396 | if (!n2) |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1397 | n->info = OC_EXEC; |
| 1398 | } |
| 1399 | break; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1400 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1401 | case OC_PRINT: |
| 1402 | case OC_PRINTF: |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1403 | n = chain_node(t_info); |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1404 | n->l.n = parse_expr(TC_OPTERM | TC_OUTRDR | TC_GRPTERM); |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1405 | if (t_tclass & TC_OUTRDR) { |
| 1406 | n->info |= t_info; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1407 | n->r.n = parse_expr(TC_OPTERM | TC_GRPTERM); |
| 1408 | } |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1409 | if (t_tclass & TC_GRPTERM) |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1410 | rollback_token(); |
| 1411 | break; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1412 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1413 | case OC_BREAK: |
| 1414 | n = chain_node(OC_EXEC); |
| 1415 | n->a.n = break_ptr; |
| 1416 | break; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1417 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1418 | case OC_CONTINUE: |
| 1419 | n = chain_node(OC_EXEC); |
| 1420 | n->a.n = continue_ptr; |
| 1421 | break; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1422 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1423 | /* delete, next, nextfile, return, exit */ |
| 1424 | default: |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1425 | chain_expr(t_info); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1430 | static void parse_program(char *p) |
| 1431 | { |
Mike Frysinger | f87b3e3 | 2005-09-27 04:16:22 +0000 | [diff] [blame] | 1432 | uint32_t tclass; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1433 | node *cn; |
| 1434 | func *f; |
| 1435 | var *v; |
| 1436 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1437 | g_pos = p; |
| 1438 | t_lineno = 1; |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 1439 | while ((tclass = next_token(TC_EOF | TC_OPSEQ | TC_GRPSTART | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1440 | TC_OPTERM | TC_BEGIN | TC_END | TC_FUNCDECL)) != TC_EOF) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1441 | |
| 1442 | if (tclass & TC_OPTERM) |
| 1443 | continue; |
| 1444 | |
| 1445 | seq = &mainseq; |
| 1446 | if (tclass & TC_BEGIN) { |
| 1447 | seq = &beginseq; |
| 1448 | chain_group(); |
| 1449 | |
| 1450 | } else if (tclass & TC_END) { |
| 1451 | seq = &endseq; |
| 1452 | chain_group(); |
| 1453 | |
| 1454 | } else if (tclass & TC_FUNCDECL) { |
| 1455 | next_token(TC_FUNCTION); |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1456 | g_pos++; |
| 1457 | f = newfunc(t_string); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1458 | f->body.first = NULL; |
| 1459 | f->nargs = 0; |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 1460 | while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1461 | v = findvar(ahash, t_string); |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 1462 | v->x.aidx = f->nargs++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1463 | |
| 1464 | if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM) |
| 1465 | break; |
| 1466 | } |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 1467 | seq = &f->body; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1468 | chain_group(); |
| 1469 | clear_array(ahash); |
| 1470 | |
| 1471 | } else if (tclass & TC_OPSEQ) { |
| 1472 | rollback_token(); |
| 1473 | cn = chain_node(OC_TEST); |
| 1474 | cn->l.n = parse_expr(TC_OPTERM | TC_EOF | TC_GRPSTART); |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1475 | if (t_tclass & TC_GRPSTART) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1476 | rollback_token(); |
| 1477 | chain_group(); |
| 1478 | } else { |
| 1479 | chain_node(OC_PRINT); |
| 1480 | } |
| 1481 | cn->r.n = mainseq.last; |
| 1482 | |
| 1483 | } else /* if (tclass & TC_GRPSTART) */ { |
| 1484 | rollback_token(); |
| 1485 | chain_group(); |
| 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | |
| 1491 | /* -------- program execution part -------- */ |
| 1492 | |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 1493 | static node *mk_splitter(const char *s, tsplitter *spl) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1494 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 1495 | regex_t *re, *ire; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1496 | node *n; |
| 1497 | |
| 1498 | re = &spl->re[0]; |
| 1499 | ire = &spl->re[1]; |
| 1500 | n = &spl->n; |
Denis Vlasenko | 890ac9d | 2006-10-07 15:16:19 +0000 | [diff] [blame] | 1501 | if ((n->info & OPCLSMASK) == OC_REGEXP) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1502 | regfree(re); |
Denis Vlasenko | b78c782 | 2007-07-18 18:31:11 +0000 | [diff] [blame] | 1503 | regfree(ire); // TODO: nuke ire, use re+1? |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1504 | } |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 1505 | if (strlen(s) > 1) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1506 | mk_re_node(s, n, re); |
| 1507 | } else { |
Mike Frysinger | f87b3e3 | 2005-09-27 04:16:22 +0000 | [diff] [blame] | 1508 | n->info = (uint32_t) *s; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | return n; |
| 1512 | } |
| 1513 | |
| 1514 | /* use node as a regular expression. Supplied with node ptr and regex_t |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 1515 | * storage space. Return ptr to regex (if result points to preg, it should |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1516 | * be later regfree'd manually |
| 1517 | */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1518 | static regex_t *as_regex(node *op, regex_t *preg) |
| 1519 | { |
Denis Vlasenko | 7a67664 | 2009-03-15 22:20:31 +0000 | [diff] [blame] | 1520 | int cflags; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1521 | var *v; |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 1522 | const char *s; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1523 | |
| 1524 | if ((op->info & OPCLSMASK) == OC_REGEXP) { |
| 1525 | return icase ? op->r.ire : op->l.re; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1526 | } |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1527 | v = nvalloc(1); |
| 1528 | s = getvar_s(evaluate(op, v)); |
Denis Vlasenko | 7a67664 | 2009-03-15 22:20:31 +0000 | [diff] [blame] | 1529 | |
| 1530 | cflags = icase ? REG_EXTENDED | REG_ICASE : REG_EXTENDED; |
| 1531 | /* Testcase where REG_EXTENDED fails (unpaired '{'): |
| 1532 | * echo Hi | awk 'gsub("@(samp|code|file)\{","");' |
| 1533 | * gawk 3.1.5 eats this. We revert to ~REG_EXTENDED |
| 1534 | * (maybe gsub is not supposed to use REG_EXTENDED?). |
| 1535 | */ |
| 1536 | if (regcomp(preg, s, cflags)) { |
| 1537 | cflags &= ~REG_EXTENDED; |
| 1538 | xregcomp(preg, s, cflags); |
| 1539 | } |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1540 | nvfree(v); |
| 1541 | return preg; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | /* gradually increasing buffer */ |
Denys Vlasenko | c9955f2 | 2010-03-10 19:21:54 +0100 | [diff] [blame] | 1545 | static char* qrealloc(char *b, int n, int *size) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1546 | { |
Denys Vlasenko | c9955f2 | 2010-03-10 19:21:54 +0100 | [diff] [blame] | 1547 | if (!b || n >= *size) { |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 1548 | *size = n + (n>>1) + 80; |
Denys Vlasenko | c9955f2 | 2010-03-10 19:21:54 +0100 | [diff] [blame] | 1549 | b = xrealloc(b, *size); |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 1550 | } |
Denys Vlasenko | c9955f2 | 2010-03-10 19:21:54 +0100 | [diff] [blame] | 1551 | return b; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | /* resize field storage space */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1555 | static void fsrealloc(int size) |
| 1556 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1557 | int i; |
| 1558 | |
| 1559 | if (size >= maxfields) { |
| 1560 | i = maxfields; |
| 1561 | maxfields = size + 16; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1562 | Fields = xrealloc(Fields, maxfields * sizeof(var)); |
| 1563 | for (; i < maxfields; i++) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1564 | Fields[i].type = VF_SPECIAL; |
| 1565 | Fields[i].string = NULL; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | if (size < nfields) { |
Denis Vlasenko | e1d3e03 | 2007-01-01 23:53:52 +0000 | [diff] [blame] | 1570 | for (i = size; i < nfields; i++) { |
| 1571 | clrvar(Fields + i); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | nfields = size; |
| 1575 | } |
| 1576 | |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 1577 | static int awk_split(const char *s, node *spl, char **slist) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1578 | { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1579 | int l, n = 0; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1580 | char c[4]; |
| 1581 | char *s1; |
Denis Vlasenko | b78c782 | 2007-07-18 18:31:11 +0000 | [diff] [blame] | 1582 | regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough... |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1583 | |
| 1584 | /* in worst case, each char would be a separate field */ |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 1585 | *slist = s1 = xzalloc(strlen(s) * 2 + 3); |
| 1586 | strcpy(s1, s); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1587 | |
| 1588 | c[0] = c[1] = (char)spl->info; |
| 1589 | c[2] = c[3] = '\0'; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1590 | if (*getvar_s(intvar[RS]) == '\0') |
| 1591 | c[2] = '\n'; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1592 | |
Denis Vlasenko | af1bd09 | 2007-07-18 18:32:25 +0000 | [diff] [blame] | 1593 | if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */ |
| 1594 | if (!*s) |
| 1595 | return n; /* "": zero fields */ |
| 1596 | n++; /* at least one field will be there */ |
| 1597 | do { |
| 1598 | l = strcspn(s, c+2); /* len till next NUL or \n */ |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 1599 | if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0 |
| 1600 | && pmatch[0].rm_so <= l |
| 1601 | ) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1602 | l = pmatch[0].rm_so; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1603 | if (pmatch[0].rm_eo == 0) { |
| 1604 | l++; |
| 1605 | pmatch[0].rm_eo++; |
| 1606 | } |
Denis Vlasenko | af1bd09 | 2007-07-18 18:32:25 +0000 | [diff] [blame] | 1607 | n++; /* we saw yet another delimiter */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1608 | } else { |
| 1609 | pmatch[0].rm_eo = l; |
Denys Vlasenko | e424423 | 2009-05-18 23:50:03 +0200 | [diff] [blame] | 1610 | if (s[l]) |
| 1611 | pmatch[0].rm_eo++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1612 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1613 | memcpy(s1, s, l); |
Denis Vlasenko | 67b5eeb | 2009-04-12 13:54:13 +0000 | [diff] [blame] | 1614 | /* make sure we remove *all* of the separator chars */ |
Denys Vlasenko | e424423 | 2009-05-18 23:50:03 +0200 | [diff] [blame] | 1615 | do { |
| 1616 | s1[l] = '\0'; |
| 1617 | } while (++l < pmatch[0].rm_eo); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1618 | nextword(&s1); |
| 1619 | s += pmatch[0].rm_eo; |
Denis Vlasenko | af1bd09 | 2007-07-18 18:32:25 +0000 | [diff] [blame] | 1620 | } while (*s); |
| 1621 | return n; |
| 1622 | } |
| 1623 | if (c[0] == '\0') { /* null split */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 1624 | while (*s) { |
Denis Vlasenko | e1d3e03 | 2007-01-01 23:53:52 +0000 | [diff] [blame] | 1625 | *s1++ = *s++; |
| 1626 | *s1++ = '\0'; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1627 | n++; |
| 1628 | } |
Denis Vlasenko | af1bd09 | 2007-07-18 18:32:25 +0000 | [diff] [blame] | 1629 | return n; |
| 1630 | } |
| 1631 | if (c[0] != ' ') { /* single-character split */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1632 | if (icase) { |
| 1633 | c[0] = toupper(c[0]); |
| 1634 | c[1] = tolower(c[1]); |
| 1635 | } |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1636 | if (*s1) |
| 1637 | n++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1638 | while ((s1 = strpbrk(s1, c))) { |
Denis Vlasenko | e1d3e03 | 2007-01-01 23:53:52 +0000 | [diff] [blame] | 1639 | *s1++ = '\0'; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1640 | n++; |
| 1641 | } |
Denis Vlasenko | af1bd09 | 2007-07-18 18:32:25 +0000 | [diff] [blame] | 1642 | return n; |
| 1643 | } |
| 1644 | /* space split */ |
| 1645 | while (*s) { |
| 1646 | s = skip_whitespace(s); |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1647 | if (!*s) |
| 1648 | break; |
Denis Vlasenko | af1bd09 | 2007-07-18 18:32:25 +0000 | [diff] [blame] | 1649 | n++; |
| 1650 | while (*s && !isspace(*s)) |
| 1651 | *s1++ = *s++; |
| 1652 | *s1++ = '\0'; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1653 | } |
| 1654 | return n; |
| 1655 | } |
| 1656 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1657 | static void split_f0(void) |
| 1658 | { |
Denis Vlasenko | af1bd09 | 2007-07-18 18:32:25 +0000 | [diff] [blame] | 1659 | /* static char *fstrings; */ |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1660 | #define fstrings (G.split_f0__fstrings) |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1661 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1662 | int i, n; |
| 1663 | char *s; |
| 1664 | |
| 1665 | if (is_f0_split) |
| 1666 | return; |
| 1667 | |
| 1668 | is_f0_split = TRUE; |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1669 | free(fstrings); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1670 | fsrealloc(0); |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1671 | n = awk_split(getvar_s(intvar[F0]), &fsplitter.n, &fstrings); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1672 | fsrealloc(n); |
| 1673 | s = fstrings; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1674 | for (i = 0; i < n; i++) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1675 | Fields[i].string = nextword(&s); |
| 1676 | Fields[i].type |= (VF_FSTR | VF_USER | VF_DIRTY); |
| 1677 | } |
| 1678 | |
| 1679 | /* set NF manually to avoid side effects */ |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1680 | clrvar(intvar[NF]); |
| 1681 | intvar[NF]->type = VF_NUMBER | VF_SPECIAL; |
| 1682 | intvar[NF]->number = nfields; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1683 | #undef fstrings |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | /* perform additional actions when some internal variables changed */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1687 | static void handle_special(var *v) |
| 1688 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1689 | int n; |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 1690 | char *b; |
| 1691 | const char *sep, *s; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1692 | int sl, l, len, i, bsize; |
| 1693 | |
Denis Vlasenko | e1d3e03 | 2007-01-01 23:53:52 +0000 | [diff] [blame] | 1694 | if (!(v->type & VF_SPECIAL)) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1695 | return; |
| 1696 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1697 | if (v == intvar[NF]) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1698 | n = (int)getvar_i(v); |
| 1699 | fsrealloc(n); |
| 1700 | |
| 1701 | /* recalculate $0 */ |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1702 | sep = getvar_s(intvar[OFS]); |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 1703 | sl = strlen(sep); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1704 | b = NULL; |
| 1705 | len = 0; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1706 | for (i = 0; i < n; i++) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1707 | s = getvar_s(&Fields[i]); |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 1708 | l = strlen(s); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1709 | if (b) { |
| 1710 | memcpy(b+len, sep, sl); |
| 1711 | len += sl; |
| 1712 | } |
Denys Vlasenko | c9955f2 | 2010-03-10 19:21:54 +0100 | [diff] [blame] | 1713 | b = qrealloc(b, len+l+sl, &bsize); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1714 | memcpy(b+len, s, l); |
| 1715 | len += l; |
| 1716 | } |
Denis Vlasenko | e1d3e03 | 2007-01-01 23:53:52 +0000 | [diff] [blame] | 1717 | if (b) |
| 1718 | b[len] = '\0'; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1719 | setvar_p(intvar[F0], b); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1720 | is_f0_split = TRUE; |
| 1721 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1722 | } else if (v == intvar[F0]) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1723 | is_f0_split = FALSE; |
| 1724 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1725 | } else if (v == intvar[FS]) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1726 | mk_splitter(getvar_s(v), &fsplitter); |
| 1727 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1728 | } else if (v == intvar[RS]) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1729 | mk_splitter(getvar_s(v), &rsplitter); |
| 1730 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1731 | } else if (v == intvar[IGNORECASE]) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1732 | icase = istrue(v); |
| 1733 | |
Denis Vlasenko | e1d3e03 | 2007-01-01 23:53:52 +0000 | [diff] [blame] | 1734 | } else { /* $n */ |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1735 | n = getvar_i(intvar[NF]); |
| 1736 | setvar_i(intvar[NF], n > v-Fields ? n : v-Fields+1); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1737 | /* right here v is invalid. Just to note... */ |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | /* step through func/builtin/etc arguments */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1742 | static node *nextarg(node **pn) |
| 1743 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1744 | node *n; |
| 1745 | |
| 1746 | n = *pn; |
| 1747 | if (n && (n->info & OPCLSMASK) == OC_COMMA) { |
| 1748 | *pn = n->r.n; |
| 1749 | n = n->l.n; |
| 1750 | } else { |
| 1751 | *pn = NULL; |
| 1752 | } |
| 1753 | return n; |
| 1754 | } |
| 1755 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1756 | static void hashwalk_init(var *v, xhash *array) |
| 1757 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1758 | hash_item *hi; |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 1759 | unsigned i; |
Denys Vlasenko | da62b09 | 2010-03-11 12:13:18 +0100 | [diff] [blame] | 1760 | walker_list *w; |
| 1761 | walker_list *prev_walker; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1762 | |
Denys Vlasenko | da62b09 | 2010-03-11 12:13:18 +0100 | [diff] [blame] | 1763 | if (v->type & VF_WALK) { |
| 1764 | prev_walker = v->x.walker; |
| 1765 | } else { |
| 1766 | v->type |= VF_WALK; |
| 1767 | prev_walker = NULL; |
| 1768 | } |
| 1769 | debug_printf_walker("hashwalk_init: prev_walker:%p\n", prev_walker); |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 1770 | |
Denys Vlasenko | da62b09 | 2010-03-11 12:13:18 +0100 | [diff] [blame] | 1771 | w = v->x.walker = xzalloc(sizeof(*w) + array->glen + 1); /* why + 1? */ |
| 1772 | debug_printf_walker(" walker@%p=%p\n", &v->x.walker, w); |
| 1773 | w->cur = w->end = w->wbuf; |
| 1774 | w->prev = prev_walker; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1775 | for (i = 0; i < array->csize; i++) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1776 | hi = array->items[i]; |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 1777 | while (hi) { |
Denys Vlasenko | da62b09 | 2010-03-11 12:13:18 +0100 | [diff] [blame] | 1778 | strcpy(w->end, hi->name); |
| 1779 | nextword(&w->end); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1780 | hi = hi->next; |
| 1781 | } |
| 1782 | } |
| 1783 | } |
| 1784 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1785 | static int hashwalk_next(var *v) |
| 1786 | { |
Denys Vlasenko | da62b09 | 2010-03-11 12:13:18 +0100 | [diff] [blame] | 1787 | walker_list *w = v->x.walker; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1788 | |
Denys Vlasenko | da62b09 | 2010-03-11 12:13:18 +0100 | [diff] [blame] | 1789 | if (w->cur >= w->end) { |
| 1790 | walker_list *prev_walker = w->prev; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1791 | |
Denys Vlasenko | da62b09 | 2010-03-11 12:13:18 +0100 | [diff] [blame] | 1792 | debug_printf_walker("end of iteration, free(walker@%p:%p), prev_walker:%p\n", &v->x.walker, w, prev_walker); |
| 1793 | free(w); |
Denys Vlasenko | 3cb60c3 | 2010-03-10 19:20:32 +0100 | [diff] [blame] | 1794 | v->x.walker = prev_walker; |
| 1795 | return FALSE; |
| 1796 | } |
| 1797 | |
Denys Vlasenko | da62b09 | 2010-03-11 12:13:18 +0100 | [diff] [blame] | 1798 | setvar_s(v, nextword(&w->cur)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1799 | return TRUE; |
| 1800 | } |
| 1801 | |
| 1802 | /* evaluate node, return 1 when result is true, 0 otherwise */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1803 | static int ptest(node *pattern) |
| 1804 | { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1805 | /* ptest__v is "static": to save stack space? */ |
| 1806 | return istrue(evaluate(pattern, &G.ptest__v)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | /* read next record from stream rsm into a variable v */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1810 | static int awk_getline(rstream *rsm, var *v) |
| 1811 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1812 | char *b; |
| 1813 | regmatch_t pmatch[2]; |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 1814 | int size, a, p, pp = 0; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1815 | int fd, so, eo, r, rp; |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 1816 | char c, *m, *s; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1817 | |
| 1818 | /* we're using our own buffer since we need access to accumulating |
| 1819 | * characters |
| 1820 | */ |
| 1821 | fd = fileno(rsm->F); |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 1822 | m = rsm->buffer; |
| 1823 | a = rsm->adv; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1824 | p = rsm->pos; |
| 1825 | size = rsm->size; |
| 1826 | c = (char) rsplitter.n.info; |
| 1827 | rp = 0; |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 1828 | |
Denys Vlasenko | c9955f2 | 2010-03-10 19:21:54 +0100 | [diff] [blame] | 1829 | if (!m) |
| 1830 | m = qrealloc(m, 256, &size); |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 1831 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1832 | do { |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 1833 | b = m + a; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1834 | so = eo = p; |
| 1835 | r = 1; |
| 1836 | if (p > 0) { |
| 1837 | if ((rsplitter.n.info & OPCLSMASK) == OC_REGEXP) { |
| 1838 | if (regexec(icase ? rsplitter.n.r.ire : rsplitter.n.l.re, |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1839 | b, 1, pmatch, 0) == 0) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1840 | so = pmatch[0].rm_so; |
| 1841 | eo = pmatch[0].rm_eo; |
| 1842 | if (b[eo] != '\0') |
| 1843 | break; |
| 1844 | } |
| 1845 | } else if (c != '\0') { |
| 1846 | s = strchr(b+pp, c); |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1847 | if (!s) |
| 1848 | s = memchr(b+pp, '\0', p - pp); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1849 | if (s) { |
| 1850 | so = eo = s-b; |
| 1851 | eo++; |
| 1852 | break; |
| 1853 | } |
| 1854 | } else { |
| 1855 | while (b[rp] == '\n') |
| 1856 | rp++; |
| 1857 | s = strstr(b+rp, "\n\n"); |
| 1858 | if (s) { |
| 1859 | so = eo = s-b; |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 1860 | while (b[eo] == '\n') |
| 1861 | eo++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1862 | if (b[eo] != '\0') |
| 1863 | break; |
| 1864 | } |
| 1865 | } |
| 1866 | } |
| 1867 | |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 1868 | if (a > 0) { |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 1869 | memmove(m, m+a, p+1); |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 1870 | b = m; |
| 1871 | a = 0; |
| 1872 | } |
| 1873 | |
Denys Vlasenko | c9955f2 | 2010-03-10 19:21:54 +0100 | [diff] [blame] | 1874 | m = qrealloc(m, a+p+128, &size); |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 1875 | b = m + a; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1876 | pp = p; |
| 1877 | p += safe_read(fd, b+p, size-p-1); |
| 1878 | if (p < pp) { |
| 1879 | p = 0; |
| 1880 | r = 0; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1881 | setvar_i(intvar[ERRNO], errno); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1882 | } |
| 1883 | b[p] = '\0'; |
| 1884 | |
| 1885 | } while (p > pp); |
| 1886 | |
| 1887 | if (p == 0) { |
| 1888 | r--; |
| 1889 | } else { |
| 1890 | c = b[so]; b[so] = '\0'; |
| 1891 | setvar_s(v, b+rp); |
| 1892 | v->type |= VF_USER; |
| 1893 | b[so] = c; |
| 1894 | c = b[eo]; b[eo] = '\0'; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 1895 | setvar_s(intvar[RT], b+so); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1896 | b[eo] = c; |
| 1897 | } |
| 1898 | |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 1899 | rsm->buffer = m; |
| 1900 | rsm->adv = a + eo; |
| 1901 | rsm->pos = p - eo; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1902 | rsm->size = size; |
| 1903 | |
| 1904 | return r; |
| 1905 | } |
| 1906 | |
"Vladimir N. Oleynik" | 5cf9a03 | 2005-10-19 09:21:51 +0000 | [diff] [blame] | 1907 | static int fmt_num(char *b, int size, const char *format, double n, int int_as_int) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1908 | { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1909 | int r = 0; |
"Vladimir N. Oleynik" | 5cf9a03 | 2005-10-19 09:21:51 +0000 | [diff] [blame] | 1910 | char c; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1911 | const char *s = format; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1912 | |
| 1913 | if (int_as_int && n == (int)n) { |
| 1914 | r = snprintf(b, size, "%d", (int)n); |
| 1915 | } else { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1916 | do { c = *s; } while (c && *++s); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1917 | if (strchr("diouxX", c)) { |
| 1918 | r = snprintf(b, size, format, (int)n); |
| 1919 | } else if (strchr("eEfgG", c)) { |
| 1920 | r = snprintf(b, size, format, n); |
| 1921 | } else { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 1922 | syntax_error(EMSG_INV_FMT); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | return r; |
| 1926 | } |
| 1927 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1928 | /* formatted output into an allocated buffer, return ptr to buffer */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1929 | static char *awk_printf(node *n) |
| 1930 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1931 | char *b = NULL; |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 1932 | char *fmt, *s, *f; |
| 1933 | const char *s1; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1934 | int i, j, incr, bsize; |
| 1935 | char c, c1; |
| 1936 | var *v, *arg; |
| 1937 | |
| 1938 | v = nvalloc(1); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1939 | fmt = f = xstrdup(getvar_s(evaluate(nextarg(&n), v))); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1940 | |
| 1941 | i = 0; |
| 1942 | while (*f) { |
| 1943 | s = f; |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1944 | while (*f && (*f != '%' || *++f == '%')) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1945 | f++; |
Denis Vlasenko | 389f9d5 | 2007-05-09 21:57:23 +0000 | [diff] [blame] | 1946 | while (*f && !isalpha(*f)) { |
| 1947 | if (*f == '*') |
| 1948 | syntax_error("%*x formats are not supported"); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1949 | f++; |
Denis Vlasenko | 389f9d5 | 2007-05-09 21:57:23 +0000 | [diff] [blame] | 1950 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1951 | |
| 1952 | incr = (f - s) + MAXVARFMT; |
Denys Vlasenko | c9955f2 | 2010-03-10 19:21:54 +0100 | [diff] [blame] | 1953 | b = qrealloc(b, incr + i, &bsize); |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1954 | c = *f; |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1955 | if (c != '\0') |
| 1956 | f++; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1957 | c1 = *f; |
| 1958 | *f = '\0'; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1959 | arg = evaluate(nextarg(&n), v); |
| 1960 | |
| 1961 | j = i; |
| 1962 | if (c == 'c' || !c) { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 1963 | i += sprintf(b+i, s, is_numeric(arg) ? |
| 1964 | (char)getvar_i(arg) : *getvar_s(arg)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1965 | } else if (c == 's') { |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 1966 | s1 = getvar_s(arg); |
Denys Vlasenko | c9955f2 | 2010-03-10 19:21:54 +0100 | [diff] [blame] | 1967 | b = qrealloc(b, incr+i+strlen(s1), &bsize); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1968 | i += sprintf(b+i, s, s1); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1969 | } else { |
| 1970 | i += fmt_num(b+i, incr, s, getvar_i(arg), FALSE); |
| 1971 | } |
| 1972 | *f = c1; |
| 1973 | |
| 1974 | /* if there was an error while sprintf, return value is negative */ |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 1975 | if (i < j) |
| 1976 | i = j; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1979 | free(fmt); |
| 1980 | nvfree(v); |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 1981 | b = xrealloc(b, i + 1); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1982 | b[i] = '\0'; |
| 1983 | return b; |
| 1984 | } |
| 1985 | |
| 1986 | /* common substitution routine |
| 1987 | * replace (nm) substring of (src) that match (n) with (repl), store |
| 1988 | * result into (dest), return number of substitutions. If nm=0, replace |
| 1989 | * all matches. If src or dst is NULL, use $0. If ex=TRUE, enable |
| 1990 | * subexpression matching (\1-\9) |
| 1991 | */ |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 1992 | static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int ex) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 1993 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1994 | char *ds = NULL; |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 1995 | const char *s; |
| 1996 | const char *sp; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 1997 | int c, i, j, di, rl, so, eo, nbs, n, dssize; |
| 1998 | regmatch_t pmatch[10]; |
| 1999 | regex_t sreg, *re; |
| 2000 | |
| 2001 | re = as_regex(rn, &sreg); |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 2002 | if (!src) |
| 2003 | src = intvar[F0]; |
| 2004 | if (!dest) |
| 2005 | dest = intvar[F0]; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2006 | |
| 2007 | i = di = 0; |
| 2008 | sp = getvar_s(src); |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 2009 | rl = strlen(repl); |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2010 | while (regexec(re, sp, 10, pmatch, sp==getvar_s(src) ? 0 : REG_NOTBOL) == 0) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2011 | so = pmatch[0].rm_so; |
| 2012 | eo = pmatch[0].rm_eo; |
| 2013 | |
Denys Vlasenko | c9955f2 | 2010-03-10 19:21:54 +0100 | [diff] [blame] | 2014 | ds = qrealloc(ds, di + eo + rl, &dssize); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2015 | memcpy(ds + di, sp, eo); |
| 2016 | di += eo; |
| 2017 | if (++i >= nm) { |
| 2018 | /* replace */ |
| 2019 | di -= (eo - so); |
| 2020 | nbs = 0; |
| 2021 | for (s = repl; *s; s++) { |
| 2022 | ds[di++] = c = *s; |
| 2023 | if (c == '\\') { |
| 2024 | nbs++; |
| 2025 | continue; |
| 2026 | } |
| 2027 | if (c == '&' || (ex && c >= '0' && c <= '9')) { |
| 2028 | di -= ((nbs + 3) >> 1); |
| 2029 | j = 0; |
| 2030 | if (c != '&') { |
| 2031 | j = c - '0'; |
| 2032 | nbs++; |
| 2033 | } |
| 2034 | if (nbs % 2) { |
| 2035 | ds[di++] = c; |
| 2036 | } else { |
| 2037 | n = pmatch[j].rm_eo - pmatch[j].rm_so; |
Denys Vlasenko | c9955f2 | 2010-03-10 19:21:54 +0100 | [diff] [blame] | 2038 | ds = qrealloc(ds, di + rl + n, &dssize); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2039 | memcpy(ds + di, sp + pmatch[j].rm_so, n); |
| 2040 | di += n; |
| 2041 | } |
| 2042 | } |
| 2043 | nbs = 0; |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | sp += eo; |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 2048 | if (i == nm) |
| 2049 | break; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2050 | if (eo == so) { |
Denis Vlasenko | b78c782 | 2007-07-18 18:31:11 +0000 | [diff] [blame] | 2051 | ds[di] = *sp++; |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 2052 | if (!ds[di++]) |
| 2053 | break; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2054 | } |
| 2055 | } |
| 2056 | |
Denys Vlasenko | c9955f2 | 2010-03-10 19:21:54 +0100 | [diff] [blame] | 2057 | ds = qrealloc(ds, di + strlen(sp), &dssize); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2058 | strcpy(ds + di, sp); |
| 2059 | setvar_p(dest, ds); |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 2060 | if (re == &sreg) |
| 2061 | regfree(re); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2062 | return i; |
| 2063 | } |
| 2064 | |
Leonid Lisovskiy | 46a0be5 | 2009-09-21 04:08:08 +0200 | [diff] [blame] | 2065 | static NOINLINE int do_mktime(const char *ds) |
| 2066 | { |
| 2067 | struct tm then; |
| 2068 | int count; |
| 2069 | |
| 2070 | /*memset(&then, 0, sizeof(then)); - not needed */ |
| 2071 | then.tm_isdst = -1; /* default is unknown */ |
| 2072 | |
| 2073 | /* manpage of mktime says these fields are ints, |
| 2074 | * so we can sscanf stuff directly into them */ |
| 2075 | count = sscanf(ds, "%u %u %u %u %u %u %d", |
| 2076 | &then.tm_year, &then.tm_mon, &then.tm_mday, |
| 2077 | &then.tm_hour, &then.tm_min, &then.tm_sec, |
| 2078 | &then.tm_isdst); |
| 2079 | |
| 2080 | if (count < 6 |
| 2081 | || (unsigned)then.tm_mon < 1 |
| 2082 | || (unsigned)then.tm_year < 1900 |
| 2083 | ) { |
| 2084 | return -1; |
| 2085 | } |
| 2086 | |
| 2087 | then.tm_mon -= 1; |
Denys Vlasenko | bc3e947 | 2009-09-21 04:16:00 +0200 | [diff] [blame] | 2088 | then.tm_year -= 1900; |
Leonid Lisovskiy | 46a0be5 | 2009-09-21 04:08:08 +0200 | [diff] [blame] | 2089 | |
| 2090 | return mktime(&then); |
| 2091 | } |
| 2092 | |
| 2093 | static NOINLINE var *exec_builtin(node *op, var *res) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 2094 | { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2095 | #define tspl (G.exec_builtin__tspl) |
| 2096 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2097 | var *tv; |
| 2098 | node *an[4]; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2099 | var *av[4]; |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 2100 | const char *as[4]; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2101 | regmatch_t pmatch[2]; |
| 2102 | regex_t sreg, *re; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2103 | node *spl; |
Mike Frysinger | f87b3e3 | 2005-09-27 04:16:22 +0000 | [diff] [blame] | 2104 | uint32_t isr, info; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2105 | int nargs; |
| 2106 | time_t tt; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2107 | int i, l, ll, n; |
| 2108 | |
| 2109 | tv = nvalloc(4); |
| 2110 | isr = info = op->info; |
| 2111 | op = op->l.n; |
| 2112 | |
| 2113 | av[2] = av[3] = NULL; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2114 | for (i = 0; i < 4 && op; i++) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2115 | an[i] = nextarg(&op); |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 2116 | if (isr & 0x09000000) |
| 2117 | av[i] = evaluate(an[i], &tv[i]); |
| 2118 | if (isr & 0x08000000) |
| 2119 | as[i] = getvar_s(av[i]); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2120 | isr >>= 1; |
| 2121 | } |
| 2122 | |
| 2123 | nargs = i; |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 2124 | if ((uint32_t)nargs < (info >> 30)) |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2125 | syntax_error(EMSG_TOO_FEW_ARGS); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2126 | |
Denys Vlasenko | 56b3eec | 2009-10-23 13:03:59 +0200 | [diff] [blame] | 2127 | info &= OPNMASK; |
| 2128 | switch (info) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2129 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2130 | case B_a2: |
Denis Vlasenko | 2d5bd80 | 2008-10-24 10:49:49 +0000 | [diff] [blame] | 2131 | #if ENABLE_FEATURE_AWK_LIBM |
Denis Vlasenko | 37890e2 | 2008-10-21 12:59:34 +0000 | [diff] [blame] | 2132 | setvar_i(res, atan2(getvar_i(av[0]), getvar_i(av[1]))); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2133 | #else |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2134 | syntax_error(EMSG_NO_MATH); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2135 | #endif |
| 2136 | break; |
| 2137 | |
Denys Vlasenko | 39fe4d1 | 2010-03-12 16:57:06 +0100 | [diff] [blame] | 2138 | case B_sp: { |
| 2139 | char *s, *s1; |
| 2140 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2141 | if (nargs > 2) { |
| 2142 | spl = (an[2]->info & OPCLSMASK) == OC_REGEXP ? |
| 2143 | an[2] : mk_splitter(getvar_s(evaluate(an[2], &tv[2])), &tspl); |
| 2144 | } else { |
| 2145 | spl = &fsplitter.n; |
| 2146 | } |
| 2147 | |
| 2148 | n = awk_split(as[0], spl, &s); |
| 2149 | s1 = s; |
| 2150 | clear_array(iamarray(av[1])); |
Denis Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 2151 | for (i = 1; i <= n; i++) |
Denys Vlasenko | 39fe4d1 | 2010-03-12 16:57:06 +0100 | [diff] [blame] | 2152 | setari_u(av[1], i, nextword(&s)); |
| 2153 | free(s1); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2154 | setvar_i(res, n); |
| 2155 | break; |
Denys Vlasenko | 39fe4d1 | 2010-03-12 16:57:06 +0100 | [diff] [blame] | 2156 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2157 | |
Denys Vlasenko | 39fe4d1 | 2010-03-12 16:57:06 +0100 | [diff] [blame] | 2158 | case B_ss: { |
| 2159 | char *s; |
| 2160 | |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 2161 | l = strlen(as[0]); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2162 | i = getvar_i(av[1]) - 1; |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 2163 | if (i > l) |
| 2164 | i = l; |
| 2165 | if (i < 0) |
| 2166 | i = 0; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2167 | n = (nargs > 2) ? getvar_i(av[2]) : l-i; |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 2168 | if (n < 0) |
| 2169 | n = 0; |
Denis Vlasenko | 8ae5b28 | 2008-07-02 22:47:49 +0000 | [diff] [blame] | 2170 | s = xstrndup(as[0]+i, n); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2171 | setvar_p(res, s); |
| 2172 | break; |
Denys Vlasenko | 39fe4d1 | 2010-03-12 16:57:06 +0100 | [diff] [blame] | 2173 | } |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 2174 | |
Denis Vlasenko | 7cbcd1c | 2008-08-28 23:16:58 +0000 | [diff] [blame] | 2175 | /* Bitwise ops must assume that operands are unsigned. GNU Awk 3.1.5: |
| 2176 | * awk '{ print or(-1,1) }' gives "4.29497e+09", not "-2.xxxe+09" */ |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2177 | case B_an: |
Denis Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 2178 | setvar_i(res, getvar_i_int(av[0]) & getvar_i_int(av[1])); |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 2179 | break; |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 2180 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2181 | case B_co: |
Denis Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 2182 | setvar_i(res, ~getvar_i_int(av[0])); |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 2183 | break; |
| 2184 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2185 | case B_ls: |
Denis Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 2186 | setvar_i(res, getvar_i_int(av[0]) << getvar_i_int(av[1])); |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 2187 | break; |
| 2188 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2189 | case B_or: |
Denis Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 2190 | setvar_i(res, getvar_i_int(av[0]) | getvar_i_int(av[1])); |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 2191 | break; |
| 2192 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2193 | case B_rs: |
Denis Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 2194 | setvar_i(res, getvar_i_int(av[0]) >> getvar_i_int(av[1])); |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 2195 | break; |
| 2196 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2197 | case B_xo: |
Denis Vlasenko | a2e1eea | 2008-09-02 09:00:23 +0000 | [diff] [blame] | 2198 | setvar_i(res, getvar_i_int(av[0]) ^ getvar_i_int(av[1])); |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 2199 | break; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2200 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2201 | case B_lo: |
Denys Vlasenko | 39fe4d1 | 2010-03-12 16:57:06 +0100 | [diff] [blame] | 2202 | case B_up: { |
| 2203 | char *s, *s1; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 2204 | s1 = s = xstrdup(as[0]); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2205 | while (*s1) { |
Denys Vlasenko | 56b3eec | 2009-10-23 13:03:59 +0200 | [diff] [blame] | 2206 | //*s1 = (info == B_up) ? toupper(*s1) : tolower(*s1); |
| 2207 | if ((unsigned char)((*s1 | 0x20) - 'a') <= ('z' - 'a')) |
| 2208 | *s1 = (info == B_up) ? (*s1 & 0xdf) : (*s1 | 0x20); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2209 | s1++; |
| 2210 | } |
| 2211 | setvar_p(res, s); |
| 2212 | break; |
Denys Vlasenko | 39fe4d1 | 2010-03-12 16:57:06 +0100 | [diff] [blame] | 2213 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2214 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2215 | case B_ix: |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2216 | n = 0; |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 2217 | ll = strlen(as[1]); |
| 2218 | l = strlen(as[0]) - ll; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2219 | if (ll > 0 && l >= 0) { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2220 | if (!icase) { |
Denys Vlasenko | 39fe4d1 | 2010-03-12 16:57:06 +0100 | [diff] [blame] | 2221 | char *s = strstr(as[0], as[1]); |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 2222 | if (s) |
| 2223 | n = (s - as[0]) + 1; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2224 | } else { |
| 2225 | /* this piece of code is terribly slow and |
| 2226 | * really should be rewritten |
| 2227 | */ |
Denys Vlasenko | 39fe4d1 | 2010-03-12 16:57:06 +0100 | [diff] [blame] | 2228 | for (i = 0; i <= l; i++) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2229 | if (strncasecmp(as[0]+i, as[1], ll) == 0) { |
| 2230 | n = i+1; |
| 2231 | break; |
| 2232 | } |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | setvar_i(res, n); |
| 2237 | break; |
| 2238 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2239 | case B_ti: |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2240 | if (nargs > 1) |
| 2241 | tt = getvar_i(av[1]); |
| 2242 | else |
| 2243 | time(&tt); |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 2244 | //s = (nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2245 | i = strftime(g_buf, MAXVARFMT, |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 2246 | ((nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"), |
| 2247 | localtime(&tt)); |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2248 | g_buf[i] = '\0'; |
| 2249 | setvar_s(res, g_buf); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2250 | break; |
| 2251 | |
Leonid Lisovskiy | 46a0be5 | 2009-09-21 04:08:08 +0200 | [diff] [blame] | 2252 | case B_mt: |
| 2253 | setvar_i(res, do_mktime(as[0])); |
| 2254 | break; |
| 2255 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2256 | case B_ma: |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2257 | re = as_regex(an[1], &sreg); |
| 2258 | n = regexec(re, as[0], 1, pmatch, 0); |
| 2259 | if (n == 0) { |
| 2260 | pmatch[0].rm_so++; |
| 2261 | pmatch[0].rm_eo++; |
| 2262 | } else { |
| 2263 | pmatch[0].rm_so = 0; |
| 2264 | pmatch[0].rm_eo = -1; |
| 2265 | } |
| 2266 | setvar_i(newvar("RSTART"), pmatch[0].rm_so); |
| 2267 | setvar_i(newvar("RLENGTH"), pmatch[0].rm_eo - pmatch[0].rm_so); |
| 2268 | setvar_i(res, pmatch[0].rm_so); |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 2269 | if (re == &sreg) |
| 2270 | regfree(re); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2271 | break; |
| 2272 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2273 | case B_ge: |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2274 | awk_sub(an[0], as[1], getvar_i(av[2]), av[3], res, TRUE); |
| 2275 | break; |
| 2276 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2277 | case B_gs: |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2278 | setvar_i(res, awk_sub(an[0], as[1], 0, av[2], av[2], FALSE)); |
| 2279 | break; |
| 2280 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2281 | case B_su: |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2282 | setvar_i(res, awk_sub(an[0], as[1], 1, av[2], av[2], FALSE)); |
| 2283 | break; |
| 2284 | } |
| 2285 | |
| 2286 | nvfree(tv); |
| 2287 | return res; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2288 | #undef tspl |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * Evaluate node - the heart of the program. Supplied with subtree |
| 2293 | * and place where to store result. returns ptr to result. |
| 2294 | */ |
| 2295 | #define XC(n) ((n) >> 8) |
| 2296 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 2297 | static var *evaluate(node *op, var *res) |
| 2298 | { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2299 | /* This procedure is recursive so we should count every byte */ |
| 2300 | #define fnargs (G.evaluate__fnargs) |
| 2301 | /* seed is initialized to 1 */ |
| 2302 | #define seed (G.evaluate__seed) |
| 2303 | #define sreg (G.evaluate__sreg) |
Denis Vlasenko | e1d3e03 | 2007-01-01 23:53:52 +0000 | [diff] [blame] | 2304 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2305 | var *v1; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2306 | |
Denis Vlasenko | e1d3e03 | 2007-01-01 23:53:52 +0000 | [diff] [blame] | 2307 | if (!op) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2308 | return setvar_s(res, NULL); |
| 2309 | |
| 2310 | v1 = nvalloc(2); |
| 2311 | |
| 2312 | while (op) { |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2313 | struct { |
| 2314 | var *v; |
| 2315 | const char *s; |
| 2316 | } L = L; /* for compiler */ |
| 2317 | struct { |
| 2318 | var *v; |
| 2319 | const char *s; |
| 2320 | } R = R; |
| 2321 | double L_d = L_d; |
| 2322 | uint32_t opinfo; |
| 2323 | int opn; |
| 2324 | node *op1; |
| 2325 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2326 | opinfo = op->info; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2327 | opn = (opinfo & OPNMASK); |
| 2328 | g_lineno = op->lineno; |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2329 | op1 = op->l.n; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2330 | |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2331 | /* execute inevitable things */ |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 2332 | if (opinfo & OF_RES1) |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2333 | L.v = evaluate(op1, v1); |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 2334 | if (opinfo & OF_RES2) |
| 2335 | R.v = evaluate(op->r.n, v1+1); |
| 2336 | if (opinfo & OF_STR1) |
| 2337 | L.s = getvar_s(L.v); |
| 2338 | if (opinfo & OF_STR2) |
| 2339 | R.s = getvar_s(R.v); |
| 2340 | if (opinfo & OF_NUM1) |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2341 | L_d = getvar_i(L.v); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2342 | |
| 2343 | switch (XC(opinfo & OPCLSMASK)) { |
| 2344 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 2345 | /* -- iterative node type -- */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2346 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 2347 | /* test pattern */ |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2348 | case XC( OC_TEST ): |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2349 | if ((op1->info & OPCLSMASK) == OC_COMMA) { |
| 2350 | /* it's range pattern */ |
| 2351 | if ((opinfo & OF_CHECKED) || ptest(op1->l.n)) { |
| 2352 | op->info |= OF_CHECKED; |
| 2353 | if (ptest(op1->r.n)) |
| 2354 | op->info &= ~OF_CHECKED; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2355 | op = op->a.n; |
| 2356 | } else { |
| 2357 | op = op->r.n; |
| 2358 | } |
| 2359 | } else { |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 2360 | op = ptest(op1) ? op->a.n : op->r.n; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2361 | } |
| 2362 | break; |
| 2363 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 2364 | /* just evaluate an expression, also used as unconditional jump */ |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2365 | case XC( OC_EXEC ): |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2366 | break; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2367 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 2368 | /* branch, used in if-else and various loops */ |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2369 | case XC( OC_BR ): |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2370 | op = istrue(L.v) ? op->a.n : op->r.n; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2371 | break; |
| 2372 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 2373 | /* initialize for-in loop */ |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2374 | case XC( OC_WALKINIT ): |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2375 | hashwalk_init(L.v, iamarray(R.v)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2376 | break; |
| 2377 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 2378 | /* get next array item */ |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2379 | case XC( OC_WALKNEXT ): |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2380 | op = hashwalk_next(L.v) ? op->a.n : op->r.n; |
| 2381 | break; |
| 2382 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2383 | case XC( OC_PRINT ): |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2384 | case XC( OC_PRINTF ): { |
| 2385 | FILE *F = stdout; |
| 2386 | |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2387 | if (op->r.n) { |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2388 | rstream *rsm = newfile(R.s); |
| 2389 | if (!rsm->F) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2390 | if (opn == '|') { |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2391 | rsm->F = popen(R.s, "w"); |
| 2392 | if (rsm->F == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 2393 | bb_perror_msg_and_die("popen"); |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2394 | rsm->is_pipe = 1; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2395 | } else { |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2396 | rsm->F = xfopen(R.s, opn=='w' ? "w" : "a"); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2397 | } |
| 2398 | } |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2399 | F = rsm->F; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | if ((opinfo & OPCLSMASK) == OC_PRINT) { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2403 | if (!op1) { |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2404 | fputs(getvar_s(intvar[F0]), F); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2405 | } else { |
| 2406 | while (op1) { |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2407 | var *v = evaluate(nextarg(&op1), v1); |
| 2408 | if (v->type & VF_NUMBER) { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2409 | fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[OFMT]), |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2410 | getvar_i(v), TRUE); |
| 2411 | fputs(g_buf, F); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2412 | } else { |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2413 | fputs(getvar_s(v), F); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 2416 | if (op1) |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2417 | fputs(getvar_s(intvar[OFS]), F); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2418 | } |
| 2419 | } |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2420 | fputs(getvar_s(intvar[ORS]), F); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2421 | |
| 2422 | } else { /* OC_PRINTF */ |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2423 | char *s = awk_printf(op1); |
| 2424 | fputs(s, F); |
| 2425 | free(s); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2426 | } |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2427 | fflush(F); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2428 | break; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2429 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2430 | |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2431 | case XC( OC_DELETE ): { |
| 2432 | uint32_t info = op1->info & OPCLSMASK; |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2433 | var *v; |
| 2434 | |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2435 | if (info == OC_VAR) { |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2436 | v = op1->l.v; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2437 | } else if (info == OC_FNARG) { |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2438 | v = &fnargs[op1->l.aidx]; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2439 | } else { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2440 | syntax_error(EMSG_NOT_ARRAY); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2443 | if (op1->r.n) { |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2444 | const char *s; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2445 | clrvar(L.v); |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2446 | s = getvar_s(evaluate(op1->r.n, v1)); |
| 2447 | hash_remove(iamarray(v), s); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2448 | } else { |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2449 | clear_array(iamarray(v)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2450 | } |
| 2451 | break; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2452 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2453 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2454 | case XC( OC_NEWSOURCE ): |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 2455 | g_progname = op->l.new_progname; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2456 | break; |
| 2457 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2458 | case XC( OC_RETURN ): |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2459 | copyvar(res, L.v); |
| 2460 | break; |
| 2461 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2462 | case XC( OC_NEXTFILE ): |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2463 | nextfile = TRUE; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2464 | case XC( OC_NEXT ): |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2465 | nextrec = TRUE; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2466 | case XC( OC_DONE ): |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2467 | clrvar(res); |
| 2468 | break; |
| 2469 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2470 | case XC( OC_EXIT ): |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2471 | awk_exit(L_d); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2472 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 2473 | /* -- recursive node type -- */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2474 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2475 | case XC( OC_VAR ): |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2476 | L.v = op->l.v; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2477 | if (L.v == intvar[NF]) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2478 | split_f0(); |
| 2479 | goto v_cont; |
| 2480 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2481 | case XC( OC_FNARG ): |
Denys Vlasenko | 7b81db1 | 2010-03-12 21:04:47 +0100 | [diff] [blame] | 2482 | L.v = &fnargs[op->l.aidx]; |
Denis Vlasenko | e1d3e03 | 2007-01-01 23:53:52 +0000 | [diff] [blame] | 2483 | v_cont: |
| 2484 | res = op->r.n ? findvar(iamarray(L.v), R.s) : L.v; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2485 | break; |
| 2486 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2487 | case XC( OC_IN ): |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2488 | setvar_i(res, hash_search(iamarray(R.v), L.s) ? 1 : 0); |
| 2489 | break; |
| 2490 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2491 | case XC( OC_REGEXP ): |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2492 | op1 = op; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2493 | L.s = getvar_s(intvar[F0]); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2494 | goto re_cont; |
| 2495 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2496 | case XC( OC_MATCH ): |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2497 | op1 = op->r.n; |
Denis Vlasenko | e1d3e03 | 2007-01-01 23:53:52 +0000 | [diff] [blame] | 2498 | re_cont: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2499 | { |
| 2500 | regex_t *re = as_regex(op1, &sreg); |
| 2501 | int i = regexec(re, L.s, 0, NULL, 0); |
| 2502 | if (re == &sreg) |
| 2503 | regfree(re); |
| 2504 | setvar_i(res, (i == 0) ^ (opn == '!')); |
| 2505 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2506 | break; |
| 2507 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2508 | case XC( OC_MOVE ): |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2509 | /* if source is a temporary string, jusk relink it to dest */ |
Denys Vlasenko | 1284774 | 2009-11-30 01:15:04 +0100 | [diff] [blame] | 2510 | //Disabled: if R.v is numeric but happens to have cached R.v->string, |
| 2511 | //then L.v ends up being a string, which is wrong |
| 2512 | // if (R.v == v1+1 && R.v->string) { |
| 2513 | // res = setvar_p(L.v, R.v->string); |
| 2514 | // R.v->string = NULL; |
| 2515 | // } else { |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2516 | res = copyvar(L.v, R.v); |
Denys Vlasenko | 1284774 | 2009-11-30 01:15:04 +0100 | [diff] [blame] | 2517 | // } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2518 | break; |
| 2519 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2520 | case XC( OC_TERNARY ): |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2521 | if ((op->r.n->info & OPCLSMASK) != OC_COLON) |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2522 | syntax_error(EMSG_POSSIBLE_ERROR); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2523 | res = evaluate(istrue(L.v) ? op->r.n->l.n : op->r.n->r.n, res); |
| 2524 | break; |
| 2525 | |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2526 | case XC( OC_FUNC ): { |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2527 | var *vbeg, *v; |
| 2528 | const char *sv_progname; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2529 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2530 | if (!op->r.f->body.first) |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2531 | syntax_error(EMSG_UNDEF_FUNC); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2532 | |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2533 | vbeg = v = nvalloc(op->r.f->nargs + 1); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2534 | while (op1) { |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2535 | var *arg = evaluate(nextarg(&op1), v1); |
| 2536 | copyvar(v, arg); |
| 2537 | v->type |= VF_CHILD; |
| 2538 | v->x.parent = arg; |
| 2539 | if (++v - vbeg >= op->r.f->nargs) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2540 | break; |
| 2541 | } |
| 2542 | |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2543 | v = fnargs; |
| 2544 | fnargs = vbeg; |
| 2545 | sv_progname = g_progname; |
| 2546 | |
| 2547 | res = evaluate(op->r.f->body.first, res); |
| 2548 | |
| 2549 | g_progname = sv_progname; |
| 2550 | nvfree(fnargs); |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2551 | fnargs = v; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2552 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2553 | break; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2554 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2555 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2556 | case XC( OC_GETLINE ): |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2557 | case XC( OC_PGETLINE ): { |
| 2558 | rstream *rsm; |
| 2559 | int i; |
| 2560 | |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2561 | if (op1) { |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2562 | rsm = newfile(L.s); |
| 2563 | if (!rsm->F) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2564 | if ((opinfo & OPCLSMASK) == OC_PGETLINE) { |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2565 | rsm->F = popen(L.s, "r"); |
| 2566 | rsm->is_pipe = TRUE; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2567 | } else { |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2568 | rsm->F = fopen_for_read(L.s); /* not xfopen! */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2569 | } |
| 2570 | } |
| 2571 | } else { |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 2572 | if (!iF) |
| 2573 | iF = next_input_file(); |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2574 | rsm = iF; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2577 | if (!rsm->F) { |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2578 | setvar_i(intvar[ERRNO], errno); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2579 | setvar_i(res, -1); |
| 2580 | break; |
| 2581 | } |
| 2582 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2583 | if (!op->r.n) |
| 2584 | R.v = intvar[F0]; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2585 | |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2586 | i = awk_getline(rsm, R.v); |
| 2587 | if (i > 0 && !op1) { |
| 2588 | incvar(intvar[FNR]); |
| 2589 | incvar(intvar[NR]); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2590 | } |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2591 | setvar_i(res, i); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2592 | break; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2593 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2594 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 2595 | /* simple builtins */ |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2596 | case XC( OC_FBLTIN ): { |
| 2597 | int i; |
| 2598 | rstream *rsm; |
| 2599 | double R_d = R_d; /* for compiler */ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2600 | |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2601 | switch (opn) { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2602 | case F_in: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2603 | R_d = (int)L_d; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2604 | break; |
| 2605 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2606 | case F_rn: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2607 | R_d = (double)rand() / (double)RAND_MAX; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2608 | break; |
Denis Vlasenko | 2d5bd80 | 2008-10-24 10:49:49 +0000 | [diff] [blame] | 2609 | #if ENABLE_FEATURE_AWK_LIBM |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2610 | case F_co: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2611 | R_d = cos(L_d); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2612 | break; |
| 2613 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2614 | case F_ex: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2615 | R_d = exp(L_d); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2616 | break; |
| 2617 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2618 | case F_lg: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2619 | R_d = log(L_d); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2620 | break; |
| 2621 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2622 | case F_si: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2623 | R_d = sin(L_d); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2624 | break; |
| 2625 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2626 | case F_sq: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2627 | R_d = sqrt(L_d); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2628 | break; |
| 2629 | #else |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2630 | case F_co: |
| 2631 | case F_ex: |
| 2632 | case F_lg: |
| 2633 | case F_si: |
| 2634 | case F_sq: |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2635 | syntax_error(EMSG_NO_MATH); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2636 | break; |
| 2637 | #endif |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2638 | case F_sr: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2639 | R_d = (double)seed; |
| 2640 | seed = op1 ? (unsigned)L_d : (unsigned)time(NULL); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2641 | srand(seed); |
| 2642 | break; |
| 2643 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2644 | case F_ti: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2645 | R_d = time(NULL); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2646 | break; |
| 2647 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2648 | case F_le: |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2649 | if (!op1) |
| 2650 | L.s = getvar_s(intvar[F0]); |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2651 | R_d = strlen(L.s); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2652 | break; |
| 2653 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2654 | case F_sy: |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2655 | fflush_all(); |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2656 | R_d = (ENABLE_FEATURE_ALLOW_EXEC && L.s && *L.s) |
Denis Vlasenko | 249fabf | 2006-12-19 00:29:22 +0000 | [diff] [blame] | 2657 | ? (system(L.s) >> 8) : 0; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2658 | break; |
| 2659 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2660 | case F_ff: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2661 | if (!op1) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2662 | fflush(stdout); |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2663 | } else if (L.s && *L.s) { |
| 2664 | rsm = newfile(L.s); |
| 2665 | fflush(rsm->F); |
| 2666 | } else { |
| 2667 | fflush_all(); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2668 | } |
| 2669 | break; |
| 2670 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2671 | case F_cl: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2672 | i = 0; |
| 2673 | rsm = (rstream *)hash_search(fdhash, L.s); |
| 2674 | if (rsm) { |
| 2675 | i = rsm->is_pipe ? pclose(rsm->F) : fclose(rsm->F); |
| 2676 | free(rsm->buffer); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2677 | hash_remove(fdhash, L.s); |
| 2678 | } |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2679 | if (i != 0) |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2680 | setvar_i(intvar[ERRNO], errno); |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2681 | R_d = (double)i; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2682 | break; |
| 2683 | } |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2684 | setvar_i(res, R_d); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2685 | break; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2686 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2687 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2688 | case XC( OC_BUILTIN ): |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2689 | res = exec_builtin(op, res); |
| 2690 | break; |
| 2691 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2692 | case XC( OC_SPRINTF ): |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2693 | setvar_p(res, awk_printf(op1)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2694 | break; |
| 2695 | |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2696 | case XC( OC_UNARY ): { |
| 2697 | double Ld, R_d; |
| 2698 | |
| 2699 | Ld = R_d = getvar_i(R.v); |
Mike Frysinger | de2b938 | 2005-09-27 03:18:00 +0000 | [diff] [blame] | 2700 | switch (opn) { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2701 | case 'P': |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2702 | Ld = ++R_d; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2703 | goto r_op_change; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2704 | case 'p': |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2705 | R_d++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2706 | goto r_op_change; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2707 | case 'M': |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2708 | Ld = --R_d; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2709 | goto r_op_change; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2710 | case 'm': |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2711 | R_d--; |
| 2712 | r_op_change: |
| 2713 | setvar_i(R.v, R_d); |
| 2714 | break; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2715 | case '!': |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2716 | Ld = !istrue(R.v); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2717 | break; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2718 | case '-': |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2719 | Ld = -R_d; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2720 | break; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2721 | } |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2722 | setvar_i(res, Ld); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2723 | break; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2724 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2725 | |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2726 | case XC( OC_FIELD ): { |
| 2727 | int i = (int)getvar_i(R.v); |
| 2728 | if (i == 0) { |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2729 | res = intvar[F0]; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2730 | } else { |
| 2731 | split_f0(); |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2732 | if (i > nfields) |
| 2733 | fsrealloc(i); |
| 2734 | res = &Fields[i - 1]; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2735 | } |
| 2736 | break; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2737 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2738 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 2739 | /* concatenation (" ") and index joining (",") */ |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2740 | case XC( OC_CONCAT ): |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2741 | case XC( OC_COMMA ): { |
| 2742 | const char *sep = ""; |
| 2743 | if ((opinfo & OPCLSMASK) == OC_COMMA) |
| 2744 | sep = getvar_s(intvar[SUBSEP]); |
| 2745 | setvar_p(res, xasprintf("%s%s%s", L.s, sep, R.s)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2746 | break; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2747 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2748 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2749 | case XC( OC_LAND ): |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2750 | setvar_i(res, istrue(L.v) ? ptest(op->r.n) : 0); |
| 2751 | break; |
| 2752 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2753 | case XC( OC_LOR ): |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2754 | setvar_i(res, istrue(L.v) ? 1 : ptest(op->r.n)); |
| 2755 | break; |
| 2756 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2757 | case XC( OC_BINARY ): |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2758 | case XC( OC_REPLACE ): { |
| 2759 | double R_d = getvar_i(R.v); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2760 | switch (opn) { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2761 | case '+': |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2762 | L_d += R_d; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2763 | break; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2764 | case '-': |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2765 | L_d -= R_d; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2766 | break; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2767 | case '*': |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2768 | L_d *= R_d; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2769 | break; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2770 | case '/': |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2771 | if (R_d == 0) |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 2772 | syntax_error(EMSG_DIV_BY_ZERO); |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2773 | L_d /= R_d; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2774 | break; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2775 | case '&': |
Denis Vlasenko | 2d5bd80 | 2008-10-24 10:49:49 +0000 | [diff] [blame] | 2776 | #if ENABLE_FEATURE_AWK_LIBM |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2777 | L_d = pow(L_d, R_d); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2778 | #else |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2779 | syntax_error(EMSG_NO_MATH); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2780 | #endif |
| 2781 | break; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2782 | case '%': |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2783 | if (R_d == 0) |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 2784 | syntax_error(EMSG_DIV_BY_ZERO); |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2785 | L_d -= (int)(L_d / R_d) * R_d; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2786 | break; |
| 2787 | } |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2788 | res = setvar_i(((opinfo & OPCLSMASK) == OC_BINARY) ? res : L.v, L_d); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2789 | break; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2790 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2791 | |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2792 | case XC( OC_COMPARE ): { |
| 2793 | int i = i; /* for compiler */ |
| 2794 | double Ld; |
| 2795 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2796 | if (is_numeric(L.v) && is_numeric(R.v)) { |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2797 | Ld = getvar_i(L.v) - getvar_i(R.v); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2798 | } else { |
Denys Vlasenko | f9782ff | 2010-03-12 21:32:13 +0100 | [diff] [blame] | 2799 | const char *l = getvar_s(L.v); |
| 2800 | const char *r = getvar_s(R.v); |
| 2801 | Ld = icase ? strcasecmp(l, r) : strcmp(l, r); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2802 | } |
| 2803 | switch (opn & 0xfe) { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2804 | case 0: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2805 | i = (Ld > 0); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2806 | break; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2807 | case 2: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2808 | i = (Ld >= 0); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2809 | break; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2810 | case 4: |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2811 | i = (Ld == 0); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2812 | break; |
| 2813 | } |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2814 | setvar_i(res, (i == 0) ^ (opn & 1)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2815 | break; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2816 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2817 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2818 | default: |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2819 | syntax_error(EMSG_POSSIBLE_ERROR); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2820 | } |
| 2821 | if ((opinfo & OPCLSMASK) <= SHIFT_TIL_THIS) |
| 2822 | op = op->a.n; |
| 2823 | if ((opinfo & OPCLSMASK) >= RECUR_FROM_THIS) |
| 2824 | break; |
| 2825 | if (nextrec) |
| 2826 | break; |
Denys Vlasenko | c6ba997 | 2010-03-12 21:05:09 +0100 | [diff] [blame] | 2827 | } /* while (op) */ |
| 2828 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2829 | nvfree(v1); |
| 2830 | return res; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2831 | #undef fnargs |
| 2832 | #undef seed |
| 2833 | #undef sreg |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | |
| 2837 | /* -------- main & co. -------- */ |
| 2838 | |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 2839 | static int awk_exit(int r) |
| 2840 | { |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2841 | var tv; |
| 2842 | unsigned i; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2843 | hash_item *hi; |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 2844 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2845 | zero_out_var(&tv); |
| 2846 | |
| 2847 | if (!exiting) { |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 2848 | exiting = TRUE; |
Glenn L McGrath | ca29ffc | 2004-09-24 09:24:27 +0000 | [diff] [blame] | 2849 | nextrec = FALSE; |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 2850 | evaluate(endseq.first, &tv); |
| 2851 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2852 | |
| 2853 | /* waiting for children */ |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2854 | for (i = 0; i < fdhash->csize; i++) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2855 | hi = fdhash->items[i]; |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 2856 | while (hi) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2857 | if (hi->data.rs.F && hi->data.rs.is_pipe) |
| 2858 | pclose(hi->data.rs.F); |
| 2859 | hi = hi->next; |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | exit(r); |
| 2864 | } |
| 2865 | |
| 2866 | /* if expr looks like "var=value", perform assignment and return 1, |
| 2867 | * otherwise return 0 */ |
"Vladimir N. Oleynik" | 5cf9a03 | 2005-10-19 09:21:51 +0000 | [diff] [blame] | 2868 | static int is_assignment(const char *expr) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 2869 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2870 | char *exprc, *s, *s0, *s1; |
| 2871 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 2872 | exprc = xstrdup(expr); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2873 | if (!isalnum_(*exprc) || (s = strchr(exprc, '=')) == NULL) { |
| 2874 | free(exprc); |
| 2875 | return FALSE; |
| 2876 | } |
| 2877 | |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 2878 | *s++ = '\0'; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2879 | s0 = s1 = s; |
| 2880 | while (*s) |
Denys Vlasenko | 6ebdf7a | 2010-03-11 12:41:55 +0100 | [diff] [blame] | 2881 | *s1++ = nextchar(&s); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2882 | |
| 2883 | *s1 = '\0'; |
| 2884 | setvar_u(newvar(exprc), s0); |
| 2885 | free(exprc); |
| 2886 | return TRUE; |
| 2887 | } |
| 2888 | |
| 2889 | /* switch to next input file */ |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 2890 | static rstream *next_input_file(void) |
| 2891 | { |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2892 | #define rsm (G.next_input_file__rsm) |
| 2893 | #define files_happen (G.next_input_file__files_happen) |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2894 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2895 | FILE *F = NULL; |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 2896 | const char *fname, *ind; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2897 | |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 2898 | if (rsm.F) |
| 2899 | fclose(rsm.F); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2900 | rsm.F = NULL; |
Glenn L McGrath | 00ed36f | 2003-10-30 13:36:39 +0000 | [diff] [blame] | 2901 | rsm.pos = rsm.adv = 0; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2902 | |
| 2903 | do { |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2904 | if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2905 | if (files_happen) |
| 2906 | return NULL; |
| 2907 | fname = "-"; |
| 2908 | F = stdin; |
| 2909 | } else { |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2910 | ind = getvar_s(incvar(intvar[ARGIND])); |
| 2911 | fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2912 | if (fname && *fname && !is_assignment(fname)) |
Denis Vlasenko | 3bb2bbd | 2008-07-01 01:57:36 +0000 | [diff] [blame] | 2913 | F = xfopen_stdin(fname); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2914 | } |
| 2915 | } while (!F); |
| 2916 | |
| 2917 | files_happen = TRUE; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2918 | setvar_s(intvar[FILENAME], fname); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2919 | rsm.F = F; |
| 2920 | return &rsm; |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2921 | #undef rsm |
| 2922 | #undef files_happen |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 2925 | int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 2926 | int awk_main(int argc, char **argv) |
Mike Frysinger | 10a11e2 | 2005-09-27 02:23:02 +0000 | [diff] [blame] | 2927 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 2928 | unsigned opt; |
Denis Vlasenko | be644a8 | 2007-03-10 17:22:14 +0000 | [diff] [blame] | 2929 | char *opt_F, *opt_W; |
Denis Vlasenko | 3bb2bbd | 2008-07-01 01:57:36 +0000 | [diff] [blame] | 2930 | llist_t *list_v = NULL; |
| 2931 | llist_t *list_f = NULL; |
| 2932 | int i, j; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2933 | var *v; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2934 | var tv; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2935 | char **envp; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2936 | char *vnames = (char *)vNames; /* cheat */ |
| 2937 | char *vvalues = (char *)vValues; |
| 2938 | |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2939 | INIT_G(); |
| 2940 | |
Denis Vlasenko | 150f402 | 2007-01-13 21:06:21 +0000 | [diff] [blame] | 2941 | /* Undo busybox.c, or else strtod may eat ','! This breaks parsing: |
Denis Vlasenko | 6dc6ebb | 2007-01-01 23:53:12 +0000 | [diff] [blame] | 2942 | * $1,$2 == '$1,' '$2', NOT '$1' ',' '$2' */ |
| 2943 | if (ENABLE_LOCALE_SUPPORT) |
| 2944 | setlocale(LC_NUMERIC, "C"); |
| 2945 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2946 | zero_out_var(&tv); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2947 | |
| 2948 | /* allocate global buffer */ |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 2949 | g_buf = xmalloc(MAXVARFMT + 1); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2950 | |
| 2951 | vhash = hash_init(); |
| 2952 | ahash = hash_init(); |
| 2953 | fdhash = hash_init(); |
| 2954 | fnhash = hash_init(); |
| 2955 | |
| 2956 | /* initialize variables */ |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2957 | for (i = 0; *vnames; i++) { |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2958 | intvar[i] = v = newvar(nextword(&vnames)); |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2959 | if (*vvalues != '\377') |
| 2960 | setvar_s(v, nextword(&vvalues)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2961 | else |
| 2962 | setvar_i(v, 0); |
| 2963 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2964 | if (*vnames == '*') { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2965 | v->type |= VF_SPECIAL; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2966 | vnames++; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2967 | } |
| 2968 | } |
| 2969 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2970 | handle_special(intvar[FS]); |
| 2971 | handle_special(intvar[RS]); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2972 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2973 | newfile("/dev/stdin")->F = stdin; |
| 2974 | newfile("/dev/stdout")->F = stdout; |
| 2975 | newfile("/dev/stderr")->F = stderr; |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2976 | |
Denis Vlasenko | f71d916 | 2007-05-03 22:57:56 +0000 | [diff] [blame] | 2977 | /* Huh, people report that sometimes environ is NULL. Oh well. */ |
| 2978 | if (environ) for (envp = environ; *envp; envp++) { |
Denis Vlasenko | b78c782 | 2007-07-18 18:31:11 +0000 | [diff] [blame] | 2979 | /* environ is writable, thus we don't strdup it needlessly */ |
| 2980 | char *s = *envp; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2981 | char *s1 = strchr(s, '='); |
| 2982 | if (s1) { |
Denis Vlasenko | b78c782 | 2007-07-18 18:31:11 +0000 | [diff] [blame] | 2983 | *s1 = '\0'; |
| 2984 | /* Both findvar and setvar_u take const char* |
| 2985 | * as 2nd arg -> environment is not trashed */ |
| 2986 | setvar_u(findvar(iamarray(intvar[ENVIRON]), s), s1 + 1); |
| 2987 | *s1 = '='; |
Eric Andersen | 67776be | 2004-07-30 23:52:08 +0000 | [diff] [blame] | 2988 | } |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 2989 | } |
Denis Vlasenko | 3bb2bbd | 2008-07-01 01:57:36 +0000 | [diff] [blame] | 2990 | opt_complementary = "v::f::"; /* -v and -f can occur multiple times */ |
| 2991 | opt = getopt32(argv, "F:v:f:W:", &opt_F, &list_v, &list_f, &opt_W); |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 2992 | argv += optind; |
| 2993 | argc -= optind; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 2994 | if (opt & 0x1) |
| 2995 | setvar_s(intvar[FS], opt_F); // -F |
Denis Vlasenko | 3bb2bbd | 2008-07-01 01:57:36 +0000 | [diff] [blame] | 2996 | while (list_v) { /* -v */ |
| 2997 | if (!is_assignment(llist_pop(&list_v))) |
Denis Vlasenko | be644a8 | 2007-03-10 17:22:14 +0000 | [diff] [blame] | 2998 | bb_show_usage(); |
| 2999 | } |
Denis Vlasenko | 3bb2bbd | 2008-07-01 01:57:36 +0000 | [diff] [blame] | 3000 | if (list_f) { /* -f */ |
| 3001 | do { |
| 3002 | char *s = NULL; |
| 3003 | FILE *from_file; |
| 3004 | |
| 3005 | g_progname = llist_pop(&list_f); |
| 3006 | from_file = xfopen_stdin(g_progname); |
| 3007 | /* one byte is reserved for some trick in next_token */ |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 3008 | for (i = j = 1; j > 0; i += j) { |
| 3009 | s = xrealloc(s, i + 4096); |
| 3010 | j = fread(s + i, 1, 4094, from_file); |
Denis Vlasenko | 099efbf | 2006-09-22 09:02:30 +0000 | [diff] [blame] | 3011 | } |
Denis Vlasenko | 3bb2bbd | 2008-07-01 01:57:36 +0000 | [diff] [blame] | 3012 | s[i] = '\0'; |
| 3013 | fclose(from_file); |
| 3014 | parse_program(s + 1); |
| 3015 | free(s); |
| 3016 | } while (list_f); |
Denis Vlasenko | 41d5ebe | 2009-01-25 01:00:15 +0000 | [diff] [blame] | 3017 | argc++; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 3018 | } else { // no -f: take program from 1st parameter |
| 3019 | if (!argc) |
| 3020 | bb_show_usage(); |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 3021 | g_progname = "cmd. line"; |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 3022 | parse_program(*argv++); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3023 | } |
Denis Vlasenko | 099efbf | 2006-09-22 09:02:30 +0000 | [diff] [blame] | 3024 | if (opt & 0x8) // -W |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 3025 | bb_error_msg("warning: unrecognized option '-W %s' ignored", opt_W); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3026 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3027 | /* fill in ARGV array */ |
Denis Vlasenko | 41d5ebe | 2009-01-25 01:00:15 +0000 | [diff] [blame] | 3028 | setvar_i(intvar[ARGC], argc); |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 3029 | setari_u(intvar[ARGV], 0, "awk"); |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 3030 | i = 0; |
| 3031 | while (*argv) |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 3032 | setari_u(intvar[ARGV], ++i, *argv++); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3033 | |
| 3034 | evaluate(beginseq.first, &tv); |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 3035 | if (!mainseq.first && !endseq.first) |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3036 | awk_exit(EXIT_SUCCESS); |
| 3037 | |
| 3038 | /* input file could already be opened in BEGIN block */ |
Denys Vlasenko | cdeda16 | 2009-11-30 01:14:16 +0100 | [diff] [blame] | 3039 | if (!iF) |
| 3040 | iF = next_input_file(); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3041 | |
| 3042 | /* passing through input files */ |
| 3043 | while (iF) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3044 | nextfile = FALSE; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 3045 | setvar_i(intvar[FNR], 0); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3046 | |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 3047 | while ((i = awk_getline(iF, intvar[F0])) > 0) { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3048 | nextrec = FALSE; |
Denis Vlasenko | ffba941 | 2007-05-17 23:03:35 +0000 | [diff] [blame] | 3049 | incvar(intvar[NR]); |
| 3050 | incvar(intvar[FNR]); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3051 | evaluate(mainseq.first, &tv); |
| 3052 | |
| 3053 | if (nextfile) |
| 3054 | break; |
| 3055 | } |
| 3056 | |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 3057 | if (i < 0) |
Denis Vlasenko | ae5a8aa | 2007-06-06 17:01:00 +0000 | [diff] [blame] | 3058 | syntax_error(strerror(errno)); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3059 | |
| 3060 | iF = next_input_file(); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3063 | awk_exit(EXIT_SUCCESS); |
Denis Vlasenko | f782f52 | 2007-01-01 23:51:30 +0000 | [diff] [blame] | 3064 | /*return 0;*/ |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 3065 | } |