-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd-parse.y
1706 lines (1509 loc) · 34.6 KB
/
cmd-parse.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $OpenBSD$ */
/*
* Copyright (c) 2019 Nicholas Marriott <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
%{
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include "tmux.h"
static int yylex(void);
static int yyparse(void);
static int printflike(1,2) yyerror(const char *, ...);
static char *yylex_token(int);
static char *yylex_format(void);
struct cmd_parse_scope {
int flag;
TAILQ_ENTRY (cmd_parse_scope) entry;
};
enum cmd_parse_argument_type {
CMD_PARSE_STRING,
CMD_PARSE_COMMANDS,
CMD_PARSE_PARSED_COMMANDS
};
struct cmd_parse_argument {
enum cmd_parse_argument_type type;
char *string;
struct cmd_parse_commands *commands;
struct cmd_list *cmdlist;
TAILQ_ENTRY(cmd_parse_argument) entry;
};
TAILQ_HEAD(cmd_parse_arguments, cmd_parse_argument);
struct cmd_parse_command {
u_int line;
struct cmd_parse_arguments arguments;
TAILQ_ENTRY(cmd_parse_command) entry;
};
TAILQ_HEAD(cmd_parse_commands, cmd_parse_command);
struct cmd_parse_state {
FILE *f;
const char *buf;
size_t len;
size_t off;
int condition;
int eol;
int eof;
struct cmd_parse_input *input;
u_int escapes;
char *error;
struct cmd_parse_commands *commands;
struct cmd_parse_scope *scope;
TAILQ_HEAD(, cmd_parse_scope) stack;
};
static struct cmd_parse_state parse_state;
static char *cmd_parse_get_error(const char *, u_int, const char *);
static void cmd_parse_free_command(struct cmd_parse_command *);
static struct cmd_parse_commands *cmd_parse_new_commands(void);
static void cmd_parse_free_commands(struct cmd_parse_commands *);
static void cmd_parse_build_commands(struct cmd_parse_commands *,
struct cmd_parse_input *, struct cmd_parse_result *);
static void cmd_parse_print_commands(struct cmd_parse_input *,
struct cmd_list *);
%}
%union
{
char *token;
struct cmd_parse_arguments *arguments;
struct cmd_parse_argument *argument;
int flag;
struct {
int flag;
struct cmd_parse_commands *commands;
} elif;
struct cmd_parse_commands *commands;
struct cmd_parse_command *command;
}
%token ERROR
%token HIDDEN
%token IF
%token ELSE
%token ELIF
%token ENDIF
%token <token> FORMAT TOKEN EQUALS
%type <token> expanded format
%type <arguments> arguments
%type <argument> argument
%type <flag> if_open if_elif
%type <elif> elif elif1
%type <commands> argument_statements statements statement
%type <commands> commands condition condition1
%type <command> command
%%
lines : /* empty */
| statements
{
struct cmd_parse_state *ps = &parse_state;
ps->commands = $1;
}
statements : statement '\n'
{
$$ = $1;
}
| statements statement '\n'
{
$$ = $1;
TAILQ_CONCAT($$, $2, entry);
free($2);
}
statement : /* empty */
{
$$ = xmalloc (sizeof *$$);
TAILQ_INIT($$);
}
| hidden_assignment
{
$$ = xmalloc (sizeof *$$);
TAILQ_INIT($$);
}
| condition
{
struct cmd_parse_state *ps = &parse_state;
if (ps->scope == NULL || ps->scope->flag)
$$ = $1;
else {
$$ = cmd_parse_new_commands();
cmd_parse_free_commands($1);
}
}
| commands
{
struct cmd_parse_state *ps = &parse_state;
if (ps->scope == NULL || ps->scope->flag)
$$ = $1;
else {
$$ = cmd_parse_new_commands();
cmd_parse_free_commands($1);
}
}
format : FORMAT
{
$$ = $1;
}
| TOKEN
{
$$ = $1;
}
expanded : format
{
struct cmd_parse_state *ps = &parse_state;
struct cmd_parse_input *pi = ps->input;
struct format_tree *ft;
struct client *c = pi->c;
struct cmd_find_state *fsp;
struct cmd_find_state fs;
int flags = FORMAT_NOJOBS;
if (cmd_find_valid_state(&pi->fs))
fsp = &pi->fs;
else {
cmd_find_from_client(&fs, c, 0);
fsp = &fs;
}
ft = format_create(NULL, pi->item, FORMAT_NONE, flags);
format_defaults(ft, c, fsp->s, fsp->wl, fsp->wp);
$$ = format_expand(ft, $1);
format_free(ft);
free($1);
}
optional_assignment : /* empty */
| assignment
assignment : EQUALS
{
struct cmd_parse_state *ps = &parse_state;
int flags = ps->input->flags;
if ((~flags & CMD_PARSE_PARSEONLY) &&
(ps->scope == NULL || ps->scope->flag))
environ_put(global_environ, $1, 0);
free($1);
}
hidden_assignment : HIDDEN EQUALS
{
struct cmd_parse_state *ps = &parse_state;
int flags = ps->input->flags;
if ((~flags & CMD_PARSE_PARSEONLY) &&
(ps->scope == NULL || ps->scope->flag))
environ_put(global_environ, $2, ENVIRON_HIDDEN);
free($2);
}
if_open : IF expanded
{
struct cmd_parse_state *ps = &parse_state;
struct cmd_parse_scope *scope;
scope = xmalloc(sizeof *scope);
$$ = scope->flag = format_true($2);
free($2);
if (ps->scope != NULL)
TAILQ_INSERT_HEAD(&ps->stack, ps->scope, entry);
ps->scope = scope;
}
if_else : ELSE
{
struct cmd_parse_state *ps = &parse_state;
struct cmd_parse_scope *scope;
scope = xmalloc(sizeof *scope);
scope->flag = !ps->scope->flag;
free(ps->scope);
ps->scope = scope;
}
if_elif : ELIF expanded
{
struct cmd_parse_state *ps = &parse_state;
struct cmd_parse_scope *scope;
scope = xmalloc(sizeof *scope);
$$ = scope->flag = format_true($2);
free($2);
free(ps->scope);
ps->scope = scope;
}
if_close : ENDIF
{
struct cmd_parse_state *ps = &parse_state;
free(ps->scope);
ps->scope = TAILQ_FIRST(&ps->stack);
if (ps->scope != NULL)
TAILQ_REMOVE(&ps->stack, ps->scope, entry);
}
condition : if_open '\n' statements if_close
{
if ($1)
$$ = $3;
else {
$$ = cmd_parse_new_commands();
cmd_parse_free_commands($3);
}
}
| if_open '\n' statements if_else '\n' statements if_close
{
if ($1) {
$$ = $3;
cmd_parse_free_commands($6);
} else {
$$ = $6;
cmd_parse_free_commands($3);
}
}
| if_open '\n' statements elif if_close
{
if ($1) {
$$ = $3;
cmd_parse_free_commands($4.commands);
} else if ($4.flag) {
$$ = $4.commands;
cmd_parse_free_commands($3);
} else {
$$ = cmd_parse_new_commands();
cmd_parse_free_commands($3);
cmd_parse_free_commands($4.commands);
}
}
| if_open '\n' statements elif if_else '\n' statements if_close
{
if ($1) {
$$ = $3;
cmd_parse_free_commands($4.commands);
cmd_parse_free_commands($7);
} else if ($4.flag) {
$$ = $4.commands;
cmd_parse_free_commands($3);
cmd_parse_free_commands($7);
} else {
$$ = $7;
cmd_parse_free_commands($3);
cmd_parse_free_commands($4.commands);
}
}
elif : if_elif '\n' statements
{
if ($1) {
$$.flag = 1;
$$.commands = $3;
} else {
$$.flag = 0;
$$.commands = cmd_parse_new_commands();
cmd_parse_free_commands($3);
}
}
| if_elif '\n' statements elif
{
if ($1) {
$$.flag = 1;
$$.commands = $3;
cmd_parse_free_commands($4.commands);
} else if ($4.flag) {
$$.flag = 1;
$$.commands = $4.commands;
cmd_parse_free_commands($3);
} else {
$$.flag = 0;
$$.commands = cmd_parse_new_commands();
cmd_parse_free_commands($3);
cmd_parse_free_commands($4.commands);
}
}
commands : command
{
struct cmd_parse_state *ps = &parse_state;
$$ = cmd_parse_new_commands();
if (!TAILQ_EMPTY(&$1->arguments) &&
(ps->scope == NULL || ps->scope->flag))
TAILQ_INSERT_TAIL($$, $1, entry);
else
cmd_parse_free_command($1);
}
| commands ';'
{
$$ = $1;
}
| commands ';' condition1
{
$$ = $1;
TAILQ_CONCAT($$, $3, entry);
free($3);
}
| commands ';' command
{
struct cmd_parse_state *ps = &parse_state;
if (!TAILQ_EMPTY(&$3->arguments) &&
(ps->scope == NULL || ps->scope->flag)) {
$$ = $1;
TAILQ_INSERT_TAIL($$, $3, entry);
} else {
$$ = cmd_parse_new_commands();
cmd_parse_free_commands($1);
cmd_parse_free_command($3);
}
}
| condition1
{
$$ = $1;
}
command : assignment
{
struct cmd_parse_state *ps = &parse_state;
$$ = xcalloc(1, sizeof *$$);
$$->line = ps->input->line;
TAILQ_INIT(&$$->arguments);
}
| optional_assignment TOKEN
{
struct cmd_parse_state *ps = &parse_state;
struct cmd_parse_argument *arg;
$$ = xcalloc(1, sizeof *$$);
$$->line = ps->input->line;
TAILQ_INIT(&$$->arguments);
arg = xcalloc(1, sizeof *arg);
arg->type = CMD_PARSE_STRING;
arg->string = $2;
TAILQ_INSERT_HEAD(&$$->arguments, arg, entry);
}
| optional_assignment TOKEN arguments
{
struct cmd_parse_state *ps = &parse_state;
struct cmd_parse_argument *arg;
$$ = xcalloc(1, sizeof *$$);
$$->line = ps->input->line;
TAILQ_INIT(&$$->arguments);
TAILQ_CONCAT(&$$->arguments, $3, entry);
free($3);
arg = xcalloc(1, sizeof *arg);
arg->type = CMD_PARSE_STRING;
arg->string = $2;
TAILQ_INSERT_HEAD(&$$->arguments, arg, entry);
}
condition1 : if_open commands if_close
{
if ($1)
$$ = $2;
else {
$$ = cmd_parse_new_commands();
cmd_parse_free_commands($2);
}
}
| if_open commands if_else commands if_close
{
if ($1) {
$$ = $2;
cmd_parse_free_commands($4);
} else {
$$ = $4;
cmd_parse_free_commands($2);
}
}
| if_open commands elif1 if_close
{
if ($1) {
$$ = $2;
cmd_parse_free_commands($3.commands);
} else if ($3.flag) {
$$ = $3.commands;
cmd_parse_free_commands($2);
} else {
$$ = cmd_parse_new_commands();
cmd_parse_free_commands($2);
cmd_parse_free_commands($3.commands);
}
}
| if_open commands elif1 if_else commands if_close
{
if ($1) {
$$ = $2;
cmd_parse_free_commands($3.commands);
cmd_parse_free_commands($5);
} else if ($3.flag) {
$$ = $3.commands;
cmd_parse_free_commands($2);
cmd_parse_free_commands($5);
} else {
$$ = $5;
cmd_parse_free_commands($2);
cmd_parse_free_commands($3.commands);
}
}
elif1 : if_elif commands
{
if ($1) {
$$.flag = 1;
$$.commands = $2;
} else {
$$.flag = 0;
$$.commands = cmd_parse_new_commands();
cmd_parse_free_commands($2);
}
}
| if_elif commands elif1
{
if ($1) {
$$.flag = 1;
$$.commands = $2;
cmd_parse_free_commands($3.commands);
} else if ($3.flag) {
$$.flag = 1;
$$.commands = $3.commands;
cmd_parse_free_commands($2);
} else {
$$.flag = 0;
$$.commands = cmd_parse_new_commands();
cmd_parse_free_commands($2);
cmd_parse_free_commands($3.commands);
}
}
arguments : argument
{
$$ = xcalloc(1, sizeof *$$);
TAILQ_INIT($$);
TAILQ_INSERT_HEAD($$, $1, entry);
}
| argument arguments
{
TAILQ_INSERT_HEAD($2, $1, entry);
$$ = $2;
}
argument : TOKEN
{
$$ = xcalloc(1, sizeof *$$);
$$->type = CMD_PARSE_STRING;
$$->string = $1;
}
| EQUALS
{
$$ = xcalloc(1, sizeof *$$);
$$->type = CMD_PARSE_STRING;
$$->string = $1;
}
| '{' argument_statements
{
$$ = xcalloc(1, sizeof *$$);
$$->type = CMD_PARSE_COMMANDS;
$$->commands = $2;
}
argument_statements : statement '}'
{
$$ = $1;
}
| statements statement '}'
{
$$ = $1;
TAILQ_CONCAT($$, $2, entry);
free($2);
}
%%
static char *
cmd_parse_get_error(const char *file, u_int line, const char *error)
{
char *s;
if (file == NULL)
s = xstrdup(error);
else
xasprintf(&s, "%s:%u: %s", file, line, error);
return (s);
}
static void
cmd_parse_print_commands(struct cmd_parse_input *pi, struct cmd_list *cmdlist)
{
char *s;
if (pi->item == NULL || (~pi->flags & CMD_PARSE_VERBOSE))
return;
s = cmd_list_print(cmdlist, 0);
if (pi->file != NULL)
cmdq_print(pi->item, "%s:%u: %s", pi->file, pi->line, s);
else
cmdq_print(pi->item, "%u: %s", pi->line, s);
free(s);
}
static void
cmd_parse_free_argument(struct cmd_parse_argument *arg)
{
switch (arg->type) {
case CMD_PARSE_STRING:
free(arg->string);
break;
case CMD_PARSE_COMMANDS:
cmd_parse_free_commands(arg->commands);
break;
case CMD_PARSE_PARSED_COMMANDS:
cmd_list_free(arg->cmdlist);
break;
}
free(arg);
}
static void
cmd_parse_free_arguments(struct cmd_parse_arguments *args)
{
struct cmd_parse_argument *arg, *arg1;
TAILQ_FOREACH_SAFE(arg, args, entry, arg1) {
TAILQ_REMOVE(args, arg, entry);
cmd_parse_free_argument(arg);
}
}
static void
cmd_parse_free_command(struct cmd_parse_command *cmd)
{
cmd_parse_free_arguments(&cmd->arguments);
free(cmd);
}
static struct cmd_parse_commands *
cmd_parse_new_commands(void)
{
struct cmd_parse_commands *cmds;
cmds = xmalloc(sizeof *cmds);
TAILQ_INIT(cmds);
return (cmds);
}
static void
cmd_parse_free_commands(struct cmd_parse_commands *cmds)
{
struct cmd_parse_command *cmd, *cmd1;
TAILQ_FOREACH_SAFE(cmd, cmds, entry, cmd1) {
TAILQ_REMOVE(cmds, cmd, entry);
cmd_parse_free_command(cmd);
}
free(cmds);
}
static struct cmd_parse_commands *
cmd_parse_run_parser(char **cause)
{
struct cmd_parse_state *ps = &parse_state;
struct cmd_parse_scope *scope, *scope1;
int retval;
ps->commands = NULL;
TAILQ_INIT(&ps->stack);
retval = yyparse();
TAILQ_FOREACH_SAFE(scope, &ps->stack, entry, scope1) {
TAILQ_REMOVE(&ps->stack, scope, entry);
free(scope);
}
if (retval != 0) {
*cause = ps->error;
return (NULL);
}
if (ps->commands == NULL)
return (cmd_parse_new_commands());
return (ps->commands);
}
static struct cmd_parse_commands *
cmd_parse_do_file(FILE *f, struct cmd_parse_input *pi, char **cause)
{
struct cmd_parse_state *ps = &parse_state;
memset(ps, 0, sizeof *ps);
ps->input = pi;
ps->f = f;
return (cmd_parse_run_parser(cause));
}
static struct cmd_parse_commands *
cmd_parse_do_buffer(const char *buf, size_t len, struct cmd_parse_input *pi,
char **cause)
{
struct cmd_parse_state *ps = &parse_state;
memset(ps, 0, sizeof *ps);
ps->input = pi;
ps->buf = buf;
ps->len = len;
return (cmd_parse_run_parser(cause));
}
static void
cmd_parse_log_commands(struct cmd_parse_commands *cmds, const char *prefix)
{
struct cmd_parse_command *cmd;
struct cmd_parse_argument *arg;
u_int i, j;
char *s;
i = 0;
TAILQ_FOREACH(cmd, cmds, entry) {
j = 0;
TAILQ_FOREACH(arg, &cmd->arguments, entry) {
switch (arg->type) {
case CMD_PARSE_STRING:
log_debug("%s %u:%u: %s", prefix, i, j,
arg->string);
break;
case CMD_PARSE_COMMANDS:
xasprintf(&s, "%s %u:%u", prefix, i, j);
cmd_parse_log_commands(arg->commands, s);
free(s);
break;
case CMD_PARSE_PARSED_COMMANDS:
s = cmd_list_print(arg->cmdlist, 0);
log_debug("%s %u:%u: %s", prefix, i, j, s);
free(s);
break;
}
j++;
}
i++;
}
}
static int
cmd_parse_expand_alias(struct cmd_parse_command *cmd,
struct cmd_parse_input *pi, struct cmd_parse_result *pr)
{
struct cmd_parse_argument *arg, *arg1, *first;
struct cmd_parse_commands *cmds;
struct cmd_parse_command *last;
char *alias, *name, *cause;
if (pi->flags & CMD_PARSE_NOALIAS)
return (0);
memset(pr, 0, sizeof *pr);
first = TAILQ_FIRST(&cmd->arguments);
if (first == NULL || first->type != CMD_PARSE_STRING) {
pr->status = CMD_PARSE_SUCCESS;
pr->cmdlist = cmd_list_new();
return (1);
}
name = first->string;
alias = cmd_get_alias(name);
if (alias == NULL)
return (0);
log_debug("%s: %u alias %s = %s", __func__, pi->line, name, alias);
cmds = cmd_parse_do_buffer(alias, strlen(alias), pi, &cause);
free(alias);
if (cmds == NULL) {
pr->status = CMD_PARSE_ERROR;
pr->error = cause;
return (1);
}
last = TAILQ_LAST(cmds, cmd_parse_commands);
if (last == NULL) {
pr->status = CMD_PARSE_SUCCESS;
pr->cmdlist = cmd_list_new();
return (1);
}
TAILQ_REMOVE(&cmd->arguments, first, entry);
cmd_parse_free_argument(first);
TAILQ_FOREACH_SAFE(arg, &cmd->arguments, entry, arg1) {
TAILQ_REMOVE(&cmd->arguments, arg, entry);
TAILQ_INSERT_TAIL(&last->arguments, arg, entry);
}
cmd_parse_log_commands(cmds, __func__);
pi->flags |= CMD_PARSE_NOALIAS;
cmd_parse_build_commands(cmds, pi, pr);
pi->flags &= ~CMD_PARSE_NOALIAS;
return (1);
}
static void
cmd_parse_build_command(struct cmd_parse_command *cmd,
struct cmd_parse_input *pi, struct cmd_parse_result *pr)
{
struct cmd_parse_argument *arg;
struct cmd *add;
char *cause;
struct args_value *values = NULL;
u_int count = 0, idx;
memset(pr, 0, sizeof *pr);
if (cmd_parse_expand_alias(cmd, pi, pr))
return;
TAILQ_FOREACH(arg, &cmd->arguments, entry) {
values = xrecallocarray(values, count, count + 1,
sizeof *values);
switch (arg->type) {
case CMD_PARSE_STRING:
values[count].type = ARGS_STRING;
values[count].string = xstrdup(arg->string);
break;
case CMD_PARSE_COMMANDS:
cmd_parse_build_commands(arg->commands, pi, pr);
if (pr->status != CMD_PARSE_SUCCESS)
goto out;
values[count].type = ARGS_COMMANDS;
values[count].cmdlist = pr->cmdlist;
break;
case CMD_PARSE_PARSED_COMMANDS:
values[count].type = ARGS_COMMANDS;
values[count].cmdlist = arg->cmdlist;
values[count].cmdlist->references++;
break;
}
count++;
}
add = cmd_parse(values, count, pi->file, pi->line, &cause);
if (add == NULL) {
pr->status = CMD_PARSE_ERROR;
pr->error = cmd_parse_get_error(pi->file, pi->line, cause);
free(cause);
goto out;
}
pr->status = CMD_PARSE_SUCCESS;
pr->cmdlist = cmd_list_new();
cmd_list_append(pr->cmdlist, add);
out:
for (idx = 0; idx < count; idx++)
args_free_value(&values[idx]);
free(values);
}
static void
cmd_parse_build_commands(struct cmd_parse_commands *cmds,
struct cmd_parse_input *pi, struct cmd_parse_result *pr)
{
struct cmd_parse_command *cmd;
u_int line = UINT_MAX;
struct cmd_list *current = NULL, *result;
char *s;
memset(pr, 0, sizeof *pr);
/* Check for an empty list. */
if (TAILQ_EMPTY(cmds)) {
pr->status = CMD_PARSE_SUCCESS;
pr->cmdlist = cmd_list_new();
return;
}
cmd_parse_log_commands(cmds, __func__);
/*
* Parse each command into a command list. Create a new command list
* for each line (unless the flag is set) so they get a new group (so
* the queue knows which ones to remove if a command fails when
* executed).
*/
result = cmd_list_new();
TAILQ_FOREACH(cmd, cmds, entry) {
if (((~pi->flags & CMD_PARSE_ONEGROUP) && cmd->line != line)) {
if (current != NULL) {
cmd_parse_print_commands(pi, current);
cmd_list_move(result, current);
cmd_list_free(current);
}
current = cmd_list_new();
}
if (current == NULL)
current = cmd_list_new();
line = pi->line = cmd->line;
cmd_parse_build_command(cmd, pi, pr);
if (pr->status != CMD_PARSE_SUCCESS) {
cmd_list_free(result);
cmd_list_free(current);
return;
}
cmd_list_append_all(current, pr->cmdlist);
cmd_list_free(pr->cmdlist);
}
if (current != NULL) {
cmd_parse_print_commands(pi, current);
cmd_list_move(result, current);
cmd_list_free(current);
}
s = cmd_list_print(result, 0);
log_debug("%s: %s", __func__, s);
free(s);
pr->status = CMD_PARSE_SUCCESS;
pr->cmdlist = result;
}
struct cmd_parse_result *
cmd_parse_from_file(FILE *f, struct cmd_parse_input *pi)
{
static struct cmd_parse_result pr;
struct cmd_parse_input input;
struct cmd_parse_commands *cmds;
char *cause;
if (pi == NULL) {
memset(&input, 0, sizeof input);
pi = &input;
}
memset(&pr, 0, sizeof pr);
cmds = cmd_parse_do_file(f, pi, &cause);
if (cmds == NULL) {
pr.status = CMD_PARSE_ERROR;
pr.error = cause;
return (&pr);
}
cmd_parse_build_commands(cmds, pi, &pr);
cmd_parse_free_commands(cmds);
return (&pr);
}
struct cmd_parse_result *
cmd_parse_from_string(const char *s, struct cmd_parse_input *pi)
{
struct cmd_parse_input input;
if (pi == NULL) {
memset(&input, 0, sizeof input);
pi = &input;
}
/*
* When parsing a string, put commands in one group even if there are
* multiple lines. This means { a \n b } is identical to "a ; b" when
* given as an argument to another command.
*/
pi->flags |= CMD_PARSE_ONEGROUP;
return (cmd_parse_from_buffer(s, strlen(s), pi));
}
enum cmd_parse_status
cmd_parse_and_insert(const char *s, struct cmd_parse_input *pi,
struct cmdq_item *after, struct cmdq_state *state, char **error)
{
struct cmd_parse_result *pr;
struct cmdq_item *item;
pr = cmd_parse_from_string(s, pi);
switch (pr->status) {
case CMD_PARSE_ERROR:
if (error != NULL)
*error = pr->error;
else
free(pr->error);
break;
case CMD_PARSE_SUCCESS:
item = cmdq_get_command(pr->cmdlist, state);
cmdq_insert_after(after, item);
cmd_list_free(pr->cmdlist);
break;
}
return (pr->status);
}
enum cmd_parse_status
cmd_parse_and_append(const char *s, struct cmd_parse_input *pi,
struct client *c, struct cmdq_state *state, char **error)
{
struct cmd_parse_result *pr;
struct cmdq_item *item;
pr = cmd_parse_from_string(s, pi);
switch (pr->status) {
case CMD_PARSE_ERROR:
if (error != NULL)
*error = pr->error;
else
free(pr->error);
break;