1 | /* A Bison parser, made by GNU Bison 3.0.4. */ |
2 | |
3 | /* Bison implementation for Yacc-like parsers in C |
4 | |
5 | Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc. |
6 | |
7 | This program is free software: you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. |
11 | |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
19 | |
20 | /* As a special exception, you may create a larger work that contains |
21 | part or all of the Bison parser skeleton and distribute that work |
22 | under terms of your choice, so long as that work isn't itself a |
23 | parser generator using the skeleton or a modified version thereof |
24 | as a parser skeleton. Alternatively, if you modify or redistribute |
25 | the parser skeleton itself, you may (at your option) remove this |
26 | special exception, which will cause the skeleton and the resulting |
27 | Bison output files to be licensed under the GNU General Public |
28 | License without this special exception. |
29 | |
30 | This special exception was added by the Free Software Foundation in |
31 | version 2.2 of Bison. */ |
32 | |
33 | /* C LALR(1) parser skeleton written by Richard Stallman, by |
34 | simplifying the original so-called "semantic" parser. */ |
35 | |
36 | /* All symbols defined below should begin with yy or YY, to avoid |
37 | infringing on user name space. This should be done even for local |
38 | variables, as they might otherwise be expanded by user macros. |
39 | There are some unavoidable exceptions within include files to |
40 | define necessary library symbols; they are noted "INFRINGES ON |
41 | USER NAME SPACE" below. */ |
42 | |
43 | /* Identify Bison output. */ |
44 | #define YYBISON 1 |
45 | |
46 | /* Bison version. */ |
47 | #define YYBISON_VERSION "3.0.4" |
48 | |
49 | /* Skeleton name. */ |
50 | #define YYSKELETON_NAME "yacc.c" |
51 | |
52 | /* Pure parsers. */ |
53 | #define YYPURE 0 |
54 | |
55 | /* Push parsers. */ |
56 | #define YYPUSH 0 |
57 | |
58 | /* Pull parsers. */ |
59 | #define YYPULL 1 |
60 | |
61 | |
62 | |
63 | |
64 | /* Copy the first part of user declarations. */ |
65 | #line 19 "cmd-parse.y" /* yacc.c:339 */ |
66 | |
67 | |
68 | #include <sys/types.h> |
69 | |
70 | #include <ctype.h> |
71 | #include <errno.h> |
72 | #include <pwd.h> |
73 | #include <stdlib.h> |
74 | #include <string.h> |
75 | #include <unistd.h> |
76 | |
77 | #include "tmux.h" |
78 | |
79 | static int yylex(void); |
80 | static int yyparse(void); |
81 | static int printflike(1,2) yyerror(const char *, ...); |
82 | |
83 | static char *yylex_token(int); |
84 | static char *yylex_format(void); |
85 | |
86 | struct cmd_parse_scope { |
87 | int flag; |
88 | TAILQ_ENTRY (cmd_parse_scope) entry; |
89 | }; |
90 | |
91 | struct cmd_parse_command { |
92 | char *name; |
93 | u_int line; |
94 | |
95 | int argc; |
96 | char **argv; |
97 | |
98 | TAILQ_ENTRY(cmd_parse_command) entry; |
99 | }; |
100 | TAILQ_HEAD(cmd_parse_commands, cmd_parse_command); |
101 | |
102 | struct cmd_parse_state { |
103 | FILE *f; |
104 | |
105 | const char *buf; |
106 | size_t len; |
107 | size_t off; |
108 | |
109 | int condition; |
110 | int eol; |
111 | int eof; |
112 | struct cmd_parse_input *input; |
113 | u_int escapes; |
114 | |
115 | char *error; |
116 | struct cmd_parse_commands *commands; |
117 | |
118 | struct cmd_parse_scope *scope; |
119 | TAILQ_HEAD(, cmd_parse_scope) stack; |
120 | }; |
121 | static struct cmd_parse_state parse_state; |
122 | |
123 | static char *cmd_parse_get_error(const char *, u_int, const char *); |
124 | static void cmd_parse_free_command(struct cmd_parse_command *); |
125 | static struct cmd_parse_commands *cmd_parse_new_commands(void); |
126 | static void cmd_parse_free_commands(struct cmd_parse_commands *); |
127 | static void cmd_parse_print_commands(struct cmd_parse_input *, u_int, |
128 | struct cmd_list *); |
129 | |
130 | |
131 | #line 132 "cmd-parse.c" /* yacc.c:339 */ |
132 | |
133 | # ifndef YY_NULLPTR |
134 | # if defined __cplusplus && 201103L <= __cplusplus |
135 | # define YY_NULLPTR nullptr |
136 | # else |
137 | # define YY_NULLPTR 0 |
138 | # endif |
139 | # endif |
140 | |
141 | /* Enabling verbose error messages. */ |
142 | #ifdef YYERROR_VERBOSE |
143 | # undef YYERROR_VERBOSE |
144 | # define YYERROR_VERBOSE 1 |
145 | #else |
146 | # define YYERROR_VERBOSE 0 |
147 | #endif |
148 | |
149 | |
150 | /* Debug traces. */ |
151 | #ifndef YYDEBUG |
152 | # define YYDEBUG 0 |
153 | #endif |
154 | #if YYDEBUG |
155 | extern int yydebug; |
156 | #endif |
157 | |
158 | /* Token type. */ |
159 | #ifndef YYTOKENTYPE |
160 | # define YYTOKENTYPE |
161 | enum yytokentype |
162 | { |
163 | ERROR = 258, |
164 | IF = 259, |
165 | ELSE = 260, |
166 | ELIF = 261, |
167 | ENDIF = 262, |
168 | FORMAT = 263, |
169 | TOKEN = 264, |
170 | EQUALS = 265 |
171 | }; |
172 | #endif |
173 | /* Tokens. */ |
174 | #define ERROR 258 |
175 | #define IF 259 |
176 | #define ELSE 260 |
177 | #define ELIF 261 |
178 | #define ENDIF 262 |
179 | #define FORMAT 263 |
180 | #define TOKEN 264 |
181 | #define EQUALS 265 |
182 | |
183 | /* Value type. */ |
184 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED |
185 | |
186 | union YYSTYPE |
187 | { |
188 | #line 86 "cmd-parse.y" /* yacc.c:355 */ |
189 | |
190 | char *token; |
191 | struct { |
192 | int argc; |
193 | char **argv; |
194 | } arguments; |
195 | int flag; |
196 | struct { |
197 | int flag; |
198 | struct cmd_parse_commands *commands; |
199 | } elif; |
200 | struct cmd_parse_commands *commands; |
201 | struct cmd_parse_command *command; |
202 | |
203 | #line 204 "cmd-parse.c" /* yacc.c:355 */ |
204 | }; |
205 | |
206 | typedef union YYSTYPE YYSTYPE; |
207 | # define YYSTYPE_IS_TRIVIAL 1 |
208 | # define YYSTYPE_IS_DECLARED 1 |
209 | #endif |
210 | |
211 | |
212 | extern YYSTYPE yylval; |
213 | |
214 | int yyparse (void); |
215 | |
216 | |
217 | |
218 | /* Copy the second part of user declarations. */ |
219 | |
220 | #line 221 "cmd-parse.c" /* yacc.c:358 */ |
221 | |
222 | #ifdef short |
223 | # undef short |
224 | #endif |
225 | |
226 | #ifdef YYTYPE_UINT8 |
227 | typedef YYTYPE_UINT8 yytype_uint8; |
228 | #else |
229 | typedef unsigned char yytype_uint8; |
230 | #endif |
231 | |
232 | #ifdef YYTYPE_INT8 |
233 | typedef YYTYPE_INT8 yytype_int8; |
234 | #else |
235 | typedef signed char yytype_int8; |
236 | #endif |
237 | |
238 | #ifdef YYTYPE_UINT16 |
239 | typedef YYTYPE_UINT16 yytype_uint16; |
240 | #else |
241 | typedef unsigned short int yytype_uint16; |
242 | #endif |
243 | |
244 | #ifdef YYTYPE_INT16 |
245 | typedef YYTYPE_INT16 yytype_int16; |
246 | #else |
247 | typedef short int yytype_int16; |
248 | #endif |
249 | |
250 | #ifndef YYSIZE_T |
251 | # ifdef __SIZE_TYPE__ |
252 | # define YYSIZE_T __SIZE_TYPE__ |
253 | # elif defined size_t |
254 | # define YYSIZE_T size_t |
255 | # elif ! defined YYSIZE_T |
256 | # include <stddef.h> /* INFRINGES ON USER NAME SPACE */ |
257 | # define YYSIZE_T size_t |
258 | # else |
259 | # define YYSIZE_T unsigned int |
260 | # endif |
261 | #endif |
262 | |
263 | #define YYSIZE_MAXIMUM ((YYSIZE_T) -1) |
264 | |
265 | #ifndef YY_ |
266 | # if defined YYENABLE_NLS && YYENABLE_NLS |
267 | # if ENABLE_NLS |
268 | # include <libintl.h> /* INFRINGES ON USER NAME SPACE */ |
269 | # define YY_(Msgid) dgettext ("bison-runtime", Msgid) |
270 | # endif |
271 | # endif |
272 | # ifndef YY_ |
273 | # define YY_(Msgid) Msgid |
274 | # endif |
275 | #endif |
276 | |
277 | #ifndef YY_ATTRIBUTE |
278 | # if (defined __GNUC__ \ |
279 | && (2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__))) \ |
280 | || defined __SUNPRO_C && 0x5110 <= __SUNPRO_C |
281 | # define YY_ATTRIBUTE(Spec) __attribute__(Spec) |
282 | # else |
283 | # define YY_ATTRIBUTE(Spec) /* empty */ |
284 | # endif |
285 | #endif |
286 | |
287 | #ifndef YY_ATTRIBUTE_PURE |
288 | # define YY_ATTRIBUTE_PURE YY_ATTRIBUTE ((__pure__)) |
289 | #endif |
290 | |
291 | #ifndef YY_ATTRIBUTE_UNUSED |
292 | # define YY_ATTRIBUTE_UNUSED YY_ATTRIBUTE ((__unused__)) |
293 | #endif |
294 | |
295 | #if !defined _Noreturn \ |
296 | && (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112) |
297 | # if defined _MSC_VER && 1200 <= _MSC_VER |
298 | # define _Noreturn __declspec (noreturn) |
299 | # else |
300 | # define _Noreturn YY_ATTRIBUTE ((__noreturn__)) |
301 | # endif |
302 | #endif |
303 | |
304 | /* Suppress unused-variable warnings by "using" E. */ |
305 | #if ! defined lint || defined __GNUC__ |
306 | # define YYUSE(E) ((void) (E)) |
307 | #else |
308 | # define YYUSE(E) /* empty */ |
309 | #endif |
310 | |
311 | #if defined __GNUC__ && 407 <= __GNUC__ * 100 + __GNUC_MINOR__ |
312 | /* Suppress an incorrect diagnostic about yylval being uninitialized. */ |
313 | # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \ |
314 | _Pragma ("GCC diagnostic push") \ |
315 | _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")\ |
316 | _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"") |
317 | # define YY_IGNORE_MAYBE_UNINITIALIZED_END \ |
318 | _Pragma ("GCC diagnostic pop") |
319 | #else |
320 | # define YY_INITIAL_VALUE(Value) Value |
321 | #endif |
322 | #ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN |
323 | # define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN |
324 | # define YY_IGNORE_MAYBE_UNINITIALIZED_END |
325 | #endif |
326 | #ifndef YY_INITIAL_VALUE |
327 | # define YY_INITIAL_VALUE(Value) /* Nothing. */ |
328 | #endif |
329 | |
330 | |
331 | #if ! defined yyoverflow || YYERROR_VERBOSE |
332 | |
333 | /* The parser invokes alloca or malloc; define the necessary symbols. */ |
334 | |
335 | # ifdef YYSTACK_USE_ALLOCA |
336 | # if YYSTACK_USE_ALLOCA |
337 | # ifdef __GNUC__ |
338 | # define YYSTACK_ALLOC __builtin_alloca |
339 | # elif defined __BUILTIN_VA_ARG_INCR |
340 | # include <alloca.h> /* INFRINGES ON USER NAME SPACE */ |
341 | # elif defined _AIX |
342 | # define YYSTACK_ALLOC __alloca |
343 | # elif defined _MSC_VER |
344 | # include <malloc.h> /* INFRINGES ON USER NAME SPACE */ |
345 | # define alloca _alloca |
346 | # else |
347 | # define YYSTACK_ALLOC alloca |
348 | # if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS |
349 | # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */ |
350 | /* Use EXIT_SUCCESS as a witness for stdlib.h. */ |
351 | # ifndef EXIT_SUCCESS |
352 | # define EXIT_SUCCESS 0 |
353 | # endif |
354 | # endif |
355 | # endif |
356 | # endif |
357 | # endif |
358 | |
359 | # ifdef YYSTACK_ALLOC |
360 | /* Pacify GCC's 'empty if-body' warning. */ |
361 | # define YYSTACK_FREE(Ptr) do { /* empty */; } while (0) |
362 | # ifndef YYSTACK_ALLOC_MAXIMUM |
363 | /* The OS might guarantee only one guard page at the bottom of the stack, |
364 | and a page size can be as small as 4096 bytes. So we cannot safely |
365 | invoke alloca (N) if N exceeds 4096. Use a slightly smaller number |
366 | to allow for a few compiler-allocated temporary stack slots. */ |
367 | # define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */ |
368 | # endif |
369 | # else |
370 | # define YYSTACK_ALLOC YYMALLOC |
371 | # define YYSTACK_FREE YYFREE |
372 | # ifndef YYSTACK_ALLOC_MAXIMUM |
373 | # define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM |
374 | # endif |
375 | # if (defined __cplusplus && ! defined EXIT_SUCCESS \ |
376 | && ! ((defined YYMALLOC || defined malloc) \ |
377 | && (defined YYFREE || defined free))) |
378 | # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */ |
379 | # ifndef EXIT_SUCCESS |
380 | # define EXIT_SUCCESS 0 |
381 | # endif |
382 | # endif |
383 | # ifndef YYMALLOC |
384 | # define YYMALLOC malloc |
385 | # if ! defined malloc && ! defined EXIT_SUCCESS |
386 | void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */ |
387 | # endif |
388 | # endif |
389 | # ifndef YYFREE |
390 | # define YYFREE free |
391 | # if ! defined free && ! defined EXIT_SUCCESS |
392 | void free (void *); /* INFRINGES ON USER NAME SPACE */ |
393 | # endif |
394 | # endif |
395 | # endif |
396 | #endif /* ! defined yyoverflow || YYERROR_VERBOSE */ |
397 | |
398 | |
399 | #if (! defined yyoverflow \ |
400 | && (! defined __cplusplus \ |
401 | || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL))) |
402 | |
403 | /* A type that is properly aligned for any stack member. */ |
404 | union yyalloc |
405 | { |
406 | yytype_int16 yyss_alloc; |
407 | YYSTYPE yyvs_alloc; |
408 | }; |
409 | |
410 | /* The size of the maximum gap between one aligned stack and the next. */ |
411 | # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1) |
412 | |
413 | /* The size of an array large to enough to hold all stacks, each with |
414 | N elements. */ |
415 | # define YYSTACK_BYTES(N) \ |
416 | ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \ |
417 | + YYSTACK_GAP_MAXIMUM) |
418 | |
419 | # define YYCOPY_NEEDED 1 |
420 | |
421 | /* Relocate STACK from its old location to the new one. The |
422 | local variables YYSIZE and YYSTACKSIZE give the old and new number of |
423 | elements in the stack, and YYPTR gives the new location of the |
424 | stack. Advance YYPTR to a properly aligned location for the next |
425 | stack. */ |
426 | # define YYSTACK_RELOCATE(Stack_alloc, Stack) \ |
427 | do \ |
428 | { \ |
429 | YYSIZE_T yynewbytes; \ |
430 | YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \ |
431 | Stack = &yyptr->Stack_alloc; \ |
432 | yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \ |
433 | yyptr += yynewbytes / sizeof (*yyptr); \ |
434 | } \ |
435 | while (0) |
436 | |
437 | #endif |
438 | |
439 | #if defined YYCOPY_NEEDED && YYCOPY_NEEDED |
440 | /* Copy COUNT objects from SRC to DST. The source and destination do |
441 | not overlap. */ |
442 | # ifndef YYCOPY |
443 | # if defined __GNUC__ && 1 < __GNUC__ |
444 | # define YYCOPY(Dst, Src, Count) \ |
445 | __builtin_memcpy (Dst, Src, (Count) * sizeof (*(Src))) |
446 | # else |
447 | # define YYCOPY(Dst, Src, Count) \ |
448 | do \ |
449 | { \ |
450 | YYSIZE_T yyi; \ |
451 | for (yyi = 0; yyi < (Count); yyi++) \ |
452 | (Dst)[yyi] = (Src)[yyi]; \ |
453 | } \ |
454 | while (0) |
455 | # endif |
456 | # endif |
457 | #endif /* !YYCOPY_NEEDED */ |
458 | |
459 | /* YYFINAL -- State number of the termination state. */ |
460 | #define YYFINAL 17 |
461 | /* YYLAST -- Last index in YYTABLE. */ |
462 | #define YYLAST 87 |
463 | |
464 | /* YYNTOKENS -- Number of terminals. */ |
465 | #define YYNTOKENS 13 |
466 | /* YYNNTS -- Number of nonterminals. */ |
467 | #define YYNNTS 20 |
468 | /* YYNRULES -- Number of rules. */ |
469 | #define YYNRULES 42 |
470 | /* YYNSTATES -- Number of states. */ |
471 | #define YYNSTATES 65 |
472 | |
473 | /* YYTRANSLATE[YYX] -- Symbol number corresponding to YYX as returned |
474 | by yylex, with out-of-bounds checking. */ |
475 | #define YYUNDEFTOK 2 |
476 | #define YYMAXUTOK 265 |
477 | |
478 | #define YYTRANSLATE(YYX) \ |
479 | ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK) |
480 | |
481 | /* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM |
482 | as returned by yylex, without out-of-bounds checking. */ |
483 | static const yytype_uint8 yytranslate[] = |
484 | { |
485 | 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
486 | 11, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
487 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
488 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
489 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
490 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 12, |
491 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
492 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
493 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
494 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
495 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
496 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
497 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
498 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
499 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
500 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
501 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
502 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
503 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
504 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
505 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
506 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
507 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
508 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
509 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
510 | 2, 2, 2, 2, 2, 2, 1, 2, 3, 4, |
511 | 5, 6, 7, 8, 9, 10 |
512 | }; |
513 | |
514 | #if YYDEBUG |
515 | /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ |
516 | static const yytype_uint16 yyrline[] = |
517 | { |
518 | 0, 117, 117, 118, 125, 129, 137, 141, 152, 164, |
519 | 168, 173, 197, 198, 200, 211, 225, 237, 250, 260, |
520 | 269, 279, 293, 310, 321, 339, 350, 354, 360, 374, |
521 | 379, 387, 396, 408, 417, 427, 441, 458, 469, 487, |
522 | 494, 501, 505 |
523 | }; |
524 | #endif |
525 | |
526 | #if YYDEBUG || YYERROR_VERBOSE || 0 |
527 | /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. |
528 | First, the terminals, then, starting at YYNTOKENS, nonterminals. */ |
529 | static const char *const yytname[] = |
530 | { |
531 | "$end" , "error" , "$undefined" , "ERROR" , "IF" , "ELSE" , "ELIF" , "ENDIF" , |
532 | "FORMAT" , "TOKEN" , "EQUALS" , "'\\n'" , "';'" , "$accept" , "lines" , |
533 | "statements" , "statement" , "format" , "expanded" , "optional_assignment" , |
534 | "assignment" , "if_open" , "if_else" , "if_elif" , "if_close" , "condition" , |
535 | "elif" , "commands" , "command" , "condition1" , "elif1" , "arguments" , |
536 | "argument" , YY_NULLPTR |
537 | }; |
538 | #endif |
539 | |
540 | # ifdef YYPRINT |
541 | /* YYTOKNUM[NUM] -- (External) token number corresponding to the |
542 | (internal) symbol number NUM (which must be that of a token). */ |
543 | static const yytype_uint16 yytoknum[] = |
544 | { |
545 | 0, 256, 257, 258, 259, 260, 261, 262, 263, 264, |
546 | 265, 10, 59 |
547 | }; |
548 | # endif |
549 | |
550 | #define YYPACT_NINF -29 |
551 | |
552 | #define yypact_value_is_default(Yystate) \ |
553 | (!!((Yystate) == (-29))) |
554 | |
555 | #define YYTABLE_NINF -14 |
556 | |
557 | #define yytable_value_is_error(Yytable_value) \ |
558 | 0 |
559 | |
560 | /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing |
561 | STATE-NUM. */ |
562 | static const yytype_int8 yypact[] = |
563 | { |
564 | -1, 67, -29, 11, -1, 10, 26, 30, 14, -29, |
565 | 32, -29, -29, -29, -29, -29, -29, -29, 12, -29, |
566 | 70, 68, 33, 45, 68, -29, -29, -29, -29, 70, |
567 | 60, -29, 67, -29, 33, 33, -29, 9, -29, -29, |
568 | -29, 36, 49, -29, 9, -29, 0, 34, 33, -29, |
569 | 68, 68, 71, -29, -29, -29, 0, 64, 52, 68, |
570 | -29, -29, -29, 64, -29 |
571 | }; |
572 | |
573 | /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM. |
574 | Performed when YYTABLE does not specify something else to do. Zero |
575 | means the default is an error. */ |
576 | static const yytype_uint8 yydefact[] = |
577 | { |
578 | 2, 0, 14, 0, 3, 0, 0, 30, 12, 7, |
579 | 8, 25, 29, 9, 10, 11, 15, 1, 0, 4, |
580 | 31, 6, 12, 0, 26, 5, 41, 42, 32, 39, |
581 | 6, 16, 0, 18, 12, 12, 33, 0, 28, 27, |
582 | 40, 0, 0, 19, 0, 17, 0, 37, 12, 35, |
583 | 6, 6, 0, 21, 34, 38, 0, 6, 23, 6, |
584 | 36, 20, 24, 6, 22 |
585 | }; |
586 | |
587 | /* YYPGOTO[NTERM-NUM]. */ |
588 | static const yytype_int8 yypgoto[] = |
589 | { |
590 | -29, -29, -17, -4, -29, 51, -29, -29, -7, -24, |
591 | -28, -8, -29, 23, -3, 61, 62, 37, 58, -29 |
592 | }; |
593 | |
594 | /* YYDEFGOTO[NTERM-NUM]. */ |
595 | static const yytype_int8 yydefgoto[] = |
596 | { |
597 | -1, 3, 4, 5, 15, 16, 6, 7, 8, 34, |
598 | 35, 36, 9, 44, 10, 11, 12, 37, 28, 29 |
599 | }; |
600 | |
601 | /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If |
602 | positive, shift that token. If negative, reduce the rule whose |
603 | number is the opposite. If YYTABLE_NINF, syntax error. */ |
604 | static const yytype_int8 yytable[] = |
605 | { |
606 | 18, 22, 42, 1, 30, 23, 41, 33, -12, 2, |
607 | -6, 17, 24, 48, 31, 22, 33, 22, 1, 23, |
608 | 52, 19, 43, 25, 2, 21, 18, 22, 22, 49, |
609 | 42, 46, 47, 57, 58, 20, 53, 1, 54, -13, |
610 | 32, 22, 63, 2, 24, 56, 24, 50, 60, 61, |
611 | 31, 32, 33, 18, 18, 64, 1, 24, 32, 18, |
612 | 51, -12, 2, -6, 1, 31, 32, 33, 1, -12, |
613 | 2, 33, 1, -12, 2, 13, 14, -12, 2, 26, |
614 | 27, 62, 59, 45, 55, 38, 39, 40 |
615 | }; |
616 | |
617 | static const yytype_uint8 yycheck[] = |
618 | { |
619 | 4, 8, 30, 4, 21, 8, 30, 7, 9, 10, |
620 | 11, 0, 12, 37, 5, 22, 7, 24, 4, 22, |
621 | 44, 11, 30, 11, 10, 11, 30, 34, 35, 37, |
622 | 58, 34, 35, 50, 51, 9, 44, 4, 46, 9, |
623 | 6, 48, 59, 10, 12, 48, 12, 11, 56, 57, |
624 | 5, 6, 7, 57, 58, 63, 4, 12, 6, 63, |
625 | 11, 9, 10, 11, 4, 5, 6, 7, 4, 9, |
626 | 10, 7, 4, 9, 10, 8, 9, 9, 10, 9, |
627 | 10, 58, 11, 32, 47, 24, 24, 29 |
628 | }; |
629 | |
630 | /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing |
631 | symbol of state STATE-NUM. */ |
632 | static const yytype_uint8 yystos[] = |
633 | { |
634 | 0, 4, 10, 14, 15, 16, 19, 20, 21, 25, |
635 | 27, 28, 29, 8, 9, 17, 18, 0, 16, 11, |
636 | 9, 11, 21, 27, 12, 11, 9, 10, 31, 32, |
637 | 15, 5, 6, 7, 22, 23, 24, 30, 28, 29, |
638 | 31, 22, 23, 24, 26, 18, 27, 27, 22, 24, |
639 | 11, 11, 22, 24, 24, 30, 27, 15, 15, 11, |
640 | 24, 24, 26, 15, 24 |
641 | }; |
642 | |
643 | /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ |
644 | static const yytype_uint8 yyr1[] = |
645 | { |
646 | 0, 13, 14, 14, 15, 15, 16, 16, 16, 17, |
647 | 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, |
648 | 25, 25, 25, 26, 26, 27, 27, 27, 27, 27, |
649 | 28, 28, 28, 29, 29, 29, 29, 30, 30, 31, |
650 | 31, 32, 32 |
651 | }; |
652 | |
653 | /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */ |
654 | static const yytype_uint8 yyr2[] = |
655 | { |
656 | 0, 2, 0, 1, 2, 3, 0, 1, 1, 1, |
657 | 1, 1, 0, 1, 1, 2, 1, 2, 1, 4, |
658 | 7, 5, 8, 3, 4, 1, 2, 3, 3, 1, |
659 | 1, 2, 3, 3, 5, 4, 6, 2, 3, 1, |
660 | 2, 1, 1 |
661 | }; |
662 | |
663 | |
664 | #define yyerrok (yyerrstatus = 0) |
665 | #define yyclearin (yychar = YYEMPTY) |
666 | #define YYEMPTY (-2) |
667 | #define YYEOF 0 |
668 | |
669 | #define YYACCEPT goto yyacceptlab |
670 | #define YYABORT goto yyabortlab |
671 | #define YYERROR goto yyerrorlab |
672 | |
673 | |
674 | #define YYRECOVERING() (!!yyerrstatus) |
675 | |
676 | #define YYBACKUP(Token, Value) \ |
677 | do \ |
678 | if (yychar == YYEMPTY) \ |
679 | { \ |
680 | yychar = (Token); \ |
681 | yylval = (Value); \ |
682 | YYPOPSTACK (yylen); \ |
683 | yystate = *yyssp; \ |
684 | goto yybackup; \ |
685 | } \ |
686 | else \ |
687 | { \ |
688 | yyerror (YY_("syntax error: cannot back up")); \ |
689 | YYERROR; \ |
690 | } \ |
691 | while (0) |
692 | |
693 | /* Error token number */ |
694 | #define YYTERROR 1 |
695 | #define YYERRCODE 256 |
696 | |
697 | |
698 | |
699 | /* Enable debugging if requested. */ |
700 | #if YYDEBUG |
701 | |
702 | # ifndef YYFPRINTF |
703 | # include <stdio.h> /* INFRINGES ON USER NAME SPACE */ |
704 | # define YYFPRINTF fprintf |
705 | # endif |
706 | |
707 | # define YYDPRINTF(Args) \ |
708 | do { \ |
709 | if (yydebug) \ |
710 | YYFPRINTF Args; \ |
711 | } while (0) |
712 | |
713 | /* This macro is provided for backward compatibility. */ |
714 | #ifndef YY_LOCATION_PRINT |
715 | # define YY_LOCATION_PRINT(File, Loc) ((void) 0) |
716 | #endif |
717 | |
718 | |
719 | # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \ |
720 | do { \ |
721 | if (yydebug) \ |
722 | { \ |
723 | YYFPRINTF (stderr, "%s ", Title); \ |
724 | yy_symbol_print (stderr, \ |
725 | Type, Value); \ |
726 | YYFPRINTF (stderr, "\n"); \ |
727 | } \ |
728 | } while (0) |
729 | |
730 | |
731 | /*----------------------------------------. |
732 | | Print this symbol's value on YYOUTPUT. | |
733 | `----------------------------------------*/ |
734 | |
735 | static void |
736 | yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep) |
737 | { |
738 | FILE *yyo = yyoutput; |
739 | YYUSE (yyo); |
740 | if (!yyvaluep) |
741 | return; |
742 | # ifdef YYPRINT |
743 | if (yytype < YYNTOKENS) |
744 | YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep); |
745 | # endif |
746 | YYUSE (yytype); |
747 | } |
748 | |
749 | |
750 | /*--------------------------------. |
751 | | Print this symbol on YYOUTPUT. | |
752 | `--------------------------------*/ |
753 | |
754 | static void |
755 | yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep) |
756 | { |
757 | YYFPRINTF (yyoutput, "%s %s (" , |
758 | yytype < YYNTOKENS ? "token" : "nterm" , yytname[yytype]); |
759 | |
760 | yy_symbol_value_print (yyoutput, yytype, yyvaluep); |
761 | YYFPRINTF (yyoutput, ")" ); |
762 | } |
763 | |
764 | /*------------------------------------------------------------------. |
765 | | yy_stack_print -- Print the state stack from its BOTTOM up to its | |
766 | | TOP (included). | |
767 | `------------------------------------------------------------------*/ |
768 | |
769 | static void |
770 | yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop) |
771 | { |
772 | YYFPRINTF (stderr, "Stack now" ); |
773 | for (; yybottom <= yytop; yybottom++) |
774 | { |
775 | int yybot = *yybottom; |
776 | YYFPRINTF (stderr, " %d" , yybot); |
777 | } |
778 | YYFPRINTF (stderr, "\n" ); |
779 | } |
780 | |
781 | # define YY_STACK_PRINT(Bottom, Top) \ |
782 | do { \ |
783 | if (yydebug) \ |
784 | yy_stack_print ((Bottom), (Top)); \ |
785 | } while (0) |
786 | |
787 | |
788 | /*------------------------------------------------. |
789 | | Report that the YYRULE is going to be reduced. | |
790 | `------------------------------------------------*/ |
791 | |
792 | static void |
793 | yy_reduce_print (yytype_int16 *yyssp, YYSTYPE *yyvsp, int yyrule) |
794 | { |
795 | unsigned long int yylno = yyrline[yyrule]; |
796 | int yynrhs = yyr2[yyrule]; |
797 | int yyi; |
798 | YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n" , |
799 | yyrule - 1, yylno); |
800 | /* The symbols being reduced. */ |
801 | for (yyi = 0; yyi < yynrhs; yyi++) |
802 | { |
803 | YYFPRINTF (stderr, " $%d = " , yyi + 1); |
804 | yy_symbol_print (stderr, |
805 | yystos[yyssp[yyi + 1 - yynrhs]], |
806 | &(yyvsp[(yyi + 1) - (yynrhs)]) |
807 | ); |
808 | YYFPRINTF (stderr, "\n" ); |
809 | } |
810 | } |
811 | |
812 | # define YY_REDUCE_PRINT(Rule) \ |
813 | do { \ |
814 | if (yydebug) \ |
815 | yy_reduce_print (yyssp, yyvsp, Rule); \ |
816 | } while (0) |
817 | |
818 | /* Nonzero means print parse trace. It is left uninitialized so that |
819 | multiple parsers can coexist. */ |
820 | int yydebug; |
821 | #else /* !YYDEBUG */ |
822 | # define YYDPRINTF(Args) |
823 | # define YY_SYMBOL_PRINT(Title, Type, Value, Location) |
824 | # define YY_STACK_PRINT(Bottom, Top) |
825 | # define YY_REDUCE_PRINT(Rule) |
826 | #endif /* !YYDEBUG */ |
827 | |
828 | |
829 | /* YYINITDEPTH -- initial size of the parser's stacks. */ |
830 | #ifndef YYINITDEPTH |
831 | # define YYINITDEPTH 200 |
832 | #endif |
833 | |
834 | /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only |
835 | if the built-in stack extension method is used). |
836 | |
837 | Do not make this value too large; the results are undefined if |
838 | YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH) |
839 | evaluated with infinite-precision integer arithmetic. */ |
840 | |
841 | #ifndef YYMAXDEPTH |
842 | # define YYMAXDEPTH 10000 |
843 | #endif |
844 | |
845 | |
846 | #if YYERROR_VERBOSE |
847 | |
848 | # ifndef yystrlen |
849 | # if defined __GLIBC__ && defined _STRING_H |
850 | # define yystrlen strlen |
851 | # else |
852 | /* Return the length of YYSTR. */ |
853 | static YYSIZE_T |
854 | yystrlen (const char *yystr) |
855 | { |
856 | YYSIZE_T yylen; |
857 | for (yylen = 0; yystr[yylen]; yylen++) |
858 | continue; |
859 | return yylen; |
860 | } |
861 | # endif |
862 | # endif |
863 | |
864 | # ifndef yystpcpy |
865 | # if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE |
866 | # define yystpcpy stpcpy |
867 | # else |
868 | /* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in |
869 | YYDEST. */ |
870 | static char * |
871 | yystpcpy (char *yydest, const char *yysrc) |
872 | { |
873 | char *yyd = yydest; |
874 | const char *yys = yysrc; |
875 | |
876 | while ((*yyd++ = *yys++) != '\0') |
877 | continue; |
878 | |
879 | return yyd - 1; |
880 | } |
881 | # endif |
882 | # endif |
883 | |
884 | # ifndef yytnamerr |
885 | /* Copy to YYRES the contents of YYSTR after stripping away unnecessary |
886 | quotes and backslashes, so that it's suitable for yyerror. The |
887 | heuristic is that double-quoting is unnecessary unless the string |
888 | contains an apostrophe, a comma, or backslash (other than |
889 | backslash-backslash). YYSTR is taken from yytname. If YYRES is |
890 | null, do not copy; instead, return the length of what the result |
891 | would have been. */ |
892 | static YYSIZE_T |
893 | yytnamerr (char *yyres, const char *yystr) |
894 | { |
895 | if (*yystr == '"') |
896 | { |
897 | YYSIZE_T yyn = 0; |
898 | char const *yyp = yystr; |
899 | |
900 | for (;;) |
901 | switch (*++yyp) |
902 | { |
903 | case '\'': |
904 | case ',': |
905 | goto do_not_strip_quotes; |
906 | |
907 | case '\\': |
908 | if (*++yyp != '\\') |
909 | goto do_not_strip_quotes; |
910 | /* Fall through. */ |
911 | default: |
912 | if (yyres) |
913 | yyres[yyn] = *yyp; |
914 | yyn++; |
915 | break; |
916 | |
917 | case '"': |
918 | if (yyres) |
919 | yyres[yyn] = '\0'; |
920 | return yyn; |
921 | } |
922 | do_not_strip_quotes: ; |
923 | } |
924 | |
925 | if (! yyres) |
926 | return yystrlen (yystr); |
927 | |
928 | return yystpcpy (yyres, yystr) - yyres; |
929 | } |
930 | # endif |
931 | |
932 | /* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message |
933 | about the unexpected token YYTOKEN for the state stack whose top is |
934 | YYSSP. |
935 | |
936 | Return 0 if *YYMSG was successfully written. Return 1 if *YYMSG is |
937 | not large enough to hold the message. In that case, also set |
938 | *YYMSG_ALLOC to the required number of bytes. Return 2 if the |
939 | required number of bytes is too large to store. */ |
940 | static int |
941 | yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg, |
942 | yytype_int16 *yyssp, int yytoken) |
943 | { |
944 | YYSIZE_T yysize0 = yytnamerr (YY_NULLPTR, yytname[yytoken]); |
945 | YYSIZE_T yysize = yysize0; |
946 | enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 }; |
947 | /* Internationalized format string. */ |
948 | const char *yyformat = YY_NULLPTR; |
949 | /* Arguments of yyformat. */ |
950 | char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM]; |
951 | /* Number of reported tokens (one for the "unexpected", one per |
952 | "expected"). */ |
953 | int yycount = 0; |
954 | |
955 | /* There are many possibilities here to consider: |
956 | - If this state is a consistent state with a default action, then |
957 | the only way this function was invoked is if the default action |
958 | is an error action. In that case, don't check for expected |
959 | tokens because there are none. |
960 | - The only way there can be no lookahead present (in yychar) is if |
961 | this state is a consistent state with a default action. Thus, |
962 | detecting the absence of a lookahead is sufficient to determine |
963 | that there is no unexpected or expected token to report. In that |
964 | case, just report a simple "syntax error". |
965 | - Don't assume there isn't a lookahead just because this state is a |
966 | consistent state with a default action. There might have been a |
967 | previous inconsistent state, consistent state with a non-default |
968 | action, or user semantic action that manipulated yychar. |
969 | - Of course, the expected token list depends on states to have |
970 | correct lookahead information, and it depends on the parser not |
971 | to perform extra reductions after fetching a lookahead from the |
972 | scanner and before detecting a syntax error. Thus, state merging |
973 | (from LALR or IELR) and default reductions corrupt the expected |
974 | token list. However, the list is correct for canonical LR with |
975 | one exception: it will still contain any token that will not be |
976 | accepted due to an error action in a later state. |
977 | */ |
978 | if (yytoken != YYEMPTY) |
979 | { |
980 | int yyn = yypact[*yyssp]; |
981 | yyarg[yycount++] = yytname[yytoken]; |
982 | if (!yypact_value_is_default (yyn)) |
983 | { |
984 | /* Start YYX at -YYN if negative to avoid negative indexes in |
985 | YYCHECK. In other words, skip the first -YYN actions for |
986 | this state because they are default actions. */ |
987 | int yyxbegin = yyn < 0 ? -yyn : 0; |
988 | /* Stay within bounds of both yycheck and yytname. */ |
989 | int yychecklim = YYLAST - yyn + 1; |
990 | int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS; |
991 | int yyx; |
992 | |
993 | for (yyx = yyxbegin; yyx < yyxend; ++yyx) |
994 | if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR |
995 | && !yytable_value_is_error (yytable[yyx + yyn])) |
996 | { |
997 | if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM) |
998 | { |
999 | yycount = 1; |
1000 | yysize = yysize0; |
1001 | break; |
1002 | } |
1003 | yyarg[yycount++] = yytname[yyx]; |
1004 | { |
1005 | YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULLPTR, yytname[yyx]); |
1006 | if (! (yysize <= yysize1 |
1007 | && yysize1 <= YYSTACK_ALLOC_MAXIMUM)) |
1008 | return 2; |
1009 | yysize = yysize1; |
1010 | } |
1011 | } |
1012 | } |
1013 | } |
1014 | |
1015 | switch (yycount) |
1016 | { |
1017 | # define YYCASE_(N, S) \ |
1018 | case N: \ |
1019 | yyformat = S; \ |
1020 | break |
1021 | YYCASE_(0, YY_("syntax error" )); |
1022 | YYCASE_(1, YY_("syntax error, unexpected %s" )); |
1023 | YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s" )); |
1024 | YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s" )); |
1025 | YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s" )); |
1026 | YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s" )); |
1027 | # undef YYCASE_ |
1028 | } |
1029 | |
1030 | { |
1031 | YYSIZE_T yysize1 = yysize + yystrlen (yyformat); |
1032 | if (! (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM)) |
1033 | return 2; |
1034 | yysize = yysize1; |
1035 | } |
1036 | |
1037 | if (*yymsg_alloc < yysize) |
1038 | { |
1039 | *yymsg_alloc = 2 * yysize; |
1040 | if (! (yysize <= *yymsg_alloc |
1041 | && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM)) |
1042 | *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM; |
1043 | return 1; |
1044 | } |
1045 | |
1046 | /* Avoid sprintf, as that infringes on the user's name space. |
1047 | Don't have undefined behavior even if the translation |
1048 | produced a string with the wrong number of "%s"s. */ |
1049 | { |
1050 | char *yyp = *yymsg; |
1051 | int yyi = 0; |
1052 | while ((*yyp = *yyformat) != '\0') |
1053 | if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount) |
1054 | { |
1055 | yyp += yytnamerr (yyp, yyarg[yyi++]); |
1056 | yyformat += 2; |
1057 | } |
1058 | else |
1059 | { |
1060 | yyp++; |
1061 | yyformat++; |
1062 | } |
1063 | } |
1064 | return 0; |
1065 | } |
1066 | #endif /* YYERROR_VERBOSE */ |
1067 | |
1068 | /*-----------------------------------------------. |
1069 | | Release the memory associated to this symbol. | |
1070 | `-----------------------------------------------*/ |
1071 | |
1072 | static void |
1073 | yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep) |
1074 | { |
1075 | YYUSE (yyvaluep); |
1076 | if (!yymsg) |
1077 | yymsg = "Deleting" ; |
1078 | YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp); |
1079 | |
1080 | YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN |
1081 | YYUSE (yytype); |
1082 | YY_IGNORE_MAYBE_UNINITIALIZED_END |
1083 | } |
1084 | |
1085 | |
1086 | |
1087 | |
1088 | /* The lookahead symbol. */ |
1089 | int yychar; |
1090 | |
1091 | /* The semantic value of the lookahead symbol. */ |
1092 | YYSTYPE yylval; |
1093 | /* Number of syntax errors so far. */ |
1094 | int yynerrs; |
1095 | |
1096 | |
1097 | /*----------. |
1098 | | yyparse. | |
1099 | `----------*/ |
1100 | |
1101 | int |
1102 | yyparse (void) |
1103 | { |
1104 | int yystate; |
1105 | /* Number of tokens to shift before error messages enabled. */ |
1106 | int yyerrstatus; |
1107 | |
1108 | /* The stacks and their tools: |
1109 | 'yyss': related to states. |
1110 | 'yyvs': related to semantic values. |
1111 | |
1112 | Refer to the stacks through separate pointers, to allow yyoverflow |
1113 | to reallocate them elsewhere. */ |
1114 | |
1115 | /* The state stack. */ |
1116 | yytype_int16 yyssa[YYINITDEPTH]; |
1117 | yytype_int16 *yyss; |
1118 | yytype_int16 *yyssp; |
1119 | |
1120 | /* The semantic value stack. */ |
1121 | YYSTYPE yyvsa[YYINITDEPTH]; |
1122 | YYSTYPE *yyvs; |
1123 | YYSTYPE *yyvsp; |
1124 | |
1125 | YYSIZE_T yystacksize; |
1126 | |
1127 | int yyn; |
1128 | int yyresult; |
1129 | /* Lookahead token as an internal (translated) token number. */ |
1130 | int yytoken = 0; |
1131 | /* The variables used to return semantic value and location from the |
1132 | action routines. */ |
1133 | YYSTYPE yyval; |
1134 | |
1135 | #if YYERROR_VERBOSE |
1136 | /* Buffer for error messages, and its allocated size. */ |
1137 | char yymsgbuf[128]; |
1138 | char *yymsg = yymsgbuf; |
1139 | YYSIZE_T yymsg_alloc = sizeof yymsgbuf; |
1140 | #endif |
1141 | |
1142 | #define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N)) |
1143 | |
1144 | /* The number of symbols on the RHS of the reduced rule. |
1145 | Keep to zero when no symbol should be popped. */ |
1146 | int yylen = 0; |
1147 | |
1148 | yyssp = yyss = yyssa; |
1149 | yyvsp = yyvs = yyvsa; |
1150 | yystacksize = YYINITDEPTH; |
1151 | |
1152 | YYDPRINTF ((stderr, "Starting parse\n" )); |
1153 | |
1154 | yystate = 0; |
1155 | yyerrstatus = 0; |
1156 | yynerrs = 0; |
1157 | yychar = YYEMPTY; /* Cause a token to be read. */ |
1158 | goto yysetstate; |
1159 | |
1160 | /*------------------------------------------------------------. |
1161 | | yynewstate -- Push a new state, which is found in yystate. | |
1162 | `------------------------------------------------------------*/ |
1163 | yynewstate: |
1164 | /* In all cases, when you get here, the value and location stacks |
1165 | have just been pushed. So pushing a state here evens the stacks. */ |
1166 | yyssp++; |
1167 | |
1168 | yysetstate: |
1169 | *yyssp = yystate; |
1170 | |
1171 | if (yyss + yystacksize - 1 <= yyssp) |
1172 | { |
1173 | /* Get the current used size of the three stacks, in elements. */ |
1174 | YYSIZE_T yysize = yyssp - yyss + 1; |
1175 | |
1176 | #ifdef yyoverflow |
1177 | { |
1178 | /* Give user a chance to reallocate the stack. Use copies of |
1179 | these so that the &'s don't force the real ones into |
1180 | memory. */ |
1181 | YYSTYPE *yyvs1 = yyvs; |
1182 | yytype_int16 *yyss1 = yyss; |
1183 | |
1184 | /* Each stack pointer address is followed by the size of the |
1185 | data in use in that stack, in bytes. This used to be a |
1186 | conditional around just the two extra args, but that might |
1187 | be undefined if yyoverflow is a macro. */ |
1188 | yyoverflow (YY_("memory exhausted" ), |
1189 | &yyss1, yysize * sizeof (*yyssp), |
1190 | &yyvs1, yysize * sizeof (*yyvsp), |
1191 | &yystacksize); |
1192 | |
1193 | yyss = yyss1; |
1194 | yyvs = yyvs1; |
1195 | } |
1196 | #else /* no yyoverflow */ |
1197 | # ifndef YYSTACK_RELOCATE |
1198 | goto yyexhaustedlab; |
1199 | # else |
1200 | /* Extend the stack our own way. */ |
1201 | if (YYMAXDEPTH <= yystacksize) |
1202 | goto yyexhaustedlab; |
1203 | yystacksize *= 2; |
1204 | if (YYMAXDEPTH < yystacksize) |
1205 | yystacksize = YYMAXDEPTH; |
1206 | |
1207 | { |
1208 | yytype_int16 *yyss1 = yyss; |
1209 | union yyalloc *yyptr = |
1210 | (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize)); |
1211 | if (! yyptr) |
1212 | goto yyexhaustedlab; |
1213 | YYSTACK_RELOCATE (yyss_alloc, yyss); |
1214 | YYSTACK_RELOCATE (yyvs_alloc, yyvs); |
1215 | # undef YYSTACK_RELOCATE |
1216 | if (yyss1 != yyssa) |
1217 | YYSTACK_FREE (yyss1); |
1218 | } |
1219 | # endif |
1220 | #endif /* no yyoverflow */ |
1221 | |
1222 | yyssp = yyss + yysize - 1; |
1223 | yyvsp = yyvs + yysize - 1; |
1224 | |
1225 | YYDPRINTF ((stderr, "Stack size increased to %lu\n" , |
1226 | (unsigned long int) yystacksize)); |
1227 | |
1228 | if (yyss + yystacksize - 1 <= yyssp) |
1229 | YYABORT; |
1230 | } |
1231 | |
1232 | YYDPRINTF ((stderr, "Entering state %d\n" , yystate)); |
1233 | |
1234 | if (yystate == YYFINAL) |
1235 | YYACCEPT; |
1236 | |
1237 | goto yybackup; |
1238 | |
1239 | /*-----------. |
1240 | | yybackup. | |
1241 | `-----------*/ |
1242 | yybackup: |
1243 | |
1244 | /* Do appropriate processing given the current state. Read a |
1245 | lookahead token if we need one and don't already have one. */ |
1246 | |
1247 | /* First try to decide what to do without reference to lookahead token. */ |
1248 | yyn = yypact[yystate]; |
1249 | if (yypact_value_is_default (yyn)) |
1250 | goto yydefault; |
1251 | |
1252 | /* Not known => get a lookahead token if don't already have one. */ |
1253 | |
1254 | /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */ |
1255 | if (yychar == YYEMPTY) |
1256 | { |
1257 | YYDPRINTF ((stderr, "Reading a token: " )); |
1258 | yychar = yylex (); |
1259 | } |
1260 | |
1261 | if (yychar <= YYEOF) |
1262 | { |
1263 | yychar = yytoken = YYEOF; |
1264 | YYDPRINTF ((stderr, "Now at end of input.\n" )); |
1265 | } |
1266 | else |
1267 | { |
1268 | yytoken = YYTRANSLATE (yychar); |
1269 | YY_SYMBOL_PRINT ("Next token is" , yytoken, &yylval, &yylloc); |
1270 | } |
1271 | |
1272 | /* If the proper action on seeing token YYTOKEN is to reduce or to |
1273 | detect an error, take that action. */ |
1274 | yyn += yytoken; |
1275 | if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken) |
1276 | goto yydefault; |
1277 | yyn = yytable[yyn]; |
1278 | if (yyn <= 0) |
1279 | { |
1280 | if (yytable_value_is_error (yyn)) |
1281 | goto yyerrlab; |
1282 | yyn = -yyn; |
1283 | goto yyreduce; |
1284 | } |
1285 | |
1286 | /* Count tokens shifted since error; after three, turn off error |
1287 | status. */ |
1288 | if (yyerrstatus) |
1289 | yyerrstatus--; |
1290 | |
1291 | /* Shift the lookahead token. */ |
1292 | YY_SYMBOL_PRINT ("Shifting" , yytoken, &yylval, &yylloc); |
1293 | |
1294 | /* Discard the shifted token. */ |
1295 | yychar = YYEMPTY; |
1296 | |
1297 | yystate = yyn; |
1298 | YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN |
1299 | *++yyvsp = yylval; |
1300 | YY_IGNORE_MAYBE_UNINITIALIZED_END |
1301 | |
1302 | goto yynewstate; |
1303 | |
1304 | |
1305 | /*-----------------------------------------------------------. |
1306 | | yydefault -- do the default action for the current state. | |
1307 | `-----------------------------------------------------------*/ |
1308 | yydefault: |
1309 | yyn = yydefact[yystate]; |
1310 | if (yyn == 0) |
1311 | goto yyerrlab; |
1312 | goto yyreduce; |
1313 | |
1314 | |
1315 | /*-----------------------------. |
1316 | | yyreduce -- Do a reduction. | |
1317 | `-----------------------------*/ |
1318 | yyreduce: |
1319 | /* yyn is the number of a rule to reduce with. */ |
1320 | yylen = yyr2[yyn]; |
1321 | |
1322 | /* If YYLEN is nonzero, implement the default value of the action: |
1323 | '$$ = $1'. |
1324 | |
1325 | Otherwise, the following line sets YYVAL to garbage. |
1326 | This behavior is undocumented and Bison |
1327 | users should not rely upon it. Assigning to YYVAL |
1328 | unconditionally makes the parser a bit smaller, and it avoids a |
1329 | GCC warning that YYVAL may be used uninitialized. */ |
1330 | yyval = yyvsp[1-yylen]; |
1331 | |
1332 | |
1333 | YY_REDUCE_PRINT (yyn); |
1334 | switch (yyn) |
1335 | { |
1336 | case 3: |
1337 | #line 119 "cmd-parse.y" /* yacc.c:1646 */ |
1338 | { |
1339 | struct cmd_parse_state *ps = &parse_state; |
1340 | |
1341 | ps->commands = (yyvsp[0].commands); |
1342 | } |
1343 | #line 1344 "cmd-parse.c" /* yacc.c:1646 */ |
1344 | break; |
1345 | |
1346 | case 4: |
1347 | #line 126 "cmd-parse.y" /* yacc.c:1646 */ |
1348 | { |
1349 | (yyval.commands) = (yyvsp[-1].commands); |
1350 | } |
1351 | #line 1352 "cmd-parse.c" /* yacc.c:1646 */ |
1352 | break; |
1353 | |
1354 | case 5: |
1355 | #line 130 "cmd-parse.y" /* yacc.c:1646 */ |
1356 | { |
1357 | (yyval.commands) = (yyvsp[-2].commands); |
1358 | TAILQ_CONCAT((yyval.commands), (yyvsp[-1].commands), entry); |
1359 | free((yyvsp[-1].commands)); |
1360 | } |
1361 | #line 1362 "cmd-parse.c" /* yacc.c:1646 */ |
1362 | break; |
1363 | |
1364 | case 6: |
1365 | #line 137 "cmd-parse.y" /* yacc.c:1646 */ |
1366 | { |
1367 | (yyval.commands) = xmalloc (sizeof *(yyval.commands)); |
1368 | TAILQ_INIT((yyval.commands)); |
1369 | } |
1370 | #line 1371 "cmd-parse.c" /* yacc.c:1646 */ |
1371 | break; |
1372 | |
1373 | case 7: |
1374 | #line 142 "cmd-parse.y" /* yacc.c:1646 */ |
1375 | { |
1376 | struct cmd_parse_state *ps = &parse_state; |
1377 | |
1378 | if (ps->scope == NULL || ps->scope->flag) |
1379 | (yyval.commands) = (yyvsp[0].commands); |
1380 | else { |
1381 | (yyval.commands) = cmd_parse_new_commands(); |
1382 | cmd_parse_free_commands((yyvsp[0].commands)); |
1383 | } |
1384 | } |
1385 | #line 1386 "cmd-parse.c" /* yacc.c:1646 */ |
1386 | break; |
1387 | |
1388 | case 8: |
1389 | #line 153 "cmd-parse.y" /* yacc.c:1646 */ |
1390 | { |
1391 | struct cmd_parse_state *ps = &parse_state; |
1392 | |
1393 | if (ps->scope == NULL || ps->scope->flag) |
1394 | (yyval.commands) = (yyvsp[0].commands); |
1395 | else { |
1396 | (yyval.commands) = cmd_parse_new_commands(); |
1397 | cmd_parse_free_commands((yyvsp[0].commands)); |
1398 | } |
1399 | } |
1400 | #line 1401 "cmd-parse.c" /* yacc.c:1646 */ |
1401 | break; |
1402 | |
1403 | case 9: |
1404 | #line 165 "cmd-parse.y" /* yacc.c:1646 */ |
1405 | { |
1406 | (yyval.token) = (yyvsp[0].token); |
1407 | } |
1408 | #line 1409 "cmd-parse.c" /* yacc.c:1646 */ |
1409 | break; |
1410 | |
1411 | case 10: |
1412 | #line 169 "cmd-parse.y" /* yacc.c:1646 */ |
1413 | { |
1414 | (yyval.token) = (yyvsp[0].token); |
1415 | } |
1416 | #line 1417 "cmd-parse.c" /* yacc.c:1646 */ |
1417 | break; |
1418 | |
1419 | case 11: |
1420 | #line 174 "cmd-parse.y" /* yacc.c:1646 */ |
1421 | { |
1422 | struct cmd_parse_state *ps = &parse_state; |
1423 | struct cmd_parse_input *pi = ps->input; |
1424 | struct format_tree *ft; |
1425 | struct client *c = pi->c; |
1426 | struct cmd_find_state *fsp; |
1427 | struct cmd_find_state fs; |
1428 | int flags = FORMAT_NOJOBS; |
1429 | |
1430 | if (cmd_find_valid_state(&pi->fs)) |
1431 | fsp = &pi->fs; |
1432 | else { |
1433 | cmd_find_from_client(&fs, c, 0); |
1434 | fsp = &fs; |
1435 | } |
1436 | ft = format_create(NULL, pi->item, FORMAT_NONE, flags); |
1437 | format_defaults(ft, c, fsp->s, fsp->wl, fsp->wp); |
1438 | |
1439 | (yyval.token) = format_expand(ft, (yyvsp[0].token)); |
1440 | format_free(ft); |
1441 | free((yyvsp[0].token)); |
1442 | } |
1443 | #line 1444 "cmd-parse.c" /* yacc.c:1646 */ |
1444 | break; |
1445 | |
1446 | case 14: |
1447 | #line 201 "cmd-parse.y" /* yacc.c:1646 */ |
1448 | { |
1449 | struct cmd_parse_state *ps = &parse_state; |
1450 | int flags = ps->input->flags; |
1451 | |
1452 | if ((~flags & CMD_PARSE_PARSEONLY) && |
1453 | (ps->scope == NULL || ps->scope->flag)) |
1454 | environ_put(global_environ, (yyvsp[0].token)); |
1455 | free((yyvsp[0].token)); |
1456 | } |
1457 | #line 1458 "cmd-parse.c" /* yacc.c:1646 */ |
1458 | break; |
1459 | |
1460 | case 15: |
1461 | #line 212 "cmd-parse.y" /* yacc.c:1646 */ |
1462 | { |
1463 | struct cmd_parse_state *ps = &parse_state; |
1464 | struct cmd_parse_scope *scope; |
1465 | |
1466 | scope = xmalloc(sizeof *scope); |
1467 | (yyval.flag) = scope->flag = format_true((yyvsp[0].token)); |
1468 | free((yyvsp[0].token)); |
1469 | |
1470 | if (ps->scope != NULL) |
1471 | TAILQ_INSERT_HEAD(&ps->stack, ps->scope, entry); |
1472 | ps->scope = scope; |
1473 | } |
1474 | #line 1475 "cmd-parse.c" /* yacc.c:1646 */ |
1475 | break; |
1476 | |
1477 | case 16: |
1478 | #line 226 "cmd-parse.y" /* yacc.c:1646 */ |
1479 | { |
1480 | struct cmd_parse_state *ps = &parse_state; |
1481 | struct cmd_parse_scope *scope; |
1482 | |
1483 | scope = xmalloc(sizeof *scope); |
1484 | scope->flag = !ps->scope->flag; |
1485 | |
1486 | free(ps->scope); |
1487 | ps->scope = scope; |
1488 | } |
1489 | #line 1490 "cmd-parse.c" /* yacc.c:1646 */ |
1490 | break; |
1491 | |
1492 | case 17: |
1493 | #line 238 "cmd-parse.y" /* yacc.c:1646 */ |
1494 | { |
1495 | struct cmd_parse_state *ps = &parse_state; |
1496 | struct cmd_parse_scope *scope; |
1497 | |
1498 | scope = xmalloc(sizeof *scope); |
1499 | (yyval.flag) = scope->flag = format_true((yyvsp[0].token)); |
1500 | free((yyvsp[0].token)); |
1501 | |
1502 | free(ps->scope); |
1503 | ps->scope = scope; |
1504 | } |
1505 | #line 1506 "cmd-parse.c" /* yacc.c:1646 */ |
1506 | break; |
1507 | |
1508 | case 18: |
1509 | #line 251 "cmd-parse.y" /* yacc.c:1646 */ |
1510 | { |
1511 | struct cmd_parse_state *ps = &parse_state; |
1512 | |
1513 | free(ps->scope); |
1514 | ps->scope = TAILQ_FIRST(&ps->stack); |
1515 | if (ps->scope != NULL) |
1516 | TAILQ_REMOVE(&ps->stack, ps->scope, entry); |
1517 | } |
1518 | #line 1519 "cmd-parse.c" /* yacc.c:1646 */ |
1519 | break; |
1520 | |
1521 | case 19: |
1522 | #line 261 "cmd-parse.y" /* yacc.c:1646 */ |
1523 | { |
1524 | if ((yyvsp[-3].flag)) |
1525 | (yyval.commands) = (yyvsp[-1].commands); |
1526 | else { |
1527 | (yyval.commands) = cmd_parse_new_commands(); |
1528 | cmd_parse_free_commands((yyvsp[-1].commands)); |
1529 | } |
1530 | } |
1531 | #line 1532 "cmd-parse.c" /* yacc.c:1646 */ |
1532 | break; |
1533 | |
1534 | case 20: |
1535 | #line 270 "cmd-parse.y" /* yacc.c:1646 */ |
1536 | { |
1537 | if ((yyvsp[-6].flag)) { |
1538 | (yyval.commands) = (yyvsp[-4].commands); |
1539 | cmd_parse_free_commands((yyvsp[-1].commands)); |
1540 | } else { |
1541 | (yyval.commands) = (yyvsp[-1].commands); |
1542 | cmd_parse_free_commands((yyvsp[-4].commands)); |
1543 | } |
1544 | } |
1545 | #line 1546 "cmd-parse.c" /* yacc.c:1646 */ |
1546 | break; |
1547 | |
1548 | case 21: |
1549 | #line 280 "cmd-parse.y" /* yacc.c:1646 */ |
1550 | { |
1551 | if ((yyvsp[-4].flag)) { |
1552 | (yyval.commands) = (yyvsp[-2].commands); |
1553 | cmd_parse_free_commands((yyvsp[-1].elif).commands); |
1554 | } else if ((yyvsp[-1].elif).flag) { |
1555 | (yyval.commands) = (yyvsp[-1].elif).commands; |
1556 | cmd_parse_free_commands((yyvsp[-2].commands)); |
1557 | } else { |
1558 | (yyval.commands) = cmd_parse_new_commands(); |
1559 | cmd_parse_free_commands((yyvsp[-2].commands)); |
1560 | cmd_parse_free_commands((yyvsp[-1].elif).commands); |
1561 | } |
1562 | } |
1563 | #line 1564 "cmd-parse.c" /* yacc.c:1646 */ |
1564 | break; |
1565 | |
1566 | case 22: |
1567 | #line 294 "cmd-parse.y" /* yacc.c:1646 */ |
1568 | { |
1569 | if ((yyvsp[-7].flag)) { |
1570 | (yyval.commands) = (yyvsp[-5].commands); |
1571 | cmd_parse_free_commands((yyvsp[-4].elif).commands); |
1572 | cmd_parse_free_commands((yyvsp[-1].commands)); |
1573 | } else if ((yyvsp[-4].elif).flag) { |
1574 | (yyval.commands) = (yyvsp[-4].elif).commands; |
1575 | cmd_parse_free_commands((yyvsp[-5].commands)); |
1576 | cmd_parse_free_commands((yyvsp[-1].commands)); |
1577 | } else { |
1578 | (yyval.commands) = (yyvsp[-1].commands); |
1579 | cmd_parse_free_commands((yyvsp[-5].commands)); |
1580 | cmd_parse_free_commands((yyvsp[-4].elif).commands); |
1581 | } |
1582 | } |
1583 | #line 1584 "cmd-parse.c" /* yacc.c:1646 */ |
1584 | break; |
1585 | |
1586 | case 23: |
1587 | #line 311 "cmd-parse.y" /* yacc.c:1646 */ |
1588 | { |
1589 | if ((yyvsp[-2].flag)) { |
1590 | (yyval.elif).flag = 1; |
1591 | (yyval.elif).commands = (yyvsp[0].commands); |
1592 | } else { |
1593 | (yyval.elif).flag = 0; |
1594 | (yyval.elif).commands = cmd_parse_new_commands(); |
1595 | cmd_parse_free_commands((yyvsp[0].commands)); |
1596 | } |
1597 | } |
1598 | #line 1599 "cmd-parse.c" /* yacc.c:1646 */ |
1599 | break; |
1600 | |
1601 | case 24: |
1602 | #line 322 "cmd-parse.y" /* yacc.c:1646 */ |
1603 | { |
1604 | if ((yyvsp[-3].flag)) { |
1605 | (yyval.elif).flag = 1; |
1606 | (yyval.elif).commands = (yyvsp[-1].commands); |
1607 | cmd_parse_free_commands((yyvsp[0].elif).commands); |
1608 | } else if ((yyvsp[0].elif).flag) { |
1609 | (yyval.elif).flag = 1; |
1610 | (yyval.elif).commands = (yyvsp[0].elif).commands; |
1611 | cmd_parse_free_commands((yyvsp[-1].commands)); |
1612 | } else { |
1613 | (yyval.elif).flag = 0; |
1614 | (yyval.elif).commands = cmd_parse_new_commands(); |
1615 | cmd_parse_free_commands((yyvsp[-1].commands)); |
1616 | cmd_parse_free_commands((yyvsp[0].elif).commands); |
1617 | } |
1618 | } |
1619 | #line 1620 "cmd-parse.c" /* yacc.c:1646 */ |
1620 | break; |
1621 | |
1622 | case 25: |
1623 | #line 340 "cmd-parse.y" /* yacc.c:1646 */ |
1624 | { |
1625 | struct cmd_parse_state *ps = &parse_state; |
1626 | |
1627 | (yyval.commands) = cmd_parse_new_commands(); |
1628 | if ((yyvsp[0].command)->name != NULL && |
1629 | (ps->scope == NULL || ps->scope->flag)) |
1630 | TAILQ_INSERT_TAIL((yyval.commands), (yyvsp[0].command), entry); |
1631 | else |
1632 | cmd_parse_free_command((yyvsp[0].command)); |
1633 | } |
1634 | #line 1635 "cmd-parse.c" /* yacc.c:1646 */ |
1635 | break; |
1636 | |
1637 | case 26: |
1638 | #line 351 "cmd-parse.y" /* yacc.c:1646 */ |
1639 | { |
1640 | (yyval.commands) = (yyvsp[-1].commands); |
1641 | } |
1642 | #line 1643 "cmd-parse.c" /* yacc.c:1646 */ |
1643 | break; |
1644 | |
1645 | case 27: |
1646 | #line 355 "cmd-parse.y" /* yacc.c:1646 */ |
1647 | { |
1648 | (yyval.commands) = (yyvsp[-2].commands); |
1649 | TAILQ_CONCAT((yyval.commands), (yyvsp[0].commands), entry); |
1650 | free((yyvsp[0].commands)); |
1651 | } |
1652 | #line 1653 "cmd-parse.c" /* yacc.c:1646 */ |
1653 | break; |
1654 | |
1655 | case 28: |
1656 | #line 361 "cmd-parse.y" /* yacc.c:1646 */ |
1657 | { |
1658 | struct cmd_parse_state *ps = &parse_state; |
1659 | |
1660 | if ((yyvsp[0].command)->name != NULL && |
1661 | (ps->scope == NULL || ps->scope->flag)) { |
1662 | (yyval.commands) = (yyvsp[-2].commands); |
1663 | TAILQ_INSERT_TAIL((yyval.commands), (yyvsp[0].command), entry); |
1664 | } else { |
1665 | (yyval.commands) = cmd_parse_new_commands(); |
1666 | cmd_parse_free_commands((yyvsp[-2].commands)); |
1667 | cmd_parse_free_command((yyvsp[0].command)); |
1668 | } |
1669 | } |
1670 | #line 1671 "cmd-parse.c" /* yacc.c:1646 */ |
1671 | break; |
1672 | |
1673 | case 29: |
1674 | #line 375 "cmd-parse.y" /* yacc.c:1646 */ |
1675 | { |
1676 | (yyval.commands) = (yyvsp[0].commands); |
1677 | } |
1678 | #line 1679 "cmd-parse.c" /* yacc.c:1646 */ |
1679 | break; |
1680 | |
1681 | case 30: |
1682 | #line 380 "cmd-parse.y" /* yacc.c:1646 */ |
1683 | { |
1684 | struct cmd_parse_state *ps = &parse_state; |
1685 | |
1686 | (yyval.command) = xcalloc(1, sizeof *(yyval.command)); |
1687 | (yyval.command)->name = NULL; |
1688 | (yyval.command)->line = ps->input->line; |
1689 | } |
1690 | #line 1691 "cmd-parse.c" /* yacc.c:1646 */ |
1691 | break; |
1692 | |
1693 | case 31: |
1694 | #line 388 "cmd-parse.y" /* yacc.c:1646 */ |
1695 | { |
1696 | struct cmd_parse_state *ps = &parse_state; |
1697 | |
1698 | (yyval.command) = xcalloc(1, sizeof *(yyval.command)); |
1699 | (yyval.command)->name = (yyvsp[0].token); |
1700 | (yyval.command)->line = ps->input->line; |
1701 | |
1702 | } |
1703 | #line 1704 "cmd-parse.c" /* yacc.c:1646 */ |
1704 | break; |
1705 | |
1706 | case 32: |
1707 | #line 397 "cmd-parse.y" /* yacc.c:1646 */ |
1708 | { |
1709 | struct cmd_parse_state *ps = &parse_state; |
1710 | |
1711 | (yyval.command) = xcalloc(1, sizeof *(yyval.command)); |
1712 | (yyval.command)->name = (yyvsp[-1].token); |
1713 | (yyval.command)->line = ps->input->line; |
1714 | |
1715 | (yyval.command)->argc = (yyvsp[0].arguments).argc; |
1716 | (yyval.command)->argv = (yyvsp[0].arguments).argv; |
1717 | } |
1718 | #line 1719 "cmd-parse.c" /* yacc.c:1646 */ |
1719 | break; |
1720 | |
1721 | case 33: |
1722 | #line 409 "cmd-parse.y" /* yacc.c:1646 */ |
1723 | { |
1724 | if ((yyvsp[-2].flag)) |
1725 | (yyval.commands) = (yyvsp[-1].commands); |
1726 | else { |
1727 | (yyval.commands) = cmd_parse_new_commands(); |
1728 | cmd_parse_free_commands((yyvsp[-1].commands)); |
1729 | } |
1730 | } |
1731 | #line 1732 "cmd-parse.c" /* yacc.c:1646 */ |
1732 | break; |
1733 | |
1734 | case 34: |
1735 | #line 418 "cmd-parse.y" /* yacc.c:1646 */ |
1736 | { |
1737 | if ((yyvsp[-4].flag)) { |
1738 | (yyval.commands) = (yyvsp[-3].commands); |
1739 | cmd_parse_free_commands((yyvsp[-1].commands)); |
1740 | } else { |
1741 | (yyval.commands) = (yyvsp[-1].commands); |
1742 | cmd_parse_free_commands((yyvsp[-3].commands)); |
1743 | } |
1744 | } |
1745 | #line 1746 "cmd-parse.c" /* yacc.c:1646 */ |
1746 | break; |
1747 | |
1748 | case 35: |
1749 | #line 428 "cmd-parse.y" /* yacc.c:1646 */ |
1750 | { |
1751 | if ((yyvsp[-3].flag)) { |
1752 | (yyval.commands) = (yyvsp[-2].commands); |
1753 | cmd_parse_free_commands((yyvsp[-1].elif).commands); |
1754 | } else if ((yyvsp[-1].elif).flag) { |
1755 | (yyval.commands) = (yyvsp[-1].elif).commands; |
1756 | cmd_parse_free_commands((yyvsp[-2].commands)); |
1757 | } else { |
1758 | (yyval.commands) = cmd_parse_new_commands(); |
1759 | cmd_parse_free_commands((yyvsp[-2].commands)); |
1760 | cmd_parse_free_commands((yyvsp[-1].elif).commands); |
1761 | } |
1762 | } |
1763 | #line 1764 "cmd-parse.c" /* yacc.c:1646 */ |
1764 | break; |
1765 | |
1766 | case 36: |
1767 | #line 442 "cmd-parse.y" /* yacc.c:1646 */ |
1768 | { |
1769 | if ((yyvsp[-5].flag)) { |
1770 | (yyval.commands) = (yyvsp[-4].commands); |
1771 | cmd_parse_free_commands((yyvsp[-3].elif).commands); |
1772 | cmd_parse_free_commands((yyvsp[-1].commands)); |
1773 | } else if ((yyvsp[-3].elif).flag) { |
1774 | (yyval.commands) = (yyvsp[-3].elif).commands; |
1775 | cmd_parse_free_commands((yyvsp[-4].commands)); |
1776 | cmd_parse_free_commands((yyvsp[-1].commands)); |
1777 | } else { |
1778 | (yyval.commands) = (yyvsp[-1].commands); |
1779 | cmd_parse_free_commands((yyvsp[-4].commands)); |
1780 | cmd_parse_free_commands((yyvsp[-3].elif).commands); |
1781 | } |
1782 | } |
1783 | #line 1784 "cmd-parse.c" /* yacc.c:1646 */ |
1784 | break; |
1785 | |
1786 | case 37: |
1787 | #line 459 "cmd-parse.y" /* yacc.c:1646 */ |
1788 | { |
1789 | if ((yyvsp[-1].flag)) { |
1790 | (yyval.elif).flag = 1; |
1791 | (yyval.elif).commands = (yyvsp[0].commands); |
1792 | } else { |
1793 | (yyval.elif).flag = 0; |
1794 | (yyval.elif).commands = cmd_parse_new_commands(); |
1795 | cmd_parse_free_commands((yyvsp[0].commands)); |
1796 | } |
1797 | } |
1798 | #line 1799 "cmd-parse.c" /* yacc.c:1646 */ |
1799 | break; |
1800 | |
1801 | case 38: |
1802 | #line 470 "cmd-parse.y" /* yacc.c:1646 */ |
1803 | { |
1804 | if ((yyvsp[-2].flag)) { |
1805 | (yyval.elif).flag = 1; |
1806 | (yyval.elif).commands = (yyvsp[-1].commands); |
1807 | cmd_parse_free_commands((yyvsp[0].elif).commands); |
1808 | } else if ((yyvsp[0].elif).flag) { |
1809 | (yyval.elif).flag = 1; |
1810 | (yyval.elif).commands = (yyvsp[0].elif).commands; |
1811 | cmd_parse_free_commands((yyvsp[-1].commands)); |
1812 | } else { |
1813 | (yyval.elif).flag = 0; |
1814 | (yyval.elif).commands = cmd_parse_new_commands(); |
1815 | cmd_parse_free_commands((yyvsp[-1].commands)); |
1816 | cmd_parse_free_commands((yyvsp[0].elif).commands); |
1817 | } |
1818 | } |
1819 | #line 1820 "cmd-parse.c" /* yacc.c:1646 */ |
1820 | break; |
1821 | |
1822 | case 39: |
1823 | #line 488 "cmd-parse.y" /* yacc.c:1646 */ |
1824 | { |
1825 | (yyval.arguments).argc = 1; |
1826 | (yyval.arguments).argv = xreallocarray(NULL, 1, sizeof *(yyval.arguments).argv); |
1827 | |
1828 | (yyval.arguments).argv[0] = (yyvsp[0].token); |
1829 | } |
1830 | #line 1831 "cmd-parse.c" /* yacc.c:1646 */ |
1831 | break; |
1832 | |
1833 | case 40: |
1834 | #line 495 "cmd-parse.y" /* yacc.c:1646 */ |
1835 | { |
1836 | cmd_prepend_argv(&(yyvsp[0].arguments).argc, &(yyvsp[0].arguments).argv, (yyvsp[-1].token)); |
1837 | free((yyvsp[-1].token)); |
1838 | (yyval.arguments) = (yyvsp[0].arguments); |
1839 | } |
1840 | #line 1841 "cmd-parse.c" /* yacc.c:1646 */ |
1841 | break; |
1842 | |
1843 | case 41: |
1844 | #line 502 "cmd-parse.y" /* yacc.c:1646 */ |
1845 | { |
1846 | (yyval.token) = (yyvsp[0].token); |
1847 | } |
1848 | #line 1849 "cmd-parse.c" /* yacc.c:1646 */ |
1849 | break; |
1850 | |
1851 | case 42: |
1852 | #line 506 "cmd-parse.y" /* yacc.c:1646 */ |
1853 | { |
1854 | (yyval.token) = (yyvsp[0].token); |
1855 | } |
1856 | #line 1857 "cmd-parse.c" /* yacc.c:1646 */ |
1857 | break; |
1858 | |
1859 | |
1860 | #line 1861 "cmd-parse.c" /* yacc.c:1646 */ |
1861 | default: break; |
1862 | } |
1863 | /* User semantic actions sometimes alter yychar, and that requires |
1864 | that yytoken be updated with the new translation. We take the |
1865 | approach of translating immediately before every use of yytoken. |
1866 | One alternative is translating here after every semantic action, |
1867 | but that translation would be missed if the semantic action invokes |
1868 | YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or |
1869 | if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an |
1870 | incorrect destructor might then be invoked immediately. In the |
1871 | case of YYERROR or YYBACKUP, subsequent parser actions might lead |
1872 | to an incorrect destructor call or verbose syntax error message |
1873 | before the lookahead is translated. */ |
1874 | YY_SYMBOL_PRINT ("-> $$ =" , yyr1[yyn], &yyval, &yyloc); |
1875 | |
1876 | YYPOPSTACK (yylen); |
1877 | yylen = 0; |
1878 | YY_STACK_PRINT (yyss, yyssp); |
1879 | |
1880 | *++yyvsp = yyval; |
1881 | |
1882 | /* Now 'shift' the result of the reduction. Determine what state |
1883 | that goes to, based on the state we popped back to and the rule |
1884 | number reduced by. */ |
1885 | |
1886 | yyn = yyr1[yyn]; |
1887 | |
1888 | yystate = yypgoto[yyn - YYNTOKENS] + *yyssp; |
1889 | if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp) |
1890 | yystate = yytable[yystate]; |
1891 | else |
1892 | yystate = yydefgoto[yyn - YYNTOKENS]; |
1893 | |
1894 | goto yynewstate; |
1895 | |
1896 | |
1897 | /*--------------------------------------. |
1898 | | yyerrlab -- here on detecting error. | |
1899 | `--------------------------------------*/ |
1900 | yyerrlab: |
1901 | /* Make sure we have latest lookahead translation. See comments at |
1902 | user semantic actions for why this is necessary. */ |
1903 | yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar); |
1904 | |
1905 | /* If not already recovering from an error, report this error. */ |
1906 | if (!yyerrstatus) |
1907 | { |
1908 | ++yynerrs; |
1909 | #if ! YYERROR_VERBOSE |
1910 | yyerror (YY_("syntax error" )); |
1911 | #else |
1912 | # define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \ |
1913 | yyssp, yytoken) |
1914 | { |
1915 | char const *yymsgp = YY_("syntax error" ); |
1916 | int yysyntax_error_status; |
1917 | yysyntax_error_status = YYSYNTAX_ERROR; |
1918 | if (yysyntax_error_status == 0) |
1919 | yymsgp = yymsg; |
1920 | else if (yysyntax_error_status == 1) |
1921 | { |
1922 | if (yymsg != yymsgbuf) |
1923 | YYSTACK_FREE (yymsg); |
1924 | yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc); |
1925 | if (!yymsg) |
1926 | { |
1927 | yymsg = yymsgbuf; |
1928 | yymsg_alloc = sizeof yymsgbuf; |
1929 | yysyntax_error_status = 2; |
1930 | } |
1931 | else |
1932 | { |
1933 | yysyntax_error_status = YYSYNTAX_ERROR; |
1934 | yymsgp = yymsg; |
1935 | } |
1936 | } |
1937 | yyerror (yymsgp); |
1938 | if (yysyntax_error_status == 2) |
1939 | goto yyexhaustedlab; |
1940 | } |
1941 | # undef YYSYNTAX_ERROR |
1942 | #endif |
1943 | } |
1944 | |
1945 | |
1946 | |
1947 | if (yyerrstatus == 3) |
1948 | { |
1949 | /* If just tried and failed to reuse lookahead token after an |
1950 | error, discard it. */ |
1951 | |
1952 | if (yychar <= YYEOF) |
1953 | { |
1954 | /* Return failure if at end of input. */ |
1955 | if (yychar == YYEOF) |
1956 | YYABORT; |
1957 | } |
1958 | else |
1959 | { |
1960 | yydestruct ("Error: discarding" , |
1961 | yytoken, &yylval); |
1962 | yychar = YYEMPTY; |
1963 | } |
1964 | } |
1965 | |
1966 | /* Else will try to reuse lookahead token after shifting the error |
1967 | token. */ |
1968 | goto yyerrlab1; |
1969 | |
1970 | |
1971 | /*---------------------------------------------------. |
1972 | | yyerrorlab -- error raised explicitly by YYERROR. | |
1973 | `---------------------------------------------------*/ |
1974 | yyerrorlab: |
1975 | |
1976 | /* Pacify compilers like GCC when the user code never invokes |
1977 | YYERROR and the label yyerrorlab therefore never appears in user |
1978 | code. */ |
1979 | if (/*CONSTCOND*/ 0) |
1980 | goto yyerrorlab; |
1981 | |
1982 | /* Do not reclaim the symbols of the rule whose action triggered |
1983 | this YYERROR. */ |
1984 | YYPOPSTACK (yylen); |
1985 | yylen = 0; |
1986 | YY_STACK_PRINT (yyss, yyssp); |
1987 | yystate = *yyssp; |
1988 | goto yyerrlab1; |
1989 | |
1990 | |
1991 | /*-------------------------------------------------------------. |
1992 | | yyerrlab1 -- common code for both syntax error and YYERROR. | |
1993 | `-------------------------------------------------------------*/ |
1994 | yyerrlab1: |
1995 | yyerrstatus = 3; /* Each real token shifted decrements this. */ |
1996 | |
1997 | for (;;) |
1998 | { |
1999 | yyn = yypact[yystate]; |
2000 | if (!yypact_value_is_default (yyn)) |
2001 | { |
2002 | yyn += YYTERROR; |
2003 | if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR) |
2004 | { |
2005 | yyn = yytable[yyn]; |
2006 | if (0 < yyn) |
2007 | break; |
2008 | } |
2009 | } |
2010 | |
2011 | /* Pop the current state because it cannot handle the error token. */ |
2012 | if (yyssp == yyss) |
2013 | YYABORT; |
2014 | |
2015 | |
2016 | yydestruct ("Error: popping" , |
2017 | yystos[yystate], yyvsp); |
2018 | YYPOPSTACK (1); |
2019 | yystate = *yyssp; |
2020 | YY_STACK_PRINT (yyss, yyssp); |
2021 | } |
2022 | |
2023 | YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN |
2024 | *++yyvsp = yylval; |
2025 | YY_IGNORE_MAYBE_UNINITIALIZED_END |
2026 | |
2027 | |
2028 | /* Shift the error token. */ |
2029 | YY_SYMBOL_PRINT ("Shifting" , yystos[yyn], yyvsp, yylsp); |
2030 | |
2031 | yystate = yyn; |
2032 | goto yynewstate; |
2033 | |
2034 | |
2035 | /*-------------------------------------. |
2036 | | yyacceptlab -- YYACCEPT comes here. | |
2037 | `-------------------------------------*/ |
2038 | yyacceptlab: |
2039 | yyresult = 0; |
2040 | goto yyreturn; |
2041 | |
2042 | /*-----------------------------------. |
2043 | | yyabortlab -- YYABORT comes here. | |
2044 | `-----------------------------------*/ |
2045 | yyabortlab: |
2046 | yyresult = 1; |
2047 | goto yyreturn; |
2048 | |
2049 | #if !defined yyoverflow || YYERROR_VERBOSE |
2050 | /*-------------------------------------------------. |
2051 | | yyexhaustedlab -- memory exhaustion comes here. | |
2052 | `-------------------------------------------------*/ |
2053 | yyexhaustedlab: |
2054 | yyerror (YY_("memory exhausted" )); |
2055 | yyresult = 2; |
2056 | /* Fall through. */ |
2057 | #endif |
2058 | |
2059 | yyreturn: |
2060 | if (yychar != YYEMPTY) |
2061 | { |
2062 | /* Make sure we have latest lookahead translation. See comments at |
2063 | user semantic actions for why this is necessary. */ |
2064 | yytoken = YYTRANSLATE (yychar); |
2065 | yydestruct ("Cleanup: discarding lookahead" , |
2066 | yytoken, &yylval); |
2067 | } |
2068 | /* Do not reclaim the symbols of the rule whose action triggered |
2069 | this YYABORT or YYACCEPT. */ |
2070 | YYPOPSTACK (yylen); |
2071 | YY_STACK_PRINT (yyss, yyssp); |
2072 | while (yyssp != yyss) |
2073 | { |
2074 | yydestruct ("Cleanup: popping" , |
2075 | yystos[*yyssp], yyvsp); |
2076 | YYPOPSTACK (1); |
2077 | } |
2078 | #ifndef yyoverflow |
2079 | if (yyss != yyssa) |
2080 | YYSTACK_FREE (yyss); |
2081 | #endif |
2082 | #if YYERROR_VERBOSE |
2083 | if (yymsg != yymsgbuf) |
2084 | YYSTACK_FREE (yymsg); |
2085 | #endif |
2086 | return yyresult; |
2087 | } |
2088 | #line 510 "cmd-parse.y" /* yacc.c:1906 */ |
2089 | |
2090 | |
2091 | static char * |
2092 | cmd_parse_get_error(const char *file, u_int line, const char *error) |
2093 | { |
2094 | char *s; |
2095 | |
2096 | if (file == NULL) |
2097 | s = xstrdup(error); |
2098 | else |
2099 | xasprintf (&s, "%s:%u: %s" , file, line, error); |
2100 | return (s); |
2101 | } |
2102 | |
2103 | static void |
2104 | cmd_parse_print_commands(struct cmd_parse_input *pi, u_int line, |
2105 | struct cmd_list *cmdlist) |
2106 | { |
2107 | char *s; |
2108 | |
2109 | if (pi->item != NULL && (pi->flags & CMD_PARSE_VERBOSE)) { |
2110 | s = cmd_list_print(cmdlist, 0); |
2111 | if (pi->file != NULL) |
2112 | cmdq_print(pi->item, "%s:%u: %s" , pi->file, line, s); |
2113 | else |
2114 | cmdq_print(pi->item, "%u: %s" , line, s); |
2115 | free(s); |
2116 | } |
2117 | } |
2118 | |
2119 | static void |
2120 | cmd_parse_free_command(struct cmd_parse_command *cmd) |
2121 | { |
2122 | free(cmd->name); |
2123 | cmd_free_argv(cmd->argc, cmd->argv); |
2124 | free(cmd); |
2125 | } |
2126 | |
2127 | static struct cmd_parse_commands * |
2128 | cmd_parse_new_commands(void) |
2129 | { |
2130 | struct cmd_parse_commands *cmds; |
2131 | |
2132 | cmds = xmalloc(sizeof *cmds); |
2133 | TAILQ_INIT (cmds); |
2134 | return (cmds); |
2135 | } |
2136 | |
2137 | static void |
2138 | cmd_parse_free_commands(struct cmd_parse_commands *cmds) |
2139 | { |
2140 | struct cmd_parse_command *cmd, *cmd1; |
2141 | |
2142 | TAILQ_FOREACH_SAFE(cmd, cmds, entry, cmd1) { |
2143 | TAILQ_REMOVE(cmds, cmd, entry); |
2144 | cmd_parse_free_command(cmd); |
2145 | } |
2146 | free(cmds); |
2147 | } |
2148 | |
2149 | static struct cmd_parse_commands * |
2150 | cmd_parse_run_parser(char **cause) |
2151 | { |
2152 | struct cmd_parse_state *ps = &parse_state; |
2153 | struct cmd_parse_scope *scope, *scope1; |
2154 | int retval; |
2155 | |
2156 | ps->commands = NULL; |
2157 | TAILQ_INIT(&ps->stack); |
2158 | |
2159 | retval = yyparse(); |
2160 | TAILQ_FOREACH_SAFE(scope, &ps->stack, entry, scope1) { |
2161 | TAILQ_REMOVE(&ps->stack, scope, entry); |
2162 | free(scope); |
2163 | } |
2164 | if (retval != 0) { |
2165 | *cause = ps->error; |
2166 | return (NULL); |
2167 | } |
2168 | |
2169 | if (ps->commands == NULL) |
2170 | return (cmd_parse_new_commands()); |
2171 | return (ps->commands); |
2172 | } |
2173 | |
2174 | static struct cmd_parse_commands * |
2175 | cmd_parse_do_file(FILE *f, struct cmd_parse_input *pi, char **cause) |
2176 | { |
2177 | struct cmd_parse_state *ps = &parse_state; |
2178 | |
2179 | memset(ps, 0, sizeof *ps); |
2180 | ps->input = pi; |
2181 | ps->f = f; |
2182 | return (cmd_parse_run_parser(cause)); |
2183 | } |
2184 | |
2185 | static struct cmd_parse_commands * |
2186 | cmd_parse_do_buffer(const char *buf, size_t len, struct cmd_parse_input *pi, |
2187 | char **cause) |
2188 | { |
2189 | struct cmd_parse_state *ps = &parse_state; |
2190 | |
2191 | memset(ps, 0, sizeof *ps); |
2192 | ps->input = pi; |
2193 | ps->buf = buf; |
2194 | ps->len = len; |
2195 | return (cmd_parse_run_parser(cause)); |
2196 | } |
2197 | |
2198 | static struct cmd_parse_result * |
2199 | cmd_parse_build_commands(struct cmd_parse_commands *cmds, |
2200 | struct cmd_parse_input *pi) |
2201 | { |
2202 | static struct cmd_parse_result pr; |
2203 | struct cmd_parse_commands *cmds2; |
2204 | struct cmd_parse_command *cmd, *cmd2, *next, *next2, *after; |
2205 | u_int line = UINT_MAX; |
2206 | int i; |
2207 | struct cmd_list *cmdlist = NULL, *result; |
2208 | struct cmd *add; |
2209 | char *alias, *cause, *s; |
2210 | |
2211 | /* Check for an empty list. */ |
2212 | if (TAILQ_EMPTY(cmds)) { |
2213 | cmd_parse_free_commands(cmds); |
2214 | pr.status = CMD_PARSE_EMPTY; |
2215 | return (&pr); |
2216 | } |
2217 | |
2218 | /* |
2219 | * Walk the commands and expand any aliases. Each alias is parsed |
2220 | * individually to a new command list, any trailing arguments appended |
2221 | * to the last command, and all commands inserted into the original |
2222 | * command list. |
2223 | */ |
2224 | TAILQ_FOREACH_SAFE(cmd, cmds, entry, next) { |
2225 | alias = cmd_get_alias(cmd->name); |
2226 | if (alias == NULL) |
2227 | continue; |
2228 | |
2229 | line = cmd->line; |
2230 | log_debug("%s: %u %s = %s" , __func__, line, cmd->name, alias); |
2231 | |
2232 | pi->line = line; |
2233 | cmds2 = cmd_parse_do_buffer(alias, strlen(alias), pi, &cause); |
2234 | free(alias); |
2235 | if (cmds2 == NULL) { |
2236 | pr.status = CMD_PARSE_ERROR; |
2237 | pr.error = cause; |
2238 | goto out; |
2239 | } |
2240 | |
2241 | cmd2 = TAILQ_LAST(cmds2, cmd_parse_commands); |
2242 | if (cmd2 == NULL) { |
2243 | TAILQ_REMOVE(cmds, cmd, entry); |
2244 | cmd_parse_free_command(cmd); |
2245 | continue; |
2246 | } |
2247 | for (i = 0; i < cmd->argc; i++) |
2248 | cmd_append_argv(&cmd2->argc, &cmd2->argv, cmd->argv[i]); |
2249 | |
2250 | after = cmd; |
2251 | TAILQ_FOREACH_SAFE(cmd2, cmds2, entry, next2) { |
2252 | cmd2->line = line; |
2253 | TAILQ_REMOVE(cmds2, cmd2, entry); |
2254 | TAILQ_INSERT_AFTER(cmds, after, cmd2, entry); |
2255 | after = cmd2; |
2256 | } |
2257 | cmd_parse_free_commands(cmds2); |
2258 | |
2259 | TAILQ_REMOVE(cmds, cmd, entry); |
2260 | cmd_parse_free_command(cmd); |
2261 | } |
2262 | |
2263 | /* |
2264 | * Parse each command into a command list. Create a new command list |
2265 | * for each line so they get a new group (so the queue knows which ones |
2266 | * to remove if a command fails when executed). |
2267 | */ |
2268 | result = cmd_list_new(); |
2269 | TAILQ_FOREACH(cmd, cmds, entry) { |
2270 | log_debug("%s: %u %s" , __func__, cmd->line, cmd->name); |
2271 | cmd_log_argv(cmd->argc, cmd->argv, __func__); |
2272 | |
2273 | if (cmdlist == NULL || cmd->line != line) { |
2274 | if (cmdlist != NULL) { |
2275 | cmd_parse_print_commands(pi, line, cmdlist); |
2276 | cmd_list_move(result, cmdlist); |
2277 | cmd_list_free(cmdlist); |
2278 | } |
2279 | cmdlist = cmd_list_new(); |
2280 | } |
2281 | line = cmd->line; |
2282 | |
2283 | cmd_prepend_argv(&cmd->argc, &cmd->argv, cmd->name); |
2284 | add = cmd_parse(cmd->argc, cmd->argv, pi->file, line, &cause); |
2285 | if (add == NULL) { |
2286 | cmd_list_free(result); |
2287 | pr.status = CMD_PARSE_ERROR; |
2288 | pr.error = cmd_parse_get_error(pi->file, line, cause); |
2289 | free(cause); |
2290 | cmd_list_free(cmdlist); |
2291 | goto out; |
2292 | } |
2293 | cmd_list_append(cmdlist, add); |
2294 | } |
2295 | if (cmdlist != NULL) { |
2296 | cmd_parse_print_commands(pi, line, cmdlist); |
2297 | cmd_list_move(result, cmdlist); |
2298 | cmd_list_free(cmdlist); |
2299 | } |
2300 | |
2301 | s = cmd_list_print(result, 0); |
2302 | log_debug("%s: %s" , __func__, s); |
2303 | free(s); |
2304 | |
2305 | pr.status = CMD_PARSE_SUCCESS; |
2306 | pr.cmdlist = result; |
2307 | |
2308 | out: |
2309 | cmd_parse_free_commands(cmds); |
2310 | |
2311 | return (&pr); |
2312 | } |
2313 | |
2314 | struct cmd_parse_result * |
2315 | cmd_parse_from_file(FILE *f, struct cmd_parse_input *pi) |
2316 | { |
2317 | static struct cmd_parse_result pr; |
2318 | struct cmd_parse_input input; |
2319 | struct cmd_parse_commands *cmds; |
2320 | char *cause; |
2321 | |
2322 | if (pi == NULL) { |
2323 | memset(&input, 0, sizeof input); |
2324 | pi = &input; |
2325 | } |
2326 | memset(&pr, 0, sizeof pr); |
2327 | |
2328 | cmds = cmd_parse_do_file(f, pi, &cause); |
2329 | if (cmds == NULL) { |
2330 | pr.status = CMD_PARSE_ERROR; |
2331 | pr.error = cause; |
2332 | return (&pr); |
2333 | } |
2334 | return (cmd_parse_build_commands(cmds, pi)); |
2335 | } |
2336 | |
2337 | struct cmd_parse_result * |
2338 | cmd_parse_from_string(const char *s, struct cmd_parse_input *pi) |
2339 | { |
2340 | return (cmd_parse_from_buffer(s, strlen(s), pi)); |
2341 | } |
2342 | |
2343 | struct cmd_parse_result * |
2344 | cmd_parse_from_buffer(const void *buf, size_t len, struct cmd_parse_input *pi) |
2345 | { |
2346 | static struct cmd_parse_result pr; |
2347 | struct cmd_parse_input input; |
2348 | struct cmd_parse_commands *cmds; |
2349 | char *cause; |
2350 | |
2351 | if (pi == NULL) { |
2352 | memset(&input, 0, sizeof input); |
2353 | pi = &input; |
2354 | } |
2355 | memset(&pr, 0, sizeof pr); |
2356 | |
2357 | if (len == 0) { |
2358 | pr.status = CMD_PARSE_EMPTY; |
2359 | pr.cmdlist = NULL; |
2360 | pr.error = NULL; |
2361 | return (&pr); |
2362 | } |
2363 | |
2364 | cmds = cmd_parse_do_buffer(buf, len, pi, &cause); |
2365 | if (cmds == NULL) { |
2366 | pr.status = CMD_PARSE_ERROR; |
2367 | pr.error = cause; |
2368 | return (&pr); |
2369 | } |
2370 | return (cmd_parse_build_commands(cmds, pi)); |
2371 | } |
2372 | |
2373 | struct cmd_parse_result * |
2374 | cmd_parse_from_arguments(int argc, char **argv, struct cmd_parse_input *pi) |
2375 | { |
2376 | struct cmd_parse_input input; |
2377 | struct cmd_parse_commands *cmds; |
2378 | struct cmd_parse_command *cmd; |
2379 | char **copy, **new_argv; |
2380 | size_t size; |
2381 | int i, last, new_argc; |
2382 | |
2383 | /* |
2384 | * The commands are already split up into arguments, so just separate |
2385 | * into a set of commands by ';'. |
2386 | */ |
2387 | |
2388 | if (pi == NULL) { |
2389 | memset(&input, 0, sizeof input); |
2390 | pi = &input; |
2391 | } |
2392 | cmd_log_argv(argc, argv, "%s" , __func__); |
2393 | |
2394 | cmds = cmd_parse_new_commands(); |
2395 | copy = cmd_copy_argv(argc, argv); |
2396 | |
2397 | last = 0; |
2398 | for (i = 0; i < argc; i++) { |
2399 | size = strlen(copy[i]); |
2400 | if (size == 0 || copy[i][size - 1] != ';') |
2401 | continue; |
2402 | copy[i][--size] = '\0'; |
2403 | if (size > 0 && copy[i][size - 1] == '\\') { |
2404 | copy[i][size - 1] = ';'; |
2405 | continue; |
2406 | } |
2407 | |
2408 | new_argc = i - last; |
2409 | new_argv = copy + last; |
2410 | if (size != 0) |
2411 | new_argc++; |
2412 | |
2413 | if (new_argc != 0) { |
2414 | cmd_log_argv(new_argc, new_argv, "%s: at %u" , __func__, |
2415 | i); |
2416 | |
2417 | cmd = xcalloc(1, sizeof *cmd); |
2418 | cmd->name = xstrdup(new_argv[0]); |
2419 | cmd->line = pi->line; |
2420 | |
2421 | cmd->argc = new_argc - 1; |
2422 | cmd->argv = cmd_copy_argv(new_argc - 1, new_argv + 1); |
2423 | |
2424 | TAILQ_INSERT_TAIL(cmds, cmd, entry); |
2425 | } |
2426 | |
2427 | last = i + 1; |
2428 | } |
2429 | if (last != argc) { |
2430 | new_argv = copy + last; |
2431 | new_argc = argc - last; |
2432 | |
2433 | if (new_argc != 0) { |
2434 | cmd_log_argv(new_argc, new_argv, "%s: at %u" , __func__, |
2435 | last); |
2436 | |
2437 | cmd = xcalloc(1, sizeof *cmd); |
2438 | cmd->name = xstrdup(new_argv[0]); |
2439 | cmd->line = pi->line; |
2440 | |
2441 | cmd->argc = new_argc - 1; |
2442 | cmd->argv = cmd_copy_argv(new_argc - 1, new_argv + 1); |
2443 | |
2444 | TAILQ_INSERT_TAIL(cmds, cmd, entry); |
2445 | } |
2446 | } |
2447 | |
2448 | cmd_free_argv(argc, copy); |
2449 | return (cmd_parse_build_commands(cmds, pi)); |
2450 | } |
2451 | |
2452 | static int printflike(1, 2) |
2453 | yyerror(const char *fmt, ...) |
2454 | { |
2455 | struct cmd_parse_state *ps = &parse_state; |
2456 | struct cmd_parse_input *pi = ps->input; |
2457 | va_list ap; |
2458 | char *error; |
2459 | |
2460 | if (ps->error != NULL) |
2461 | return (0); |
2462 | |
2463 | va_start(ap, fmt); |
2464 | xvasprintf(&error, fmt, ap); |
2465 | va_end(ap); |
2466 | |
2467 | ps->error = cmd_parse_get_error(pi->file, pi->line, error); |
2468 | free(error); |
2469 | return (0); |
2470 | } |
2471 | |
2472 | static int |
2473 | yylex_is_var(char ch, int first) |
2474 | { |
2475 | if (ch == '=') |
2476 | return (0); |
2477 | if (first && isdigit((u_char)ch)) |
2478 | return (0); |
2479 | return (isalnum((u_char)ch) || ch == '_'); |
2480 | } |
2481 | |
2482 | static void |
2483 | yylex_append(char **buf, size_t *len, const char *add, size_t addlen) |
2484 | { |
2485 | if (addlen > SIZE_MAX - 1 || *len > SIZE_MAX - 1 - addlen) |
2486 | fatalx("buffer is too big" ); |
2487 | *buf = xrealloc(*buf, (*len) + 1 + addlen); |
2488 | memcpy((*buf) + *len, add, addlen); |
2489 | (*len) += addlen; |
2490 | } |
2491 | |
2492 | static void |
2493 | yylex_append1(char **buf, size_t *len, char add) |
2494 | { |
2495 | yylex_append(buf, len, &add, 1); |
2496 | } |
2497 | |
2498 | static int |
2499 | yylex_getc1(void) |
2500 | { |
2501 | struct cmd_parse_state *ps = &parse_state; |
2502 | int ch; |
2503 | |
2504 | if (ps->f != NULL) |
2505 | ch = getc(ps->f); |
2506 | else { |
2507 | if (ps->off == ps->len) |
2508 | ch = EOF; |
2509 | else |
2510 | ch = ps->buf[ps->off++]; |
2511 | } |
2512 | return (ch); |
2513 | } |
2514 | |
2515 | static void |
2516 | yylex_ungetc(int ch) |
2517 | { |
2518 | struct cmd_parse_state *ps = &parse_state; |
2519 | |
2520 | if (ps->f != NULL) |
2521 | ungetc(ch, ps->f); |
2522 | else if (ps->off > 0 && ch != EOF) |
2523 | ps->off--; |
2524 | } |
2525 | |
2526 | static int |
2527 | yylex_getc(void) |
2528 | { |
2529 | struct cmd_parse_state *ps = &parse_state; |
2530 | int ch; |
2531 | |
2532 | if (ps->escapes != 0) { |
2533 | ps->escapes--; |
2534 | return ('\\'); |
2535 | } |
2536 | for (;;) { |
2537 | ch = yylex_getc1(); |
2538 | if (ch == '\\') { |
2539 | ps->escapes++; |
2540 | continue; |
2541 | } |
2542 | if (ch == '\n' && (ps->escapes % 2) == 1) { |
2543 | ps->input->line++; |
2544 | ps->escapes--; |
2545 | continue; |
2546 | } |
2547 | |
2548 | if (ps->escapes != 0) { |
2549 | yylex_ungetc(ch); |
2550 | ps->escapes--; |
2551 | return ('\\'); |
2552 | } |
2553 | return (ch); |
2554 | } |
2555 | } |
2556 | |
2557 | static char * |
2558 | yylex_get_word(int ch) |
2559 | { |
2560 | char *buf; |
2561 | size_t len; |
2562 | |
2563 | len = 0; |
2564 | buf = xmalloc(1); |
2565 | |
2566 | do |
2567 | yylex_append1(&buf, &len, ch); |
2568 | while ((ch = yylex_getc()) != EOF && strchr(" \t\n" , ch) == NULL); |
2569 | yylex_ungetc(ch); |
2570 | |
2571 | buf[len] = '\0'; |
2572 | log_debug("%s: %s" , __func__, buf); |
2573 | return (buf); |
2574 | } |
2575 | |
2576 | static int |
2577 | yylex(void) |
2578 | { |
2579 | struct cmd_parse_state *ps = &parse_state; |
2580 | char *token, *cp; |
2581 | int ch, next, condition; |
2582 | |
2583 | if (ps->eol) |
2584 | ps->input->line++; |
2585 | ps->eol = 0; |
2586 | |
2587 | condition = ps->condition; |
2588 | ps->condition = 0; |
2589 | |
2590 | for (;;) { |
2591 | ch = yylex_getc(); |
2592 | |
2593 | if (ch == EOF) { |
2594 | /* |
2595 | * Ensure every file or string is terminated by a |
2596 | * newline. This keeps the parser simpler and avoids |
2597 | * having to add a newline to each string. |
2598 | */ |
2599 | if (ps->eof) |
2600 | break; |
2601 | ps->eof = 1; |
2602 | return ('\n'); |
2603 | } |
2604 | |
2605 | if (ch == ' ' || ch == '\t') { |
2606 | /* |
2607 | * Ignore whitespace. |
2608 | */ |
2609 | continue; |
2610 | } |
2611 | |
2612 | if (ch == '\n') { |
2613 | /* |
2614 | * End of line. Update the line number. |
2615 | */ |
2616 | ps->eol = 1; |
2617 | return ('\n'); |
2618 | } |
2619 | |
2620 | if (ch == ';') { |
2621 | /* |
2622 | * A semicolon is itself. |
2623 | */ |
2624 | return (';'); |
2625 | } |
2626 | |
2627 | if (ch == '#') { |
2628 | /* |
2629 | * #{ after a condition opens a format; anything else |
2630 | * is a comment, ignore up to the end of the line. |
2631 | */ |
2632 | next = yylex_getc(); |
2633 | if (condition && next == '{') { |
2634 | yylval.token = yylex_format(); |
2635 | if (yylval.token == NULL) |
2636 | return (ERROR); |
2637 | return (FORMAT); |
2638 | } |
2639 | while (next != '\n' && next != EOF) |
2640 | next = yylex_getc(); |
2641 | if (next == '\n') { |
2642 | ps->input->line++; |
2643 | return ('\n'); |
2644 | } |
2645 | continue; |
2646 | } |
2647 | |
2648 | if (ch == '%') { |
2649 | /* |
2650 | * % is a condition unless it is all % or all numbers, |
2651 | * then it is a token. |
2652 | */ |
2653 | yylval.token = yylex_get_word('%'); |
2654 | for (cp = yylval.token; *cp != '\0'; cp++) { |
2655 | if (*cp != '%' && !isdigit((u_char)*cp)) |
2656 | break; |
2657 | } |
2658 | if (*cp == '\0') |
2659 | return (TOKEN); |
2660 | ps->condition = 1; |
2661 | if (strcmp(yylval.token, "%if" ) == 0) { |
2662 | free(yylval.token); |
2663 | return (IF); |
2664 | } |
2665 | if (strcmp(yylval.token, "%else" ) == 0) { |
2666 | free(yylval.token); |
2667 | return (ELSE); |
2668 | } |
2669 | if (strcmp(yylval.token, "%elif" ) == 0) { |
2670 | free(yylval.token); |
2671 | return (ELIF); |
2672 | } |
2673 | if (strcmp(yylval.token, "%endif" ) == 0) { |
2674 | free(yylval.token); |
2675 | return (ENDIF); |
2676 | } |
2677 | free(yylval.token); |
2678 | return (ERROR); |
2679 | } |
2680 | |
2681 | /* |
2682 | * Otherwise this is a token. |
2683 | */ |
2684 | token = yylex_token(ch); |
2685 | if (token == NULL) |
2686 | return (ERROR); |
2687 | yylval.token = token; |
2688 | |
2689 | if (strchr(token, '=') != NULL && yylex_is_var(*token, 1)) { |
2690 | for (cp = token + 1; *cp != '='; cp++) { |
2691 | if (!yylex_is_var(*cp, 0)) |
2692 | break; |
2693 | } |
2694 | if (*cp == '=') |
2695 | return (EQUALS); |
2696 | } |
2697 | return (TOKEN); |
2698 | } |
2699 | return (0); |
2700 | } |
2701 | |
2702 | static char * |
2703 | yylex_format(void) |
2704 | { |
2705 | char *buf; |
2706 | size_t len; |
2707 | int ch, brackets = 1; |
2708 | |
2709 | len = 0; |
2710 | buf = xmalloc(1); |
2711 | |
2712 | yylex_append(&buf, &len, "#{" , 2); |
2713 | for (;;) { |
2714 | if ((ch = yylex_getc()) == EOF || ch == '\n') |
2715 | goto error; |
2716 | if (ch == '#') { |
2717 | if ((ch = yylex_getc()) == EOF || ch == '\n') |
2718 | goto error; |
2719 | if (ch == '{') |
2720 | brackets++; |
2721 | yylex_append1(&buf, &len, '#'); |
2722 | } else if (ch == '}') { |
2723 | if (brackets != 0 && --brackets == 0) { |
2724 | yylex_append1(&buf, &len, ch); |
2725 | break; |
2726 | } |
2727 | } |
2728 | yylex_append1(&buf, &len, ch); |
2729 | } |
2730 | if (brackets != 0) |
2731 | goto error; |
2732 | |
2733 | buf[len] = '\0'; |
2734 | log_debug("%s: %s" , __func__, buf); |
2735 | return (buf); |
2736 | |
2737 | error: |
2738 | free(buf); |
2739 | return (NULL); |
2740 | } |
2741 | |
2742 | static int |
2743 | yylex_token_escape(char **buf, size_t *len) |
2744 | { |
2745 | int ch, type, o2, o3; |
2746 | u_int size, i, tmp; |
2747 | char s[9]; |
2748 | struct utf8_data ud; |
2749 | |
2750 | ch = yylex_getc(); |
2751 | |
2752 | if (ch >= '4' && ch <= '7') { |
2753 | yyerror("invalid octal escape" ); |
2754 | return (0); |
2755 | } |
2756 | if (ch >= '0' && ch <= '3') { |
2757 | o2 = yylex_getc(); |
2758 | if (o2 >= '0' && o2 <= '7') { |
2759 | o3 = yylex_getc(); |
2760 | if (o3 >= '0' && o3 <= '7') { |
2761 | ch = 64 * (ch - '0') + |
2762 | 8 * (o2 - '0') + |
2763 | (o3 - '0'); |
2764 | yylex_append1(buf, len, ch); |
2765 | return (1); |
2766 | } |
2767 | } |
2768 | yyerror("invalid octal escape" ); |
2769 | return (0); |
2770 | } |
2771 | |
2772 | switch (ch) { |
2773 | case EOF: |
2774 | return (0); |
2775 | case 'a': |
2776 | ch = '\a'; |
2777 | break; |
2778 | case 'b': |
2779 | ch = '\b'; |
2780 | break; |
2781 | case 'e': |
2782 | ch = '\033'; |
2783 | break; |
2784 | case 'f': |
2785 | ch = '\f'; |
2786 | break; |
2787 | case 's': |
2788 | ch = ' '; |
2789 | break; |
2790 | case 'v': |
2791 | ch = '\v'; |
2792 | break; |
2793 | case 'r': |
2794 | ch = '\r'; |
2795 | break; |
2796 | case 'n': |
2797 | ch = '\n'; |
2798 | break; |
2799 | case 't': |
2800 | ch = '\t'; |
2801 | break; |
2802 | case 'u': |
2803 | type = 'u'; |
2804 | size = 4; |
2805 | goto unicode; |
2806 | case 'U': |
2807 | type = 'U'; |
2808 | size = 8; |
2809 | goto unicode; |
2810 | } |
2811 | |
2812 | yylex_append1(buf, len, ch); |
2813 | return (1); |
2814 | |
2815 | unicode: |
2816 | for (i = 0; i < size; i++) { |
2817 | ch = yylex_getc(); |
2818 | if (ch == EOF || ch == '\n') |
2819 | return (0); |
2820 | if (!isxdigit((u_char)ch)) { |
2821 | yyerror("invalid \\%c argument" , type); |
2822 | return (0); |
2823 | } |
2824 | s[i] = ch; |
2825 | } |
2826 | s[i] = '\0'; |
2827 | |
2828 | if ((size == 4 && sscanf(s, "%4x" , &tmp) != 1) || |
2829 | (size == 8 && sscanf(s, "%8x" , &tmp) != 1)) { |
2830 | yyerror("invalid \\%c argument" , type); |
2831 | return (0); |
2832 | } |
2833 | if (utf8_split(tmp, &ud) != UTF8_DONE) { |
2834 | yyerror("invalid \\%c argument" , type); |
2835 | return (0); |
2836 | } |
2837 | yylex_append(buf, len, ud.data, ud.size); |
2838 | return (1); |
2839 | } |
2840 | |
2841 | static int |
2842 | yylex_token_variable(char **buf, size_t *len) |
2843 | { |
2844 | struct environ_entry *envent; |
2845 | int ch, brackets = 0; |
2846 | char name[1024]; |
2847 | size_t namelen = 0; |
2848 | const char *value; |
2849 | |
2850 | ch = yylex_getc(); |
2851 | if (ch == EOF) |
2852 | return (0); |
2853 | if (ch == '{') |
2854 | brackets = 1; |
2855 | else { |
2856 | if (!yylex_is_var(ch, 1)) { |
2857 | yylex_append1(buf, len, '$'); |
2858 | yylex_ungetc(ch); |
2859 | return (1); |
2860 | } |
2861 | name[namelen++] = ch; |
2862 | } |
2863 | |
2864 | for (;;) { |
2865 | ch = yylex_getc(); |
2866 | if (brackets && ch == '}') |
2867 | break; |
2868 | if (ch == EOF || !yylex_is_var(ch, 0)) { |
2869 | if (!brackets) { |
2870 | yylex_ungetc(ch); |
2871 | break; |
2872 | } |
2873 | yyerror("invalid environment variable" ); |
2874 | return (0); |
2875 | } |
2876 | if (namelen == (sizeof name) - 2) { |
2877 | yyerror("environment variable is too long" ); |
2878 | return (0); |
2879 | } |
2880 | name[namelen++] = ch; |
2881 | } |
2882 | name[namelen] = '\0'; |
2883 | |
2884 | envent = environ_find(global_environ, name); |
2885 | if (envent != NULL) { |
2886 | value = envent->value; |
2887 | log_debug("%s: %s -> %s" , __func__, name, value); |
2888 | yylex_append(buf, len, value, strlen(value)); |
2889 | } |
2890 | return (1); |
2891 | } |
2892 | |
2893 | static int |
2894 | yylex_token_tilde(char **buf, size_t *len) |
2895 | { |
2896 | struct environ_entry *envent; |
2897 | int ch; |
2898 | char name[1024]; |
2899 | size_t namelen = 0; |
2900 | struct passwd *pw; |
2901 | const char *home = NULL; |
2902 | |
2903 | for (;;) { |
2904 | ch = yylex_getc(); |
2905 | if (ch == EOF || strchr("/ \t\n\"'" , ch) != NULL) { |
2906 | yylex_ungetc(ch); |
2907 | break; |
2908 | } |
2909 | if (namelen == (sizeof name) - 2) { |
2910 | yyerror("user name is too long" ); |
2911 | return (0); |
2912 | } |
2913 | name[namelen++] = ch; |
2914 | } |
2915 | name[namelen] = '\0'; |
2916 | |
2917 | if (*name == '\0') { |
2918 | envent = environ_find(global_environ, "HOME" ); |
2919 | if (envent != NULL && *envent->value != '\0') |
2920 | home = envent->value; |
2921 | else if ((pw = getpwuid(getuid())) != NULL) |
2922 | home = pw->pw_dir; |
2923 | } else { |
2924 | if ((pw = getpwnam(name)) != NULL) |
2925 | home = pw->pw_dir; |
2926 | } |
2927 | if (home == NULL) |
2928 | return (0); |
2929 | |
2930 | log_debug("%s: ~%s -> %s" , __func__, name, home); |
2931 | yylex_append(buf, len, home, strlen(home)); |
2932 | return (1); |
2933 | } |
2934 | |
2935 | static int |
2936 | yylex_token_brace(char **buf, size_t *len) |
2937 | { |
2938 | struct cmd_parse_state *ps = &parse_state; |
2939 | int ch, lines = 0, nesting = 1, escape = 0; |
2940 | int quote = '\0', token = 0; |
2941 | |
2942 | /* |
2943 | * Extract a string up to the matching unquoted '}', including newlines |
2944 | * and handling nested braces. |
2945 | * |
2946 | * To detect the final and intermediate braces which affect the nesting |
2947 | * depth, we scan the input as if it was a tmux config file, and ignore |
2948 | * braces which would be considered quoted, escaped, or in a comment. |
2949 | * |
2950 | * We update the token state after every character because '#' begins a |
2951 | * comment only when it begins a token. For simplicity, we treat an |
2952 | * unquoted directive format as comment. |
2953 | * |
2954 | * The result is verbatim copy of the input excluding the final brace. |
2955 | */ |
2956 | |
2957 | for (ch = yylex_getc1(); ch != EOF; ch = yylex_getc1()) { |
2958 | yylex_append1(buf, len, ch); |
2959 | if (ch == '\n') |
2960 | lines++; |
2961 | |
2962 | /* |
2963 | * If the previous character was a backslash (escape is set), |
2964 | * escape anything if unquoted or in double quotes, otherwise |
2965 | * escape only '\n' and '\\'. |
2966 | */ |
2967 | if (escape && |
2968 | (quote == '\0' || |
2969 | quote == '"' || |
2970 | ch == '\n' || |
2971 | ch == '\\')) { |
2972 | escape = 0; |
2973 | if (ch != '\n') |
2974 | token = 1; |
2975 | continue; |
2976 | } |
2977 | |
2978 | /* |
2979 | * The character is not escaped. If it is a backslash, set the |
2980 | * escape flag. |
2981 | */ |
2982 | if (ch == '\\') { |
2983 | escape = 1; |
2984 | continue; |
2985 | } |
2986 | escape = 0; |
2987 | |
2988 | /* A newline always resets to unquoted. */ |
2989 | if (ch == '\n') { |
2990 | quote = token = 0; |
2991 | continue; |
2992 | } |
2993 | |
2994 | if (quote) { |
2995 | /* |
2996 | * Inside quotes or comment. Check if this is the |
2997 | * closing quote. |
2998 | */ |
2999 | if (ch == quote && quote != '#') |
3000 | quote = 0; |
3001 | token = 1; /* token continues regardless */ |
3002 | } else { |
3003 | /* Not inside quotes or comment. */ |
3004 | switch (ch) { |
3005 | case '"': |
3006 | case '\'': |
3007 | case '#': |
3008 | /* Beginning of quote or maybe comment. */ |
3009 | if (ch != '#' || !token) |
3010 | quote = ch; |
3011 | token = 1; |
3012 | break; |
3013 | case ' ': |
3014 | case '\t': |
3015 | case ';': |
3016 | /* Delimiter - token resets. */ |
3017 | token = 0; |
3018 | break; |
3019 | case '{': |
3020 | nesting++; |
3021 | token = 0; /* new commands set - token resets */ |
3022 | break; |
3023 | case '}': |
3024 | nesting--; |
3025 | token = 1; /* same as after quotes */ |
3026 | if (nesting == 0) { |
3027 | (*len)--; /* remove closing } */ |
3028 | ps->input->line += lines; |
3029 | return (1); |
3030 | } |
3031 | break; |
3032 | default: |
3033 | token = 1; |
3034 | break; |
3035 | } |
3036 | } |
3037 | } |
3038 | |
3039 | /* |
3040 | * Update line count after error as reporting the opening line is more |
3041 | * useful than EOF. |
3042 | */ |
3043 | yyerror("unterminated brace string" ); |
3044 | ps->input->line += lines; |
3045 | return (0); |
3046 | } |
3047 | |
3048 | static char * |
3049 | yylex_token(int ch) |
3050 | { |
3051 | char *buf; |
3052 | size_t len; |
3053 | enum { START, |
3054 | NONE, |
3055 | DOUBLE_QUOTES, |
3056 | SINGLE_QUOTES } state = NONE, last = START; |
3057 | |
3058 | len = 0; |
3059 | buf = xmalloc(1); |
3060 | |
3061 | for (;;) { |
3062 | /* |
3063 | * EOF or \n are always the end of the token. If inside quotes |
3064 | * they are an error. |
3065 | */ |
3066 | if (ch == EOF || ch == '\n') { |
3067 | if (state != NONE) |
3068 | goto error; |
3069 | break; |
3070 | } |
3071 | |
3072 | /* Whitespace or ; ends a token unless inside quotes. */ |
3073 | if ((ch == ' ' || ch == '\t' || ch == ';') && state == NONE) |
3074 | break; |
3075 | |
3076 | /* |
3077 | * \ ~ and $ are expanded except in single quotes. |
3078 | */ |
3079 | if (ch == '\\' && state != SINGLE_QUOTES) { |
3080 | if (!yylex_token_escape(&buf, &len)) |
3081 | goto error; |
3082 | goto skip; |
3083 | } |
3084 | if (ch == '~' && last != state && state != SINGLE_QUOTES) { |
3085 | if (!yylex_token_tilde(&buf, &len)) |
3086 | goto error; |
3087 | goto skip; |
3088 | } |
3089 | if (ch == '$' && state != SINGLE_QUOTES) { |
3090 | if (!yylex_token_variable(&buf, &len)) |
3091 | goto error; |
3092 | goto skip; |
3093 | } |
3094 | if (ch == '{' && state == NONE) { |
3095 | if (!yylex_token_brace(&buf, &len)) |
3096 | goto error; |
3097 | goto skip; |
3098 | } |
3099 | if (ch == '}' && state == NONE) |
3100 | goto error; /* unmatched (matched ones were handled) */ |
3101 | |
3102 | /* |
3103 | * ' and " starts or end quotes (and is consumed). |
3104 | */ |
3105 | if (ch == '\'') { |
3106 | if (state == NONE) { |
3107 | state = SINGLE_QUOTES; |
3108 | goto next; |
3109 | } |
3110 | if (state == SINGLE_QUOTES) { |
3111 | state = NONE; |
3112 | goto next; |
3113 | } |
3114 | } |
3115 | if (ch == '"') { |
3116 | if (state == NONE) { |
3117 | state = DOUBLE_QUOTES; |
3118 | goto next; |
3119 | } |
3120 | if (state == DOUBLE_QUOTES) { |
3121 | state = NONE; |
3122 | goto next; |
3123 | } |
3124 | } |
3125 | |
3126 | /* |
3127 | * Otherwise add the character to the buffer. |
3128 | */ |
3129 | yylex_append1(&buf, &len, ch); |
3130 | |
3131 | skip: |
3132 | last = state; |
3133 | |
3134 | next: |
3135 | ch = yylex_getc(); |
3136 | } |
3137 | yylex_ungetc(ch); |
3138 | |
3139 | buf[len] = '\0'; |
3140 | log_debug("%s: %s" , __func__, buf); |
3141 | return (buf); |
3142 | |
3143 | error: |
3144 | free(buf); |
3145 | return (NULL); |
3146 | } |
3147 | |