diff options
author | Nicholas Marriott <nicholas.marriott@gmail.com> | 2009-07-14 06:43:33 +0000 |
---|---|---|
committer | Nicholas Marriott <nicholas.marriott@gmail.com> | 2009-07-14 06:43:33 +0000 |
commit | 097b96ea44d5a09ac1f3553f1789ef6e932a9eba (patch) | |
tree | 73b9da430bb07a47de4a95690427bc14768eb61a | |
parent | 5ccd0025a0867c9f5d9f173537c8336c7492bc8e (diff) |
Having fixed flags for single-character getopt options is a bit hard to
maintain and is only going to get worse as more are used. So instead, add a new
uint64_t member to cmd_entry which is a bitmask of upper and lowercase options
accepted by the command.
This means new single character options can be used without the need to add it
explicitly to the list.
73 files changed, 264 insertions, 302 deletions
diff --git a/cmd-attach-session.c b/cmd-attach-session.c index 898bf645..2516b528 100644 --- a/cmd-attach-session.c +++ b/cmd-attach-session.c @@ -1,4 +1,4 @@ -/* $Id: cmd-attach-session.c,v 1.26 2009-06-25 16:34:50 nicm Exp $ */ +/* $Id: cmd-attach-session.c,v 1.27 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -29,7 +29,7 @@ int cmd_attach_session_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_attach_session_entry = { "attach-session", "attach", "[-d] " CMD_TARGET_SESSION_USAGE, - CMD_DFLAG|CMD_CANTNEST|CMD_STARTSERVER, + CMD_CANTNEST|CMD_STARTSERVER, CMD_CHFLAG('d'), cmd_target_init, cmd_target_parse, cmd_attach_session_exec, @@ -67,7 +67,7 @@ cmd_attach_session_exec(struct cmd *self, struct cmd_ctx *ctx) return (-1); } - if (data->flags & CMD_DFLAG) + if (data->chflags & CMD_CHFLAG('d')) server_write_session(s, MSG_DETACH, NULL, 0); ctx->cmdclient->session = s; diff --git a/cmd-bind-key.c b/cmd-bind-key.c index 33a04164..393bc46e 100644 --- a/cmd-bind-key.c +++ b/cmd-bind-key.c @@ -1,4 +1,4 @@ -/* $Id: cmd-bind-key.c,v 1.21 2009-05-04 17:58:26 nicm Exp $ */ +/* $Id: cmd-bind-key.c,v 1.22 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -40,7 +40,7 @@ struct cmd_bind_key_data { const struct cmd_entry cmd_bind_key_entry = { "bind-key", "bind", "[-r] key command [arguments]", - 0, + 0, 0, NULL, cmd_bind_key_parse, cmd_bind_key_exec, diff --git a/cmd-break-pane.c b/cmd-break-pane.c index 88271971..0c4b89e4 100644 --- a/cmd-break-pane.c +++ b/cmd-break-pane.c @@ -1,4 +1,4 @@ -/* $Id: cmd-break-pane.c,v 1.3 2009-05-18 21:01:38 nicm Exp $ */ +/* $Id: cmd-break-pane.c,v 1.4 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> @@ -31,7 +31,7 @@ int cmd_break_pane_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_break_pane_entry = { "break-pane", "breakp", CMD_PANE_WINDOW_USAGE " [-d]", - CMD_DFLAG, + 0, CMD_CHFLAG('d'), cmd_pane_init, cmd_pane_parse, cmd_break_pane_exec, @@ -82,7 +82,7 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_ctx *ctx) w->name = default_window_name(w); wl = session_attach(s, w, -1, &cause); /* can't fail */ - if (!(data->flags & CMD_DFLAG)) + if (!(data->chflags & CMD_CHFLAG('d'))) session_select(s, wl->idx); layout_refresh(w, 0); diff --git a/cmd-choose-session.c b/cmd-choose-session.c index 84569a68..d92fabb8 100644 --- a/cmd-choose-session.c +++ b/cmd-choose-session.c @@ -1,4 +1,4 @@ -/* $Id: cmd-choose-session.c,v 1.7 2009-05-04 17:58:26 nicm Exp $ */ +/* $Id: cmd-choose-session.c,v 1.8 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> @@ -31,7 +31,7 @@ void cmd_choose_session_callback(void *, int); const struct cmd_entry cmd_choose_session_entry = { "choose-session", NULL, CMD_TARGET_WINDOW_USAGE, - 0, + 0, 0, cmd_target_init, cmd_target_parse, cmd_choose_session_exec, diff --git a/cmd-choose-window.c b/cmd-choose-window.c index ede89516..fa1e6f24 100644 --- a/cmd-choose-window.c +++ b/cmd-choose-window.c @@ -1,4 +1,4 @@ -/* $Id: cmd-choose-window.c,v 1.9 2009-05-04 17:58:26 nicm Exp $ */ +/* $Id: cmd-choose-window.c,v 1.10 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> @@ -31,7 +31,7 @@ void cmd_choose_window_callback(void *, int); const struct cmd_entry cmd_choose_window_entry = { "choose-window", NULL, CMD_TARGET_WINDOW_USAGE, - 0, + 0, 0, cmd_target_init, cmd_target_parse, cmd_choose_window_exec, diff --git a/cmd-clear-history.c b/cmd-clear-history.c index d012c043..a000d486 100644 --- a/cmd-clear-history.c +++ b/cmd-clear-history.c @@ -1,4 +1,4 @@ -/* $Id: cmd-clear-history.c,v 1.3 2009-06-25 16:21:32 nicm Exp $ */ +/* $Id: cmd-clear-history.c,v 1.4 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> @@ -29,7 +29,7 @@ int cmd_clear_history_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_clear_history_entry = { "clear-history", "clearhist", CMD_PANE_WINDOW_USAGE, - 0, + 0, 0, cmd_pane_init, cmd_pane_parse, cmd_clear_history_exec, diff --git a/cmd-clock-mode.c b/cmd-clock-mode.c index de10833d..b1649d53 100644 --- a/cmd-clock-mode.c +++ b/cmd-clock-mode.c @@ -1,4 +1,4 @@ -/* $Id: cmd-clock-mode.c,v 1.3 2009-01-19 18:23:40 nicm Exp $ */ +/* $Id: cmd-clock-mode.c,v 1.4 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> @@ -29,7 +29,7 @@ int cmd_clock_mode_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_clock_mode_entry = { "clock-mode", NULL, CMD_TARGET_WINDOW_USAGE, - 0, + 0, 0, cmd_target_init, cmd_target_parse, cmd_clock_mode_exec, diff --git a/cmd-command-prompt.c b/cmd-command-prompt.c index 52f8db1b..05d7b1f3 100644 --- a/cmd-command-prompt.c +++ b/cmd-command-prompt.c @@ -1,4 +1,4 @@ -/* $Id: cmd-command-prompt.c,v 1.16 2009-02-16 18:58:14 nicm Exp $ */ +/* $Id: cmd-command-prompt.c,v 1.17 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net> @@ -35,7 +35,7 @@ int cmd_command_prompt_callback(void *, const char *); const struct cmd_entry cmd_command_prompt_entry = { "command-prompt", NULL, CMD_TARGET_CLIENT_USAGE " [template]", - CMD_ARG01, + CMD_ARG01, 0, cmd_command_prompt_init, cmd_target_parse, cmd_command_prompt_exec, diff --git a/cmd-confirm-before.c b/cmd-confirm-before.c index 60a0a1c9..dd3e88e7 100644 --- a/cmd-confirm-before.c +++ b/cmd-confirm-before.c @@ -1,4 +1,4 @@ -/* $Id: cmd-confirm-before.c,v 1.4 2009-04-28 18:29:44 tcunha Exp $ */ +/* $Id: cmd-confirm-before.c,v 1.5 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org> @@ -38,7 +38,7 @@ struct cmd_confirm_before_data { const struct cmd_entry cmd_confirm_before_entry = { "confirm-before", "confirm", CMD_TARGET_CLIENT_USAGE " command", - CMD_ARG1, + CMD_ARG1, 0, cmd_confirm_before_init, cmd_target_parse, cmd_confirm_before_exec, diff --git a/cmd-copy-buffer.c b/cmd-copy-buffer.c index c72cbb5b..53538104 100644 --- a/cmd-copy-buffer.c +++ b/cmd-copy-buffer.c @@ -1,4 +1,4 @@ -/* $Id: cmd-copy-buffer.c,v 1.1 2009-02-03 17:21:19 tcunha Exp $ */ +/* $Id: cmd-copy-buffer.c,v 1.2 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org> @@ -42,7 +42,7 @@ struct cmd_copy_buffer_data { const struct cmd_entry cmd_copy_buffer_entry = { "copy-buffer", "copyb", "[-a src-index] [-b dst-index] [-s src-session] [-t dst-session]", - 0, + 0, 0, cmd_copy_buffer_init, cmd_copy_buffer_parse, cmd_copy_buffer_exec, diff --git a/cmd-copy-mode.c b/cmd-copy-mode.c index e46abe70..19486539 100644 --- a/cmd-copy-mode.c +++ b/cmd-copy-mode.c @@ -1,4 +1,4 @@ -/* $Id: cmd-copy-mode.c,v 1.20 2009-07-14 06:42:22 nicm Exp $ */ +/* $Id: cmd-copy-mode.c,v 1.21 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -29,7 +29,7 @@ int cmd_copy_mode_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_copy_mode_entry = { "copy-mode", NULL, "[-u] " CMD_TARGET_WINDOW_USAGE, - CMD_UFLAG, + 0, CMD_CHFLAG('u'), cmd_target_init, cmd_target_parse, cmd_copy_mode_exec, @@ -51,7 +51,7 @@ cmd_copy_mode_exec(struct cmd *self, struct cmd_ctx *ctx) wp = wl->window->active; window_pane_set_mode(wp, &window_copy_mode); - if (wp->mode == &window_copy_mode && data->flags & CMD_UFLAG) + if (wp->mode == &window_copy_mode && data->chflags & CMD_CHFLAG('u')) window_copy_pageup(wp); return (0); diff --git a/cmd-delete-buffer.c b/cmd-delete-buffer.c index f94e4c13..0b190eee 100644 --- a/cmd-delete-buffer.c +++ b/cmd-delete-buffer.c @@ -1,4 +1,4 @@ -/* $Id: cmd-delete-buffer.c,v 1.5 2009-05-04 17:58:26 nicm Exp $ */ +/* $Id: cmd-delete-buffer.c,v 1.6 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -31,7 +31,7 @@ int cmd_delete_buffer_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_delete_buffer_entry = { "delete-buffer", "deleteb", CMD_BUFFER_SESSION_USAGE, - 0, + 0, 0, cmd_buffer_init, cmd_buffer_parse, cmd_delete_buffer_exec, diff --git a/cmd-detach-client.c b/cmd-detach-client.c index 3c0879e1..67f85a1e 100644 --- a/cmd-detach-client.c +++ b/cmd-detach-client.c @@ -1,4 +1,4 @@ -/* $Id: cmd-detach-client.c,v 1.7 2009-01-19 18:23:40 nicm Exp $ */ +/* $Id: cmd-detach-client.c,v 1.8 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -29,7 +29,7 @@ int cmd_detach_client_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_detach_client_entry = { "detach-client", "detach", CMD_TARGET_CLIENT_USAGE, - 0, + 0, 0, cmd_target_init, cmd_target_parse, cmd_detach_client_exec, diff --git a/cmd-down-pane.c b/cmd-down-pane.c index af67bbac..8a48c38a 100644 --- a/cmd-down-pane.c +++ b/cmd-down-pane.c @@ -1,4 +1,4 @@ -/* $Id: cmd-down-pane.c,v 1.7 2009-04-01 21:10:08 nicm Exp $ */ +/* $Id: cmd-down-pane.c,v 1.8 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> @@ -29,7 +29,7 @@ int cmd_down_pane_exec(struct cmd *, struct cmd_ctx *); const struct cmd_entry cmd_down_pane_entry = { "down-pane", "downp", CMD_TARGET_WINDOW_USAGE, - 0, + 0, 0, cmd_target_init, cmd_target_parse, cmd_down_pane_exec, diff --git a/cmd-find-window.c b/cmd-find-window.c index 1e024efd..2534d067 100644 --- a/cmd-find-window.c +++ b/cmd-find-window.c @@ -1,4 +1,4 @@ -/* $Id: cmd-find-window.c,v 1.10 2009-06-25 16:21:32 nicm Exp $ */ +/* $Id: cmd-find-window.c,v 1.11 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> @@ -34,7 +34,7 @@ void cmd_find_window_callback(void *, int); const struct cmd_entry cmd_find_window_entry = { "find-window", "findw", CMD_TARGET_WINDOW_USAGE " match-string", - CMD_ARG1, + CMD_ARG1, 0, cmd_target_init, cmd_target_parse, cmd_find_window_exec, diff --git a/cmd-generic.c b/cmd-generic.c index b82f9c8e..ea0f0039 100644 --- a/cmd-generic.c +++ b/cmd-generic.c @@ -1,4 +1,4 @@ -/* $Id: cmd-generic.c,v 1.29 2009-07-12 17:11:39 nicm Exp $ */ +/* $Id: cmd-generic.c,v 1.30 2009-07-14 06:43:32 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net> @@ -23,12 +23,9 @@ #include "tmux.h" -#define CMD_FLAGS "adDgkruU" -#define CMD_FLAGMASK (CMD_AFLAG|CMD_DFLAG|CMD_BIGDFLAG|CMD_GFLAG|CMD_KFLAG| \ - CMD_RFLAG|CMD_UFLAG|CMD_BIGUFLAG) - -int cmd_do_flags(int, int, int *); -size_t cmd_print_flags(char *, size_t, size_t, int); +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 **); size_t @@ -39,86 +36,70 @@ cmd_prarg(char *buf, size_t len, const char *prefix, char *arg) return (xsnprintf(buf, len, "%s%s", prefix, arg)); } +/* Prepend flags from chflags onto flagstr and call getopt. */ int -cmd_do_flags(int opt, int iflags, int *oflags) +cmd_getopt(int argc, char **argv, const char *flagstr, uint64_t chflags) { - switch (opt) { - case 'a': - if (iflags & CMD_AFLAG) { - (*oflags) |= CMD_AFLAG; - return (0); - } - return (-1); - case 'd': - if (iflags & CMD_DFLAG) { - (*oflags) |= CMD_DFLAG; - return (0); - } - return (-1); - case 'D': - if (iflags & CMD_BIGDFLAG) { - (*oflags) |= CMD_BIGDFLAG; - return (0); - } - return (-1); - case 'g': - if (iflags & CMD_GFLAG) { - (*oflags) |= CMD_GFLAG; - return (0); - } - return (-1); - case 'k': - if (iflags & CMD_KFLAG) { - (*oflags) |= CMD_KFLAG; - return (0); - } - return (-1); - case 'r': - if (iflags & CMD_RFLAG) { - (*oflags) |= CMD_RFLAG; - return (0); - } - return (-1); - case 'u': - if (iflags & CMD_UFLAG) { - (*oflags) |= CMD_UFLAG; + u_char ch; + char buf[128]; + size_t len, off; + + *buf = '\0'; + + len = sizeof buf; + off = 0; + + for (ch = 0; ch < 26; ch++) { + if (chflags & CMD_CHFLAG('a' + ch)) + off += xsnprintf(buf + off, len - off, "%c", 'a' + ch); + if (chflags & CMD_CHFLAG('A' + ch)) + off += xsnprintf(buf + off, len - off, "%c", 'A' + ch); + } + + strlcat(buf, flagstr, sizeof buf); + + return (getopt(argc, argv, buf)); +} + +/* + * If this option is expected (in ichflags), set it in ochflags, otherwise + * return -1. + */ +int +cmd_flags(int opt, uint64_t ichflags, uint64_t *ochflags) +{ + u_char ch; + + for (ch = 0; ch < 26; ch++) { + if (opt == 'a' + ch && ichflags & CMD_CHFLAG(opt)) { + (*ochflags) |= CMD_CHFLAG(opt); return (0); } - return (-1); - case 'U': - if (iflags & CMD_BIGUFLAG) { - (*oflags) |= CMD_BIGUFLAG; + if (opt == 'A' + ch && ichflags & CMD_CHFLAG(opt)) { + (*ochflags) |= CMD_CHFLAG(opt); return (0); } - return (-1); } - return (1); + return (-1); } +/* Print the flags supported in chflags. */ size_t -cmd_print_flags(char *buf, size_t len, size_t off, int flags) +cmd_print_flags(char *buf, size_t len, size_t off, uint64_t chflags) { + u_char ch; size_t boff = off; - if ((flags & CMD_FLAGMASK) == 0) + if (chflags == 0) return (0); off += xsnprintf(buf + off, len - off, " -"); - if (off < len && flags & CMD_AFLAG) - off += xsnprintf(buf + off, len - off, "a"); - if (off < len && flags & CMD_BIGDFLAG) - off += xsnprintf(buf + off, len - off, "D"); - if (off < len && flags & CMD_DFLAG) - off += xsnprintf(buf + off, len - off, "d"); - if (off < len && flags & CMD_GFLAG) - off += xsnprintf(buf + off, len - off, "g"); - if (off < len && flags & CMD_KFLAG) - off += xsnprintf(buf + off, len - off, "k"); - if (off < len && flags & CMD_RFLAG) - off += xsnprintf(buf + off, len - off, "r"); - if (off < len && flags & CMD_UFLAG) - off += xsnprintf(buf + off, len - off, "u"); - if (off < len && flags & CMD_BIGUFLAG) - off += xsnprintf(buf + off, len - off, "U"); + + for (ch = 0; ch < 26; ch++) { + if (chflags & CMD_CHFLAG('a' + ch)) + off += xsnprintf(buf + off, len - off, "%c", 'a' + ch); + if (chflags & CMD_CHFLAG('A' + ch)) + off += xsnprintf(buf + off, len - off, "%c", 'A' + ch); + } return (off - boff); } @@ -153,7 +134,7 @@ cmd_target_init(struct cmd *self, unused int key) struct cmd_target_data *data; self->data = data = xmalloc(sizeof *data); - data->flags = 0; + data->chflags = 0; data->target = NULL; data->arg = NULL; } @@ -162,19 +143,16 @@ int cmd_target_parse(struct cmd *self, int argc, char **argv, char **cause) { struct cmd_target_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_target_init(self, 0); data = self->data; - while ((opt = getopt(argc, argv, CMD_FLAGS "t:")) != -1) { - switch (cmd_do_flags(opt, self->entry->flags, &data->flags)) { - case -1: - goto usage; - case 0: + 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) @@ -240,7 +218,7 @@ cmd_target_print(struct cmd *self, char *buf, size_t len) off += xsnprintf(buf, len, "%s", self->entry->name); if (data == NULL) return (off); - off += cmd_print_flags(buf, len, off, data->flags); + 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->arg != NULL) @@ -254,7 +232,7 @@ cmd_srcdst_init(struct cmd *self, unused int key) struct cmd_srcdst_data *data; self->data = data = xmalloc(sizeof *dat |