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