Coverage Report

Created: 2026-05-30 06:39

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