Merge branch 'obsd-master' into master

This commit is contained in:
Thomas Adam 2021-08-22 12:08:05 +01:00
commit 324f87cf14
6 changed files with 271 additions and 110 deletions

View File

@ -18,6 +18,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -39,8 +40,8 @@ struct args_entry {
struct args { struct args {
struct args_tree tree; struct args_tree tree;
int argc; u_int count;
char **argv; struct args_value *values;
}; };
static struct args_entry *args_find(struct args *, u_char); static struct args_entry *args_find(struct args *, u_char);
@ -65,6 +66,40 @@ args_find(struct args *args, u_char flag)
return (RB_FIND(args_tree, &args->tree, &entry)); 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);
}
}
/* Create an empty arguments set. */ /* Create an empty arguments set. */
struct args * struct args *
args_create(void) args_create(void)
@ -76,42 +111,108 @@ args_create(void)
return (args); return (args);
} }
/* Parse an argv and argc into a new argument set. */ /* Parse arguments into a new argument set. */
struct args * struct args *
args_parse(const struct args_parse *parse, int argc, char **argv) args_parse(const struct args_parse *parse, struct args_value *values,
u_int count)
{ {
struct args *args; struct args *args;
int opt; u_int i;
struct args_value *value, *new;
u_char flag, argument;
const char *found, *string, *s;
optreset = 1; if (count == 0)
optind = 1; return (args_create());
optarg = NULL;
args = args_create(); args = args_create();
while ((opt = getopt(argc, argv, parse->template)) != -1) { for (i = 1; i < count; /* nothing */) {
if (opt < 0) value = &values[i];
continue; if (value->type != ARGS_STRING)
if (opt == '?' || strchr(parse->template, opt) == NULL) { 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 (!isalnum(flag)) {
args_free(args); args_free(args);
return (NULL); return (NULL);
} }
args_set(args, opt, optarg); found = strchr(parse->template, flag);
optarg = NULL; if (found == NULL) {
args_free(args);
return (NULL);
} }
argc -= optind; argument = *++found;
argv += optind; if (argument != ':') {
log_debug("%s: add -%c", __func__, flag);
args_set(args, flag, NULL);
continue;
}
new = xcalloc(1, sizeof *value);
if (*string != '\0') {
new->type = ARGS_STRING;
new->string = xstrdup(string);
} else {
if (i == count) {
args_free(args);
return (NULL);
}
args_copy_value(new, &values[i++]);
}
s = args_value_as_string(new);
log_debug("%s: add -%c = %s", __func__, flag, s);
args_set(args, flag, new);
break;
}
}
log_debug("%s: flags end at %u of %u", __func__, i, count);
if (i != count) {
for (/* nothing */; i < count; i++) {
value = &values[i];
args->argc = argc; s = args_value_as_string(value);
args->argv = cmd_copy_argv(argc, argv); log_debug("%s: %u = %s", __func__, i, s);
if ((parse->lower != -1 && argc < parse->lower) || args->values = xrecallocarray(args->values,
(parse->upper != -1 && argc > parse->upper)) { args->count, args->count + 1, sizeof *args->values);
args_copy_value(&args->values[args->count++], value);
}
}
if ((parse->lower != -1 && args->count < (u_int)parse->lower) ||
(parse->upper != -1 && args->count > (u_int)parse->upper)) {
args_free(args); args_free(args);
return (NULL); return (NULL);
} }
return (args); return (args);
} }
/* 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;
}
free(value->cached);
}
/* Free an arguments set. */ /* Free an arguments set. */
void void
args_free(struct args *args) args_free(struct args *args)
@ -120,14 +221,17 @@ args_free(struct args *args)
struct args_entry *entry1; struct args_entry *entry1;
struct args_value *value; struct args_value *value;
struct args_value *value1; struct args_value *value1;
u_int i;
cmd_free_argv(args->argc, args->argv); for (i = 0; i < args->count; i++)
args_free_value(&args->values[i]);
free(args->values);
RB_FOREACH_SAFE(entry, args_tree, &args->tree, entry1) { RB_FOREACH_SAFE(entry, args_tree, &args->tree, entry1) {
RB_REMOVE(args_tree, &args->tree, entry); RB_REMOVE(args_tree, &args->tree, entry);
TAILQ_FOREACH_SAFE(value, &entry->values, entry, value1) { TAILQ_FOREACH_SAFE(value, &entry->values, entry, value1) {
TAILQ_REMOVE(&entry->values, value, entry); TAILQ_REMOVE(&entry->values, value, entry);
free(value->string); args_free_value(value);
free(value); free(value);
} }
free(entry); free(entry);
@ -140,8 +244,16 @@ args_free(struct args *args)
void void
args_vector(struct args *args, int *argc, char ***argv) args_vector(struct args *args, int *argc, char ***argv)
{ {
*argc = args->argc; struct args_value *value;
*argv = cmd_copy_argv(args->argc, args->argv); u_int i;
*argc = 0;
*argv = NULL;
for (i = 0; i < args->count; i++) {
value = &args->values[i];
cmd_append_argv(argc, argv, args_value_as_string(value));
}
} }
/* Add to string. */ /* Add to string. */
@ -163,18 +275,28 @@ args_print_add(char **buf, size_t *len, const char *fmt, ...)
free(s); free(s);
} }
/* Add argument to string. */ /* Add value to string. */
static void static void
args_print_add_argument(char **buf, size_t *len, const char *argument) args_print_add_value(char **buf, size_t *len, struct args_value *value)
{ {
char *escaped; char *expanded = NULL;
if (**buf != '\0') if (**buf != '\0')
args_print_add(buf, len, " "); args_print_add(buf, len, " ");
escaped = args_escape(argument); switch (value->type) {
args_print_add(buf, len, "%s", escaped); case ARGS_NONE:
free(escaped); 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);
} }
/* Print a set of arguments. */ /* Print a set of arguments. */
@ -183,8 +305,7 @@ args_print(struct args *args)
{ {
size_t len; size_t len;
char *buf; char *buf;
int i; u_int i, j;
u_int j;
struct args_entry *entry; struct args_entry *entry;
struct args_value *value; struct args_value *value;
@ -209,13 +330,13 @@ args_print(struct args *args)
args_print_add(&buf, &len, " -%c", entry->flag); args_print_add(&buf, &len, " -%c", entry->flag);
else else
args_print_add(&buf, &len, "-%c", entry->flag); args_print_add(&buf, &len, "-%c", entry->flag);
args_print_add_argument(&buf, &len, value->string); args_print_add_value(&buf, &len, value);
} }
} }
/* And finally the argument vector. */ /* And finally the argument vector. */
for (i = 0; i < args->argc; i++) for (i = 0; i < args->count; i++)
args_print_add_argument(&buf, &len, args->argv[i]); args_print_add_value(&buf, &len, &args->values[i]);
return (buf); return (buf);
} }
@ -281,10 +402,9 @@ args_has(struct args *args, u_char flag)
/* Set argument value in the arguments tree. */ /* Set argument value in the arguments tree. */
void void
args_set(struct args *args, u_char flag, const char *s) args_set(struct args *args, u_char flag, struct args_value *value)
{ {
struct args_entry *entry; struct args_entry *entry;
struct args_value *value;
entry = args_find(args, flag); entry = args_find(args, flag);
if (entry == NULL) { if (entry == NULL) {
@ -295,13 +415,9 @@ args_set(struct args *args, u_char flag, const char *s)
RB_INSERT(args_tree, &args->tree, entry); RB_INSERT(args_tree, &args->tree, entry);
} else } else
entry->count++; entry->count++;
if (value != NULL && value->type != ARGS_NONE)
if (s != NULL) {
value = xcalloc(1, sizeof *value);
value->string = xstrdup(s);
TAILQ_INSERT_TAIL(&entry->values, value, entry); TAILQ_INSERT_TAIL(&entry->values, value, entry);
} }
}
/* Get argument value. Will be NULL if it isn't present. */ /* Get argument value. Will be NULL if it isn't present. */
const char * const char *
@ -340,16 +456,25 @@ args_next(struct args_entry **entry)
u_int u_int
args_count(struct args *args) args_count(struct args *args)
{ {
return (args->argc); return (args->count);
}
/* Get argument value. */
struct args_value *
args_value(struct args *args, u_int idx)
{
if (idx >= args->count)
return (NULL);
return (&args->values[idx]);
} }
/* Return argument as string. */ /* Return argument as string. */
const char * const char *
args_string(struct args *args, u_int idx) args_string(struct args *args, u_int idx)
{ {
if (idx >= (u_int)args->argc) if (idx >= args->count)
return (NULL); return (NULL);
return (args->argv[idx]); return (args_value_as_string(&args->values[idx]));
} }
/* Get first value in argument. */ /* Get first value in argument. */

View File

@ -50,6 +50,7 @@ cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item)
struct cmd_parse_result *pr; struct cmd_parse_result *pr;
char **argv; char **argv;
int argc, repeat; int argc, repeat;
struct args_value *value;
u_int count = args_count(args); u_int count = args_count(args);
key = key_string_lookup_string(args_string(args, 0)); key = key_string_lookup_string(args_string(args, 0));
@ -66,7 +67,17 @@ cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item)
tablename = "prefix"; tablename = "prefix";
repeat = args_has(args, 'r'); repeat = args_has(args, 'r');
if (count != 1) { if (count == 1) {
key_bindings_add(tablename, key, note, repeat, NULL);
return (CMD_RETURN_NORMAL);
}
value = args_value(args, 1);
if (count == 2 && value->type == ARGS_COMMANDS) {
key_bindings_add(tablename, key, note, repeat, value->cmdlist);
return (CMD_RETURN_NORMAL);
}
if (count == 2) if (count == 2)
pr = cmd_parse_from_string(args_string(args, 1), NULL); pr = cmd_parse_from_string(args_string(args, 1), NULL);
else { else {
@ -83,7 +94,5 @@ cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item)
break; break;
} }
key_bindings_add(tablename, key, note, repeat, pr->cmdlist); key_bindings_add(tablename, key, note, repeat, pr->cmdlist);
} else
key_bindings_add(tablename, key, note, repeat, NULL);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }

View File

@ -48,7 +48,7 @@ cmd_find_window_exec(struct cmd *self, struct cmdq_item *item)
struct cmd_find_state *target = cmdq_get_target(item); struct cmd_find_state *target = cmdq_get_target(item);
struct window_pane *wp = target->wp; struct window_pane *wp = target->wp;
const char *s = args_string(args, 0), *suffix = ""; const char *s = args_string(args, 0), *suffix = "";
char *filter; struct args_value *filter;
int C, N, T; int C, N, T;
C = args_has(args, 'C'); C = args_has(args, 'C');
@ -65,31 +65,41 @@ cmd_find_window_exec(struct cmd *self, struct cmdq_item *item)
if (!C && !N && !T) if (!C && !N && !T)
C = N = T = 1; C = N = T = 1;
filter = xcalloc(1, sizeof *filter);
filter->type = ARGS_STRING;
if (C && N && T) { if (C && N && T) {
xasprintf(&filter, xasprintf(&filter->string,
"#{||:" "#{||:"
"#{C%s:%s},#{||:#{m%s:*%s*,#{window_name}}," "#{C%s:%s},#{||:#{m%s:*%s*,#{window_name}},"
"#{m%s:*%s*,#{pane_title}}}}", "#{m%s:*%s*,#{pane_title}}}}",
suffix, s, suffix, s, suffix, s); suffix, s, suffix, s, suffix, s);
} else if (C && N) { } else if (C && N) {
xasprintf(&filter, xasprintf(&filter->string,
"#{||:#{C%s:%s},#{m%s:*%s*,#{window_name}}}", "#{||:#{C%s:%s},#{m%s:*%s*,#{window_name}}}",
suffix, s, suffix, s); suffix, s, suffix, s);
} else if (C && T) { } else if (C && T) {
xasprintf(&filter, xasprintf(&filter->string,
"#{||:#{C%s:%s},#{m%s:*%s*,#{pane_title}}}", "#{||:#{C%s:%s},#{m%s:*%s*,#{pane_title}}}",
suffix, s, suffix, s); suffix, s, suffix, s);
} else if (N && T) { } else if (N && T) {
xasprintf(&filter, xasprintf(&filter->string,
"#{||:#{m%s:*%s*,#{window_name}}," "#{||:#{m%s:*%s*,#{window_name}},"
"#{m%s:*%s*,#{pane_title}}}", "#{m%s:*%s*,#{pane_title}}}",
suffix, s, suffix, s); suffix, s, suffix, s);
} else if (C) } else if (C) {
xasprintf(&filter, "#{C%s:%s}", suffix, s); xasprintf(&filter->string,
else if (N) "#{C%s:%s}",
xasprintf(&filter, "#{m%s:*%s*,#{window_name}}", suffix, s); suffix, s);
else } else if (N) {
xasprintf(&filter, "#{m%s:*%s*,#{pane_title}}", suffix, s); xasprintf(&filter->string,
"#{m%s:*%s*,#{window_name}}",
suffix, s);
} else {
xasprintf(&filter->string,
"#{m%s:*%s*,#{pane_title}}",
suffix, s);
}
new_args = args_create(); new_args = args_create();
if (args_has(args, 'Z')) if (args_has(args, 'Z'))
@ -97,9 +107,7 @@ cmd_find_window_exec(struct cmd *self, struct cmdq_item *item)
args_set(new_args, 'f', filter); args_set(new_args, 'f', filter);
window_pane_set_mode(wp, NULL, &window_tree_mode, target, new_args); window_pane_set_mode(wp, NULL, &window_tree_mode, target, new_args);
args_free(new_args); args_free(new_args);
free(filter);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }

View File

@ -794,39 +794,49 @@ cmd_parse_build_command(struct cmd_parse_command *cmd,
struct cmd_parse_input *pi, struct cmd_parse_result *pr) struct cmd_parse_input *pi, struct cmd_parse_result *pr)
{ {
struct cmd_parse_argument *arg; struct cmd_parse_argument *arg;
struct cmd_list *cmdlist; struct cmd_list *cmdlist = NULL;
struct cmd *add; struct cmd *add;
char *s, **argv = NULL, *cause; char *cause;
int argc = 0; struct args_value *values = NULL;
u_int count = 0, idx;
if (cmd_parse_expand_alias(cmd, pi, pr, &cmdlist)) if (cmd_parse_expand_alias(cmd, pi, pr, &cmdlist))
return (cmdlist); return (cmdlist);
TAILQ_FOREACH(arg, &cmd->arguments, entry) { TAILQ_FOREACH(arg, &cmd->arguments, entry) {
values = xrecallocarray(values, count, count + 1,
sizeof *values);
switch (arg->type) { switch (arg->type) {
case CMD_PARSE_STRING: case CMD_PARSE_STRING:
cmd_append_argv(&argc, &argv, arg->string); values[count].type = ARGS_STRING;
values[count].string = xstrdup(arg->string);
break; break;
case CMD_PARSE_COMMANDS: case CMD_PARSE_COMMANDS:
cmd_parse_build_commands(arg->commands, pi, pr); cmd_parse_build_commands(arg->commands, pi, pr);
if (pr->status != CMD_PARSE_SUCCESS) if (pr->status != CMD_PARSE_SUCCESS)
return (NULL); goto out;
s = cmd_list_print(pr->cmdlist, 0); values[count].type = ARGS_COMMANDS;
cmd_append_argv(&argc, &argv, s); values[count].cmdlist = pr->cmdlist;
free(s); values[count].cmdlist->references++;
break; break;
} }
count++;
} }
add = cmd_parse(argc, argv, pi->file, pi->line, &cause); add = cmd_parse(values, count, pi->file, pi->line, &cause);
if (add == NULL) { if (add == NULL) {
pr->status = CMD_PARSE_ERROR; pr->status = CMD_PARSE_ERROR;
pr->error = cmd_parse_get_error(pi->file, pi->line, cause); pr->error = cmd_parse_get_error(pi->file, pi->line, cause);
free(cause); free(cause);
return (NULL); goto out;
} }
cmdlist = cmd_list_new(); cmdlist = cmd_list_new();
cmd_list_append(cmdlist, add); cmd_list_append(cmdlist, add);
out:
for (idx = 0; idx < count; idx++)
args_free_value(&values[idx]);
free(values);
return (cmdlist); return (cmdlist);
} }

25
cmd.c
View File

@ -495,27 +495,26 @@ ambiguous:
/* Parse a single command from an argument vector. */ /* Parse a single command from an argument vector. */
struct cmd * struct cmd *
cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause) cmd_parse(struct args_value *values, u_int count, const char *file, u_int line,
char **cause)
{ {
const struct cmd_entry *entry; const struct cmd_entry *entry;
const char *name;
struct cmd *cmd; struct cmd *cmd;
struct args *args; struct args *args;
if (argc == 0) { if (count == 0 || values[0].type != ARGS_STRING) {
xasprintf(cause, "no command"); xasprintf(cause, "no command");
return (NULL); return (NULL);
} }
name = argv[0]; entry = cmd_find(values[0].string, cause);
entry = cmd_find(name, cause);
if (entry == NULL) if (entry == NULL)
return (NULL); return (NULL);
cmd_log_argv(argc, argv, "%s: %s", __func__, entry->name);
args = args_parse(&entry->args, argc, argv); args = args_parse(&entry->args, values, count);
if (args == NULL) if (args == NULL) {
goto usage; xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
return (NULL);
}
cmd = xcalloc(1, sizeof *cmd); cmd = xcalloc(1, sizeof *cmd);
cmd->entry = entry; cmd->entry = entry;
@ -526,12 +525,6 @@ cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
cmd->line = line; cmd->line = line;
return (cmd); return (cmd);
usage:
if (args != NULL)
args_free(args);
xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
return (NULL);
} }
/* Free a command. */ /* Free a command. */

22
tmux.h
View File

@ -1356,9 +1356,21 @@ struct message_entry {
}; };
TAILQ_HEAD(message_list, message_entry); TAILQ_HEAD(message_list, message_entry);
/* Argument type. */
enum args_type {
ARGS_NONE,
ARGS_STRING,
ARGS_COMMANDS
};
/* Argument value. */ /* Argument value. */
struct args_value { struct args_value {
enum args_type type;
union {
char *string; char *string;
struct cmd_list *cmdlist;
};
char *cached;
TAILQ_ENTRY(args_value) entry; TAILQ_ENTRY(args_value) entry;
}; };
@ -2186,10 +2198,12 @@ void tty_keys_free(struct tty *);
int tty_keys_next(struct tty *); int tty_keys_next(struct tty *);
/* arguments.c */ /* arguments.c */
void args_set(struct args *, u_char, const char *); void args_set(struct args *, u_char, struct args_value *);
struct args *args_create(void); struct args *args_create(void);
struct args *args_parse(const struct args_parse *, int, char **); struct args *args_parse(const struct args_parse *, struct args_value *,
u_int);
void args_vector(struct args *, int *, char ***); void args_vector(struct args *, int *, char ***);
void args_free_value(struct args_value *);
void args_free(struct args *); void args_free(struct args *);
char *args_print(struct args *); char *args_print(struct args *);
char *args_escape(const char *); char *args_escape(const char *);
@ -2198,6 +2212,7 @@ const char *args_get(struct args *, u_char);
u_char args_first(struct args *, struct args_entry **); u_char args_first(struct args *, struct args_entry **);
u_char args_next(struct args_entry **); u_char args_next(struct args_entry **);
u_int args_count(struct args *); u_int args_count(struct args *);
struct args_value *args_value(struct args *, u_int);
const char *args_string(struct args *, u_int); const char *args_string(struct args *, u_int);
struct args_value *args_first_value(struct args *, u_char); struct args_value *args_first_value(struct args *, u_char);
struct args_value *args_next_value(struct args_value *); struct args_value *args_next_value(struct args_value *);
@ -2251,7 +2266,8 @@ const struct cmd_entry *cmd_get_entry(struct cmd *);
struct args *cmd_get_args(struct cmd *); struct args *cmd_get_args(struct cmd *);
u_int cmd_get_group(struct cmd *); u_int cmd_get_group(struct cmd *);
void cmd_get_source(struct cmd *, const char **, u_int *); void cmd_get_source(struct cmd *, const char **, u_int *);
struct cmd *cmd_parse(int, char **, const char *, u_int, char **); struct cmd *cmd_parse(struct args_value *, u_int, const char *, u_int,
char **);
void cmd_free(struct cmd *); void cmd_free(struct cmd *);
char *cmd_print(struct cmd *); char *cmd_print(struct cmd *);
struct cmd_list *cmd_list_new(void); struct cmd_list *cmd_list_new(void);