Reduce escape-time default to 10 milliseconds, 500 is far too long for
[tmux.git] / arguments.c
blob70fa40d0ff7a1c1b9f24fe83c0c8d1fdaf2100a1
1 /* $OpenBSD$ */
3 /*
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>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <vis.h>
26 #include "tmux.h"
29 * Manipulate command arguments.
32 /* List of argument values. */
33 TAILQ_HEAD(args_values, args_value);
35 /* Single arguments flag. */
36 struct args_entry {
37 u_char flag;
38 struct args_values values;
39 u_int count;
41 int flags;
42 #define ARGS_ENTRY_OPTIONAL_VALUE 0x1
44 RB_ENTRY(args_entry) entry;
47 /* Parsed argument flags and values. */
48 struct args {
49 struct args_tree tree;
50 u_int count;
51 struct args_value *values;
54 /* Prepared command state. */
55 struct args_command_state {
56 struct cmd_list *cmdlist;
57 char *cmd;
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. */
67 static int
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;
79 entry.flag = flag;
80 return (RB_FIND(args_tree, &args->tree, &entry));
83 /* Copy value. */
84 static void
85 args_copy_value(struct args_value *to, struct args_value *from)
87 to->type = from->type;
88 switch (from->type) {
89 case ARGS_NONE:
90 break;
91 case ARGS_COMMANDS:
92 to->cmdlist = from->cmdlist;
93 to->cmdlist->references++;
94 break;
95 case ARGS_STRING:
96 to->string = xstrdup(from->string);
97 break;
101 /* Type to string. */
102 static const char *
103 args_type_to_string (enum args_type type)
105 switch (type)
107 case ARGS_NONE:
108 return "NONE";
109 case ARGS_STRING:
110 return "STRING";
111 case ARGS_COMMANDS:
112 return "COMMANDS";
114 return "INVALID";
117 /* Get value as string. */
118 static const char *
119 args_value_as_string(struct args_value *value)
121 switch (value->type) {
122 case ARGS_NONE:
123 return ("");
124 case ARGS_COMMANDS:
125 if (value->cached == NULL)
126 value->cached = cmd_list_print(value->cmdlist, 0);
127 return (value->cached);
128 case ARGS_STRING:
129 return (value->string);
131 fatalx("unexpected argument type");
134 /* Create an empty arguments set. */
135 struct args *
136 args_create(void)
138 struct args *args;
140 args = xcalloc(1, sizeof *args);
141 RB_INIT(&args->tree);
142 return (args);
145 /* Parse a single flag. */
146 static int
147 args_parse_flag_argument(struct args_value *values, u_int count, char **cause,
148 struct args *args, u_int *i, const char *string, int flag,
149 int optional_argument)
151 struct args_value *argument, *new;
152 const char *s;
154 new = xcalloc(1, sizeof *new);
155 if (*string != '\0') {
156 new->type = ARGS_STRING;
157 new->string = xstrdup(string);
158 goto out;
161 if (*i == count)
162 argument = NULL;
163 else {
164 argument = &values[*i];
165 if (argument->type != ARGS_STRING) {
166 xasprintf(cause, "-%c argument must be a string", flag);
167 return (-1);
170 if (argument == NULL) {
171 if (optional_argument) {
172 log_debug("%s: -%c (optional)", __func__, flag);
173 args_set(args, flag, NULL, ARGS_ENTRY_OPTIONAL_VALUE);
174 return (0); /* either - or end */
176 xasprintf(cause, "-%c expects an argument", flag);
177 return (-1);
179 args_copy_value(new, argument);
180 (*i)++;
182 out:
183 s = args_value_as_string(new);
184 log_debug("%s: -%c = %s", __func__, flag, s);
185 args_set(args, flag, new, 0);
186 return (0);
189 /* Parse flags argument. */
190 static int
191 args_parse_flags(const struct args_parse *parse, struct args_value *values,
192 u_int count, char **cause, struct args *args, u_int *i)
194 struct args_value *value;
195 u_char flag;
196 const char *found, *string;
197 int optional_argument;
199 value = &values[*i];
200 if (value->type != ARGS_STRING)
201 return (1);
203 string = value->string;
204 log_debug("%s: next %s", __func__, string);
205 if (*string++ != '-' || *string == '\0')
206 return (1);
207 (*i)++;
208 if (string[0] == '-' && string[1] == '\0')
209 return (1);
211 for (;;) {
212 flag = *string++;
213 if (flag == '\0')
214 return (0);
215 if (flag == '?')
216 return (-1);
217 if (!isalnum(flag)) {
218 xasprintf(cause, "invalid flag -%c", flag);
219 return (-1);
222 found = strchr(parse->template, flag);
223 if (found == NULL) {
224 xasprintf(cause, "unknown flag -%c", flag);
225 return (-1);
227 if (found[1] != ':') {
228 log_debug("%s: -%c", __func__, flag);
229 args_set(args, flag, NULL, 0);
230 continue;
232 optional_argument = (found[2] == ':');
233 return (args_parse_flag_argument(values, count, cause, args, i,
234 string, flag, optional_argument));
238 /* Parse arguments into a new argument set. */
239 struct args *
240 args_parse(const struct args_parse *parse, struct args_value *values,
241 u_int count, char **cause)
243 struct args *args;
244 u_int i;
245 enum args_parse_type type;
246 struct args_value *value, *new;
247 const char *s;
248 int stop;
250 if (count == 0)
251 return (args_create());
253 args = args_create();
254 for (i = 1; i < count; /* nothing */) {
255 stop = args_parse_flags(parse, values, count, cause, args, &i);
256 if (stop == -1) {
257 args_free(args);
258 return (NULL);
260 if (stop == 1)
261 break;
263 log_debug("%s: flags end at %u of %u", __func__, i, count);
264 if (i != count) {
265 for (/* nothing */; i < count; i++) {
266 value = &values[i];
268 s = args_value_as_string(value);
269 log_debug("%s: %u = %s (type %s)", __func__, i, s,
270 args_type_to_string (value->type));
272 if (parse->cb != NULL) {
273 type = parse->cb(args, args->count, cause);
274 if (type == ARGS_PARSE_INVALID) {
275 args_free(args);
276 return (NULL);
278 } else
279 type = ARGS_PARSE_STRING;
281 args->values = xrecallocarray(args->values,
282 args->count, args->count + 1, sizeof *args->values);
283 new = &args->values[args->count++];
285 switch (type) {
286 case ARGS_PARSE_INVALID:
287 fatalx("unexpected argument type");
288 case ARGS_PARSE_STRING:
289 if (value->type != ARGS_STRING) {
290 xasprintf(cause,
291 "argument %u must be \"string\"",
292 args->count);
293 args_free(args);
294 return (NULL);
296 args_copy_value(new, value);
297 break;
298 case ARGS_PARSE_COMMANDS_OR_STRING:
299 args_copy_value(new, value);
300 break;
301 case ARGS_PARSE_COMMANDS:
302 if (value->type != ARGS_COMMANDS) {
303 xasprintf(cause,
304 "argument %u must be { commands }",
305 args->count);
306 args_free(args);
307 return (NULL);
309 args_copy_value(new, value);
310 break;
315 if (parse->lower != -1 && args->count < (u_int)parse->lower) {
316 xasprintf(cause,
317 "too few arguments (need at least %u)",
318 parse->lower);
319 args_free(args);
320 return (NULL);
322 if (parse->upper != -1 && args->count > (u_int)parse->upper) {
323 xasprintf(cause,
324 "too many arguments (need at most %u)",
325 parse->upper);
326 args_free(args);
327 return (NULL);
329 return (args);
332 /* Copy and expand a value. */
333 static void
334 args_copy_copy_value(struct args_value *to, struct args_value *from, int argc,
335 char **argv)
337 char *s, *expanded;
338 int i;
340 to->type = from->type;
341 switch (from->type) {
342 case ARGS_NONE:
343 break;
344 case ARGS_STRING:
345 expanded = xstrdup(from->string);
346 for (i = 0; i < argc; i++) {
347 s = cmd_template_replace(expanded, argv[i], i + 1);
348 free(expanded);
349 expanded = s;
351 to->string = expanded;
352 break;
353 case ARGS_COMMANDS:
354 to->cmdlist = cmd_list_copy(from->cmdlist, argc, argv);
355 break;
359 /* Copy an arguments set. */
360 struct args *
361 args_copy(struct args *args, int argc, char **argv)
363 struct args *new_args;
364 struct args_entry *entry;
365 struct args_value *value, *new_value;
366 u_int i;
368 cmd_log_argv(argc, argv, "%s", __func__);
370 new_args = args_create();
371 RB_FOREACH(entry, args_tree, &args->tree) {
372 if (TAILQ_EMPTY(&entry->values)) {
373 for (i = 0; i < entry->count; i++)
374 args_set(new_args, entry->flag, NULL, 0);
375 continue;
377 TAILQ_FOREACH(value, &entry->values, entry) {
378 new_value = xcalloc(1, sizeof *new_value);
379 args_copy_copy_value(new_value, value, argc, argv);
380 args_set(new_args, entry->flag, new_value, 0);
383 if (args->count == 0)
384 return (new_args);
385 new_args->count = args->count;
386 new_args->values = xcalloc(args->count, sizeof *new_args->values);
387 for (i = 0; i < args->count; i++) {
388 new_value = &new_args->values[i];
389 args_copy_copy_value(new_value, &args->values[i], argc, argv);
391 return (new_args);
394 /* Free a value. */
395 void
396 args_free_value(struct args_value *value)
398 switch (value->type) {
399 case ARGS_NONE:
400 break;
401 case ARGS_STRING:
402 free(value->string);
403 break;
404 case ARGS_COMMANDS:
405 cmd_list_free(value->cmdlist);
406 break;
408 free(value->cached);
411 /* Free values. */
412 void
413 args_free_values(struct args_value *values, u_int count)
415 u_int i;
417 for (i = 0; i < count; i++)
418 args_free_value(&values[i]);
421 /* Free an arguments set. */
422 void
423 args_free(struct args *args)
425 struct args_entry *entry;
426 struct args_entry *entry1;
427 struct args_value *value;
428 struct args_value *value1;
430 args_free_values(args->values, args->count);
431 free(args->values);
433 RB_FOREACH_SAFE(entry, args_tree, &args->tree, entry1) {
434 RB_REMOVE(args_tree, &args->tree, entry);
435 TAILQ_FOREACH_SAFE(value, &entry->values, entry, value1) {
436 TAILQ_REMOVE(&entry->values, value, entry);
437 args_free_value(value);
438 free(value);
440 free(entry);
443 free(args);
446 /* Convert arguments to vector. */
447 void
448 args_to_vector(struct args *args, int *argc, char ***argv)
450 char *s;
451 u_int i;
453 *argc = 0;
454 *argv = NULL;
456 for (i = 0; i < args->count; i++) {
457 switch (args->values[i].type) {
458 case ARGS_NONE:
459 break;
460 case ARGS_STRING:
461 cmd_append_argv(argc, argv, args->values[i].string);
462 break;
463 case ARGS_COMMANDS:
464 s = cmd_list_print(args->values[i].cmdlist, 0);
465 cmd_append_argv(argc, argv, s);
466 free(s);
467 break;
472 /* Convert arguments from vector. */
473 struct args_value *
474 args_from_vector(int argc, char **argv)
476 struct args_value *values;
477 int i;
479 values = xcalloc(argc, sizeof *values);
480 for (i = 0; i < argc; i++) {
481 values[i].type = ARGS_STRING;
482 values[i].string = xstrdup(argv[i]);
484 return (values);
487 /* Add to string. */
488 static void printflike(3, 4)
489 args_print_add(char **buf, size_t *len, const char *fmt, ...)
491 va_list ap;
492 char *s;
493 size_t slen;
495 va_start(ap, fmt);
496 slen = xvasprintf(&s, fmt, ap);
497 va_end(ap);
499 *len += slen;
500 *buf = xrealloc(*buf, *len);
502 strlcat(*buf, s, *len);
503 free(s);
506 /* Add value to string. */
507 static void
508 args_print_add_value(char **buf, size_t *len, struct args_value *value)
510 char *expanded = NULL;
512 if (**buf != '\0')
513 args_print_add(buf, len, " ");
515 switch (value->type) {
516 case ARGS_NONE:
517 break;
518 case ARGS_COMMANDS:
519 expanded = cmd_list_print(value->cmdlist, 0);
520 args_print_add(buf, len, "{ %s }", expanded);
521 break;
522 case ARGS_STRING:
523 expanded = args_escape(value->string);
524 args_print_add(buf, len, "%s", expanded);
525 break;
527 free(expanded);
530 /* Print a set of arguments. */
531 char *
532 args_print(struct args *args)
534 size_t len;
535 char *buf;
536 u_int i, j;
537 struct args_entry *entry;
538 struct args_entry *last = NULL;
539 struct args_value *value;
541 len = 1;
542 buf = xcalloc(1, len);
544 /* Process the flags first. */
545 RB_FOREACH(entry, args_tree, &args->tree) {
546 if (entry->flags & ARGS_ENTRY_OPTIONAL_VALUE)
547 continue;
548 if (!TAILQ_EMPTY(&entry->values))
549 continue;
551 if (*buf == '\0')
552 args_print_add(&buf, &len, "-");
553 for (j = 0; j < entry->count; j++)
554 args_print_add(&buf, &len, "%c", entry->flag);
557 /* Then the flags with arguments. */
558 RB_FOREACH(entry, args_tree, &args->tree) {
559 if (entry->flags & ARGS_ENTRY_OPTIONAL_VALUE) {
560 if (*buf != '\0')
561 args_print_add(&buf, &len, " -%c", entry->flag);
562 else
563 args_print_add(&buf, &len, "-%c", entry->flag);
564 last = entry;
565 continue;
567 if (TAILQ_EMPTY(&entry->values))
568 continue;
569 TAILQ_FOREACH(value, &entry->values, entry) {
570 if (*buf != '\0')
571 args_print_add(&buf, &len, " -%c", entry->flag);
572 else
573 args_print_add(&buf, &len, "-%c", entry->flag);
574 args_print_add_value(&buf, &len, value);
576 last = entry;
578 if (last && (last->flags & ARGS_ENTRY_OPTIONAL_VALUE))
579 args_print_add(&buf, &len, " --");
581 /* And finally the argument vector. */
582 for (i = 0; i < args->count; i++)
583 args_print_add_value(&buf, &len, &args->values[i]);
585 return (buf);
588 /* Escape an argument. */
589 char *
590 args_escape(const char *s)
592 static const char dquoted[] = " #';${}%";
593 static const char squoted[] = " \"";
594 char *escaped, *result;
595 int flags, quotes = 0;
597 if (*s == '\0') {
598 xasprintf(&result, "''");
599 return (result);
601 if (s[strcspn(s, dquoted)] != '\0')
602 quotes = '"';
603 else if (s[strcspn(s, squoted)] != '\0')
604 quotes = '\'';
606 if (s[0] != ' ' &&
607 s[1] == '\0' &&
608 (quotes != 0 || s[0] == '~')) {
609 xasprintf(&escaped, "\\%c", s[0]);
610 return (escaped);
613 flags = VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL;
614 if (quotes == '"')
615 flags |= VIS_DQ;
616 utf8_stravis(&escaped, s, flags);
618 if (quotes == '\'')
619 xasprintf(&result, "'%s'", escaped);
620 else if (quotes == '"') {
621 if (*escaped == '~')
622 xasprintf(&result, "\"\\%s\"", escaped);
623 else
624 xasprintf(&result, "\"%s\"", escaped);
625 } else {
626 if (*escaped == '~')
627 xasprintf(&result, "\\%s", escaped);
628 else
629 result = xstrdup(escaped);
631 free(escaped);
632 return (result);
635 /* Return if an argument is present. */
637 args_has(struct args *args, u_char flag)
639 struct args_entry *entry;
641 entry = args_find(args, flag);
642 if (entry == NULL)
643 return (0);
644 return (entry->count);
647 /* Set argument value in the arguments tree. */
648 void
649 args_set(struct args *args, u_char flag, struct args_value *value, int flags)
651 struct args_entry *entry;
653 entry = args_find(args, flag);
654 if (entry == NULL) {
655 entry = xcalloc(1, sizeof *entry);
656 entry->flag = flag;
657 entry->count = 1;
658 entry->flags = flags;
659 TAILQ_INIT(&entry->values);
660 RB_INSERT(args_tree, &args->tree, entry);
661 } else
662 entry->count++;
663 if (value != NULL && value->type != ARGS_NONE)
664 TAILQ_INSERT_TAIL(&entry->values, value, entry);
667 /* Get argument value. Will be NULL if it isn't present. */
668 const char *
669 args_get(struct args *args, u_char flag)
671 struct args_entry *entry;
673 if ((entry = args_find(args, flag)) == NULL)
674 return (NULL);
675 if (TAILQ_EMPTY(&entry->values))
676 return (NULL);
677 return (TAILQ_LAST(&entry->values, args_values)->string);
680 /* Get first argument. */
681 u_char
682 args_first(struct args *args, struct args_entry **entry)
684 *entry = RB_MIN(args_tree, &args->tree);
685 if (*entry == NULL)
686 return (0);
687 return ((*entry)->flag);
690 /* Get next argument. */
691 u_char
692 args_next(struct args_entry **entry)
694 *entry = RB_NEXT(args_tree, &args->tree, *entry);
695 if (*entry == NULL)
696 return (0);
697 return ((*entry)->flag);
700 /* Get argument count. */
701 u_int
702 args_count(struct args *args)
704 return (args->count);
707 /* Get argument values. */
708 struct args_value *
709 args_values(struct args *args)
711 return (args->values);
714 /* Get argument value. */
715 struct args_value *
716 args_value(struct args *args, u_int idx)
718 if (idx >= args->count)
719 return (NULL);
720 return (&args->values[idx]);
723 /* Return argument as string. */
724 const char *
725 args_string(struct args *args, u_int idx)
727 if (idx >= args->count)
728 return (NULL);
729 return (args_value_as_string(&args->values[idx]));
732 /* Make a command now. */
733 struct cmd_list *
734 args_make_commands_now(struct cmd *self, struct cmdq_item *item, u_int idx,
735 int expand)
737 struct args_command_state *state;
738 char *error;
739 struct cmd_list *cmdlist;
741 state = args_make_commands_prepare(self, item, idx, NULL, 0, expand);
742 cmdlist = args_make_commands(state, 0, NULL, &error);
743 if (cmdlist == NULL) {
744 cmdq_error(item, "%s", error);
745 free(error);
747 else
748 cmdlist->references++;
749 args_make_commands_free(state);
750 return (cmdlist);
753 /* Save bits to make a command later. */
754 struct args_command_state *
755 args_make_commands_prepare(struct cmd *self, struct cmdq_item *item, u_int idx,
756 const char *default_command, int wait, int expand)
758 struct args *args = cmd_get_args(self);
759 struct cmd_find_state *target = cmdq_get_target(item);
760 struct client *tc = cmdq_get_target_client(item);
761 struct args_value *value;
762 struct args_command_state *state;
763 const char *cmd;
764 const char *file;
766 state = xcalloc(1, sizeof *state);
768 if (idx < args->count) {
769 value = &args->values[idx];
770 if (value->type == ARGS_COMMANDS) {
771 state->cmdlist = value->cmdlist;
772 state->cmdlist->references++;
773 return (state);
775 cmd = value->string;
776 } else {
777 if (default_command == NULL)
778 fatalx("argument out of range");
779 cmd = default_command;
783 if (expand)
784 state->cmd = format_single_from_target(item, cmd);
785 else
786 state->cmd = xstrdup(cmd);
787 log_debug("%s: %s", __func__, state->cmd);
789 if (wait)
790 state->pi.item = item;
791 cmd_get_source(self, &file, &state->pi.line);
792 if (file != NULL)
793 state->pi.file = xstrdup(file);
794 state->pi.c = tc;
795 if (state->pi.c != NULL)
796 state->pi.c->references++;
797 cmd_find_copy_state(&state->pi.fs, target);
799 return (state);
802 /* Return argument as command. */
803 struct cmd_list *
804 args_make_commands(struct args_command_state *state, int argc, char **argv,
805 char **error)
807 struct cmd_parse_result *pr;
808 char *cmd, *new_cmd;
809 int i;
811 if (state->cmdlist != NULL) {
812 if (argc == 0)
813 return (state->cmdlist);
814 return (cmd_list_copy(state->cmdlist, argc, argv));
817 cmd = xstrdup(state->cmd);
818 log_debug("%s: %s", __func__, cmd);
819 cmd_log_argv(argc, argv, __func__);
820 for (i = 0; i < argc; i++) {
821 new_cmd = cmd_template_replace(cmd, argv[i], i + 1);
822 log_debug("%s: %%%u %s: %s", __func__, i + 1, argv[i], new_cmd);
823 free(cmd);
824 cmd = new_cmd;
826 log_debug("%s: %s", __func__, cmd);
828 pr = cmd_parse_from_string(cmd, &state->pi);
829 free(cmd);
830 switch (pr->status) {
831 case CMD_PARSE_ERROR:
832 *error = pr->error;
833 return (NULL);
834 case CMD_PARSE_SUCCESS:
835 return (pr->cmdlist);
837 fatalx("invalid parse return state");
840 /* Free commands state. */
841 void
842 args_make_commands_free(struct args_command_state *state)
844 if (state->cmdlist != NULL)
845 cmd_list_free(state->cmdlist);
846 if (state->pi.c != NULL)
847 server_client_unref(state->pi.c);
848 free((void *)state->pi.file);
849 free(state->cmd);
850 free(state);
853 /* Get prepared command. */
854 char *
855 args_make_commands_get_command(struct args_command_state *state)
857 struct cmd *first;
858 int n;
859 char *s;
861 if (state->cmdlist != NULL) {
862 first = cmd_list_first(state->cmdlist);
863 if (first == NULL)
864 return (xstrdup(""));
865 return (xstrdup(cmd_get_entry(first)->name));
867 n = strcspn(state->cmd, " ,");
868 xasprintf(&s, "%.*s", n, state->cmd);
869 return (s);
872 /* Get first value in argument. */
873 struct args_value *
874 args_first_value(struct args *args, u_char flag)
876 struct args_entry *entry;
878 if ((entry = args_find(args, flag)) == NULL)
879 return (NULL);
880 return (TAILQ_FIRST(&entry->values));
883 /* Get next value in argument. */
884 struct args_value *
885 args_next_value(struct args_value *value)
887 return (TAILQ_NEXT(value, entry));
890 /* Convert an argument value to a number. */
891 long long
892 args_strtonum(struct args *args, u_char flag, long long minval,
893 long long maxval, char **cause)
895 const char *errstr;
896 long long ll;
897 struct args_entry *entry;
898 struct args_value *value;
900 if ((entry = args_find(args, flag)) == NULL) {
901 *cause = xstrdup("missing");
902 return (0);
904 value = TAILQ_LAST(&entry->values, args_values);
905 if (value == NULL ||
906 value->type != ARGS_STRING ||
907 value->string == NULL) {
908 *cause = xstrdup("missing");
909 return (0);
912 ll = strtonum(value->string, minval, maxval, &errstr);
913 if (errstr != NULL) {
914 *cause = xstrdup(errstr);
915 return (0);
918 *cause = NULL;
919 return (ll);
922 /* Convert an argument value to a number, and expand formats. */
923 long long
924 args_strtonum_and_expand(struct args *args, u_char flag, long long minval,
925 long long maxval, struct cmdq_item *item, char **cause)
927 const char *errstr;
928 char *formatted;
929 long long ll;
930 struct args_entry *entry;
931 struct args_value *value;
933 if ((entry = args_find(args, flag)) == NULL) {
934 *cause = xstrdup("missing");
935 return (0);
937 value = TAILQ_LAST(&entry->values, args_values);
938 if (value == NULL ||
939 value->type != ARGS_STRING ||
940 value->string == NULL) {
941 *cause = xstrdup("missing");
942 return (0);
945 formatted = format_single_from_target(item, value->string);
946 ll = strtonum(formatted, minval, maxval, &errstr);
947 free(formatted);
948 if (errstr != NULL) {
949 *cause = xstrdup(errstr);
950 return (0);
953 *cause = NULL;
954 return (ll);
957 /* Convert an argument to a number which may be a percentage. */
958 long long
959 args_percentage(struct args *args, u_char flag, long long minval,
960 long long maxval, long long curval, char **cause)
962 const char *value;
963 struct args_entry *entry;
965 if ((entry = args_find(args, flag)) == NULL) {
966 *cause = xstrdup("missing");
967 return (0);
969 if (TAILQ_EMPTY(&entry->values)) {
970 *cause = xstrdup("empty");
971 return (0);
973 value = TAILQ_LAST(&entry->values, args_values)->string;
974 return (args_string_percentage(value, minval, maxval, curval, cause));
977 /* Convert a string to a number which may be a percentage. */
978 long long
979 args_string_percentage(const char *value, long long minval, long long maxval,
980 long long curval, char **cause)
982 const char *errstr;
983 long long ll;
984 size_t valuelen = strlen(value);
985 char *copy;
987 if (valuelen == 0) {
988 *cause = xstrdup("empty");
989 return (0);
991 if (value[valuelen - 1] == '%') {
992 copy = xstrdup(value);
993 copy[valuelen - 1] = '\0';
995 ll = strtonum(copy, 0, 100, &errstr);
996 free(copy);
997 if (errstr != NULL) {
998 *cause = xstrdup(errstr);
999 return (0);
1001 ll = (curval * ll) / 100;
1002 if (ll < minval) {
1003 *cause = xstrdup("too small");
1004 return (0);
1006 if (ll > maxval) {
1007 *cause = xstrdup("too large");
1008 return (0);
1010 } else {
1011 ll = strtonum(value, minval, maxval, &errstr);
1012 if (errstr != NULL) {
1013 *cause = xstrdup(errstr);
1014 return (0);
1018 *cause = NULL;
1019 return (ll);
1023 * Convert an argument to a number which may be a percentage, and expand
1024 * formats.
1026 long long
1027 args_percentage_and_expand(struct args *args, u_char flag, long long minval,
1028 long long maxval, long long curval, struct cmdq_item *item, char **cause)
1030 const char *value;
1031 struct args_entry *entry;
1033 if ((entry = args_find(args, flag)) == NULL) {
1034 *cause = xstrdup("missing");
1035 return (0);
1037 if (TAILQ_EMPTY(&entry->values)) {
1038 *cause = xstrdup("empty");
1039 return (0);
1041 value = TAILQ_LAST(&entry->values, args_values)->string;
1042 return (args_string_percentage_and_expand(value, minval, maxval, curval,
1043 item, cause));
1047 * Convert a string to a number which may be a percentage, and expand formats.
1049 long long
1050 args_string_percentage_and_expand(const char *value, long long minval,
1051 long long maxval, long long curval, struct cmdq_item *item, char **cause)
1053 const char *errstr;
1054 long long ll;
1055 size_t valuelen = strlen(value);
1056 char *copy, *f;
1058 if (value[valuelen - 1] == '%') {
1059 copy = xstrdup(value);
1060 copy[valuelen - 1] = '\0';
1062 f = format_single_from_target(item, copy);
1063 ll = strtonum(f, 0, 100, &errstr);
1064 free(f);
1065 free(copy);
1066 if (errstr != NULL) {
1067 *cause = xstrdup(errstr);
1068 return (0);
1070 ll = (curval * ll) / 100;
1071 if (ll < minval) {
1072 *cause = xstrdup("too small");
1073 return (0);
1075 if (ll > maxval) {
1076 *cause = xstrdup("too large");
1077 return (0);
1079 } else {
1080 f = format_single_from_target(item, value);
1081 ll = strtonum(f, minval, maxval, &errstr);
1082 free(f);
1083 if (errstr != NULL) {
1084 *cause = xstrdup(errstr);
1085 return (0);
1089 *cause = NULL;
1090 return (ll);