/* $OpenBSD$ */
/*
* Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* 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>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <vis.h>
#include "tmux.h"
/*
* Manipulate command arguments.
*/
/* List of argument values. */
TAILQ_HEAD(args_values, args_value);
/* Single arguments flag. */
struct args_entry {
u_char flag;
struct args_values values;
u_int count;
RB_ENTRY(args_entry) entry;
};
/* Parsed argument flags and values. */
struct args {
struct args_tree tree;
u_int count;
struct args_value *values;
};
/* Prepared command state. */
struct args_command_state {
struct cmd_list *cmdlist;
char *cmd;
struct cmd_parse_input pi;
};
static struct args_entry *args_find(struct args *, u_char);
static int args_cmp(struct args_entry *, struct args_entry *);
RB_GENERATE_STATIC(args_tree, args_entry, entry, args_cmp);
/* Arguments tree comparison function. */
static int
args_cmp(struct args_entry *a1, struct args_entry *a2)
{
return (a1->flag - a2->flag);
}
/* Find a flag in the arguments tree. */
static struct args_entry *
args_find(struct args *args, u_char flag)
{
struct args_entry entry;
entry.flag = flag;
return (RB_FIND(args_tree, &args->tree, &entry));
}
/* 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;
}
}
/* Get value as string. */
static const char *
args_value_as_string(struct args_value *value)
{
switch (value->type) {
case ARGS_NONE:
return ("");
case ARGS_COMMANDS:
if (value->cached == NULL)
value->cached = cmd_list_print(value->cmdlist, 0);
return (value->cached);
case ARGS_STRING:
return (value->string);
}
fatalx("unexpected argument type");
}
/* Create an empty arguments set. */
struct args *
args_create(void)
{
struct args *args;
args = xcalloc(1, sizeof *args);
RB_INIT(&args->tree);
return (args);
}
/* Parse arguments into a new argument set. */
struct args *
args_parse(const struct args_parse *parse, struct args_value *values,
u_int count, char **cause)
{
struct args *args;
u_int i;
enum args_parse_type type;
struct args_value *value, *new;
u_char flag;
const char *found, *string, *s;
int optional_argument;
if (count == 0)
return (args_create());
args = args_create();
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;
if (flag == '?') {
args_free(args);
return (NULL);
}
if (!isalnum(flag)) {
xasprintf(cause, "invalid flag -%c"