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>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-11-17 20:19:36 +00:00
|
|
|
#include <unistd.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.
|
|
|
|
*/
|
|
|
|
|
2019-04-28 20:05:50 +00:00
|
|
|
struct args_value {
|
|
|
|
char *value;
|
|
|
|
TAILQ_ENTRY(args_value) entry;
|
|
|
|
};
|
|
|
|
TAILQ_HEAD(args_values, args_value);
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
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 *
|
2013-05-31 12:19:34 +00:00
|
|
|
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;
|
2020-03-17 16:02:38 +00:00
|
|
|
optarg = NULL;
|
2011-01-04 00:42:46 +00:00
|
|
|
|
|
|
|
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);
|
2020-03-17 16:02:38 +00:00
|
|
|
optarg = NULL;
|
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;
|
2019-04-28 20:05:50 +00:00
|
|
|
struct args_value *value;
|
|
|
|
struct args_value *value1;
|
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);
|
2019-04-28 20:05:50 +00:00
|
|
|
TAILQ_FOREACH_SAFE(value, &entry->values, entry, value1) {
|
|
|
|
TAILQ_REMOVE(&entry->values, value, entry);
|
|
|
|
free(value->value);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-04-28 20:05:50 +00:00
|
|
|
/* Add value to string. */
|
|
|
|
static void
|
|
|
|
args_print_add_value(char **buf, size_t *len, struct args_entry *entry,
|
|
|
|
struct args_value *value)
|
|
|
|
{
|
2019-05-23 14:03:44 +00:00
|
|
|
char *escaped;
|
2019-04-28 20:05:50 +00:00
|
|
|
|
|
|
|
if (**buf != '\0')
|
|
|
|
args_print_add(buf, len, " -%c ", entry->flag);
|
|
|
|
else
|
|
|
|
args_print_add(buf, len, "-%c ", entry->flag);
|
|
|
|
|
2019-05-23 14:03:44 +00:00
|
|
|
escaped = args_escape(value->value);
|
|
|
|
args_print_add(buf, len, "%s", escaped);
|
2019-04-28 20:05:50 +00:00
|
|
|
free(escaped);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add argument to string. */
|
|
|
|
static void
|
|
|
|
args_print_add_argument(char **buf, size_t *len, const char *argument)
|
|
|
|
{
|
2019-05-23 14:03:44 +00:00
|
|
|
char *escaped;
|
2019-04-28 20:05:50 +00:00
|
|
|
|
|
|
|
if (**buf != '\0')
|
|
|
|
args_print_add(buf, len, " ");
|
|
|
|
|
2019-05-23 14:03:44 +00:00
|
|
|
escaped = args_escape(argument);
|
|
|
|
args_print_add(buf, len, "%s", escaped);
|
2019-04-28 20:05:50 +00:00
|
|
|
free(escaped);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-11-27 15:06:43 +00:00
|
|
|
size_t len;
|
2019-04-28 20:05:50 +00:00
|
|
|
char *buf;
|
|
|
|
int i;
|
2019-07-09 14:03:12 +00:00
|
|
|
u_int 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) {
|
2019-04-28 20:05:50 +00:00
|
|
|
TAILQ_FOREACH(value, &entry->values, entry)
|
|
|
|
args_print_add_value(&buf, &len, entry, value);
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* And finally the argument vector. */
|
2019-04-28 20:05:50 +00:00
|
|
|
for (i = 0; i < args->argc; i++)
|
|
|
|
args_print_add_argument(&buf, &len, args->argv[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)
|
|
|
|
{
|
2019-05-27 12:16:27 +00:00
|
|
|
static const char quoted[] = " #\"';${}";
|
2019-05-23 14:03:44 +00:00
|
|
|
char *escaped, *result;
|
|
|
|
int flags;
|
|
|
|
|
2019-05-28 18:30:30 +00:00
|
|
|
if (*s == '\0')
|
|
|
|
return (xstrdup(s));
|
2019-06-20 07:10:56 +00:00
|
|
|
if (s[0] != ' ' &&
|
|
|
|
(strchr(quoted, s[0]) != NULL || s[0] == '~') &&
|
|
|
|
s[1] == '\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;
|
2019-05-23 14:03:44 +00:00
|
|
|
if (s[strcspn(s, quoted)] != '\0')
|
|
|
|
flags |= VIS_DQ;
|
|
|
|
utf8_stravis(&escaped, s, flags);
|
|
|
|
|
|
|
|
if (flags & VIS_DQ) {
|
|
|
|
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
|
|
|
|
args_has(struct args *args, u_char ch)
|
|
|
|
{
|
2019-07-09 14:03:12 +00:00
|
|
|
struct args_entry *entry;
|
|
|
|
|
|
|
|
entry = args_find(args, ch);
|
|
|
|
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
|
2019-04-28 20:05:50 +00:00
|
|
|
args_set(struct args *args, u_char ch, const char *s)
|
2011-01-04 00:42:46 +00:00
|
|
|
{
|
2013-05-31 12:19:34 +00:00
|
|
|
struct args_entry *entry;
|
2019-04-28 20:05:50 +00:00
|
|
|
struct args_value *value;
|
2013-05-31 12:19:34 +00:00
|
|
|
|
2019-04-28 20:05:50 +00:00
|
|
|
entry = args_find(args, ch);
|
|
|
|
if (entry == NULL) {
|
2014-01-09 13:51:57 +00:00
|
|
|
entry = xcalloc(1, sizeof *entry);
|
|
|
|
entry->flag = ch;
|
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++;
|
2013-05-31 12:19:34 +00:00
|
|
|
|
2019-04-28 20:05:50 +00:00
|
|
|
if (s != NULL) {
|
|
|
|
value = xcalloc(1, sizeof *value);
|
|
|
|
value->value = xstrdup(s);
|
|
|
|
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 *
|
|
|
|
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);
|
2019-04-28 20:05:50 +00:00
|
|
|
return (TAILQ_LAST(&entry->values, args_values)->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get first value in argument. */
|
|
|
|
const char *
|
|
|
|
args_first_value(struct args *args, u_char ch, struct args_value **value)
|
|
|
|
{
|
|
|
|
struct args_entry *entry;
|
|
|
|
|
|
|
|
if ((entry = args_find(args, ch)) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
*value = TAILQ_FIRST(&entry->values);
|
|
|
|
if (*value == NULL)
|
|
|
|
return (NULL);
|
|
|
|
return ((*value)->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get next value in argument. */
|
|
|
|
const char *
|
|
|
|
args_next_value(struct args_value **value)
|
|
|
|
{
|
|
|
|
if (*value == NULL)
|
|
|
|
return (NULL);
|
|
|
|
*value = TAILQ_NEXT(*value, entry);
|
|
|
|
if (*value == NULL)
|
|
|
|
return (NULL);
|
|
|
|
return ((*value)->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;
|
2019-04-28 20:05:50 +00:00
|
|
|
struct args_value *value;
|
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);
|
|
|
|
}
|
2019-04-28 20:05:50 +00:00
|
|
|
value = TAILQ_LAST(&entry->values, args_values);
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2019-04-28 20:05:50 +00:00
|
|
|
ll = strtonum(value->value, 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
|
|
|
|
args_percentage(struct args *args, u_char ch, long long minval,
|
|
|
|
long long maxval, long long curval, char **cause)
|
|
|
|
{
|
|
|
|
const char *errstr;
|
|
|
|
long long ll;
|
|
|
|
struct args_entry *entry;
|
|
|
|
struct args_value *value;
|
|
|
|
size_t valuelen;
|
|
|
|
char *copy;
|
|
|
|
|
|
|
|
if ((entry = args_find(args, ch)) == NULL) {
|
|
|
|
*cause = xstrdup("missing");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
value = TAILQ_LAST(&entry->values, args_values);
|
|
|
|
valuelen = strlen(value->value);
|
|
|
|
|
|
|
|
if (value->value[valuelen - 1] == '%') {
|
|
|
|
copy = xstrdup(value->value);
|
|
|
|
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) {
|
|
|
|
*cause = xstrdup("too large");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (ll > maxval) {
|
|
|
|
*cause = xstrdup("too small");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ll = strtonum(value->value, minval, maxval, &errstr);
|
|
|
|
if (errstr != NULL) {
|
|
|
|
*cause = xstrdup(errstr);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*cause = NULL;
|
|
|
|
return (ll);
|
|
|
|
}
|