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
);
154 if (argument
== NULL
) {
155 if (optional_argument
) {
156 log_debug("%s: -%c (optional)", __func__
, flag
);
157 args_set(args
, flag
, NULL
, ARGS_ENTRY_OPTIONAL_VALUE
);
158 return (0); /* either - or end */
160 xasprintf(cause
, "-%c expects an argument", flag
);
163 args_copy_value(new, argument
);
167 s
= args_value_as_string(new);
168 log_debug("%s: -%c = %s", __func__
, flag
, s
);
169 args_set(args
, flag
, new, 0);
173 /* Parse flags argument. */
175 args_parse_flags(const struct args_parse
*parse
, struct args_value
*values
,
176 u_int count
, char **cause
, struct args
*args
, int *i
)
178 struct args_value
*value
;
180 const char *found
, *string
;
181 int optional_argument
;
184 if (value
->type
!= ARGS_STRING
)
187 string
= value
->string
;
188 log_debug("%s: next %s", __func__
, string
);
189 if (*string
++ != '-' || *string
== '\0')
192 if (string
[0] == '-' && string
[1] == '\0')
201 if (!isalnum(flag
)) {
202 xasprintf(cause
, "invalid flag -%c", flag
);
206 found
= strchr(parse
->template, flag
);
208 xasprintf(cause
, "unknown flag -%c", flag
);
211 if (found
[1] != ':') {
212 log_debug("%s: -%c", __func__
, flag
);
213 args_set(args
, flag
, NULL
, 0);
216 optional_argument
= (found
[2] == ':');
217 return (args_parse_flag_argument(values
, count
, cause
, args
, i
,
218 string
, flag
, optional_argument
));
222 /* Parse arguments into a new argument set. */
224 args_parse(const struct args_parse
*parse
, struct args_value
*values
,
225 u_int count
, char **cause
)
229 enum args_parse_type type
;
230 struct args_value
*value
, *new;
235 return (args_create());
237 args
= args_create();
238 for (i
= 1; i
< count
; /* nothing */) {
239 stop
= args_parse_flags(parse
, values
, count
, cause
, args
, &i
);
247 log_debug("%s: flags end at %u of %u", __func__
, i
, count
);
249 for (/* nothing */; i
< count
; i
++) {
252 s
= args_value_as_string(value
);
253 log_debug("%s: %u = %s (type %d)", __func__
, i
, s
,
256 if (parse
->cb
!= NULL
) {
257 type
= parse
->cb(args
, args
->count
, cause
);
258 if (type
== ARGS_PARSE_INVALID
) {
263 type
= ARGS_PARSE_STRING
;
265 args
->values
= xrecallocarray(args
->values
,
266 args
->count
, args
->count
+ 1, sizeof *args
->values
);
267 new = &args
->values
[args
->count
++];
270 case ARGS_PARSE_INVALID
:
271 fatalx("unexpected argument type");
272 case ARGS_PARSE_STRING
:
273 if (value
->type
!= ARGS_STRING
) {
275 "argument %u must be \"string\"",
280 args_copy_value(new, value
);
282 case ARGS_PARSE_COMMANDS_OR_STRING
:
283 args_copy_value(new, value
);
285 case ARGS_PARSE_COMMANDS
:
286 if (value
->type
!= ARGS_COMMANDS
) {
288 "argument %u must be { commands }",
293 args_copy_value(new, value
);
299 if (parse
->lower
!= -1 && args
->count
< (u_int
)parse
->lower
) {
301 "too few arguments (need at least %u)",
306 if (parse
->upper
!= -1 && args
->count
> (u_int
)parse
->upper
) {
308 "too many arguments (need at most %u)",
316 /* Copy and expand a value. */
318 args_copy_copy_value(struct args_value
*to
, struct args_value
*from
, int argc
,
324 to
->type
= from
->type
;
325 switch (from
->type
) {
329 expanded
= xstrdup(from
->string
);
330 for (i
= 0; i
< argc
; i
++) {
331 s
= cmd_template_replace(expanded
, argv
[i
], i
+ 1);
335 to
->string
= expanded
;
338 to
->cmdlist
= cmd_list_copy(from
->cmdlist
, argc
, argv
);
343 /* Copy an arguments set. */
345 args_copy(struct args
*args
, int argc
, char **argv
)
347 struct args
*new_args
;
348 struct args_entry
*entry
;
349 struct args_value
*value
, *new_value
;
352 cmd_log_argv(argc
, argv
, "%s", __func__
);
354 new_args
= args_create();
355 RB_FOREACH(entry
, args_tree
, &args
->tree
) {
356 if (TAILQ_EMPTY(&entry
->values
)) {
357 for (i
= 0; i
< entry
->count
; i
++)
358 args_set(new_args
, entry
->flag
, NULL
, 0);
361 TAILQ_FOREACH(value
, &entry
->values
, entry
) {
362 new_value
= xcalloc(1, sizeof *new_value
);
363 args_copy_copy_value(new_value
, value
, argc
, argv
);
364 args_set(new_args
, entry
->flag
, new_value
, 0);
367 if (args
->count
== 0)
369 new_args
->count
= args
->count
;
370 new_args
->values
= xcalloc(args
->count
, sizeof *new_args
->values
);
371 for (i
= 0; i
< args
->count
; i
++) {
372 new_value
= &new_args
->values
[i
];
373 args_copy_copy_value(new_value
, &args
->values
[i
], argc
, argv
);
380 args_free_value(struct args_value
*value
)
382 switch (value
->type
) {
389 cmd_list_free(value
->cmdlist
);
397 args_free_values(struct args_value
*values
, u_int count
)
401 for (i
= 0; i
< count
; i
++)
402 args_free_value(&values
[i
]);
405 /* Free an arguments set. */
407 args_free(struct args
*args
)
409 struct args_entry
*entry
;
410 struct args_entry
*entry1
;
411 struct args_value
*value
;
412 struct args_value
*value1
;
414 args_free_values(args
->values
, args
->count
);
417 RB_FOREACH_SAFE(entry
, args_tree
, &args
->tree
, entry1
) {
418 RB_REMOVE(args_tree
, &args
->tree
, entry
);
419 TAILQ_FOREACH_SAFE(value
, &entry
->values
, entry
, value1
) {
420 TAILQ_REMOVE(&entry
->values
, value
, entry
);
421 args_free_value(value
);
430 /* Convert arguments to vector. */
432 args_to_vector(struct args
*args
, int *argc
, char ***argv
)
440 for (i
= 0; i
< args
->count
; i
++) {
441 switch (args
->values
[i
].type
) {
445 cmd_append_argv(argc
, argv
, args
->values
[i
].string
);
448 s
= cmd_list_print(args
->values
[i
].cmdlist
, 0);
449 cmd_append_argv(argc
, argv
, s
);
456 /* Convert arguments from vector. */
458 args_from_vector(int argc
, char **argv
)
460 struct args_value
*values
;
463 values
= xcalloc(argc
, sizeof *values
);
464 for (i
= 0; i
< argc
; i
++) {
465 values
[i
].type
= ARGS_STRING
;
466 values
[i
].string
= xstrdup(argv
[i
]);
472 static void printflike(3, 4)
473 args_print_add(char **buf
, size_t *len
, const char *fmt
, ...)
480 slen
= xvasprintf(&s
, fmt
, ap
);
484 *buf
= xrealloc(*buf
, *len
);
486 strlcat(*buf
, s
, *len
);
490 /* Add value to string. */
492 args_print_add_value(char **buf
, size_t *len
, struct args_value
*value
)
494 char *expanded
= NULL
;
497 args_print_add(buf
, len
, " ");
499 switch (value
->type
) {
503 expanded
= cmd_list_print(value
->cmdlist
, 0);
504 args_print_add(buf
, len
, "{ %s }", expanded
);
507 expanded
= args_escape(value
->string
);
508 args_print_add(buf
, len
, "%s", expanded
);
514 /* Print a set of arguments. */
516 args_print(struct args
*args
)
521 struct args_entry
*entry
;
522 struct args_entry
*last
= NULL
;
523 struct args_value
*value
;
526 buf
= xcalloc(1, len
);
528 /* Process the flags first. */
529 RB_FOREACH(entry
, args_tree
, &args
->tree
) {
530 if (entry
->flags
& ARGS_ENTRY_OPTIONAL_VALUE
)
532 if (!TAILQ_EMPTY(&entry
->values
))
536 args_print_add(&buf
, &len
, "-");
537 for (j
= 0; j
< entry
->count
; j
++)
538 args_print_add(&buf
, &len
, "%c", entry
->flag
);
541 /* Then the flags with arguments. */
542 RB_FOREACH(entry
, args_tree
, &args
->tree
) {
543 if (entry
->flags
& ARGS_ENTRY_OPTIONAL_VALUE
) {
545 args_print_add(&buf
, &len
, " -%c", entry
->flag
);
547 args_print_add(&buf
, &len
, "-%c", entry
->flag
);
551 if (TAILQ_EMPTY(&entry
->values
))
553 TAILQ_FOREACH(value
, &entry
->values
, entry
) {
555 args_print_add(&buf
, &len
, " -%c", entry
->flag
);
557 args_print_add(&buf
, &len
, "-%c", entry
->flag
);
558 args_print_add_value(&buf
, &len
, value
);
562 if (last
&& (last
->flags
& ARGS_ENTRY_OPTIONAL_VALUE
))
563 args_print_add(&buf
, &len
, " --");
565 /* And finally the argument vector. */
566 for (i
= 0; i
< args
->count
; i
++)
567 args_print_add_value(&buf
, &len
, &args
->values
[i
]);
572 /* Escape an argument. */
574 args_escape(const char *s
)
576 static const char dquoted
[] = " #';${}%";
577 static const char squoted
[] = " \"";
578 char *escaped
, *result
;
579 int flags
, quotes
= 0;
582 xasprintf(&result
, "''");
585 if (s
[strcspn(s
, dquoted
)] != '\0')
587 else if (s
[strcspn(s
, squoted
)] != '\0')
592 (quotes
!= 0 || s
[0] == '~')) {
593 xasprintf(&escaped
, "\\%c", s
[0]);
597 flags
= VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
;
600 utf8_stravis(&escaped
, s
, flags
);
603 xasprintf(&result
, "'%s'", escaped
);
604 else if (quotes
== '"') {
606 xasprintf(&result
, "\"\\%s\"", escaped
);
608 xasprintf(&result
, "\"%s\"", escaped
);
611 xasprintf(&result
, "\\%s", escaped
);
613 result
= xstrdup(escaped
);
619 /* Return if an argument is present. */
621 args_has(struct args
*args
, u_char flag
)
623 struct args_entry
*entry
;
625 entry
= args_find(args
, flag
);
628 return (entry
->count
);
631 /* Set argument value in the arguments tree. */
633 args_set(struct args
*args
, u_char flag
, struct args_value
*value
, int flags
)
635 struct args_entry
*entry
;
637 entry
= args_find(args
, flag
);
639 entry
= xcalloc(1, sizeof *entry
);
642 entry
->flags
= flags
;
643 TAILQ_INIT(&entry
->values
);
644 RB_INSERT(args_tree
, &args
->tree
, entry
);
647 if (value
!= NULL
&& value
->type
!= ARGS_NONE
)
648 TAILQ_INSERT_TAIL(&entry
->values
, value
, entry
);
651 /* Get argument value. Will be NULL if it isn't present. */
653 args_get(struct args
*args
, u_char flag
)
655 struct args_entry
*entry
;
657 if ((entry
= args_find(args
, flag
)) == NULL
)
659 if (TAILQ_EMPTY(&entry
->values
))
661 return (TAILQ_LAST(&entry
->values
, args_values
)->string
);
664 /* Get first argument. */
666 args_first(struct args
*args
, struct args_entry
**entry
)
668 *entry
= RB_MIN(args_tree
, &args
->tree
);
671 return ((*entry
)->flag
);
674 /* Get next argument. */
676 args_next(struct args_entry
**entry
)
678 *entry
= RB_NEXT(args_tree
, &args
->tree
, *entry
);
681 return ((*entry
)->flag
);
684 /* Get argument count. */
686 args_count(struct args
*args
)
688 return (args
->count
);
691 /* Get argument values. */
693 args_values(struct args
*args
)
695 return (args
->values
);
698 /* Get argument value. */
700 args_value(struct args
*args
, u_int idx
)
702 if (idx
>= args
->count
)
704 return (&args
->values
[idx
]);
707 /* Return argument as string. */
709 args_string(struct args
*args
, u_int idx
)
711 if (idx
>= args
->count
)
713 return (args_value_as_string(&args
->values
[idx
]));
716 /* Make a command now. */
718 args_make_commands_now(struct cmd
*self
, struct cmdq_item
*item
, u_int idx
,
721 struct args_command_state
*state
;
723 struct cmd_list
*cmdlist
;
725 state
= args_make_commands_prepare(self
, item
, idx
, NULL
, 0, expand
);
726 cmdlist
= args_make_commands(state
, 0, NULL
, &error
);
727 if (cmdlist
== NULL
) {
728 cmdq_error(item
, "%s", error
);
732 cmdlist
->references
++;
733 args_make_commands_free(state
);
737 /* Save bits to make a command later. */
738 struct args_command_state
*
739 args_make_commands_prepare(struct cmd
*self
, struct cmdq_item
*item
, u_int idx
,
740 const char *default_command
, int wait
, int expand
)
742 struct args
*args
= cmd_get_args(self
);
743 struct cmd_find_state
*target
= cmdq_get_target(item
);
744 struct client
*tc
= cmdq_get_target_client(item
);
745 struct args_value
*value
;
746 struct args_command_state
*state
;
749 state
= xcalloc(1, sizeof *state
);
751 if (idx
< args
->count
) {
752 value
= &args
->values
[idx
];
753 if (value
->type
== ARGS_COMMANDS
) {
754 state
->cmdlist
= value
->cmdlist
;
755 state
->cmdlist
->references
++;
760 if (default_command
== NULL
)
761 fatalx("argument out of range");
762 cmd
= default_command
;
767 state
->cmd
= format_single_from_target(item
, cmd
);
769 state
->cmd
= xstrdup(cmd
);
770 log_debug("%s: %s", __func__
, state
->cmd
);
773 state
->pi
.item
= item
;
774 cmd_get_source(self
, &state
->pi
.file
, &state
->pi
.line
);
776 if (state
->pi
.c
!= NULL
)
777 state
->pi
.c
->references
++;
778 cmd_find_copy_state(&state
->pi
.fs
, target
);
783 /* Return argument as command. */
785 args_make_commands(struct args_command_state
*state
, int argc
, char **argv
,
788 struct cmd_parse_result
*pr
;
792 if (state
->cmdlist
!= NULL
) {
794 return (state
->cmdlist
);
795 return (cmd_list_copy(state
->cmdlist
, argc
, argv
));
798 cmd
= xstrdup(state
->cmd
);
799 for (i
= 0; i
< argc
; i
++) {
800 new_cmd
= cmd_template_replace(cmd
, argv
[i
], i
+ 1);
801 log_debug("%s: %%%u %s: %s", __func__
, i
+ 1, argv
[i
], new_cmd
);
805 log_debug("%s: %s", __func__
, cmd
);
807 pr
= cmd_parse_from_string(cmd
, &state
->pi
);
809 switch (pr
->status
) {
810 case CMD_PARSE_ERROR
:
813 case CMD_PARSE_SUCCESS
:
814 return (pr
->cmdlist
);
816 fatalx("invalid parse return state");
819 /* Free commands state. */
821 args_make_commands_free(struct args_command_state
*state
)
823 if (state
->cmdlist
!= NULL
)
824 cmd_list_free(state
->cmdlist
);
825 if (state
->pi
.c
!= NULL
)
826 server_client_unref(state
->pi
.c
);
831 /* Get prepared command. */
833 args_make_commands_get_command(struct args_command_state
*state
)
839 if (state
->cmdlist
!= NULL
) {
840 first
= cmd_list_first(state
->cmdlist
);
842 return (xstrdup(""));
843 return (xstrdup(cmd_get_entry(first
)->name
));
845 n
= strcspn(state
->cmd
, " ,");
846 xasprintf(&s
, "%.*s", n
, state
->cmd
);
850 /* Get first value in argument. */
852 args_first_value(struct args
*args
, u_char flag
)
854 struct args_entry
*entry
;
856 if ((entry
= args_find(args
, flag
)) == NULL
)
858 return (TAILQ_FIRST(&entry
->values
));
861 /* Get next value in argument. */
863 args_next_value(struct args_value
*value
)
865 return (TAILQ_NEXT(value
, entry
));
868 /* Convert an argument value to a number. */
870 args_strtonum(struct args
*args
, u_char flag
, long long minval
,
871 long long maxval
, char **cause
)
875 struct args_entry
*entry
;
876 struct args_value
*value
;
878 if ((entry
= args_find(args
, flag
)) == NULL
) {
879 *cause
= xstrdup("missing");
882 value
= TAILQ_LAST(&entry
->values
, args_values
);
884 value
->type
!= ARGS_STRING
||
885 value
->string
== NULL
) {
886 *cause
= xstrdup("missing");
890 ll
= strtonum(value
->string
, minval
, maxval
, &errstr
);
891 if (errstr
!= NULL
) {
892 *cause
= xstrdup(errstr
);
900 /* Convert an argument value to a number, and expand formats. */
902 args_strtonum_and_expand(struct args
*args
, u_char flag
, long long minval
,
903 long long maxval
, struct cmdq_item
*item
, char **cause
)
908 struct args_entry
*entry
;
909 struct args_value
*value
;
911 if ((entry
= args_find(args
, flag
)) == NULL
) {
912 *cause
= xstrdup("missing");
915 value
= TAILQ_LAST(&entry
->values
, args_values
);
917 value
->type
!= ARGS_STRING
||
918 value
->string
== NULL
) {
919 *cause
= xstrdup("missing");
923 formatted
= format_single_from_target(item
, value
->string
);
924 ll
= strtonum(formatted
, minval
, maxval
, &errstr
);
926 if (errstr
!= NULL
) {
927 *cause
= xstrdup(errstr
);
935 /* Convert an argument to a number which may be a percentage. */
937 args_percentage(struct args
*args
, u_char flag
, long long minval
,
938 long long maxval
, long long curval
, char **cause
)
941 struct args_entry
*entry
;
943 if ((entry
= args_find(args
, flag
)) == NULL
) {
944 *cause
= xstrdup("missing");
947 if (TAILQ_EMPTY(&entry
->values
)) {
948 *cause
= xstrdup("empty");
951 value
= TAILQ_LAST(&entry
->values
, args_values
)->string
;
952 return (args_string_percentage(value
, minval
, maxval
, curval
, cause
));
955 /* Convert a string to a number which may be a percentage. */
957 args_string_percentage(const char *value
, long long minval
, long long maxval
,
958 long long curval
, char **cause
)
962 size_t valuelen
= strlen(value
);
966 *cause
= xstrdup("empty");
969 if (value
[valuelen
- 1] == '%') {
970 copy
= xstrdup(value
);
971 copy
[valuelen
- 1] = '\0';
973 ll
= strtonum(copy
, 0, 100, &errstr
);
975 if (errstr
!= NULL
) {
976 *cause
= xstrdup(errstr
);
979 ll
= (curval
* ll
) / 100;
981 *cause
= xstrdup("too small");
985 *cause
= xstrdup("too large");
989 ll
= strtonum(value
, minval
, maxval
, &errstr
);
990 if (errstr
!= NULL
) {
991 *cause
= xstrdup(errstr
);
1001 * Convert an argument to a number which may be a percentage, and expand
1005 args_percentage_and_expand(struct args
*args
, u_char flag
, long long minval
,
1006 long long maxval
, long long curval
, struct cmdq_item
*item
, char **cause
)
1009 struct args_entry
*entry
;
1011 if ((entry
= args_find(args
, flag
)) == NULL
) {
1012 *cause
= xstrdup("missing");
1015 if (TAILQ_EMPTY(&entry
->values
)) {
1016 *cause
= xstrdup("empty");
1019 value
= TAILQ_LAST(&entry
->values
, args_values
)->string
;
1020 return (args_string_percentage_and_expand(value
, minval
, maxval
, curval
,
1025 * Convert a string to a number which may be a percentage, and expand formats.
1028 args_string_percentage_and_expand(const char *value
, long long minval
,
1029 long long maxval
, long long curval
, struct cmdq_item
*item
, char **cause
)
1033 size_t valuelen
= strlen(value
);
1036 if (value
[valuelen
- 1] == '%') {
1037 copy
= xstrdup(value
);
1038 copy
[valuelen
- 1] = '\0';
1040 f
= format_single_from_target(item
, copy
);
1041 ll
= strtonum(f
, 0, 100, &errstr
);
1044 if (errstr
!= NULL
) {
1045 *cause
= xstrdup(errstr
);
1048 ll
= (curval
* ll
) / 100;
1050 *cause
= xstrdup("too small");
1054 *cause
= xstrdup("too large");
1058 f
= format_single_from_target(item
, value
);
1059 ll
= strtonum(f
, minval
, maxval
, &errstr
);
1061 if (errstr
!= NULL
) {
1062 *cause
= xstrdup(errstr
);