2011-01-04 00:42:46 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2011-01-04 00:42:46 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2021-08-21 18:39:07 +00:00
|
|
|
#include <ctype.h>
|
2011-01-04 00:42:46 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-01-18 10:00:50 +00:00
|
|
|
#include <vis.h>
|
2011-01-04 00:42:46 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2013-05-31 12:19:34 +00:00
|
|
|
/*
|
|
|
|
* Manipulate command arguments.
|
|
|
|
*/
|
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
/* List of argument values. */
|
2019-04-28 20:05:50 +00:00
|
|
|
TAILQ_HEAD(args_values, args_value);
|
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
/* Single arguments flag. */
|
2015-08-29 23:19:52 +00:00
|
|
|
struct args_entry {
|
|
|
|
u_char flag;
|
2019-04-28 20:05:50 +00:00
|
|
|
struct args_values values;
|
2019-07-09 14:03:12 +00:00
|
|
|
u_int count;
|
2015-08-29 23:19:52 +00:00
|
|
|
RB_ENTRY(args_entry) entry;
|
|
|
|
};
|
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
/* Parsed argument flags and values. */
|
2021-08-20 19:50:16 +00:00
|
|
|
struct args {
|
2021-08-21 20:57:52 +00:00
|
|
|
struct args_tree tree;
|
|
|
|
u_int count;
|
|
|
|
struct args_value *values;
|
2021-08-20 19:50:16 +00:00
|
|
|
};
|
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
/* Prepared command state. */
|
|
|
|
struct args_command_state {
|
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
char *cmd;
|
|
|
|
struct cmd_parse_input pi;
|
|
|
|
};
|
|
|
|
|
2016-10-10 13:54:47 +00:00
|
|
|
static struct args_entry *args_find(struct args *, u_char);
|
2013-05-31 12:19:34 +00:00
|
|
|
|
2016-10-10 13:54:47 +00:00
|
|
|
static int args_cmp(struct args_entry *, struct args_entry *);
|
|
|
|
RB_GENERATE_STATIC(args_tree, args_entry, entry, args_cmp);
|
2013-05-31 12:19:34 +00:00
|
|
|
|
|
|
|
/* Arguments tree comparison function. */
|
2016-10-10 13:54:47 +00:00
|
|
|
static int
|
2013-05-31 12:19:34 +00:00
|
|
|
args_cmp(struct args_entry *a1, struct args_entry *a2)
|
|
|
|
{
|
|
|
|
return (a1->flag - a2->flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find a flag in the arguments tree. */
|
2016-10-10 13:54:47 +00:00
|
|
|
static struct args_entry *
|
2020-05-16 15:40:04 +00:00
|
|
|
args_find(struct args *args, u_char flag)
|
2013-05-31 12:19:34 +00:00
|
|
|
{
|
|
|
|
struct args_entry entry;
|
|
|
|
|
2020-05-16 15:40:04 +00:00
|
|
|
entry.flag = flag;
|
2013-05-31 12:19:34 +00:00
|
|
|
return (RB_FIND(args_tree, &args->tree, &entry));
|
|
|
|
}
|
|
|
|
|
2021-08-21 20:57:52 +00:00
|
|
|
/* Copy value. */
|
|
|
|
static void
|
|
|
|
args_copy_value(struct args_value *to, struct args_value *from)
|
|
|
|
{
|
|
|
|
to->type = from->type;
|
|
|
|
switch (from->type) {
|
|
|
|
case ARGS_NONE:
|
|
|
|
break;
|
|
|
|
case ARGS_COMMANDS:
|
|
|
|
to->cmdlist = from->cmdlist;
|
|
|
|
to->cmdlist->references++;
|
|
|
|
break;
|
|
|
|
case ARGS_STRING:
|
|
|
|
to->string = xstrdup(from->string);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 18:39:07 +00:00
|
|
|
/* Get value as string. */
|
2021-08-21 20:57:52 +00:00
|
|
|
static const char *
|
2021-08-21 18:39:07 +00:00
|
|
|
args_value_as_string(struct args_value *value)
|
|
|
|
{
|
|
|
|
switch (value->type) {
|
|
|
|
case ARGS_NONE:
|
2021-08-21 20:57:52 +00:00
|
|
|
return ("");
|
2021-08-21 18:39:07 +00:00
|
|
|
case ARGS_COMMANDS:
|
2021-08-21 20:57:52 +00:00
|
|
|
if (value->cached == NULL)
|
|
|
|
value->cached = cmd_list_print(value->cmdlist, 0);
|
|
|
|
return (value->cached);
|
2021-08-21 18:39:07 +00:00
|
|
|
case ARGS_STRING:
|
2021-08-21 20:57:52 +00:00
|
|
|
return (value->string);
|
2021-08-21 18:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 17:53:54 +00:00
|
|
|
/* Create an empty arguments set. */
|
|
|
|
struct args *
|
|
|
|
args_create(void)
|
|
|
|
{
|
|
|
|
struct args *args;
|
|
|
|
|
|
|
|
args = xcalloc(1, sizeof *args);
|
|
|
|
RB_INIT(&args->tree);
|
|
|
|
return (args);
|
|
|
|
}
|
|
|
|
|
2021-08-21 18:39:07 +00:00
|
|
|
/* Parse arguments into a new argument set. */
|
2011-01-04 00:42:46 +00:00
|
|
|
struct args *
|
2021-08-21 18:39:07 +00:00
|
|
|
args_parse(const struct args_parse *parse, struct args_value *values,
|
2021-08-25 08:51:55 +00:00
|
|
|
u_int count, char **cause)
|
2011-01-04 00:42:46 +00:00
|
|
|
{
|
2021-08-21 18:39:07 +00:00
|
|
|
struct args *args;
|
2021-08-23 17:05:43 +00:00
|
|
|
u_int i;
|
2021-08-25 08:51:55 +00:00
|
|
|
enum args_parse_type type;
|
2021-08-21 20:57:52 +00:00
|
|
|
struct args_value *value, *new;
|
2021-08-21 18:39:07 +00:00
|
|
|
u_char flag, argument;
|
2021-08-21 20:57:52 +00:00
|
|
|
const char *found, *string, *s;
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2021-08-21 18:39:07 +00:00
|
|
|
if (count == 0)
|
|
|
|
return (args_create());
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2021-08-20 17:53:54 +00:00
|
|
|
args = args_create();
|
2021-08-21 18:39:07 +00:00
|
|
|
for (i = 1; i < count; /* nothing */) {
|
|
|
|
value = &values[i];
|
|
|
|
if (value->type != ARGS_STRING)
|
|
|
|
break;
|
|
|
|
|
|
|
|
string = value->string;
|
|
|
|
if (*string++ != '-' || *string == '\0')
|
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
if (string[0] == '-' && string[1] == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
flag = *string++;
|
|
|
|
if (flag == '\0')
|
|
|
|
break;
|
2021-09-09 21:55:03 +00:00
|
|
|
if (flag == '?') {
|
|
|
|
args_free(args);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2021-08-21 18:39:07 +00:00
|
|
|
if (!isalnum(flag)) {
|
2021-08-25 08:51:55 +00:00
|
|
|
xasprintf(cause, "invalid flag -%c", flag);
|
2021-08-21 18:39:07 +00:00
|
|
|
args_free(args);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
found = strchr(parse->template, flag);
|
|
|
|
if (found == NULL) {
|
2021-08-25 08:51:55 +00:00
|
|
|
xasprintf(cause, "unknown flag -%c", flag);
|
2021-08-21 18:39:07 +00:00
|
|
|
args_free(args);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
argument = *++found;
|
|
|
|
if (argument != ':') {
|
2021-08-27 17:25:55 +00:00
|
|
|
log_debug("%s: -%c", __func__, flag);
|
2021-08-21 18:39:07 +00:00
|
|
|
args_set(args, flag, NULL);
|
|
|
|
continue;
|
|
|
|
}
|
2021-08-25 08:51:55 +00:00
|
|
|
new = xcalloc(1, sizeof *new);
|
2021-08-21 20:57:52 +00:00
|
|
|
if (*string != '\0') {
|
|
|
|
new->type = ARGS_STRING;
|
|
|
|
new->string = xstrdup(string);
|
|
|
|
} else {
|
2021-08-21 18:39:07 +00:00
|
|
|
if (i == count) {
|
2021-08-25 08:51:55 +00:00
|
|
|
xasprintf(cause,
|
|
|
|
"-%c expects an argument",
|
|
|
|
flag);
|
|
|
|
args_free(args);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (values[i].type != ARGS_STRING) {
|
|
|
|
xasprintf(cause,
|
|
|
|
"-%c argument must be a string",
|
|
|
|
flag);
|
2021-08-21 18:39:07 +00:00
|
|
|
args_free(args);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2021-08-21 20:57:52 +00:00
|
|
|
args_copy_value(new, &values[i++]);
|
2021-08-21 18:39:07 +00:00
|
|
|
}
|
2021-08-21 20:57:52 +00:00
|
|
|
s = args_value_as_string(new);
|
2021-08-27 17:25:55 +00:00
|
|
|
log_debug("%s: -%c = %s", __func__, flag, s);
|
2021-08-21 20:57:52 +00:00
|
|
|
args_set(args, flag, new);
|
2021-08-21 18:39:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log_debug("%s: flags end at %u of %u", __func__, i, count);
|
|
|
|
if (i != count) {
|
|
|
|
for (/* nothing */; i < count; i++) {
|
|
|
|
value = &values[i];
|
|
|
|
|
|
|
|
s = args_value_as_string(value);
|
2021-08-27 17:25:55 +00:00
|
|
|
log_debug("%s: %u = %s (type %d)", __func__, i, s,
|
|
|
|
value->type);
|
2021-08-21 20:57:52 +00:00
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
if (parse->cb != NULL) {
|
|
|
|
type = parse->cb(args, args->count, cause);
|
|
|
|
if (type == ARGS_PARSE_INVALID) {
|
|
|
|
args_free(args);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
type = ARGS_PARSE_STRING;
|
|
|
|
|
2021-08-21 20:57:52 +00:00
|
|
|
args->values = xrecallocarray(args->values,
|
|
|
|
args->count, args->count + 1, sizeof *args->values);
|
2021-08-25 08:51:55 +00:00
|
|
|
new = &args->values[args->count++];
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case ARGS_PARSE_INVALID:
|
|
|
|
fatalx("unexpected argument type");
|
|
|
|
case ARGS_PARSE_STRING:
|
|
|
|
if (value->type != ARGS_STRING) {
|
|
|
|
xasprintf(cause,
|
|
|
|
"argument %u must be \"string\"",
|
|
|
|
args->count);
|
|
|
|
args_free(args);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
args_copy_value(new, value);
|
|
|
|
break;
|
|
|
|
case ARGS_PARSE_COMMANDS_OR_STRING:
|
|
|
|
args_copy_value(new, value);
|
|
|
|
break;
|
|
|
|
case ARGS_PARSE_COMMANDS:
|
|
|
|
if (value->type != ARGS_COMMANDS) {
|
|
|
|
xasprintf(cause,
|
|
|
|
"argument %u must be { commands }",
|
|
|
|
args->count);
|
|
|
|
args_free(args);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
args_copy_value(new, value);
|
|
|
|
break;
|
|
|
|
}
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:51:55 +00:00
|
|
|
if (parse->lower != -1 && args->count < (u_int)parse->lower) {
|
|
|
|
xasprintf(cause,
|
|
|
|
"too few arguments (need at least %u)",
|
|
|
|
parse->lower);
|
|
|
|
args_free(args);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (parse->upper != -1 && args->count > (u_int)parse->upper) {
|
|
|
|
xasprintf(cause,
|
|
|
|
"too many arguments (need at most %u)",
|
|
|
|
parse->upper);
|
2021-08-20 19:50:16 +00:00
|
|
|
args_free(args);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2011-01-04 00:42:46 +00:00
|
|
|
return (args);
|
|
|
|
}
|
|
|
|
|
2021-08-27 17:25:55 +00:00
|
|
|
/* Copy and expand a value. */
|
|
|
|
static void
|
|
|
|
args_copy_copy_value(struct args_value *to, struct args_value *from, int argc,
|
|
|
|
char **argv)
|
|
|
|
{
|
|
|
|
char *s, *expanded;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
to->type = from->type;
|
|
|
|
switch (from->type) {
|
|
|
|
case ARGS_NONE:
|
|
|
|
break;
|
|
|
|
case ARGS_STRING:
|
|
|
|
expanded = xstrdup(from->string);
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
s = cmd_template_replace(expanded, argv[i], i + 1);
|
|
|
|
free(expanded);
|
|
|
|
expanded = s;
|
|
|
|
}
|
|
|
|
to->string = expanded;
|
|
|
|
break;
|
|
|
|
case ARGS_COMMANDS:
|
|
|
|
to->cmdlist = cmd_list_copy(from->cmdlist, argc, argv);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy an arguments set. */
|
|
|
|
struct args *
|
|
|
|
args_copy(struct args *args, int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct args *new_args;
|
|
|
|
struct args_entry *entry;
|
|
|
|
struct args_value *value, *new_value;
|
|
|
|
u_int i;
|
|
|
|
|
2021-09-02 07:11:03 +00:00
|
|
|
cmd_log_argv(argc, argv, "%s", __func__);
|
|
|
|
|
2021-08-27 17:25:55 +00:00
|
|
|
new_args = args_create();
|
|
|
|
RB_FOREACH(entry, args_tree, &args->tree) {
|
2021-09-02 07:11:03 +00:00
|
|
|
if (TAILQ_EMPTY(&entry->values)) {
|
|
|
|
for (i = 0; i < entry->count; i++)
|
|
|
|
args_set(new_args, entry->flag, NULL);
|
2021-08-27 17:25:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TAILQ_FOREACH(value, &entry->values, entry) {
|
|
|
|
new_value = xcalloc(1, sizeof *new_value);
|
|
|
|
args_copy_copy_value(new_value, value, argc, argv);
|
|
|
|
args_set(new_args, entry->flag, new_value);
|
|
|
|
}
|
|
|
|
}
|
2021-09-02 07:11:03 +00:00
|
|
|
if (args->count == 0)
|
|
|
|
return (new_args);
|
2021-08-27 17:25:55 +00:00
|
|
|
new_args->count = args->count;
|
|
|
|
new_args->values = xcalloc(args->count, sizeof *new_args->values);
|
|
|
|
for (i = 0; i < args->count; i++) {
|
|
|
|
new_value = &new_args->values[i];
|
|
|
|
args_copy_copy_value(new_value, &args->values[i], argc, argv);
|
|
|
|
}
|
|
|
|
return (new_args);
|
|
|
|
}
|
|
|
|
|
2021-08-21 18:39:07 +00:00
|
|
|
/* Free a value. */
|
|
|
|
void
|
|
|
|
args_free_value(struct args_value *value)
|
|
|
|
{
|
|
|
|
switch (value->type) {
|
|
|
|
case ARGS_NONE:
|
|
|
|
break;
|
|
|
|
case ARGS_STRING:
|
|
|
|
free(value->string);
|
|
|
|
break;
|
|
|
|
case ARGS_COMMANDS:
|
|
|
|
cmd_list_free(value->cmdlist);
|
|
|
|
break;
|
|
|
|
}
|
2021-08-21 20:57:52 +00:00
|
|
|
free(value->cached);
|
2021-08-21 18:39:07 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 17:25:55 +00:00
|
|
|
/* Free values. */
|
|
|
|
void
|
|
|
|
args_free_values(struct args_value *values, u_int count)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
args_free_value(&values[i]);
|
|
|
|
}
|
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
/* Free an arguments set. */
|
|
|
|
void
|
|
|
|
args_free(struct args *args)
|
|
|
|
{
|
2013-05-31 12:19:34 +00:00
|
|
|
struct args_entry *entry;
|
|
|
|
struct args_entry *entry1;
|
2019-04-28 20:05:50 +00:00
|
|
|
struct args_value *value;
|
|
|
|
struct args_value *value1;
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2021-08-27 17:25:55 +00:00
|
|
|
args_free_values(args->values, args->count);
|
2021-08-21 20:57:52 +00:00
|
|
|
free(args->values);
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2013-05-31 12:19:34 +00:00
|
|
|
RB_FOREACH_SAFE(entry, args_tree, &args->tree, entry1) {
|
|
|
|
RB_REMOVE(args_tree, &args->tree, entry);
|
2019-04-28 20:05:50 +00:00
|
|
|
TAILQ_FOREACH_SAFE(value, &entry->values, entry, value1) {
|
|
|
|
TAILQ_REMOVE(&entry->values, value, entry);
|
2021-08-21 20:57:52 +00:00
|
|
|
args_free_value(value);
|
2019-04-28 20:05:50 +00:00
|
|
|
free(value);
|
|
|
|
}
|
2013-05-31 12:19:34 +00:00
|
|
|
free(entry);
|
|
|
|
}
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(args);
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 19:50:16 +00:00
|
|
|
/* Convert arguments to vector. */
|
|
|
|
void
|
2021-08-27 17:25:55 +00:00
|
|
|
args_to_vector(struct args *args, int *argc, char ***argv)
|
2021-08-20 19:50:16 +00:00
|
|
|
{
|
2021-08-25 08:51:55 +00:00
|
|
|
char *s;
|
|
|
|
u_int i;
|
2021-08-21 20:57:52 +00:00
|
|
|
|
|
|
|
*argc = 0;
|
|
|
|
*argv = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < args->count; i++) {
|
2021-08-25 08:51:55 +00:00
|
|
|
switch (args->values[i].type) {
|
|
|
|
case ARGS_NONE:
|
|
|
|
break;
|
|
|
|
case ARGS_STRING:
|
|
|
|
cmd_append_argv(argc, argv, args->values[i].string);
|
|
|
|
break;
|
|
|
|
case ARGS_COMMANDS:
|
|
|
|
s = cmd_list_print(args->values[i].cmdlist, 0);
|
|
|
|
cmd_append_argv(argc, argv, s);
|
|
|
|
free(s);
|
|
|
|
break;
|
|
|
|
}
|
2021-08-21 20:57:52 +00:00
|
|
|
}
|
2021-08-20 19:50:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 17:25:55 +00:00
|
|
|
/* Convert arguments from vector. */
|
|
|
|
struct args_value *
|
|
|
|
args_from_vector(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct args_value *values;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
values = xcalloc(argc, sizeof *values);
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
values[i].type = ARGS_STRING;
|
|
|
|
values[i].string = xstrdup(argv[i]);
|
|
|
|
}
|
|
|
|
return (values);
|
|
|
|
}
|
|
|
|
|
2015-11-27 15:06:43 +00:00
|
|
|
/* Add to string. */
|
|
|
|
static void printflike(3, 4)
|
|
|
|
args_print_add(char **buf, size_t *len, const char *fmt, ...)
|
|
|
|
{
|
2021-08-20 20:08:30 +00:00
|
|
|
va_list ap;
|
2015-11-27 15:06:43 +00:00
|
|
|
char *s;
|
|
|
|
size_t slen;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
slen = xvasprintf(&s, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
*len += slen;
|
|
|
|
*buf = xrealloc(*buf, *len);
|
|
|
|
|
|
|
|
strlcat(*buf, s, *len);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
2021-08-21 20:57:52 +00:00
|
|
|
/* Add value to string. */
|
2019-04-28 20:05:50 +00:00
|
|
|
static void
|
2021-08-21 20:57:52 +00:00
|
|
|
args_print_add_value(char **buf, size_t *len, struct args_value *value)
|
2019-04-28 20:05:50 +00:00
|
|
|
{
|
2021-08-21 20:57:52 +00:00
|
|
|
char *expanded = NULL;
|
2019-04-28 20:05:50 +00:00
|
|
|
|
|
|
|
if (**buf != '\0')
|
|
|
|
args_print_add(buf, len, " ");
|
|
|
|
|
2021-08-21 20:57:52 +00:00
|
|
|
switch (value->type) {
|
|
|
|
case ARGS_NONE:
|
|
|
|
break;
|
|
|
|
case ARGS_COMMANDS:
|
|
|
|
expanded = cmd_list_print(value->cmdlist, 0);
|
|
|
|
args_print_add(buf, len, "{ %s }", expanded);
|
|
|
|
break;
|
|
|
|
case ARGS_STRING:
|
|
|
|
expanded = args_escape(value->string);
|
|
|
|
args_print_add(buf, len, "%s", expanded);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(expanded);
|
2019-04-28 20:05:50 +00:00
|
|
|
}
|
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
/* Print a set of arguments. */
|
2015-11-27 15:06:43 +00:00
|
|
|
char *
|
|
|
|
args_print(struct args *args)
|
2011-01-04 00:42:46 +00:00
|
|
|
{
|
2021-08-20 20:08:30 +00:00
|
|
|
size_t len;
|
2019-04-28 20:05:50 +00:00
|
|
|
char *buf;
|
2021-08-21 20:57:52 +00:00
|
|
|
u_int i, j;
|
2013-05-31 12:19:34 +00:00
|
|
|
struct args_entry *entry;
|
2019-04-28 20:05:50 +00:00
|
|
|
struct args_value *value;
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2015-11-27 15:06:43 +00:00
|
|
|
len = 1;
|
|
|
|
buf = xcalloc(1, len);
|
2011-01-04 00:42:46 +00:00
|
|
|
|
|
|
|
/* Process the flags first. */
|
2013-05-31 12:19:34 +00:00
|
|
|
RB_FOREACH(entry, args_tree, &args->tree) {
|
2019-04-28 20:05:50 +00:00
|
|
|
if (!TAILQ_EMPTY(&entry->values))
|
2011-01-04 00:42:46 +00:00
|
|
|
continue;
|
|
|
|
|
2015-11-27 15:06:43 +00:00
|
|
|
if (*buf == '\0')
|
|
|
|
args_print_add(&buf, &len, "-");
|
2019-07-09 14:03:12 +00:00
|
|
|
for (j = 0; j < entry->count; j++)
|
|
|
|
args_print_add(&buf, &len, "%c", entry->flag);
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then the flags with arguments. */
|
2013-05-31 12:19:34 +00:00
|
|
|
RB_FOREACH(entry, args_tree, &args->tree) {
|
2021-08-20 19:50:16 +00:00
|
|
|
TAILQ_FOREACH(value, &entry->values, entry) {
|
|
|
|
if (*buf != '\0')
|
|
|
|
args_print_add(&buf, &len, " -%c", entry->flag);
|
|
|
|
else
|
|
|
|
args_print_add(&buf, &len, "-%c", entry->flag);
|
2021-08-21 20:57:52 +00:00
|
|
|
args_print_add_value(&buf, &len, value);
|
2021-08-20 19:50:16 +00:00
|
|
|
}
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* And finally the argument vector. */
|
2021-08-21 20:57:52 +00:00
|
|
|
for (i = 0; i < args->count; i++)
|
|
|
|
args_print_add_value(&buf, &len, &args->values[i]);
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2015-11-27 15:06:43 +00:00
|
|
|
return (buf);
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 14:03:44 +00:00
|
|
|
/* Escape an argument. */
|
|
|
|
char *
|
|
|
|
args_escape(const char *s)
|
|
|
|
{
|
2021-08-27 17:25:55 +00:00
|
|
|
static const char dquoted[] = " #';${}%";
|
2020-06-12 07:10:43 +00:00
|
|
|
static const char squoted[] = " \"";
|
2019-05-23 14:03:44 +00:00
|
|
|
char *escaped, *result;
|
2020-06-12 07:10:43 +00:00
|
|
|
int flags, quotes = 0;
|
2019-05-23 14:03:44 +00:00
|
|
|
|
2020-04-12 20:54:28 +00:00
|
|
|
if (*s == '\0') {
|
|
|
|
xasprintf(&result, "''");
|
|
|
|
return (result);
|
|
|
|
}
|
2020-06-12 07:10:43 +00:00
|
|
|
if (s[strcspn(s, dquoted)] != '\0')
|
|
|
|
quotes = '"';
|
|
|
|
else if (s[strcspn(s, squoted)] != '\0')
|
|
|
|
quotes = '\'';
|
|
|
|
|
2019-06-20 07:10:56 +00:00
|
|
|
if (s[0] != ' ' &&
|
2020-06-12 07:10:43 +00:00
|
|
|
s[1] == '\0' &&
|
|
|
|
(quotes != 0 || s[0] == '~')) {
|
2019-05-23 14:03:44 +00:00
|
|
|
xasprintf(&escaped, "\\%c", s[0]);
|
|
|
|
return (escaped);
|
|
|
|
}
|
|
|
|
|
2019-05-29 20:05:14 +00:00
|
|
|
flags = VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL;
|
2020-06-12 07:10:43 +00:00
|
|
|
if (quotes == '"')
|
2019-05-23 14:03:44 +00:00
|
|
|
flags |= VIS_DQ;
|
|
|
|
utf8_stravis(&escaped, s, flags);
|
|
|
|
|
2020-06-12 07:10:43 +00:00
|
|
|
if (quotes == '\'')
|
|
|
|
xasprintf(&result, "'%s'", escaped);
|
|
|
|
else if (quotes == '"') {
|
2019-05-23 14:03:44 +00:00
|
|
|
if (*escaped == '~')
|
|
|
|
xasprintf(&result, "\"\\%s\"", escaped);
|
|
|
|
else
|
|
|
|
xasprintf(&result, "\"%s\"", escaped);
|
|
|
|
} else {
|
|
|
|
if (*escaped == '~')
|
|
|
|
xasprintf(&result, "\\%s", escaped);
|
|
|
|
else
|
|
|
|
result = xstrdup(escaped);
|
|
|
|
}
|
|
|
|
free(escaped);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
/* Return if an argument is present. */
|
|
|
|
int
|
2020-05-16 15:40:04 +00:00
|
|
|
args_has(struct args *args, u_char flag)
|
2011-01-04 00:42:46 +00:00
|
|
|
{
|
2019-07-09 14:03:12 +00:00
|
|
|
struct args_entry *entry;
|
|
|
|
|
2020-05-16 15:40:04 +00:00
|
|
|
entry = args_find(args, flag);
|
2019-07-09 14:03:12 +00:00
|
|
|
if (entry == NULL)
|
|
|
|
return (0);
|
|
|
|
return (entry->count);
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 12:19:34 +00:00
|
|
|
/* Set argument value in the arguments tree. */
|
Rewrite of choose mode, both to simplify and tidy the code and to add
some modern features.
Now the common code is in mode-tree.c, which provides an API used by the
three modes now separated into window-{buffer,client,tree}.c. Buffer
mode shows buffers, client mode clients and tree mode a tree of
sessions, windows and panes.
Each mode has a common set of key bindings plus a few that are specific
to the mode. Other changes are:
- each mode has a preview pane: for buffers this is the buffer content
(very useful), for others it is a preview of the pane;
- items may be sorted in different ways ('O' key);
- multiple items may be tagged and an operation applied to all of them
(for example, to delete multiple buffers at once);
- in tree mode a command may be run on the selected item (session,
window, pane) or on tagged items (key ':');
- displayed items may be filtered in tree mode by using a format (this
is used to implement find-window) (key 'f');
- the custom format (-F) for the display is no longer available;
- shortcut keys change from 0-9, a-z, A-Z which was always a bit weird
with keys used for other uses to 0-9, M-a to M-z.
Now that the code is simpler, other improvements will come later.
Primary key bindings for each mode are documented under the commands in
the man page (choose-buffer, choose-client, choose-tree).
Parts written by Thomas Adam.
2017-05-30 21:44:59 +00:00
|
|
|
void
|
2021-08-21 20:57:52 +00:00
|
|
|
args_set(struct args *args, u_char flag, struct args_value *value)
|
2011-01-04 00:42:46 +00:00
|
|
|
{
|
2013-05-31 12:19:34 +00:00
|
|
|
struct args_entry *entry;
|
|
|
|
|
2020-05-16 15:40:04 +00:00
|
|
|
entry = args_find(args, flag);
|
2019-04-28 20:05:50 +00:00
|
|
|
if (entry == NULL) {
|
2014-01-09 13:51:57 +00:00
|
|
|
entry = xcalloc(1, sizeof *entry);
|
2020-05-16 15:40:04 +00:00
|
|
|
entry->flag = flag;
|
2019-07-09 14:03:12 +00:00
|
|
|
entry->count = 1;
|
2019-04-28 20:05:50 +00:00
|
|
|
TAILQ_INIT(&entry->values);
|
2014-01-09 13:51:57 +00:00
|
|
|
RB_INSERT(args_tree, &args->tree, entry);
|
2019-07-09 14:03:12 +00:00
|
|
|
} else
|
|
|
|
entry->count++;
|
2021-08-21 20:57:52 +00:00
|
|
|
if (value != NULL && value->type != ARGS_NONE)
|
2019-04-28 20:05:50 +00:00
|
|
|
TAILQ_INSERT_TAIL(&entry->values, value, entry);
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get argument value. Will be NULL if it isn't present. */
|
|
|
|
const char *
|
2020-05-16 15:40:04 +00:00
|
|
|
args_get(struct args *args, u_char flag)
|
2011-01-04 00:42:46 +00:00
|
|
|
{
|
2013-05-31 12:19:34 +00:00
|
|
|
struct args_entry *entry;
|
|
|
|
|
2020-05-16 15:40:04 +00:00
|
|
|
if ((entry = args_find(args, flag)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (TAILQ_EMPTY(&entry->values))
|
2013-05-31 12:19:34 +00:00
|
|
|
return (NULL);
|
2021-08-21 10:28:05 +00:00
|
|
|
return (TAILQ_LAST(&entry->values, args_values)->string);
|
2019-04-28 20:05:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 15:40:04 +00:00
|
|
|
/* Get first argument. */
|
|
|
|
u_char
|
|
|
|
args_first(struct args *args, struct args_entry **entry)
|
|
|
|
{
|
|
|
|
*entry = RB_MIN(args_tree, &args->tree);
|
|
|
|
if (*entry == NULL)
|
|
|
|
return (0);
|
|
|
|
return ((*entry)->flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get next argument. */
|
|
|
|
u_char
|
|
|
|
args_next(struct args_entry **entry)
|
|
|
|
{
|
|
|
|
*entry = RB_NEXT(args_tree, &args->tree, *entry);
|
|
|
|
if (*entry == NULL)
|
|
|
|
return (0);
|
|
|
|
return ((*entry)->flag);
|
|
|
|
}
|
|
|
|
|
2021-08-20 19:50:16 +00:00
|
|
|
/* Get argument count. */
|
|
|
|
u_int
|
|
|
|
args_count(struct args *args)
|
|
|
|
{
|
2021-08-21 20:57:52 +00:00
|
|
|
return (args->count);
|
|
|
|
}
|
|
|
|
|
2021-08-27 17:25:55 +00:00
|
|
|
/* Get argument values. */
|
|
|
|
struct args_value *
|
|
|
|
args_values(struct args *args)
|
|
|
|
{
|
|
|
|
return (args->values);
|
|
|
|
}
|
|
|
|
|
2021-08-21 20:57:52 +00:00
|
|
|
/* Get argument value. */
|
|
|
|
struct args_value *
|
|
|
|
args_value(struct args *args, u_int idx)
|
|
|
|
{
|
|
|
|
if (idx >= args->count)
|
|
|
|
return (NULL);
|
|
|
|
return (&args->values[idx]);
|
2021-08-20 19:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return argument as string. */
|
|
|
|
const char *
|
|
|
|
args_string(struct args *args, u_int idx)
|
|
|
|
{
|
2021-08-21 20:57:52 +00:00
|
|
|
if (idx >= args->count)
|
2021-08-20 19:50:16 +00:00
|
|
|
return (NULL);
|
2021-08-21 20:57:52 +00:00
|
|
|
return (args_value_as_string(&args->values[idx]));
|
2021-08-20 19:50:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 12:33:55 +00:00
|
|
|
/* Make a command now. */
|
|
|
|
struct cmd_list *
|
2021-09-09 13:38:32 +00:00
|
|
|
args_make_commands_now(struct cmd *self, struct cmdq_item *item, u_int idx,
|
|
|
|
int expand)
|
2021-08-23 12:33:55 +00:00
|
|
|
{
|
|
|
|
struct args_command_state *state;
|
|
|
|
char *error;
|
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
|
2021-09-09 13:38:32 +00:00
|
|
|
state = args_make_commands_prepare(self, item, idx, NULL, 0, expand);
|
2021-08-23 12:33:55 +00:00
|
|
|
cmdlist = args_make_commands(state, 0, NULL, &error);
|
|
|
|
if (cmdlist == NULL) {
|
|
|
|
cmdq_error(item, "%s", error);
|
|
|
|
free(error);
|
|
|
|
}
|
2021-08-27 17:25:55 +00:00
|
|
|
else
|
|
|
|
cmdlist->references++;
|
2021-08-23 17:05:43 +00:00
|
|
|
args_make_commands_free(state);
|
2021-08-23 12:33:55 +00:00
|
|
|
return (cmdlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save bits to make a command later. */
|
|
|
|
struct args_command_state *
|
|
|
|
args_make_commands_prepare(struct cmd *self, struct cmdq_item *item, u_int idx,
|
|
|
|
const char *default_command, int wait, int expand)
|
|
|
|
{
|
|
|
|
struct args *args = cmd_get_args(self);
|
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
|
|
|
struct args_value *value;
|
|
|
|
struct args_command_state *state;
|
|
|
|
const char *cmd;
|
|
|
|
|
|
|
|
state = xcalloc(1, sizeof *state);
|
|
|
|
|
|
|
|
if (idx < args->count) {
|
|
|
|
value = &args->values[idx];
|
|
|
|
if (value->type == ARGS_COMMANDS) {
|
|
|
|
state->cmdlist = value->cmdlist;
|
|
|
|
state->cmdlist->references++;
|
|
|
|
return (state);
|
|
|
|
}
|
|
|
|
cmd = value->string;
|
|
|
|
} else {
|
|
|
|
if (default_command == NULL)
|
|
|
|
fatalx("argument out of range");
|
|
|
|
cmd = default_command;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (expand)
|
|
|
|
state->cmd = format_single_from_target(item, cmd);
|
|
|
|
else
|
|
|
|
state->cmd = xstrdup(cmd);
|
|
|
|
log_debug("%s: %s", __func__, state->cmd);
|
|
|
|
|
|
|
|
if (wait)
|
|
|
|
state->pi.item = item;
|
|
|
|
cmd_get_source(self, &state->pi.file, &state->pi.line);
|
|
|
|
state->pi.c = tc;
|
|
|
|
if (state->pi.c != NULL)
|
|
|
|
state->pi.c->references++;
|
|
|
|
cmd_find_copy_state(&state->pi.fs, target);
|
|
|
|
|
|
|
|
return (state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return argument as command. */
|
|
|
|
struct cmd_list *
|
|
|
|
args_make_commands(struct args_command_state *state, int argc, char **argv,
|
|
|
|
char **error)
|
|
|
|
{
|
|
|
|
struct cmd_parse_result *pr;
|
|
|
|
char *cmd, *new_cmd;
|
|
|
|
int i;
|
|
|
|
|
2021-08-27 17:25:55 +00:00
|
|
|
if (state->cmdlist != NULL) {
|
|
|
|
if (argc == 0)
|
|
|
|
return (state->cmdlist);
|
|
|
|
return (cmd_list_copy(state->cmdlist, argc, argv));
|
|
|
|
}
|
2021-08-23 12:33:55 +00:00
|
|
|
|
|
|
|
cmd = xstrdup(state->cmd);
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
new_cmd = cmd_template_replace(cmd, argv[i], i + 1);
|
|
|
|
log_debug("%s: %%%u %s: %s", __func__, i + 1, argv[i], new_cmd);
|
|
|
|
free(cmd);
|
|
|
|
cmd = new_cmd;
|
|
|
|
}
|
|
|
|
log_debug("%s: %s", __func__, cmd);
|
|
|
|
|
|
|
|
pr = cmd_parse_from_string(cmd, &state->pi);
|
|
|
|
free(cmd);
|
|
|
|
switch (pr->status) {
|
|
|
|
case CMD_PARSE_ERROR:
|
|
|
|
*error = pr->error;
|
|
|
|
return (NULL);
|
|
|
|
case CMD_PARSE_SUCCESS:
|
|
|
|
return (pr->cmdlist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free commands state. */
|
|
|
|
void
|
|
|
|
args_make_commands_free(struct args_command_state *state)
|
|
|
|
{
|
|
|
|
if (state->cmdlist != NULL)
|
|
|
|
cmd_list_free(state->cmdlist);
|
|
|
|
if (state->pi.c != NULL)
|
|
|
|
server_client_unref(state->pi.c);
|
|
|
|
free(state->cmd);
|
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get prepared command. */
|
|
|
|
char *
|
|
|
|
args_make_commands_get_command(struct args_command_state *state)
|
|
|
|
{
|
|
|
|
struct cmd *first;
|
|
|
|
int n;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (state->cmdlist != NULL) {
|
|
|
|
first = cmd_list_first(state->cmdlist);
|
|
|
|
if (first == NULL)
|
|
|
|
return (xstrdup(""));
|
|
|
|
return (xstrdup(cmd_get_entry(first)->name));
|
|
|
|
}
|
|
|
|
n = strcspn(state->cmd, " ,");
|
|
|
|
xasprintf(&s, "%.*s", n, state->cmd);
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2019-04-28 20:05:50 +00:00
|
|
|
/* Get first value in argument. */
|
2021-08-20 18:59:53 +00:00
|
|
|
struct args_value *
|
|
|
|
args_first_value(struct args *args, u_char flag)
|
2019-04-28 20:05:50 +00:00
|
|
|
{
|
|
|
|
struct args_entry *entry;
|
|
|
|
|
2020-05-16 15:40:04 +00:00
|
|
|
if ((entry = args_find(args, flag)) == NULL)
|
2019-04-28 20:05:50 +00:00
|
|
|
return (NULL);
|
2021-08-20 18:59:53 +00:00
|
|
|
return (TAILQ_FIRST(&entry->values));
|
2019-04-28 20:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get next value in argument. */
|
2021-08-20 18:59:53 +00:00
|
|
|
struct args_value *
|
|
|
|
args_next_value(struct args_value *value)
|
2019-04-28 20:05:50 +00:00
|
|
|
{
|
2021-08-20 18:59:53 +00:00
|
|
|
return (TAILQ_NEXT(value, entry));
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert an argument value to a number. */
|
|
|
|
long long
|
2020-05-16 15:40:04 +00:00
|
|
|
args_strtonum(struct args *args, u_char flag, long long minval,
|
|
|
|
long long maxval, char **cause)
|
2011-01-04 00:42:46 +00:00
|
|
|
{
|
2013-05-31 12:19:34 +00:00
|
|
|
const char *errstr;
|
2021-08-20 20:08:30 +00:00
|
|
|
long long ll;
|
2013-05-31 12:19:34 +00:00
|
|
|
struct args_entry *entry;
|
2019-04-28 20:05:50 +00:00
|
|
|
struct args_value *value;
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2020-05-16 15:40:04 +00:00
|
|
|
if ((entry = args_find(args, flag)) == NULL) {
|
2011-01-04 00:42:46 +00:00
|
|
|
*cause = xstrdup("missing");
|
|
|
|
return (0);
|
|
|
|
}
|
2019-04-28 20:05:50 +00:00
|
|
|
value = TAILQ_LAST(&entry->values, args_values);
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2021-08-21 10:28:05 +00:00
|
|
|
ll = strtonum(value->string, minval, maxval, &errstr);
|
2011-01-04 00:42:46 +00:00
|
|
|
if (errstr != NULL) {
|
|
|
|
*cause = xstrdup(errstr);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
*cause = NULL;
|
|
|
|
return (ll);
|
|
|
|
}
|
2020-03-21 13:16:15 +00:00
|
|
|
|
|
|
|
/* Convert an argument to a number which may be a percentage. */
|
|
|
|
long long
|
2020-05-16 15:40:04 +00:00
|
|
|
args_percentage(struct args *args, u_char flag, long long minval,
|
2020-03-21 13:16:15 +00:00
|
|
|
long long maxval, long long curval, char **cause)
|
|
|
|
{
|
2020-04-22 06:57:13 +00:00
|
|
|
const char *value;
|
2020-03-21 13:16:15 +00:00
|
|
|
struct args_entry *entry;
|
|
|
|
|
2020-05-16 15:40:04 +00:00
|
|
|
if ((entry = args_find(args, flag)) == NULL) {
|
2020-03-21 13:16:15 +00:00
|
|
|
*cause = xstrdup("missing");
|
|
|
|
return (0);
|
|
|
|
}
|
2021-08-21 10:28:05 +00:00
|
|
|
value = TAILQ_LAST(&entry->values, args_values)->string;
|
2020-04-22 06:57:13 +00:00
|
|
|
return (args_string_percentage(value, minval, maxval, curval, cause));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a string to a number which may be a percentage. */
|
|
|
|
long long
|
|
|
|
args_string_percentage(const char *value, long long minval, long long maxval,
|
|
|
|
long long curval, char **cause)
|
|
|
|
{
|
|
|
|
const char *errstr;
|
2021-08-20 20:08:30 +00:00
|
|
|
long long ll;
|
2020-04-22 06:57:13 +00:00
|
|
|
size_t valuelen = strlen(value);
|
|
|
|
char *copy;
|
2020-03-21 13:16:15 +00:00
|
|
|
|
2020-04-22 06:57:13 +00:00
|
|
|
if (value[valuelen - 1] == '%') {
|
|
|
|
copy = xstrdup(value);
|
2020-03-21 13:16:15 +00:00
|
|
|
copy[valuelen - 1] = '\0';
|
|
|
|
|
|
|
|
ll = strtonum(copy, 0, 100, &errstr);
|
|
|
|
free(copy);
|
|
|
|
if (errstr != NULL) {
|
|
|
|
*cause = xstrdup(errstr);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
ll = (curval * ll) / 100;
|
|
|
|
if (ll < minval) {
|
2020-05-25 18:17:14 +00:00
|
|
|
*cause = xstrdup("too small");
|
2020-03-21 13:16:15 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (ll > maxval) {
|
2020-05-25 18:17:14 +00:00
|
|
|
*cause = xstrdup("too large");
|
2020-03-21 13:16:15 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-22 06:57:13 +00:00
|
|
|
ll = strtonum(value, minval, maxval, &errstr);
|
2020-03-21 13:16:15 +00:00
|
|
|
if (errstr != NULL) {
|
|
|
|
*cause = xstrdup(errstr);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*cause = NULL;
|
|
|
|
return (ll);
|
|
|
|
}
|