Add flags for 1+2 and 2 arguments to the generic target code, use it for

cmd-set-environment/option/window-option and remove the generic options
parsing.
This commit is contained in:
Nicholas Marriott 2009-08-11 12:53:37 +00:00
parent edcb22a6fb
commit 60db6e3df4
5 changed files with 92 additions and 162 deletions

View File

@ -26,7 +26,7 @@
int cmd_getopt(int, char **, const char *, uint64_t);
int cmd_flags(int, uint64_t, uint64_t *);
size_t cmd_print_flags(char *, size_t, size_t, uint64_t);
int cmd_fill_argument(int, char **, int, char **);
int cmd_fill_argument(int, char **, char **, int, char **);
size_t
cmd_prarg(char *buf, size_t len, const char *prefix, char *arg)
@ -104,9 +104,10 @@ cmd_print_flags(char *buf, size_t len, size_t off, uint64_t chflags)
}
int
cmd_fill_argument(int flags, char **arg, int argc, char **argv)
cmd_fill_argument(int flags, char **arg, char **arg2, int argc, char **argv)
{
*arg = NULL;
*arg2 = NULL;
if (flags & CMD_ARG1) {
if (argc != 1)
@ -123,6 +124,23 @@ cmd_fill_argument(int flags, char **arg, int argc, char **argv)
return (0);
}
if (flags & CMD_ARG2) {
if (argc != 2)
return (-1);
*arg = xstrdup(argv[0]);
*arg2 = xstrdup(argv[1]);
return (0);
}
if (flags & CMD_ARG12) {
if (argc != 1 && argc != 2)
return (-1);
*arg = xstrdup(argv[0]);
if (argc == 2)
*arg2 = xstrdup(argv[1]);
return (0);
}
if (argc != 0)
return (-1);
return (0);
@ -165,7 +183,8 @@ cmd_target_parse(struct cmd *self, int argc, char **argv, char **cause)
argc -= optind;
argv += optind;
if (cmd_fill_argument(self->entry->flags, &data->arg, argc, argv) != 0)
if (cmd_fill_argument(
self->entry->flags, &data->arg, &data->arg2, argc, argv) != 0)
goto usage;
return (0);
@ -202,6 +221,8 @@ cmd_target_print(struct cmd *self, char *buf, size_t len)
off += cmd_prarg(buf + off, len - off, " -t ", data->target);
if (off < len && data->arg != NULL)
off += cmd_prarg(buf + off, len - off, " ", data->arg);
if (off < len && data->arg2 != NULL)
off += cmd_prarg(buf + off, len - off, " ", data->arg2);
return (off);
}
@ -246,7 +267,8 @@ cmd_srcdst_parse(struct cmd *self, int argc, char **argv, char **cause)
argc -= optind;
argv += optind;
if (cmd_fill_argument(self->entry->flags, &data->arg, argc, argv) != 0)
if (cmd_fill_argument(
self->entry->flags, &data->arg, &data->arg2, argc, argv) != 0)
goto usage;
return (0);
@ -287,6 +309,8 @@ cmd_srcdst_print(struct cmd *self, char *buf, size_t len)
off += xsnprintf(buf + off, len - off, " -t %s", data->dst);
if (off < len && data->arg != NULL)
off += cmd_prarg(buf + off, len - off, " ", data->arg);
if (off < len && data->arg2 != NULL)
off += cmd_prarg(buf + off, len - off, " ", data->arg2);
return (off);
}
@ -338,7 +362,8 @@ cmd_buffer_parse(struct cmd *self, int argc, char **argv, char **cause)
argc -= optind;
argv += optind;
if (cmd_fill_argument(self->entry->flags, &data->arg, argc, argv) != 0)
if (cmd_fill_argument(
self->entry->flags, &data->arg, &data->arg2, argc, argv) != 0)
goto usage;
return (0);
@ -378,92 +403,7 @@ cmd_buffer_print(struct cmd *self, char *buf, size_t len)
off += cmd_prarg(buf + off, len - off, " -t ", data->target);
if (off < len && data->arg != NULL)
off += cmd_prarg(buf + off, len - off, " ", data->arg);
return (off);
}
void
cmd_option_init(struct cmd *self, unused int key)
{
struct cmd_option_data *data;
self->data = data = xmalloc(sizeof *data);
data->chflags = 0;
data->target = NULL;
data->option = NULL;
data->value = NULL;
}
int
cmd_option_parse(struct cmd *self, int argc, char **argv, char **cause)
{
struct cmd_option_data *data;
const struct cmd_entry *entry = self->entry;
int opt;
/* Don't use the entry version since it may be dependent on key. */
cmd_option_init(self, 0);
data = self->data;
while ((opt = cmd_getopt(argc, argv, "t:", entry->chflags)) != -1) {
if (cmd_flags(opt, entry->chflags, &data->chflags) == 0)
continue;
switch (opt) {
case 't':
if (data->target == NULL)
data->target = xstrdup(optarg);
break;
default:
goto usage;
}
}
argc -= optind;
argv += optind;
if (argc == 2) {
data->option = xstrdup(argv[0]);
data->value = xstrdup(argv[1]);
} else if (argc == 1)
data->option = xstrdup(argv[0]);
else
goto usage;
return (0);
usage:
xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
self->entry->free(self);
return (-1);
}
void
cmd_option_free(struct cmd *self)
{
struct cmd_option_data *data = self->data;
if (data->target != NULL)
xfree(data->target);
if (data->option != NULL)
xfree(data->option);
if (data->value != NULL)
xfree(data->value);
xfree(data);
}
size_t
cmd_option_print(struct cmd *self, char *buf, size_t len)
{
struct cmd_option_data *data = self->data;
size_t off = 0;
off += xsnprintf(buf, len, "%s", self->entry->name);
if (data == NULL)
return (off);
off += cmd_print_flags(buf, len, off, data->chflags);
if (off < len && data->target != NULL)
off += cmd_prarg(buf + off, len - off, " -t ", data->target);
if (off < len && data->option != NULL)
off += xsnprintf(buf + off, len - off, " %s", data->option);
if (off < len && data->value != NULL)
off += xsnprintf(buf + off, len - off, " %s", data->value);
if (off < len && data->arg2 != NULL)
off += cmd_prarg(buf + off, len - off, " ", data->arg2);
return (off);
}

View File

@ -31,27 +31,27 @@ int cmd_set_environment_exec(struct cmd *, struct cmd_ctx *);
const struct cmd_entry cmd_set_environment_entry = {
"set-environment", "setenv",
"[-gru] " CMD_OPTION_SESSION_USAGE,
0, CMD_CHFLAG('g')|CMD_CHFLAG('r')|CMD_CHFLAG('u'),
"[-gru] " CMD_TARGET_SESSION_USAGE " name [value]",
CMD_ARG12, CMD_CHFLAG('g')|CMD_CHFLAG('r')|CMD_CHFLAG('u'),
NULL,
cmd_option_parse,
cmd_target_parse,
cmd_set_environment_exec,
cmd_option_free,
cmd_option_print
cmd_target_free,
cmd_target_print
};
int
cmd_set_environment_exec(struct cmd *self, struct cmd_ctx *ctx)
{
struct cmd_option_data *data = self->data;
struct cmd_target_data *data = self->data;
struct session *s;
struct environ *env;
if (*data->option == '\0') {
if (*data->arg == '\0') {
ctx->error(ctx, "empty variable name");
return (-1);
}
if (strchr(data->option, '=') != NULL) {
if (strchr(data->arg, '=') != NULL) {
ctx->error(ctx, "variable name contains =");
return (-1);
}
@ -65,23 +65,23 @@ cmd_set_environment_exec(struct cmd *self, struct cmd_ctx *ctx)
}
if (data->chflags & CMD_CHFLAG('u')) {
if (data->value != NULL) {
if (data->arg2 != NULL) {
ctx->error(ctx, "can't specify a value with -u");
return (-1);
}
environ_unset(env, data->option);
environ_unset(env, data->arg);
} else if (data->chflags & CMD_CHFLAG('r')) {
if (data->value != NULL) {
if (data->arg2 != NULL) {
ctx->error(ctx, "can't specify a value with -r");
return (-1);
}
environ_set(env, data->option, NULL);
environ_set(env, data->arg, NULL);
} else {
if (data->value == NULL) {
if (data->arg2 == NULL) {
ctx->error(ctx, "no value specified");
return (-1);
}
environ_set(env, data->option, data->value);
environ_set(env, data->arg, data->arg2);
}
return (0);

View File

@ -31,13 +31,13 @@ int cmd_set_option_exec(struct cmd *, struct cmd_ctx *);
const struct cmd_entry cmd_set_option_entry = {
"set-option", "set",
"[-agu] " CMD_OPTION_SESSION_USAGE,
0, CMD_CHFLAG('a')|CMD_CHFLAG('g')|CMD_CHFLAG('u'),
"[-agu] " CMD_TARGET_SESSION_USAGE " option [value]",
CMD_ARG12, CMD_CHFLAG('a')|CMD_CHFLAG('g')|CMD_CHFLAG('u'),
NULL,
cmd_option_parse,
cmd_target_parse,
cmd_set_option_exec,
cmd_option_free,
cmd_option_print
cmd_target_free,
cmd_target_print
};
const char *set_option_status_keys_list[] = {
@ -95,7 +95,7 @@ const struct set_option_entry set_option_table[] = {
int
cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx)
{
struct cmd_option_data *data = self->data;
struct cmd_target_data *data = self->data;
struct session *s;
struct client *c;
struct options *oo;
@ -110,27 +110,27 @@ cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx)
oo = &s->options;
}
if (*data->option == '\0') {
if (*data->arg == '\0') {
ctx->error(ctx, "invalid option");
return (-1);
}
entry = NULL;
for (opt = set_option_table; opt->name != NULL; opt++) {
if (strncmp(opt->name, data->option, strlen(data->option)) != 0)
if (strncmp(opt->name, data->arg, strlen(data->arg)) != 0)
continue;
if (entry != NULL) {
ctx->error(ctx, "ambiguous option: %s", data->option);
ctx->error(ctx, "ambiguous option: %s", data->arg);
return (-1);
}
entry = opt;
/* Bail now if an exact match. */
if (strcmp(entry->name, data->option) == 0)
if (strcmp(entry->name, data->arg) == 0)
break;
}
if (entry == NULL) {
ctx->error(ctx, "unknown option: %s", data->option);
ctx->error(ctx, "unknown option: %s", data->arg);
return (-1);
}
@ -140,7 +140,7 @@ cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx)
"can't unset global option: %s", entry->name);
return (-1);
}
if (data->value != NULL) {
if (data->arg2 != NULL) {
ctx->error(ctx,
"value passed to unset option: %s", entry->name);
return (-1);
@ -152,25 +152,25 @@ cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx)
switch (entry->type) {
case SET_OPTION_STRING:
set_option_string(ctx, oo, entry,
data->value, data->chflags & CMD_CHFLAG('a'));
data->arg2, data->chflags & CMD_CHFLAG('a'));
break;
case SET_OPTION_NUMBER:
set_option_number(ctx, oo, entry, data->value);
set_option_number(ctx, oo, entry, data->arg2);
break;
case SET_OPTION_KEY:
set_option_key(ctx, oo, entry, data->value);
set_option_key(ctx, oo, entry, data->arg2);
break;
case SET_OPTION_COLOUR:
set_option_colour(ctx, oo, entry, data->value);
set_option_colour(ctx, oo, entry, data->arg2);
break;
case SET_OPTION_ATTRIBUTES:
set_option_attributes(ctx, oo, entry, data->value);
set_option_attributes(ctx, oo, entry, data->arg2);
break;
case SET_OPTION_FLAG:
set_option_flag(ctx, oo, entry, data->value);
set_option_flag(ctx, oo, entry, data->arg2);
break;
case SET_OPTION_CHOICE:
set_option_choice(ctx, oo, entry, data->value);
set_option_choice(ctx, oo, entry, data->arg2);
break;
}
}

View File

@ -31,13 +31,13 @@ int cmd_set_window_option_exec(struct cmd *, struct cmd_ctx *);
const struct cmd_entry cmd_set_window_option_entry = {
"set-window-option", "setw",
"[-agu] " CMD_OPTION_WINDOW_USAGE,
0, CMD_CHFLAG('a')|CMD_CHFLAG('g')|CMD_CHFLAG('u'),
"[-agu] " CMD_TARGET_WINDOW_USAGE " option [value]",
CMD_ARG12, CMD_CHFLAG('a')|CMD_CHFLAG('g')|CMD_CHFLAG('u'),
NULL,
cmd_option_parse,
cmd_target_parse,
cmd_set_window_option_exec,
cmd_option_free,
cmd_option_print
cmd_target_free,
cmd_target_print
};
const char *set_option_mode_keys_list[] = {
@ -78,7 +78,7 @@ const struct set_option_entry set_window_option_table[] = {
int
cmd_set_window_option_exec(struct cmd *self, struct cmd_ctx *ctx)
{
struct cmd_option_data *data = self->data;
struct cmd_target_data *data = self->data;
struct winlink *wl;
struct client *c;
struct options *oo;
@ -93,27 +93,27 @@ cmd_set_window_option_exec(struct cmd *self, struct cmd_ctx *ctx)
oo = &wl->window->options;
}
if (*data->option == '\0') {
if (*data->arg == '\0') {
ctx->error(ctx, "invalid option");
return (-1);
}
entry = NULL;
for (opt = set_window_option_table; opt->name != NULL; opt++) {
if (strncmp(opt->name, data->option, strlen(data->option)) != 0)
if (strncmp(opt->name, data->arg, strlen(data->arg)) != 0)
continue;
if (entry != NULL) {
ctx->error(ctx, "ambiguous option: %s", data->option);
ctx->error(ctx, "ambiguous option: %s", data->arg);
return (-1);
}
entry = opt;
/* Bail now if an exact match. */
if (strcmp(entry->name, data->option) == 0)
if (strcmp(entry->name, data->arg) == 0)
break;
}
if (entry == NULL) {
ctx->error(ctx, "unknown option: %s", data->option);
ctx->error(ctx, "unknown option: %s", data->arg);
return (-1);
}
@ -123,7 +123,7 @@ cmd_set_window_option_exec(struct cmd *self, struct cmd_ctx *ctx)
"can't unset global option: %s", entry->name);
return (-1);
}
if (data->value != NULL) {
if (data->arg2 != NULL) {
ctx->error(ctx,
"value passed to unset option: %s", entry->name);
return (-1);
@ -135,25 +135,25 @@ cmd_set_window_option_exec(struct cmd *self, struct cmd_ctx *ctx)
switch (entry->type) {
case SET_OPTION_STRING:
set_option_string(ctx, oo, entry,
data->value, data->chflags & CMD_CHFLAG('a'));
data->arg2, data->chflags & CMD_CHFLAG('a'));
break;
case SET_OPTION_NUMBER:
set_option_number(ctx, oo, entry, data->value);
set_option_number(ctx, oo, entry, data->arg2);
break;
case SET_OPTION_KEY:
set_option_key(ctx, oo, entry, data->value);
set_option_key(ctx, oo, entry, data->arg2);
break;
case SET_OPTION_COLOUR:
set_option_colour(ctx, oo, entry, data->value);
set_option_colour(ctx, oo, entry, data->arg2);
break;
case SET_OPTION_ATTRIBUTES:
set_option_attributes(ctx, oo, entry, data->value);
set_option_attributes(ctx, oo, entry, data->arg2);
break;
case SET_OPTION_FLAG:
set_option_flag(ctx, oo, entry, data->value);
set_option_flag(ctx, oo, entry, data->arg2);
break;
case SET_OPTION_CHOICE:
set_option_choice(ctx, oo, entry, data->value);
set_option_choice(ctx, oo, entry, data->arg2);
break;
}
}

26
tmux.h
View File

@ -1010,9 +1010,11 @@ struct cmd_entry {
#define CMD_STARTSERVER 0x1
#define CMD_CANTNEST 0x2
#define CMD_ARG1 0x4
#define CMD_ARG01 0x8
#define CMD_SENDENVIRON 0x10
#define CMD_SENDENVIRON 0x4
#define CMD_ARG1 0x8
#define CMD_ARG01 0x10
#define CMD_ARG2 0x20
#define CMD_ARG12 0x40
int flags;
#define CMD_CHFLAG(flag) \
@ -1032,6 +1034,7 @@ struct cmd_target_data {
uint64_t chflags;
char *target;
char *arg;
char *arg2;
};
struct cmd_srcdst_data {
@ -1039,6 +1042,7 @@ struct cmd_srcdst_data {
char *src;
char *dst;
char *arg;
char *arg2;
};
struct cmd_buffer_data {
@ -1046,13 +1050,7 @@ struct cmd_buffer_data {
char *target;
int buffer;
char *arg;
};
struct cmd_option_data {
uint64_t chflags;
char *target;
char *option;
char *value;
char *arg2;
};
/* Key binding. */
@ -1373,14 +1371,6 @@ void cmd_buffer_init(struct cmd *, int);
int cmd_buffer_parse(struct cmd *, int, char **, char **);
void cmd_buffer_free(struct cmd *);
size_t cmd_buffer_print(struct cmd *, char *, size_t);
#define CMD_OPTION_PANE_USAGE "[-t target-pane] option [value]"
#define CMD_OPTION_WINDOW_USAGE "[-t target-window] option [value]"
#define CMD_OPTION_SESSION_USAGE "[-t target-session] option [value]"
#define CMD_OPTION_CLIENT_USAGE "[-t target-client] option [value]"
void cmd_option_init(struct cmd *, int);
int cmd_option_parse(struct cmd *, int, char **, char **);
void cmd_option_free(struct cmd *);
size_t cmd_option_print(struct cmd *, char *, size_t);
/* client.c */
int client_init(char *, struct client_ctx *, int, int);