2011-01-04 00:42:46 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2014-10-20 23:27:14 +00:00
|
|
|
#include <getopt.h>
|
2011-01-04 00:42:46 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-11-17 20:19:36 +00:00
|
|
|
#include <unistd.h>
|
2011-01-04 00:42:46 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2013-05-31 12:19:34 +00:00
|
|
|
/*
|
|
|
|
* Manipulate command arguments.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct args_entry *args_find(struct args *, u_char);
|
|
|
|
|
|
|
|
RB_GENERATE(args_tree, args_entry, entry, args_cmp);
|
|
|
|
|
|
|
|
/* Arguments tree comparison function. */
|
|
|
|
int
|
|
|
|
args_cmp(struct args_entry *a1, struct args_entry *a2)
|
|
|
|
{
|
|
|
|
return (a1->flag - a2->flag);
|
|
|
|
}
|
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
/* Create an arguments set with no flags. */
|
|
|
|
struct args *
|
|
|
|
args_create(int argc, ...)
|
|
|
|
{
|
|
|
|
struct args *args;
|
|
|
|
va_list ap;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
args = xcalloc(1, sizeof *args);
|
|
|
|
|
|
|
|
args->argc = argc;
|
|
|
|
if (argc == 0)
|
|
|
|
args->argv = NULL;
|
|
|
|
else
|
2011-07-09 01:36:42 +00:00
|
|
|
args->argv = xcalloc(argc, sizeof *args->argv);
|
2011-01-04 00:42:46 +00:00
|
|
|
|
|
|
|
va_start(ap, argc);
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
args->argv[i] = xstrdup(va_arg(ap, char *));
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return (args);
|
|
|
|
}
|
|
|
|
|
2013-05-31 12:19:34 +00:00
|
|
|
/* Find a flag in the arguments tree. */
|
|
|
|
struct args_entry *
|
|
|
|
args_find(struct args *args, u_char ch)
|
|
|
|
{
|
|
|
|
struct args_entry entry;
|
|
|
|
|
|
|
|
entry.flag = ch;
|
|
|
|
return (RB_FIND(args_tree, &args->tree, &entry));
|
|
|
|
}
|
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
/* Parse an argv and argc into a new argument set. */
|
|
|
|
struct args *
|
|
|
|
args_parse(const char *template, int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct args *args;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
args = xcalloc(1, sizeof *args);
|
|
|
|
|
|
|
|
optreset = 1;
|
|
|
|
optind = 1;
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, template)) != -1) {
|
2013-05-31 12:19:34 +00:00
|
|
|
if (opt < 0)
|
2011-01-04 00:42:46 +00:00
|
|
|
continue;
|
2014-01-15 11:44:18 +00:00
|
|
|
if (opt == '?' || strchr(template, opt) == NULL) {
|
2013-05-31 12:19:34 +00:00
|
|
|
args_free(args);
|
2011-01-04 00:42:46 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
2013-05-31 12:19:34 +00:00
|
|
|
args_set(args, opt, optarg);
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
args->argc = argc;
|
|
|
|
args->argv = cmd_copy_argv(argc, argv);
|
|
|
|
|
|
|
|
return (args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2011-01-04 00:42:46 +00:00
|
|
|
|
|
|
|
cmd_free_argv(args->argc, args->argv);
|
|
|
|
|
2013-05-31 12:19:34 +00:00
|
|
|
RB_FOREACH_SAFE(entry, args_tree, &args->tree, entry1) {
|
|
|
|
RB_REMOVE(args_tree, &args->tree, entry);
|
|
|
|
free(entry->value);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* Print a set of arguments. */
|
|
|
|
size_t
|
|
|
|
args_print(struct args *args, char *buf, size_t len)
|
|
|
|
{
|
2014-04-11 19:35:54 +00:00
|
|
|
size_t off, used;
|
2013-05-31 12:19:34 +00:00
|
|
|
int i;
|
|
|
|
const char *quotes;
|
|
|
|
struct args_entry *entry;
|
2011-01-04 00:42:46 +00:00
|
|
|
|
|
|
|
/* There must be at least one byte at the start. */
|
|
|
|
if (len == 0)
|
|
|
|
return (0);
|
|
|
|
off = 0;
|
|
|
|
|
|
|
|
/* Process the flags first. */
|
|
|
|
buf[off++] = '-';
|
2013-05-31 12:19:34 +00:00
|
|
|
RB_FOREACH(entry, args_tree, &args->tree) {
|
|
|
|
if (entry->value != NULL)
|
2011-01-04 00:42:46 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (off == len - 1) {
|
|
|
|
buf[off] = '\0';
|
|
|
|
return (len);
|
|
|
|
}
|
2013-05-31 12:19:34 +00:00
|
|
|
buf[off++] = entry->flag;
|
2011-01-04 00:42:46 +00:00
|
|
|
buf[off] = '\0';
|
|
|
|
}
|
|
|
|
if (off == 1)
|
|
|
|
buf[--off] = '\0';
|
|
|
|
|
|
|
|
/* Then the flags with arguments. */
|
2013-05-31 12:19:34 +00:00
|
|
|
RB_FOREACH(entry, args_tree, &args->tree) {
|
|
|
|
if (entry->value == NULL)
|
2011-01-04 00:42:46 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (off >= len) {
|
|
|
|
/* snprintf will have zero terminated. */
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
2013-05-31 12:19:34 +00:00
|
|
|
if (strchr(entry->value, ' ') != NULL)
|
2011-01-04 00:42:46 +00:00
|
|
|
quotes = "\"";
|
|
|
|
else
|
|
|
|
quotes = "";
|
2014-04-11 19:35:54 +00:00
|
|
|
used = xsnprintf(buf + off, len - off, "%s-%c %s%s%s",
|
2013-05-31 12:19:34 +00:00
|
|
|
off != 0 ? " " : "", entry->flag, quotes, entry->value,
|
|
|
|
quotes);
|
2014-04-11 19:35:54 +00:00
|
|
|
if (used > len - off)
|
|
|
|
used = len - off;
|
|
|
|
off += used;
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* And finally the argument vector. */
|
|
|
|
for (i = 0; i < args->argc; i++) {
|
|
|
|
if (off >= len) {
|
|
|
|
/* snprintf will have zero terminated. */
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strchr(args->argv[i], ' ') != NULL)
|
|
|
|
quotes = "\"";
|
|
|
|
else
|
|
|
|
quotes = "";
|
2014-04-11 19:35:54 +00:00
|
|
|
used = xsnprintf(buf + off, len - off, "%s%s%s%s",
|
2011-01-04 00:42:46 +00:00
|
|
|
off != 0 ? " " : "", quotes, args->argv[i], quotes);
|
2014-04-11 19:35:54 +00:00
|
|
|
if (used > len - off)
|
|
|
|
used = len - off;
|
|
|
|
off += used;
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (off);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return if an argument is present. */
|
|
|
|
int
|
|
|
|
args_has(struct args *args, u_char ch)
|
|
|
|
{
|
2013-05-31 12:19:34 +00:00
|
|
|
return (args_find(args, ch) == NULL ? 0 : 1);
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 12:19:34 +00:00
|
|
|
/* Set argument value in the arguments tree. */
|
2011-01-04 00:42:46 +00:00
|
|
|
void
|
|
|
|
args_set(struct args *args, u_char ch, const char *value)
|
|
|
|
{
|
2013-05-31 12:19:34 +00:00
|
|
|
struct args_entry *entry;
|
|
|
|
|
|
|
|
/* Replace existing argument. */
|
|
|
|
if ((entry = args_find(args, ch)) != NULL) {
|
|
|
|
free(entry->value);
|
2014-01-09 13:51:57 +00:00
|
|
|
entry->value = NULL;
|
|
|
|
} else {
|
|
|
|
entry = xcalloc(1, sizeof *entry);
|
|
|
|
entry->flag = ch;
|
|
|
|
RB_INSERT(args_tree, &args->tree, entry);
|
2013-05-31 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
2011-01-30 12:09:30 +00:00
|
|
|
if (value != NULL)
|
2013-05-31 12:19:34 +00:00
|
|
|
entry->value = xstrdup(value);
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get argument value. Will be NULL if it isn't present. */
|
|
|
|
const char *
|
|
|
|
args_get(struct args *args, u_char ch)
|
|
|
|
{
|
2013-05-31 12:19:34 +00:00
|
|
|
struct args_entry *entry;
|
|
|
|
|
|
|
|
if ((entry = args_find(args, ch)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
return (entry->value);
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert an argument value to a number. */
|
|
|
|
long long
|
2013-05-31 12:19:34 +00:00
|
|
|
args_strtonum(struct args *args, u_char ch, 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;
|
|
|
|
long long ll;
|
|
|
|
struct args_entry *entry;
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2013-05-31 12:19:34 +00:00
|
|
|
if ((entry = args_find(args, ch)) == NULL) {
|
2011-01-04 00:42:46 +00:00
|
|
|
*cause = xstrdup("missing");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-05-31 12:19:34 +00:00
|
|
|
ll = strtonum(entry->value, minval, maxval, &errstr);
|
2011-01-04 00:42:46 +00:00
|
|
|
if (errstr != NULL) {
|
|
|
|
*cause = xstrdup(errstr);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
*cause = NULL;
|
|
|
|
return (ll);
|
|
|
|
}
|