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