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