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