4 * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
33 static int yylex(void);
34 static int yyparse(void);
35 static int printflike
(1,2) yyerror(const char *, ...
);
37 static char *yylex_token
(int);
38 static char *yylex_format
(void);
40 struct cmd_parse_scope
{
42 TAILQ_ENTRY
(cmd_parse_scope
) entry
;
45 enum cmd_parse_argument_type
{
48 CMD_PARSE_PARSED_COMMANDS
51 struct cmd_parse_argument
{
52 enum cmd_parse_argument_type type
;
54 struct cmd_parse_commands
*commands
;
55 struct cmd_list
*cmdlist
;
57 TAILQ_ENTRY
(cmd_parse_argument
) entry
;
59 TAILQ_HEAD
(cmd_parse_arguments
, cmd_parse_argument
);
61 struct cmd_parse_command
{
63 struct cmd_parse_arguments arguments
;
65 TAILQ_ENTRY
(cmd_parse_command
) entry
;
67 TAILQ_HEAD
(cmd_parse_commands
, cmd_parse_command
);
69 struct cmd_parse_state
{
79 struct cmd_parse_input
*input
;
83 struct cmd_parse_commands
*commands
;
85 struct cmd_parse_scope
*scope
;
86 TAILQ_HEAD
(, cmd_parse_scope
) stack
;
88 static struct cmd_parse_state parse_state
;
90 static char *cmd_parse_get_error
(const char *, u_int
, const char *);
91 static void cmd_parse_free_command
(struct cmd_parse_command
*);
92 static struct cmd_parse_commands
*cmd_parse_new_commands
(void);
93 static void cmd_parse_free_commands
(struct cmd_parse_commands
*);
94 static void cmd_parse_build_commands
(struct cmd_parse_commands
*,
95 struct cmd_parse_input
*, struct cmd_parse_result
*);
96 static void cmd_parse_print_commands
(struct cmd_parse_input
*,
104 struct cmd_parse_arguments
*arguments
;
105 struct cmd_parse_argument
*argument
;
109 struct cmd_parse_commands
*commands
;
111 struct cmd_parse_commands
*commands
;
112 struct cmd_parse_command
*command
;
121 %token
<token
> FORMAT TOKEN EQUALS
123 %type
<token
> expanded format
124 %type
<arguments
> arguments
125 %type
<argument
> argument
126 %type
<flag
> if_open if_elif
127 %type
<elif
> elif elif1
128 %type
<commands
> argument_statements statements statement
129 %type
<commands
> commands condition condition1
130 %type
<command
> command
137 struct cmd_parse_state
*ps
= &parse_state
;
142 statements
: statement
'\n'
146 | statements statement
'\n'
149 TAILQ_CONCAT
($$
, $2, entry
);
153 statement
: /* empty */
155 $$
= xmalloc
(sizeof
*$$
);
160 $$
= xmalloc
(sizeof
*$$
);
165 struct cmd_parse_state
*ps
= &parse_state
;
167 if
(ps
->scope
== NULL || ps
->scope
->flag
)
170 $$
= cmd_parse_new_commands
();
171 cmd_parse_free_commands
($1);
176 struct cmd_parse_state
*ps
= &parse_state
;
178 if
(ps
->scope
== NULL || ps
->scope
->flag
)
181 $$
= cmd_parse_new_commands
();
182 cmd_parse_free_commands
($1);
197 struct cmd_parse_state
*ps
= &parse_state
;
198 struct cmd_parse_input
*pi
= ps
->input
;
199 struct format_tree
*ft
;
200 struct client
*c
= pi
->c
;
201 struct cmd_find_state
*fsp
;
202 struct cmd_find_state fs
;
203 int flags
= FORMAT_NOJOBS
;
205 if
(cmd_find_valid_state
(&pi
->fs
))
208 cmd_find_from_client
(&fs
, c
, 0);
211 ft
= format_create
(NULL
, pi
->item
, FORMAT_NONE
, flags
);
212 format_defaults
(ft
, c
, fsp
->s
, fsp
->wl
, fsp
->wp
);
214 $$
= format_expand
(ft
, $1);
219 optional_assignment
: /* empty */
224 struct cmd_parse_state
*ps
= &parse_state
;
225 int flags
= ps
->input
->flags
;
227 if
((~flags
& CMD_PARSE_PARSEONLY
) &&
228 (ps
->scope
== NULL || ps
->scope
->flag
))
229 environ_put
(global_environ
, $1, 0);
233 hidden_assignment
: HIDDEN EQUALS
235 struct cmd_parse_state
*ps
= &parse_state
;
236 int flags
= ps
->input
->flags
;
238 if
((~flags
& CMD_PARSE_PARSEONLY
) &&
239 (ps
->scope
== NULL || ps
->scope
->flag
))
240 environ_put
(global_environ
, $2, ENVIRON_HIDDEN
);
244 if_open
: IF expanded
246 struct cmd_parse_state
*ps
= &parse_state
;
247 struct cmd_parse_scope
*scope
;
249 scope
= xmalloc
(sizeof
*scope
);
250 $$
= scope
->flag
= format_true
($2);
253 if
(ps
->scope
!= NULL
)
254 TAILQ_INSERT_HEAD
(&ps
->stack
, ps
->scope
, entry
);
260 struct cmd_parse_state
*ps
= &parse_state
;
261 struct cmd_parse_scope
*scope
;
263 scope
= xmalloc
(sizeof
*scope
);
264 scope
->flag
= !ps
->scope
->flag
;
270 if_elif
: ELIF expanded
272 struct cmd_parse_state
*ps
= &parse_state
;
273 struct cmd_parse_scope
*scope
;
275 scope
= xmalloc
(sizeof
*scope
);
276 $$
= scope
->flag
= format_true
($2);
285 struct cmd_parse_state
*ps
= &parse_state
;
288 ps
->scope
= TAILQ_FIRST
(&ps
->stack
);
289 if
(ps
->scope
!= NULL
)
290 TAILQ_REMOVE
(&ps
->stack
, ps
->scope
, entry
);
293 condition
: if_open
'\n' statements if_close
298 $$
= cmd_parse_new_commands
();
299 cmd_parse_free_commands
($3);
302 | if_open
'\n' statements if_else
'\n' statements if_close
306 cmd_parse_free_commands
($6);
309 cmd_parse_free_commands
($3);
312 | if_open
'\n' statements elif if_close
316 cmd_parse_free_commands
($4.commands
);
317 } else if
($4.flag
) {
319 cmd_parse_free_commands
($3);
321 $$
= cmd_parse_new_commands
();
322 cmd_parse_free_commands
($3);
323 cmd_parse_free_commands
($4.commands
);
326 | if_open
'\n' statements elif if_else
'\n' statements if_close
330 cmd_parse_free_commands
($4.commands
);
331 cmd_parse_free_commands
($7);
332 } else if
($4.flag
) {
334 cmd_parse_free_commands
($3);
335 cmd_parse_free_commands
($7);
338 cmd_parse_free_commands
($3);
339 cmd_parse_free_commands
($4.commands
);
343 elif
: if_elif
'\n' statements
350 $$.commands
= cmd_parse_new_commands
();
351 cmd_parse_free_commands
($3);
354 | if_elif
'\n' statements elif
359 cmd_parse_free_commands
($4.commands
);
360 } else if
($4.flag
) {
362 $$.commands
= $4.commands
;
363 cmd_parse_free_commands
($3);
366 $$.commands
= cmd_parse_new_commands
();
367 cmd_parse_free_commands
($3);
368 cmd_parse_free_commands
($4.commands
);
374 struct cmd_parse_state
*ps
= &parse_state
;
376 $$
= cmd_parse_new_commands
();
377 if
(!TAILQ_EMPTY
(&$1->arguments
) &&
378 (ps
->scope
== NULL || ps
->scope
->flag
))
379 TAILQ_INSERT_TAIL
($$
, $1, entry
);
381 cmd_parse_free_command
($1);
387 | commands
';' condition1
390 TAILQ_CONCAT
($$
, $3, entry
);
393 | commands
';' command
395 struct cmd_parse_state
*ps
= &parse_state
;
397 if
(!TAILQ_EMPTY
(&$3->arguments
) &&
398 (ps
->scope
== NULL || ps
->scope
->flag
)) {
400 TAILQ_INSERT_TAIL
($$
, $3, entry
);
402 $$
= cmd_parse_new_commands
();
403 cmd_parse_free_commands
($1);
404 cmd_parse_free_command
($3);
414 struct cmd_parse_state
*ps
= &parse_state
;
416 $$
= xcalloc
(1, sizeof
*$$
);
417 $$
->line
= ps
->input
->line
;
418 TAILQ_INIT
(&$$
->arguments
);
420 | optional_assignment TOKEN
422 struct cmd_parse_state
*ps
= &parse_state
;
423 struct cmd_parse_argument
*arg
;
425 $$
= xcalloc
(1, sizeof
*$$
);
426 $$
->line
= ps
->input
->line
;
427 TAILQ_INIT
(&$$
->arguments
);
429 arg
= xcalloc
(1, sizeof
*arg
);
430 arg
->type
= CMD_PARSE_STRING
;
432 TAILQ_INSERT_HEAD
(&$$
->arguments
, arg
, entry
);
434 | optional_assignment TOKEN arguments
436 struct cmd_parse_state
*ps
= &parse_state
;
437 struct cmd_parse_argument
*arg
;
439 $$
= xcalloc
(1, sizeof
*$$
);
440 $$
->line
= ps
->input
->line
;
441 TAILQ_INIT
(&$$
->arguments
);
443 TAILQ_CONCAT
(&$$
->arguments
, $3, entry
);
446 arg
= xcalloc
(1, sizeof
*arg
);
447 arg
->type
= CMD_PARSE_STRING
;
449 TAILQ_INSERT_HEAD
(&$$
->arguments
, arg
, entry
);
452 condition1
: if_open commands if_close
457 $$
= cmd_parse_new_commands
();
458 cmd_parse_free_commands
($2);
461 | if_open commands if_else commands if_close
465 cmd_parse_free_commands
($4);
468 cmd_parse_free_commands
($2);
471 | if_open commands elif1 if_close
475 cmd_parse_free_commands
($3.commands
);
476 } else if
($3.flag
) {
478 cmd_parse_free_commands
($2);
480 $$
= cmd_parse_new_commands
();
481 cmd_parse_free_commands
($2);
482 cmd_parse_free_commands
($3.commands
);
485 | if_open commands elif1 if_else commands if_close
489 cmd_parse_free_commands
($3.commands
);
490 cmd_parse_free_commands
($5);
491 } else if
($3.flag
) {
493 cmd_parse_free_commands
($2);
494 cmd_parse_free_commands
($5);
497 cmd_parse_free_commands
($2);
498 cmd_parse_free_commands
($3.commands
);
502 elif1
: if_elif commands
509 $$.commands
= cmd_parse_new_commands
();
510 cmd_parse_free_commands
($2);
513 | if_elif commands elif1
518 cmd_parse_free_commands
($3.commands
);
519 } else if
($3.flag
) {
521 $$.commands
= $3.commands
;
522 cmd_parse_free_commands
($2);
525 $$.commands
= cmd_parse_new_commands
();
526 cmd_parse_free_commands
($2);
527 cmd_parse_free_commands
($3.commands
);
533 $$
= xcalloc
(1, sizeof
*$$
);
536 TAILQ_INSERT_HEAD
($$
, $1, entry
);
540 TAILQ_INSERT_HEAD
($2, $1, entry
);
546 $$
= xcalloc
(1, sizeof
*$$
);
547 $$
->type
= CMD_PARSE_STRING
;
552 $$
= xcalloc
(1, sizeof
*$$
);
553 $$
->type
= CMD_PARSE_STRING
;
556 |
'{' argument_statements
558 $$
= xcalloc
(1, sizeof
*$$
);
559 $$
->type
= CMD_PARSE_COMMANDS
;
563 argument_statements
: statement
'}'
567 | statements statement
'}'
570 TAILQ_CONCAT
($$
, $2, entry
);
577 cmd_parse_get_error
(const char *file
, u_int line
, const char *error)
584 xasprintf
(&s
, "%s:%u: %s", file
, line
, error);
589 cmd_parse_print_commands
(struct cmd_parse_input
*pi
, struct cmd_list
*cmdlist
)
593 if
(pi
->item
== NULL ||
(~pi
->flags
& CMD_PARSE_VERBOSE
))
595 s
= cmd_list_print
(cmdlist
, 0);
596 if
(pi
->file
!= NULL
)
597 cmdq_print
(pi
->item
, "%s:%u: %s", pi
->file
, pi
->line
, s
);
599 cmdq_print
(pi
->item
, "%u: %s", pi
->line
, s
);
604 cmd_parse_free_argument
(struct cmd_parse_argument
*arg
)
607 case CMD_PARSE_STRING
:
610 case CMD_PARSE_COMMANDS
:
611 cmd_parse_free_commands
(arg
->commands
);
613 case CMD_PARSE_PARSED_COMMANDS
:
614 cmd_list_free
(arg
->cmdlist
);
621 cmd_parse_free_arguments
(struct cmd_parse_arguments
*args
)
623 struct cmd_parse_argument
*arg
, *arg1
;
625 TAILQ_FOREACH_SAFE
(arg
, args
, entry
, arg1
) {
626 TAILQ_REMOVE
(args
, arg
, entry
);
627 cmd_parse_free_argument
(arg
);
632 cmd_parse_free_command
(struct cmd_parse_command
*cmd
)
634 cmd_parse_free_arguments
(&cmd
->arguments
);
638 static struct cmd_parse_commands
*
639 cmd_parse_new_commands
(void)
641 struct cmd_parse_commands
*cmds
;
643 cmds
= xmalloc
(sizeof
*cmds
);
649 cmd_parse_free_commands
(struct cmd_parse_commands
*cmds
)
651 struct cmd_parse_command
*cmd
, *cmd1
;
653 TAILQ_FOREACH_SAFE
(cmd
, cmds
, entry
, cmd1
) {
654 TAILQ_REMOVE
(cmds
, cmd
, entry
);
655 cmd_parse_free_command
(cmd
);
660 static struct cmd_parse_commands
*
661 cmd_parse_run_parser
(char **cause
)
663 struct cmd_parse_state
*ps
= &parse_state
;
664 struct cmd_parse_scope
*scope
, *scope1
;
668 TAILQ_INIT
(&ps
->stack
);
671 TAILQ_FOREACH_SAFE
(scope
, &ps
->stack
, entry
, scope1
) {
672 TAILQ_REMOVE
(&ps
->stack
, scope
, entry
);
680 if
(ps
->commands
== NULL
)
681 return
(cmd_parse_new_commands
());
682 return
(ps
->commands
);
685 static struct cmd_parse_commands
*
686 cmd_parse_do_file
(FILE *f
, struct cmd_parse_input
*pi
, char **cause
)
688 struct cmd_parse_state
*ps
= &parse_state
;
690 memset
(ps
, 0, sizeof
*ps
);
693 return
(cmd_parse_run_parser
(cause
));
696 static struct cmd_parse_commands
*
697 cmd_parse_do_buffer
(const char *buf
, size_t len
, struct cmd_parse_input
*pi
,
700 struct cmd_parse_state
*ps
= &parse_state
;
702 memset
(ps
, 0, sizeof
*ps
);
706 return
(cmd_parse_run_parser
(cause
));
710 cmd_parse_log_commands
(struct cmd_parse_commands
*cmds
, const char *prefix
)
712 struct cmd_parse_command
*cmd
;
713 struct cmd_parse_argument
*arg
;
718 TAILQ_FOREACH
(cmd
, cmds
, entry
) {
720 TAILQ_FOREACH
(arg
, &cmd
->arguments
, entry
) {
722 case CMD_PARSE_STRING
:
723 log_debug
("%s %u:%u: %s", prefix
, i
, j
,
726 case CMD_PARSE_COMMANDS
:
727 xasprintf
(&s
, "%s %u:%u", prefix
, i
, j
);
728 cmd_parse_log_commands
(arg
->commands
, s
);
731 case CMD_PARSE_PARSED_COMMANDS
:
732 s
= cmd_list_print
(arg
->cmdlist
, 0);
733 log_debug
("%s %u:%u: %s", prefix
, i
, j
, s
);
744 cmd_parse_expand_alias
(struct cmd_parse_command
*cmd
,
745 struct cmd_parse_input
*pi
, struct cmd_parse_result
*pr
)
747 struct cmd_parse_argument
*arg
, *arg1
, *first
;
748 struct cmd_parse_commands
*cmds
;
749 struct cmd_parse_command
*last
;
750 char *alias
, *name
, *cause
;
752 if
(pi
->flags
& CMD_PARSE_NOALIAS
)
754 memset
(pr
, 0, sizeof
*pr
);
756 first
= TAILQ_FIRST
(&cmd
->arguments
);
757 if
(first
== NULL || first
->type
!= CMD_PARSE_STRING
) {
758 pr
->status
= CMD_PARSE_SUCCESS
;
759 pr
->cmdlist
= cmd_list_new
();
762 name
= first
->string;
764 alias
= cmd_get_alias
(name
);
767 log_debug
("%s: %u alias %s = %s", __func__
, pi
->line
, name
, alias
);
769 cmds
= cmd_parse_do_buffer
(alias
, strlen
(alias
), pi
, &cause
);
772 pr
->status
= CMD_PARSE_ERROR
;
777 last
= TAILQ_LAST
(cmds
, cmd_parse_commands
);
779 pr
->status
= CMD_PARSE_SUCCESS
;
780 pr
->cmdlist
= cmd_list_new
();
784 TAILQ_REMOVE
(&cmd
->arguments
, first
, entry
);
785 cmd_parse_free_argument
(first
);
787 TAILQ_FOREACH_SAFE
(arg
, &cmd
->arguments
, entry
, arg1
) {
788 TAILQ_REMOVE
(&cmd
->arguments
, arg
, entry
);
789 TAILQ_INSERT_TAIL
(&last
->arguments
, arg
, entry
);
791 cmd_parse_log_commands
(cmds
, __func__
);
793 pi
->flags |
= CMD_PARSE_NOALIAS
;
794 cmd_parse_build_commands
(cmds
, pi
, pr
);
795 pi
->flags
&= ~CMD_PARSE_NOALIAS
;
800 cmd_parse_build_command
(struct cmd_parse_command
*cmd
,
801 struct cmd_parse_input
*pi
, struct cmd_parse_result
*pr
)
803 struct cmd_parse_argument
*arg
;
806 struct args_value
*values
= NULL
;
807 u_int count
= 0, idx
;
809 memset
(pr
, 0, sizeof
*pr
);
811 if
(cmd_parse_expand_alias
(cmd
, pi
, pr
))
814 TAILQ_FOREACH
(arg
, &cmd
->arguments
, entry
) {
815 values
= xrecallocarray
(values
, count
, count
+ 1,
818 case CMD_PARSE_STRING
:
819 values
[count
].type
= ARGS_STRING
;
820 values
[count
].
string = xstrdup
(arg
->string);
822 case CMD_PARSE_COMMANDS
:
823 cmd_parse_build_commands
(arg
->commands
, pi
, pr
);
824 if
(pr
->status
!= CMD_PARSE_SUCCESS
)
826 values
[count
].type
= ARGS_COMMANDS
;
827 values
[count
].cmdlist
= pr
->cmdlist
;
829 case CMD_PARSE_PARSED_COMMANDS
:
830 values
[count
].type
= ARGS_COMMANDS
;
831 values
[count
].cmdlist
= arg
->cmdlist
;
832 values
[count
].cmdlist
->references
++;
838 add
= cmd_parse
(values
, count
, pi
->file
, pi
->line
, &cause
);
840 pr
->status
= CMD_PARSE_ERROR
;
841 pr
->error = cmd_parse_get_error
(pi
->file
, pi
->line
, cause
);
845 pr
->status
= CMD_PARSE_SUCCESS
;
846 pr
->cmdlist
= cmd_list_new
();
847 cmd_list_append
(pr
->cmdlist
, add
);
850 for
(idx
= 0; idx
< count
; idx
++)
851 args_free_value
(&values
[idx
]);
856 cmd_parse_build_commands
(struct cmd_parse_commands
*cmds
,
857 struct cmd_parse_input
*pi
, struct cmd_parse_result
*pr
)
859 struct cmd_parse_command
*cmd
;
860 u_int line
= UINT_MAX
;
861 struct cmd_list
*current
= NULL
, *result
;
864 memset
(pr
, 0, sizeof
*pr
);
866 /* Check for an empty list. */
867 if
(TAILQ_EMPTY
(cmds
)) {
868 pr
->status
= CMD_PARSE_SUCCESS
;
869 pr
->cmdlist
= cmd_list_new
();
872 cmd_parse_log_commands
(cmds
, __func__
);
875 * Parse each command into a command list. Create a new command list
876 * for each line (unless the flag is set) so they get a new group (so
877 * the queue knows which ones to remove if a command fails when
880 result
= cmd_list_new
();
881 TAILQ_FOREACH
(cmd
, cmds
, entry
) {
882 if
(((~pi
->flags
& CMD_PARSE_ONEGROUP
) && cmd
->line
!= line
)) {
883 if
(current
!= NULL
) {
884 cmd_parse_print_commands
(pi
, current
);
885 cmd_list_move
(result
, current
);
886 cmd_list_free
(current
);
888 current
= cmd_list_new
();
891 current
= cmd_list_new
();
892 line
= pi
->line
= cmd
->line
;
894 cmd_parse_build_command
(cmd
, pi
, pr
);
895 if
(pr
->status
!= CMD_PARSE_SUCCESS
) {
896 cmd_list_free
(result
);
897 cmd_list_free
(current
);
900 cmd_list_append_all
(current
, pr
->cmdlist
);
901 cmd_list_free
(pr
->cmdlist
);
903 if
(current
!= NULL
) {
904 cmd_parse_print_commands
(pi
, current
);
905 cmd_list_move
(result
, current
);
906 cmd_list_free
(current
);
909 s
= cmd_list_print
(result
, 0);
910 log_debug
("%s: %s", __func__
, s
);
913 pr
->status
= CMD_PARSE_SUCCESS
;
914 pr
->cmdlist
= result
;
917 struct cmd_parse_result
*
918 cmd_parse_from_file
(FILE *f
, struct cmd_parse_input
*pi
)
920 static struct cmd_parse_result pr
;
921 struct cmd_parse_input input
;
922 struct cmd_parse_commands
*cmds
;
926 memset
(&input
, 0, sizeof input
);
929 memset
(&pr
, 0, sizeof pr
);
931 cmds
= cmd_parse_do_file
(f
, pi
, &cause
);
933 pr.status
= CMD_PARSE_ERROR
;
937 cmd_parse_build_commands
(cmds
, pi
, &pr
);
938 cmd_parse_free_commands
(cmds
);
943 struct cmd_parse_result
*
944 cmd_parse_from_string
(const char *s
, struct cmd_parse_input
*pi
)
946 struct cmd_parse_input input
;
949 memset
(&input
, 0, sizeof input
);
954 * When parsing a string, put commands in one group even if there are
955 * multiple lines. This means { a \n b } is identical to "a ; b" when
956 * given as an argument to another command.
958 pi
->flags |
= CMD_PARSE_ONEGROUP
;
959 return
(cmd_parse_from_buffer
(s
, strlen
(s
), pi
));
962 enum cmd_parse_status
963 cmd_parse_and_insert
(const char *s
, struct cmd_parse_input
*pi
,
964 struct cmdq_item
*after
, struct cmdq_state
*state
, char **error)
966 struct cmd_parse_result
*pr
;
967 struct cmdq_item
*item
;
969 pr
= cmd_parse_from_string
(s
, pi
);
970 switch
(pr
->status
) {
971 case CMD_PARSE_ERROR
:
977 case CMD_PARSE_SUCCESS
:
978 item
= cmdq_get_command
(pr
->cmdlist
, state
);
979 cmdq_insert_after
(after
, item
);
980 cmd_list_free
(pr
->cmdlist
);
986 enum cmd_parse_status
987 cmd_parse_and_append
(const char *s
, struct cmd_parse_input
*pi
,
988 struct client
*c
, struct cmdq_state
*state
, char **error)
990 struct cmd_parse_result
*pr
;
991 struct cmdq_item
*item
;
993 pr
= cmd_parse_from_string
(s
, pi
);
994 switch
(pr
->status
) {
995 case CMD_PARSE_ERROR
:
1001 case CMD_PARSE_SUCCESS
:
1002 item
= cmdq_get_command
(pr
->cmdlist
, state
);
1003 cmdq_append
(c
, item
);
1004 cmd_list_free
(pr
->cmdlist
);
1007 return
(pr
->status
);
1010 struct cmd_parse_result
*
1011 cmd_parse_from_buffer
(const void *buf
, size_t len
, struct cmd_parse_input
*pi
)
1013 static struct cmd_parse_result pr
;
1014 struct cmd_parse_input input
;
1015 struct cmd_parse_commands
*cmds
;
1019 memset
(&input
, 0, sizeof input
);
1022 memset
(&pr
, 0, sizeof pr
);
1025 pr.status
= CMD_PARSE_SUCCESS
;
1026 pr.cmdlist
= cmd_list_new
();
1030 cmds
= cmd_parse_do_buffer
(buf
, len
, pi
, &cause
);
1032 pr.status
= CMD_PARSE_ERROR
;
1036 cmd_parse_build_commands
(cmds
, pi
, &pr
);
1037 cmd_parse_free_commands
(cmds
);
1041 struct cmd_parse_result
*
1042 cmd_parse_from_arguments
(struct args_value
*values
, u_int count
,
1043 struct cmd_parse_input
*pi
)
1045 static struct cmd_parse_result pr
;
1046 struct cmd_parse_input input
;
1047 struct cmd_parse_commands
*cmds
;
1048 struct cmd_parse_command
*cmd
;
1049 struct cmd_parse_argument
*arg
;
1056 * The commands are already split up into arguments, so just separate
1057 * into a set of commands by ';'.
1061 memset
(&input
, 0, sizeof input
);
1064 memset
(&pr
, 0, sizeof pr
);
1066 cmds
= cmd_parse_new_commands
();
1068 cmd
= xcalloc
(1, sizeof
*cmd
);
1069 cmd
->line
= pi
->line
;
1070 TAILQ_INIT
(&cmd
->arguments
);
1072 for
(i
= 0; i
< count
; i
++) {
1074 if
(values
[i
].type
== ARGS_STRING
) {
1075 copy
= xstrdup
(values
[i
].
string);
1076 size
= strlen
(copy
);
1077 if
(size
!= 0 && copy
[size
- 1] == ';') {
1078 copy
[--size
] = '\0';
1079 if
(size
> 0 && copy
[size
- 1] == '\\')
1080 copy
[size
- 1] = ';';
1084 if
(!end || size
!= 0) {
1085 arg
= xcalloc
(1, sizeof
*arg
);
1086 arg
->type
= CMD_PARSE_STRING
;
1088 TAILQ_INSERT_TAIL
(&cmd
->arguments
, arg
, entry
);
1090 } else if
(values
[i
].type
== ARGS_COMMANDS
) {
1091 arg
= xcalloc
(1, sizeof
*arg
);
1092 arg
->type
= CMD_PARSE_PARSED_COMMANDS
;
1093 arg
->cmdlist
= values
[i
].cmdlist
;
1094 arg
->cmdlist
->references
++;
1095 TAILQ_INSERT_TAIL
(&cmd
->arguments
, arg
, entry
);
1097 fatalx
("unknown argument type");
1099 TAILQ_INSERT_TAIL
(cmds
, cmd
, entry
);
1100 cmd
= xcalloc
(1, sizeof
*cmd
);
1101 cmd
->line
= pi
->line
;
1102 TAILQ_INIT
(&cmd
->arguments
);
1105 if
(!TAILQ_EMPTY
(&cmd
->arguments
))
1106 TAILQ_INSERT_TAIL
(cmds
, cmd
, entry
);
1110 cmd_parse_build_commands
(cmds
, pi
, &pr
);
1111 cmd_parse_free_commands
(cmds
);
1115 static int printflike
(1, 2)
1116 yyerror(const char *fmt
, ...
)
1118 struct cmd_parse_state
*ps
= &parse_state
;
1119 struct cmd_parse_input
*pi
= ps
->input
;
1123 if
(ps
->error != NULL
)
1127 xvasprintf
(&error, fmt
, ap
);
1130 ps
->error = cmd_parse_get_error
(pi
->file
, pi
->line
, error);
1136 yylex_is_var
(char ch
, int first
)
1140 if
(first
&& isdigit
((u_char
)ch
))
1142 return
(isalnum
((u_char
)ch
) || ch
== '_');
1146 yylex_append
(char **buf
, size_t *len
, const char *add
, size_t addlen
)
1148 if
(addlen
> SIZE_MAX
- 1 ||
*len
> SIZE_MAX
- 1 - addlen
)
1149 fatalx
("buffer is too big");
1150 *buf
= xrealloc
(*buf
, (*len
) + 1 + addlen
);
1151 memcpy
((*buf
) + *len
, add
, addlen
);
1156 yylex_append1
(char **buf
, size_t *len
, char add
)
1158 yylex_append
(buf
, len
, &add
, 1);
1164 struct cmd_parse_state
*ps
= &parse_state
;
1170 if
(ps
->off
== ps
->len
)
1173 ch
= ps
->buf
[ps
->off
++];
1179 yylex_ungetc
(int ch
)
1181 struct cmd_parse_state
*ps
= &parse_state
;
1185 else if
(ps
->off
> 0 && ch
!= EOF
)
1192 struct cmd_parse_state
*ps
= &parse_state
;
1195 if
(ps
->escapes
!= 0) {
1205 if
(ch
== '\n' && (ps
->escapes %
2) == 1) {
1211 if
(ps
->escapes
!= 0) {
1221 yylex_get_word
(int ch
)
1230 yylex_append1
(&buf
, &len
, ch
);
1231 while
((ch
= yylex_getc
()) != EOF
&& strchr
(" \t\n", ch
) == NULL
);
1235 log_debug
("%s: %s", __func__
, buf
);
1242 struct cmd_parse_state
*ps
= &parse_state
;
1244 int ch
, next
, condition
;
1250 condition
= ps
->condition
;
1258 * Ensure every file or string is terminated by a
1259 * newline. This keeps the parser simpler and avoids
1260 * having to add a newline to each string.
1268 if
(ch
== ' ' || ch
== '\t') {
1270 * Ignore whitespace.
1277 * End of line. Update the line number.
1283 if
(ch
== ';' || ch
== '{' || ch
== '}') {
1285 * A semicolon or { or } is itself.
1292 * #{ after a condition opens a format; anything else
1293 * is a comment, ignore up to the end of the line.
1295 next
= yylex_getc
();
1296 if
(condition
&& next
== '{') {
1297 yylval.token
= yylex_format
();
1298 if
(yylval.token
== NULL
)
1302 while
(next
!= '\n' && next
!= EOF
)
1303 next
= yylex_getc
();
1313 * % is a condition unless it is all % or all numbers,
1314 * then it is a token.
1316 yylval.token
= yylex_get_word
('%');
1317 for
(cp
= yylval.token
; *cp
!= '\0'; cp
++) {
1318 if
(*cp
!= '%' && !isdigit
((u_char
)*cp
))
1324 if
(strcmp
(yylval.token
, "%hidden") == 0) {
1328 if
(strcmp
(yylval.token
, "%if") == 0) {
1332 if
(strcmp
(yylval.token
, "%else") == 0) {
1336 if
(strcmp
(yylval.token
, "%elif") == 0) {
1340 if
(strcmp
(yylval.token
, "%endif") == 0) {
1349 * Otherwise this is a token.
1351 token
= yylex_token
(ch
);
1354 yylval.token
= token
;
1356 if
(strchr
(token
, '=') != NULL
&& yylex_is_var
(*token
, 1)) {
1357 for
(cp
= token
+ 1; *cp
!= '='; cp
++) {
1358 if
(!yylex_is_var
(*cp
, 0))
1374 int ch
, brackets
= 1;
1379 yylex_append
(&buf
, &len
, "#{", 2);
1381 if
((ch
= yylex_getc
()) == EOF || ch
== '\n')
1384 if
((ch
= yylex_getc
()) == EOF || ch
== '\n')
1388 yylex_append1
(&buf
, &len
, '#');
1389 } else if
(ch
== '}') {
1390 if
(brackets
!= 0 && --brackets
== 0) {
1391 yylex_append1
(&buf
, &len
, ch
);
1395 yylex_append1
(&buf
, &len
, ch
);
1401 log_debug
("%s: %s", __func__
, buf
);
1410 yylex_token_escape
(char **buf
, size_t *len
)
1412 int ch
, type
, o2
, o3
, mlen
;
1414 char s
[9], m
[MB_LEN_MAX
];
1418 if
(ch
>= '4' && ch
<= '7') {
1419 yyerror("invalid octal escape");
1422 if
(ch
>= '0' && ch
<= '3') {
1424 if
(o2
>= '0' && o2
<= '7') {
1426 if
(o3
>= '0' && o3
<= '7') {
1427 ch
= 64 * (ch
- '0') +
1430 yylex_append1
(buf
, len
, ch
);
1434 yyerror("invalid octal escape");
1478 yylex_append1
(buf
, len
, ch
);
1482 for
(i
= 0; i
< size
; i
++) {
1484 if
(ch
== EOF || ch
== '\n')
1486 if
(!isxdigit
((u_char
)ch
)) {
1487 yyerror("invalid \\%c argument", type
);
1494 if
((size
== 4 && sscanf
(s
, "%4x", &tmp
) != 1) ||
1495 (size
== 8 && sscanf
(s
, "%8x", &tmp
) != 1)) {
1496 yyerror("invalid \\%c argument", type
);
1499 mlen
= wctomb
(m
, tmp
);
1500 if
(mlen
<= 0 || mlen
> (int)sizeof m
) {
1501 yyerror("invalid \\%c argument", type
);
1504 yylex_append
(buf
, len
, m
, mlen
);
1509 yylex_token_variable
(char **buf
, size_t *len
)
1511 struct environ_entry
*envent
;
1512 int ch
, brackets
= 0;
1523 if
(!yylex_is_var
(ch
, 1)) {
1524 yylex_append1
(buf
, len
, '$');
1528 name
[namelen
++] = ch
;
1533 if
(brackets
&& ch
== '}')
1535 if
(ch
== EOF ||
!yylex_is_var
(ch
, 0)) {
1540 yyerror("invalid environment variable");
1543 if
(namelen
== (sizeof name
) - 2) {
1544 yyerror("environment variable is too long");
1547 name
[namelen
++] = ch
;
1549 name
[namelen
] = '\0';
1551 envent
= environ_find
(global_environ
, name
);
1552 if
(envent
!= NULL
&& envent
->value
!= NULL
) {
1553 value
= envent
->value
;
1554 log_debug
("%s: %s -> %s", __func__
, name
, value
);
1555 yylex_append
(buf
, len
, value
, strlen
(value
));
1561 yylex_token_tilde
(char **buf
, size_t *len
)
1563 struct environ_entry
*envent
;
1568 const char *home
= NULL
;
1572 if
(ch
== EOF || strchr
("/ \t\n\"'", ch
) != NULL
) {
1576 if
(namelen
== (sizeof name
) - 2) {
1577 yyerror("user name is too long");
1580 name
[namelen
++] = ch
;
1582 name
[namelen
] = '\0';
1584 if
(*name
== '\0') {
1585 envent
= environ_find
(global_environ
, "HOME");
1586 if
(envent
!= NULL
&& *envent
->value
!= '\0')
1587 home
= envent
->value
;
1588 else if
((pw
= getpwuid
(getuid
())) != NULL
)
1591 if
((pw
= getpwnam
(name
)) != NULL
)
1597 log_debug
("%s: ~%s -> %s", __func__
, name
, home
);
1598 yylex_append
(buf
, len
, home
, strlen
(home
));
1610 SINGLE_QUOTES
} state
= NONE
, last
= START
;
1616 /* EOF or \n are always the end of the token. */
1617 if
(ch
== EOF ||
(state
== NONE
&& ch
== '\n'))
1620 /* Whitespace or ; or } ends a token unless inside quotes. */
1621 if
((ch
== ' ' || ch
== '\t' || ch
== ';' || ch
== '}') &&
1626 * Spaces and comments inside quotes after \n are removed but
1629 if
(ch
== '\n' && state
!= NONE
) {
1630 yylex_append1
(&buf
, &len
, '\n');
1631 while
((ch
= yylex_getc
()) == ' ' || ch
== '\t')
1636 if
(strchr
(",#{}:", ch
) != NULL
) {
1640 while
((ch
= yylex_getc
()) != '\n' && ch
!= EOF
)
1646 /* \ ~ and $ are expanded except in single quotes. */
1647 if
(ch
== '\\' && state
!= SINGLE_QUOTES
) {
1648 if
(!yylex_token_escape
(&buf
, &len
))
1652 if
(ch
== '~' && last
!= state
&& state
!= SINGLE_QUOTES
) {
1653 if
(!yylex_token_tilde
(&buf
, &len
))
1657 if
(ch
== '$' && state
!= SINGLE_QUOTES
) {
1658 if
(!yylex_token_variable
(&buf
, &len
))
1662 if
(ch
== '}' && state
== NONE
)
1663 goto
error; /* unmatched (matched ones were handled) */
1665 /* ' and " starts or end quotes (and is consumed). */
1667 if
(state
== NONE
) {
1668 state
= SINGLE_QUOTES
;
1671 if
(state
== SINGLE_QUOTES
) {
1677 if
(state
== NONE
) {
1678 state
= DOUBLE_QUOTES
;
1681 if
(state
== DOUBLE_QUOTES
) {
1687 /* Otherwise add the character to the buffer. */
1688 yylex_append1
(&buf
, &len
, ch
);
1699 log_debug
("%s: %s", __func__
, buf
);