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