4 * Copyright (c) 2010 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.
19 #include <sys/types.h>
29 * Manipulate command arguments.
32 /* List of argument values. */
33 TAILQ_HEAD(args_values
, args_value
);
35 /* Single arguments flag. */
38 struct args_values values
;
42 #define ARGS_ENTRY_OPTIONAL_VALUE 0x1
44 RB_ENTRY(args_entry
) entry
;
47 /* Parsed argument flags and values. */
49 struct args_tree tree
;
51 struct args_value
*values
;
54 /* Prepared command state. */
55 struct args_command_state
{
56 struct cmd_list
*cmdlist
;
58 struct cmd_parse_input pi
;
61 static struct args_entry
*args_find(struct args
*, u_char
);
63 static int args_cmp(struct args_entry
*, struct args_entry
*);
64 RB_GENERATE_STATIC(args_tree
, args_entry
, entry
, args_cmp
);
66 /* Arguments tree comparison function. */
68 args_cmp(struct args_entry
*a1
, struct args_entry
*a2
)
70 return (a1
->flag
- a2
->flag
);
73 /* Find a flag in the arguments tree. */
74 static struct args_entry
*
75 args_find(struct args
*args
, u_char flag
)
77 struct args_entry entry
;
80 return (RB_FIND(args_tree
, &args
->tree
, &entry
));
85 args_copy_value(struct args_value
*to
, struct args_value
*from
)
87 to
->type
= from
->type
;
92 to
->cmdlist
= from
->cmdlist
;
93 to
->cmdlist
->references
++;
96 to
->string
= xstrdup(from
->string
);
101 /* Get value as string. */
103 args_value_as_string(struct args_value
*value
)
105 switch (value
->type
) {
109 if (value
->cached
== NULL
)
110 value
->cached
= cmd_list_print(value
->cmdlist
, 0);
111 return (value
->cached
);
113 return (value
->string
);
115 fatalx("unexpected argument type");
118 /* Create an empty arguments set. */
124 args
= xcalloc(1, sizeof *args
);
125 RB_INIT(&args
->tree
);
129 /* Parse a single flag. */
131 args_parse_flag_argument(struct args_value
*values
, u_int count
, char **cause
,
132 struct args
*args
, u_int
*i
, const char *string
, int flag
,
133 int optional_argument
)
135 struct args_value
*argument
, *new;
138 new = xcalloc(1, sizeof *new);
139 if (*string
!= '\0') {
140 new->type
= ARGS_STRING
;
141 new->string
= xstrdup(string
);
148 argument
= &values
[*i
];
149 if (argument
->type
!= ARGS_STRING
) {
150 xasprintf(cause
, "-%c argument must be a string", flag
);
153 if (argument
->string
[0] == '-')
156 if (argument
== NULL
) {
157 if (optional_argument
) {
158 log_debug("%s: -%c (optional)", __func__
, flag
);
159 args_set(args
, flag
, NULL
, ARGS_ENTRY_OPTIONAL_VALUE
);
160 return (0); /* either - or end */
162 xasprintf(cause
, "-%c expects an argument", flag
);
165 args_copy_value(new, argument
);
169 s
= args_value_as_string(new);
170 log_debug("%s: -%c = %s", __func__
, flag
, s
);
171 args_set(args
, flag
, new, 0);
175 /* Parse flags argument. */
177 args_parse_flags(const struct args_parse
*parse
, struct args_value
*values
,
178 u_int count
, char **cause
, struct args
*args
, int *i
)
180 struct args_value
*value
;
182 const char *found
, *string
;
183 int optional_argument
;
186 if (value
->type
!= ARGS_STRING
)
189 string
= value
->string
;
190 log_debug("%s: next %s", __func__
, string
);
191 if (*string
++ != '-' || *string
== '\0')
194 if (string
[0] == '-' && string
[1] == '\0')
203 if (!isalnum(flag
)) {
204 xasprintf(cause
, "invalid flag -%c", flag
);
208 found
= strchr(parse
->template, flag
);
210 xasprintf(cause
, "unknown flag -%c", flag
);
213 if (*++found
!= ':') {
214 log_debug("%s: -%c", __func__
, flag
);
215 args_set(args
, flag
, NULL
, 0);
218 optional_argument
= (*found
== ':');
219 return (args_parse_flag_argument(values
, count
, cause
, args
, i
,
220 string
, flag
, optional_argument
));
224 /* Parse arguments into a new argument set. */
226 args_parse(const struct args_parse
*parse
, struct args_value
*values
,
227 u_int count
, char **cause
)
231 enum args_parse_type type
;
232 struct args_value
*value
, *new;
237 return (args_create());
239 args
= args_create();
240 for (i
= 1; i
< count
; /* nothing */) {
241 stop
= args_parse_flags(parse
, values
, count
, cause
, args
, &i
);
249 log_debug("%s: flags end at %u of %u", __func__
, i
, count
);
251 for (/* nothing */; i
< count
; i
++) {
254 s
= args_value_as_string(value
);
255 log_debug("%s: %u = %s (type %d)", __func__
, i
, s
,
258 if (parse
->cb
!= NULL
) {
259 type
= parse
->cb(args
, args
->count
, cause
);
260 if (type
== ARGS_PARSE_INVALID
) {
265 type
= ARGS_PARSE_STRING
;
267 args
->values
= xrecallocarray(args
->values
,
268 args
->count
, args
->count
+ 1, sizeof *args
->values
);
269 new = &args
->values
[args
->count
++];
272 case ARGS_PARSE_INVALID
:
273 fatalx("unexpected argument type");
274 case ARGS_PARSE_STRING
:
275 if (value
->type
!= ARGS_STRING
) {
277 "argument %u must be \"string\"",
282 args_copy_value(new, value
);
284 case ARGS_PARSE_COMMANDS_OR_STRING
:
285 args_copy_value(new, value
);
287 case ARGS_PARSE_COMMANDS
:
288 if (value
->type
!= ARGS_COMMANDS
) {
290 "argument %u must be { commands }",
295 args_copy_value(new, value
);
301 if (parse
->lower
!= -1 && args
->count
< (u_int
)parse
->lower
) {
303 "too few arguments (need at least %u)",
308 if (parse
->upper
!= -1 && args
->count
> (u_int
)parse
->upper
) {
310 "too many arguments (need at most %u)",
318 /* Copy and expand a value. */
320 args_copy_copy_value(struct args_value
*to
, struct args_value
*from
, int argc
,
326 to
->type
= from
->type
;
327 switch (from
->type
) {
331 expanded
= xstrdup(from
->string
);
332 for (i
= 0; i
< argc
; i
++) {
333 s
= cmd_template_replace(expanded
, argv
[i
], i
+ 1);
337 to
->string
= expanded
;
340 to
->cmdlist
= cmd_list_copy(from
->cmdlist
, argc
, argv
);
345 /* Copy an arguments set. */
347 args_copy(struct args
*args
, int argc
, char **argv
)
349 struct args
*new_args
;
350 struct args_entry
*entry
;
351 struct args_value
*value
, *new_value
;
354 cmd_log_argv(argc
, argv
, "%s", __func__
);
356 new_args
= args_create();
357 RB_FOREACH(entry
, args_tree
, &args
->tree
) {
358 if (TAILQ_EMPTY(&entry
->values
)) {
359 for (i
= 0; i
< entry
->count
; i
++)
360 args_set(new_args
, entry
->flag
, NULL
, 0);
363 TAILQ_FOREACH(value
, &entry
->values
, entry
) {
364 new_value
= xcalloc(1, sizeof *new_value
);
365 args_copy_copy_value(new_value
, value
, argc
, argv
);
366 args_set(new_args
, entry
->flag
, new_value
, 0);
369 if (args
->count
== 0)
371 new_args
->count
= args
->count
;
372 new_args
->values
= xcalloc(args
->count
, sizeof *new_args
->values
);
373 for (i
= 0; i
< args
->count
; i
++) {
374 new_value
= &new_args
->values
[i
];
375 args_copy_copy_value(new_value
, &args
->values
[i
], argc
, argv
);
382 args_free_value(struct args_value
*value
)
384 switch (value
->type
) {
391 cmd_list_free(value
->cmdlist
);
399 args_free_values(struct args_value
*values
, u_int count
)
403 for (i
= 0; i
< count
; i
++)
404 args_free_value(&values
[i
]);
407 /* Free an arguments set. */
409 args_free(struct args
*args
)
411 struct args_entry
*entry
;
412 struct args_entry
*entry1
;
413 struct args_value
*value
;
414 struct args_value
*value1
;
416 args_free_values(args
->values
, args
->count
);
419 RB_FOREACH_SAFE(entry
, args_tree
, &args
->tree
, entry1
) {
420 RB_REMOVE(args_tree
, &args
->tree
, entry
);
421 TAILQ_FOREACH_SAFE(value
, &entry
->values
, entry
, value1
) {
422 TAILQ_REMOVE(&entry
->values
, value
, entry
);
423 args_free_value(value
);
432 /* Convert arguments to vector. */
434 args_to_vector(struct args
*args
, int *argc
, char ***argv
)
442 for (i
= 0; i
< args
->count
; i
++) {
443 switch (args
->values
[i
].type
) {
447 cmd_append_argv(argc
, argv
, args
->values
[i
].string
);
450 s
= cmd_list_print(args
->values
[i
].cmdlist
, 0);
451 cmd_append_argv(argc
, argv
, s
);
458 /* Convert arguments from vector. */
460 args_from_vector(int argc
, char **argv
)
462 struct args_value
*values
;
465 values
= xcalloc(argc
, sizeof *values
);
466 for (i
= 0; i
< argc
; i
++) {
467 values
[i
].type
= ARGS_STRING
;
468 values
[i
].string
= xstrdup(argv
[i
]);
474 static void printflike(3, 4)
475 args_print_add(char **buf
, size_t *len
, const char *fmt
, ...)
482 slen
= xvasprintf(&s
, fmt
, ap
);
486 *buf
= xrealloc(*buf
, *len
);
488 strlcat(*buf
, s
, *len
);
492 /* Add value to string. */
494 args_print_add_value(char **buf
, size_t *len
, struct args_value
*value
)
496 char *expanded
= NULL
;
499 args_print_add(buf
, len
, " ");
501 switch (value
->type
) {
505 expanded
= cmd_list_print(value
->cmdlist
, 0);
506 args_print_add(buf
, len
, "{ %s }", expanded
);
509 expanded
= args_escape(value
->string
);
510 args_print_add(buf
, len
, "%s", expanded
);
516 /* Print a set of arguments. */
518 args_print(struct args
*args
)
523 struct args_entry
*entry
;
524 struct args_entry
*last
= NULL
;
525 struct args_value
*value
;
528 buf
= xcalloc(1, len
);
530 /* Process the flags first. */
531 RB_FOREACH(entry
, args_tree
, &args
->tree
) {
532 if (entry
->flags
& ARGS_ENTRY_OPTIONAL_VALUE
)
534 if (!TAILQ_EMPTY(&entry
->values
))
538 args_print_add(&buf
, &len
, "-");
539 for (j
= 0; j
< entry
->count
; j
++)
540 args_print_add(&buf
, &len
, "%c", entry
->flag
);
543 /* Then the flags with arguments. */
544 RB_FOREACH(entry
, args_tree
, &args
->tree
) {
545 if (entry
->flags
& ARGS_ENTRY_OPTIONAL_VALUE
) {
547 args_print_add(&buf
, &len
, " -%c", entry
->flag
);
549 args_print_add(&buf
, &len
, "-%c", entry
->flag
);
553 if (TAILQ_EMPTY(&entry
->values
))
555 TAILQ_FOREACH(value
, &entry
->values
, entry
) {
557 args_print_add(&buf
, &len
, " -%c", entry
->flag
);
559 args_print_add(&buf
, &len
, "-%c", entry
->flag
);
560 args_print_add_value(&buf
, &len
, value
);
564 if (last
&& (last
->flags
& ARGS_ENTRY_OPTIONAL_VALUE
))
565 args_print_add(&buf
, &len
, " --");
567 /* And finally the argument vector. */
568 for (i
= 0; i
< args
->count
; i
++)
569 args_print_add_value(&buf
, &len
, &args
->values
[i
]);
574 /* Escape an argument. */
576 args_escape(const char *s
)
578 static const char dquoted
[] = " #';${}%";
579 static const char squoted
[] = " \"";
580 char *escaped
, *result
;
581 int flags
, quotes
= 0;
584 xasprintf(&result
, "''");
587 if (s
[strcspn(s
, dquoted
)] != '\0')
589 else if (s
[strcspn(s
, squoted
)] != '\0')
594 (quotes
!= 0 || s
[0] == '~')) {
595 xasprintf(&escaped
, "\\%c", s
[0]);
599 flags
= VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
;
602 utf8_stravis(&escaped
, s
, flags
);
605 xasprintf(&result
, "'%s'", escaped
);
606 else if (quotes
== '"') {
608 xasprintf(&result
, "\"\\%s\"", escaped
);
610 xasprintf(&result
, "\"%s\"", escaped
);
613 xasprintf(&result
, "\\%s", escaped
);
615 result
= xstrdup(escaped
);
621 /* Return if an argument is present. */
623 args_has(struct args
*args
, u_char flag
)
625 struct args_entry
*entry
;
627 entry
= args_find(args
, flag
);
630 return (entry
->count
);
633 /* Set argument value in the arguments tree. */
635 args_set(struct args
*args
, u_char flag
, struct args_value
*value
, int flags
)
637 struct args_entry
*entry
;
639 entry
= args_find(args
, flag
);
641 entry
= xcalloc(1, sizeof *entry
);
644 entry
->flags
= flags
;
645 TAILQ_INIT(&entry
->values
);
646 RB_INSERT(args_tree
, &args
->tree
, entry
);
649 if (value
!= NULL
&& value
->type
!= ARGS_NONE
)
650 TAILQ_INSERT_TAIL(&entry
->values
, value
, entry
);
653 /* Get argument value. Will be NULL if it isn't present. */
655 args_get(struct args
*args
, u_char flag
)
657 struct args_entry
*entry
;
659 if ((entry
= args_find(args
, flag
)) == NULL
)
661 if (TAILQ_EMPTY(&entry
->values
))
663 return (TAILQ_LAST(&entry
->values
, args_values
)->string
);
666 /* Get first argument. */
668 args_first(struct args
*args
, struct args_entry
**entry
)
670 *entry
= RB_MIN(args_tree
, &args
->tree
);
673 return ((*entry
)->flag
);
676 /* Get next argument. */
678 args_next(struct args_entry
**entry
)
680 *entry
= RB_NEXT(args_tree
, &args
->tree
, *entry
);
683 return ((*entry
)->flag
);
686 /* Get argument count. */
688 args_count(struct args
*args
)
690 return (args
->count
);
693 /* Get argument values. */
695 args_values(struct args
*args
)
697 return (args
->values
);
700 /* Get argument value. */
702 args_value(struct args
*args
, u_int idx
)
704 if (idx
>= args
->count
)
706 return (&args
->values
[idx
]);
709 /* Return argument as string. */
711 args_string(struct args
*args
, u_int idx
)
713 if (idx
>= args
->count
)
715 return (args_value_as_string(&args
->values
[idx
]));
718 /* Make a command now. */
720 args_make_commands_now(struct cmd
*self
, struct cmdq_item
*item
, u_int idx
,
723 struct args_command_state
*state
;
725 struct cmd_list
*cmdlist
;
727 state
= args_make_commands_prepare(self
, item
, idx
, NULL
, 0, expand
);
728 cmdlist
= args_make_commands(state
, 0, NULL
, &error
);
729 if (cmdlist
== NULL
) {
730 cmdq_error(item
, "%s", error
);
734 cmdlist
->references
++;
735 args_make_commands_free(state
);
739 /* Save bits to make a command later. */
740 struct args_command_state
*
741 args_make_commands_prepare(struct cmd
*self
, struct cmdq_item
*item
, u_int idx
,
742 const char *default_command
, int wait
, int expand
)
744 struct args
*args
= cmd_get_args(self
);
745 struct cmd_find_state
*target
= cmdq_get_target(item
);
746 struct client
*tc
= cmdq_get_target_client(item
);
747 struct args_value
*value
;
748 struct args_command_state
*state
;
751 state
= xcalloc(1, sizeof *state
);
753 if (idx
< args
->count
) {
754 value
= &args
->values
[idx
];
755 if (value
->type
== ARGS_COMMANDS
) {
756 state
->cmdlist
= value
->cmdlist
;
757 state
->cmdlist
->references
++;
762 if (default_command
== NULL
)
763 fatalx("argument out of range");
764 cmd
= default_command
;
769 state
->cmd
= format_single_from_target(item
, cmd
);
771 state
->cmd
= xstrdup(cmd
);
772 log_debug("%s: %s", __func__
, state
->cmd
);
775 state
->pi
.item
= item
;
776 cmd_get_source(self
, &state
->pi
.file
, &state
->pi
.line
);
778 if (state
->pi
.c
!= NULL
)
779 state
->pi
.c
->references
++;
780 cmd_find_copy_state(&state
->pi
.fs
, target
);
785 /* Return argument as command. */
787 args_make_commands(struct args_command_state
*state
, int argc
, char **argv
,
790 struct cmd_parse_result
*pr
;
794 if (state
->cmdlist
!= NULL
) {
796 return (state
->cmdlist
);
797 return (cmd_list_copy(state
->cmdlist
, argc
, argv
));
800 cmd
= xstrdup(state
->cmd
);
801 for (i
= 0; i
< argc
; i
++) {
802 new_cmd
= cmd_template_replace(cmd
, argv
[i
], i
+ 1);
803 log_debug("%s: %%%u %s: %s", __func__
, i
+ 1, argv
[i
], new_cmd
);
807 log_debug("%s: %s", __func__
, cmd
);
809 pr
= cmd_parse_from_string(cmd
, &state
->pi
);
811 switch (pr
->status
) {
812 case CMD_PARSE_ERROR
:
815 case CMD_PARSE_SUCCESS
:
816 return (pr
->cmdlist
);
818 fatalx("invalid parse return state");
821 /* Free commands state. */
823 args_make_commands_free(struct args_command_state
*state
)
825 if (state
->cmdlist
!= NULL
)
826 cmd_list_free(state
->cmdlist
);
827 if (state
->pi
.c
!= NULL
)
828 server_client_unref(state
->pi
.c
);
833 /* Get prepared command. */
835 args_make_commands_get_command(struct args_command_state
*state
)
841 if (state
->cmdlist
!= NULL
) {
842 first
= cmd_list_first(state
->cmdlist
);
844 return (xstrdup(""));
845 return (xstrdup(cmd_get_entry(first
)->name
));
847 n
= strcspn(state
->cmd
, " ,");
848 xasprintf(&s
, "%.*s", n
, state
->cmd
);
852 /* Get first value in argument. */
854 args_first_value(struct args
*args
, u_char flag
)
856 struct args_entry
*entry
;
858 if ((entry
= args_find(args
, flag
)) == NULL
)
860 return (TAILQ_FIRST(&entry
->values
));
863 /* Get next value in argument. */
865 args_next_value(struct args_value
*value
)
867 return (TAILQ_NEXT(value
, entry
));
870 /* Convert an argument value to a number. */
872 args_strtonum(struct args
*args
, u_char flag
, long long minval
,
873 long long maxval
, char **cause
)
877 struct args_entry
*entry
;
878 struct args_value
*value
;
880 if ((entry
= args_find(args
, flag
)) == NULL
) {
881 *cause
= xstrdup("missing");
884 value
= TAILQ_LAST(&entry
->values
, args_values
);
886 value
->type
!= ARGS_STRING
||
887 value
->string
== NULL
) {
888 *cause
= xstrdup("missing");
892 ll
= strtonum(value
->string
, minval
, maxval
, &errstr
);
893 if (errstr
!= NULL
) {
894 *cause
= xstrdup(errstr
);
902 /* Convert an argument value to a number, and expand formats. */
904 args_strtonum_and_expand(struct args
*args
, u_char flag
, long long minval
,
905 long long maxval
, struct cmdq_item
*item
, char **cause
)
910 struct args_entry
*entry
;
911 struct args_value
*value
;
913 if ((entry
= args_find(args
, flag
)) == NULL
) {
914 *cause
= xstrdup("missing");
917 value
= TAILQ_LAST(&entry
->values
, args_values
);
919 value
->type
!= ARGS_STRING
||
920 value
->string
== NULL
) {
921 *cause
= xstrdup("missing");
925 formatted
= format_single_from_target(item
, value
->string
);
926 ll
= strtonum(formatted
, minval
, maxval
, &errstr
);
928 if (errstr
!= NULL
) {
929 *cause
= xstrdup(errstr
);
937 /* Convert an argument to a number which may be a percentage. */
939 args_percentage(struct args
*args
, u_char flag
, long long minval
,
940 long long maxval
, long long curval
, char **cause
)
943 struct args_entry
*entry
;
945 if ((entry
= args_find(args
, flag
)) == NULL
) {
946 *cause
= xstrdup("missing");
949 if (TAILQ_EMPTY(&entry
->values
)) {
950 *cause
= xstrdup("empty");
953 value
= TAILQ_LAST(&entry
->values
, args_values
)->string
;
954 return (args_string_percentage(value
, minval
, maxval
, curval
, cause
));
957 /* Convert a string to a number which may be a percentage. */
959 args_string_percentage(const char *value
, long long minval
, long long maxval
,
960 long long curval
, char **cause
)
964 size_t valuelen
= strlen(value
);
968 *cause
= xstrdup("empty");
971 if (value
[valuelen
- 1] == '%') {
972 copy
= xstrdup(value
);
973 copy
[valuelen
- 1] = '\0';
975 ll
= strtonum(copy
, 0, 100, &errstr
);
977 if (errstr
!= NULL
) {
978 *cause
= xstrdup(errstr
);
981 ll
= (curval
* ll
) / 100;
983 *cause
= xstrdup("too small");
987 *cause
= xstrdup("too large");
991 ll
= strtonum(value
, minval
, maxval
, &errstr
);
992 if (errstr
!= NULL
) {
993 *cause
= xstrdup(errstr
);
1003 * Convert an argument to a number which may be a percentage, and expand
1007 args_percentage_and_expand(struct args
*args
, u_char flag
, long long minval
,
1008 long long maxval
, long long curval
, struct cmdq_item
*item
, char **cause
)
1011 struct args_entry
*entry
;
1013 if ((entry
= args_find(args
, flag
)) == NULL
) {
1014 *cause
= xstrdup("missing");
1017 if (TAILQ_EMPTY(&entry
->values
)) {
1018 *cause
= xstrdup("empty");
1021 value
= TAILQ_LAST(&entry
->values
, args_values
)->string
;
1022 return (args_string_percentage_and_expand(value
, minval
, maxval
, curval
,
1027 * Convert a string to a number which may be a percentage, and expand formats.
1030 args_string_percentage_and_expand(const char *value
, long long minval
,
1031 long long maxval
, long long curval
, struct cmdq_item
*item
, char **cause
)
1035 size_t valuelen
= strlen(value
);
1038 if (value
[valuelen
- 1] == '%') {
1039 copy
= xstrdup(value
);
1040 copy
[valuelen
- 1] = '\0';
1042 f
= format_single_from_target(item
, copy
);
1043 ll
= strtonum(f
, 0, 100, &errstr
);
1046 if (errstr
!= NULL
) {
1047 *cause
= xstrdup(errstr
);
1050 ll
= (curval
* ll
) / 100;
1052 *cause
= xstrdup("too small");
1056 *cause
= xstrdup("too large");
1060 f
= format_single_from_target(item
, value
);
1061 ll
= strtonum(f
, minval
, maxval
, &errstr
);
1063 if (errstr
!= NULL
) {
1064 *cause
= xstrdup(errstr
);