Skip to content

Commit

Permalink
Get rid of the extra layer of flags and cmd_prepare() and just store the
Browse files Browse the repository at this point in the history
CMD_FIND_* flags in the cmd_entry and call it for the command. Commands
with special requirements call it themselves and update the target for
hooks to use.
  • Loading branch information
nicm committed Apr 22, 2017
1 parent 2c0f826 commit ee45a8a
Show file tree
Hide file tree
Showing 53 changed files with 323 additions and 438 deletions.
32 changes: 24 additions & 8 deletions cmd-attach-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@ const struct cmd_entry cmd_attach_session_entry = {
.args = { "c:dErt:", 0, 0 },
.usage = "[-dEr] [-c working-directory] " CMD_TARGET_SESSION_USAGE,

.tflag = CMD_SESSION_WITHPANE,
/* -t is special */

.flags = CMD_STARTSERVER,
.exec = cmd_attach_session_exec
};

enum cmd_retval
cmd_attach_session(struct cmdq_item *item, int dflag, int rflag,
const char *cflag, int Eflag)
cmd_attach_session(struct cmdq_item *item, const char *tflag, int dflag,
int rflag, const char *cflag, int Eflag)
{
struct cmd_find_state *current = &item->shared->current;
struct session *s = item->state.tflag.s;
enum cmd_find_type type;
int flags;
struct client *c = item->client, *c_loop;
struct winlink *wl = item->state.tflag.wl;
struct window_pane *wp = item->state.tflag.wp;
struct session *s;
struct winlink *wl;
struct window_pane *wp;
char *cause;

if (RB_EMPTY(&sessions)) {
Expand All @@ -70,6 +72,19 @@ cmd_attach_session(struct cmdq_item *item, int dflag, int rflag,
return (CMD_RETURN_ERROR);
}

if (tflag != NULL && tflag[strcspn(tflag, ":.")] != '\0') {
type = CMD_FIND_PANE;
flags = 0;
} else {
type = CMD_FIND_SESSION;
flags = CMD_FIND_PREFER_UNATTACHED;
}
if (cmd_find_target(&item->target, item, tflag, type, flags) != 0)
return (CMD_RETURN_ERROR);
s = item->target.s;
wl = item->target.wl;
wp = item->target.wp;

if (wl != NULL) {
if (wp != NULL)
window_set_active_pane(wp->window, wp);
Expand Down Expand Up @@ -150,6 +165,7 @@ cmd_attach_session_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;

return (cmd_attach_session(item, args_has(args, 'd'),
args_has(args, 'r'), args_get(args, 'c'), args_has(args, 'E')));
return (cmd_attach_session(item, args_get(args, 't'),
args_has(args, 'd'), args_has(args, 'r'), args_get(args, 'c'),
args_has(args, 'E')));
}
19 changes: 10 additions & 9 deletions cmd-break-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ const struct cmd_entry cmd_break_pane_entry = {
.alias = "breakp",

.args = { "dPF:n:s:t:", 0, 0 },
.usage = "[-dP] [-F format] [-n window-name] [-s src-pane] [-t dst-window]",
.usage = "[-dP] [-F format] [-n window-name] [-s src-pane] "
"[-t dst-window]",

.sflag = CMD_PANE,
.tflag = CMD_WINDOW_INDEX,
.source = { 's', CMD_FIND_PANE, 0 },
.target = { 't', CMD_FIND_WINDOW, CMD_FIND_WINDOW_INDEX },

.flags = 0,
.exec = cmd_break_pane_exec
Expand All @@ -49,14 +50,14 @@ cmd_break_pane_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct cmd_find_state *current = &item->shared->current;
struct client *c = item->state.c;
struct winlink *wl = item->state.sflag.wl;
struct session *src_s = item->state.sflag.s;
struct session *dst_s = item->state.tflag.s;
struct window_pane *wp = item->state.sflag.wp;
struct client *c = cmd_find_client(item, NULL, 1);
struct winlink *wl = item->source.wl;
struct session *src_s = item->source.s;
struct session *dst_s = item->target.s;
struct window_pane *wp = item->source.wp;
struct window *w = wl->window;
char *name, *cause;
int idx = item->state.tflag.idx;
int idx = item->target.idx;
const char *template;
char *cp;

Expand Down
6 changes: 3 additions & 3 deletions cmd-capture-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const struct cmd_entry cmd_capture_pane_entry = {
.usage = "[-aCeJpPq] " CMD_BUFFER_USAGE " [-E end-line] "
"[-S start-line]" CMD_TARGET_PANE_USAGE,

.tflag = CMD_PANE,
.target = { 't', CMD_FIND_PANE, 0 },

.flags = CMD_AFTERHOOK,
.exec = cmd_capture_pane_exec
Expand All @@ -56,7 +56,7 @@ const struct cmd_entry cmd_clear_history_entry = {
.args = { "t:", 0, 0 },
.usage = CMD_TARGET_PANE_USAGE,

.tflag = CMD_PANE,
.target = { 't', CMD_FIND_PANE, 0 },

.flags = CMD_AFTERHOOK,
.exec = cmd_capture_pane_exec
Expand Down Expand Up @@ -193,7 +193,7 @@ cmd_capture_pane_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct client *c;
struct window_pane *wp = item->state.tflag.wp;
struct window_pane *wp = item->target.wp;
char *buf, *cause;
const char *bufname;
size_t len;
Expand Down
6 changes: 3 additions & 3 deletions cmd-choose-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const struct cmd_entry cmd_choose_buffer_entry = {
.args = { "F:t:", 0, 1 },
.usage = CMD_TARGET_WINDOW_USAGE " [-F format] [template]",

.tflag = CMD_WINDOW,
.target = { 't', CMD_FIND_WINDOW, 0 },

.flags = 0,
.exec = cmd_choose_buffer_exec
Expand All @@ -50,8 +50,8 @@ static enum cmd_retval
cmd_choose_buffer_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct client *c = item->state.c;
struct winlink *wl = item->state.tflag.wl;
struct client *c = cmd_find_client(item, NULL, 1);
struct winlink *wl = item->target.wl;
struct window_choose_data *cdata;
struct paste_buffer *pb;
char *action, *action_data;
Expand Down
6 changes: 3 additions & 3 deletions cmd-choose-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const struct cmd_entry cmd_choose_client_entry = {
.args = { "F:t:", 0, 1 },
.usage = CMD_TARGET_WINDOW_USAGE " [-F format] [template]",

.tflag = CMD_WINDOW,
.target = { 't', CMD_FIND_WINDOW, 0 },

.flags = 0,
.exec = cmd_choose_client_exec
Expand All @@ -59,10 +59,10 @@ static enum cmd_retval
cmd_choose_client_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct client *c = item->state.c;
struct client *c = cmd_find_client(item, NULL, 1);
struct client *c1;
struct window_choose_data *cdata;
struct winlink *wl = item->state.tflag.wl;
struct winlink *wl = item->target.wl;
const char *template;
char *action;
u_int idx, cur;
Expand Down
12 changes: 6 additions & 6 deletions cmd-choose-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const struct cmd_entry cmd_choose_tree_entry = {
.usage = "[-suw] [-b session-template] [-c window template] "
"[-S format] [-W format] " CMD_TARGET_WINDOW_USAGE,

.tflag = CMD_WINDOW,
.target = { 't', CMD_FIND_WINDOW, 0 },

.flags = 0,
.exec = cmd_choose_tree_exec
Expand All @@ -64,7 +64,7 @@ const struct cmd_entry cmd_choose_session_entry = {
.args = { "F:t:", 0, 1 },
.usage = CMD_TARGET_WINDOW_USAGE " [-F format] [template]",

.tflag = CMD_WINDOW,
.target = { 't', CMD_FIND_WINDOW, 0 },

.flags = 0,
.exec = cmd_choose_tree_exec
Expand All @@ -77,7 +77,7 @@ const struct cmd_entry cmd_choose_window_entry = {
.args = { "F:t:", 0, 1 },
.usage = CMD_TARGET_WINDOW_USAGE "[-F format] [template]",

.tflag = CMD_WINDOW,
.target = { 't', CMD_FIND_WINDOW, 0 },

.flags = 0,
.exec = cmd_choose_tree_exec
Expand All @@ -87,9 +87,9 @@ static enum cmd_retval
cmd_choose_tree_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct client *c = item->state.c;
struct winlink *wl = item->state.tflag.wl, *wm;
struct session *s = item->state.tflag.s, *s2;
struct client *c = cmd_find_client(item, NULL, 1);
struct winlink *wl = item->target.wl, *wm;
struct session *s = item->target.s, *s2;
struct window_choose_data *wcd = NULL;
const char *ses_template, *win_template;
char *final_win_action, *cur_win_template;
Expand Down
7 changes: 4 additions & 3 deletions cmd-command-prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ const struct cmd_entry cmd_command_prompt_entry = {
.usage = "[-1Ni] [-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " "
"[template]",

.tflag = CMD_CLIENT,

.flags = 0,
.exec = cmd_command_prompt_exec
};
Expand All @@ -69,10 +67,13 @@ cmd_command_prompt_exec(struct cmd *self, struct cmdq_item *item)
struct args *args = self->args;
const char *inputs, *prompts;
struct cmd_command_prompt_cdata *cdata;
struct client *c = item->state.c;
struct client *c;
char *prompt, *ptr, *input = NULL;
size_t n;

if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);

if (c->prompt_string != NULL)
return (CMD_RETURN_NORMAL);

Expand Down
7 changes: 4 additions & 3 deletions cmd-confirm-before.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ const struct cmd_entry cmd_confirm_before_entry = {
.args = { "p:t:", 1, 1 },
.usage = "[-p prompt] " CMD_TARGET_CLIENT_USAGE " command",

.tflag = CMD_CLIENT,

.flags = 0,
.exec = cmd_confirm_before_exec
};
Expand All @@ -57,10 +55,13 @@ cmd_confirm_before_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct cmd_confirm_before_data *cdata;
struct client *c = item->state.c;
struct client *c;
char *cmd, *copy, *new_prompt, *ptr;
const char *prompt;

if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);

if ((prompt = args_get(args, 'p')) != NULL)
xasprintf(&new_prompt, "%s ", prompt);
else {
Expand Down
6 changes: 3 additions & 3 deletions cmd-copy-mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const struct cmd_entry cmd_copy_mode_entry = {
.args = { "Met:u", 0, 0 },
.usage = "[-Mu] " CMD_TARGET_PANE_USAGE,

.tflag = CMD_PANE,
.target = { 't', CMD_FIND_PANE, 0 },

.flags = CMD_AFTERHOOK,
.exec = cmd_copy_mode_exec
Expand All @@ -46,7 +46,7 @@ const struct cmd_entry cmd_clock_mode_entry = {
.args = { "t:", 0, 0 },
.usage = CMD_TARGET_PANE_USAGE,

.tflag = CMD_PANE,
.target = { 't', CMD_FIND_PANE, 0 },

.flags = CMD_AFTERHOOK,
.exec = cmd_copy_mode_exec
Expand All @@ -59,7 +59,7 @@ cmd_copy_mode_exec(struct cmd *self, struct cmdq_item *item)
struct cmdq_shared *shared = item->shared;
struct client *c = item->client;
struct session *s;
struct window_pane *wp = item->state.tflag.wp;
struct window_pane *wp = item->target.wp;

if (args_has(args, 'M')) {
if ((wp = cmd_mouse_pane(&shared->mouse, &s, NULL)) == NULL)
Expand Down
14 changes: 8 additions & 6 deletions cmd-detach-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ const struct cmd_entry cmd_detach_client_entry = {
.usage = "[-aP] [-E shell-command] "
"[-s target-session] " CMD_TARGET_CLIENT_USAGE,

.sflag = CMD_SESSION,
.tflag = CMD_CLIENT,
.source = { 's', CMD_FIND_SESSION, CMD_FIND_CANFAIL },

.flags = CMD_READONLY,
.exec = cmd_detach_client_exec
Expand All @@ -51,8 +50,6 @@ const struct cmd_entry cmd_suspend_client_entry = {
.args = { "t:", 0, 0 },
.usage = CMD_TARGET_CLIENT_USAGE,

.tflag = CMD_CLIENT,

.flags = 0,
.exec = cmd_detach_client_exec
};
Expand All @@ -61,11 +58,14 @@ static enum cmd_retval
cmd_detach_client_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct client *c = item->state.c, *cloop;
struct client *c, *cloop;
struct session *s;
enum msgtype msgtype;
const char *cmd = args_get(args, 'E');

if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);

if (self->entry == &cmd_suspend_client_entry) {
server_client_suspend(c);
return (CMD_RETURN_NORMAL);
Expand All @@ -77,7 +77,9 @@ cmd_detach_client_exec(struct cmd *self, struct cmdq_item *item)
msgtype = MSG_DETACH;

if (args_has(args, 's')) {
s = item->state.sflag.s;
s = item->source.s;
if (s == NULL)
return (CMD_RETURN_NORMAL);
TAILQ_FOREACH(cloop, &clients, entry) {
if (cloop->session == s) {
if (cmd != NULL)
Expand Down
12 changes: 6 additions & 6 deletions cmd-display-message.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ const struct cmd_entry cmd_display_message_entry = {
.usage = "[-p] [-c target-client] [-F format] "
CMD_TARGET_PANE_USAGE " [message]",

.cflag = CMD_CLIENT_CANFAIL,
.tflag = CMD_PANE,
.target = { 't', CMD_FIND_PANE, 0 },

.flags = CMD_AFTERHOOK,
.exec = cmd_display_message_exec
Expand All @@ -54,10 +53,10 @@ static enum cmd_retval
cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct client *c = item->state.c;
struct session *s = item->state.tflag.s;
struct winlink *wl = item->state.tflag.wl;
struct window_pane *wp = item->state.tflag.wp;
struct client *c;
struct session *s = item->target.s;
struct winlink *wl = item->target.wl;
struct window_pane *wp = item->target.wp;
const char *template;
char *msg;
struct format_tree *ft;
Expand All @@ -66,6 +65,7 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
cmdq_error(item, "only one of -F or argument must be given");
return (CMD_RETURN_ERROR);
}
c = cmd_find_client(item, args_get(args, 'c'), 1);

template = args_get(args, 'F');
if (args->argc != 0)
Expand Down
7 changes: 4 additions & 3 deletions cmd-display-panes.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ const struct cmd_entry cmd_display_panes_entry = {
.args = { "t:", 0, 1 },
.usage = CMD_TARGET_CLIENT_USAGE,

.tflag = CMD_CLIENT,

.flags = CMD_AFTERHOOK,
.exec = cmd_display_panes_exec
};
Expand All @@ -50,7 +48,10 @@ static enum cmd_retval
cmd_display_panes_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct client *c = item->state.c;
struct client *c;

if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);

if (c->identify_callback != NULL)
return (CMD_RETURN_NORMAL);
Expand Down
Loading

0 comments on commit ee45a8a

Please sign in to comment.