From 3b2ac4f9d31b028c037b08b09465c6477a09689c Mon Sep 17 00:00:00 2001 From: nicm Date: Sat, 25 Apr 2015 18:09:28 +0000 Subject: [PATCH] Move the functions to convert ids from strings into session.c and window.c. --- usr.bin/tmux/cmd-attach-session.c | 11 ++++-- usr.bin/tmux/cmd-switch-client.c | 12 +++--- usr.bin/tmux/cmd.c | 62 ++++--------------------------- usr.bin/tmux/session.c | 18 ++++++++- usr.bin/tmux/tmux.h | 7 ++-- usr.bin/tmux/window.c | 33 +++++++++++++++- 6 files changed, 73 insertions(+), 70 deletions(-) diff --git a/usr.bin/tmux/cmd-attach-session.c b/usr.bin/tmux/cmd-attach-session.c index 4d6438efac9..7117021f310 100644 --- a/usr.bin/tmux/cmd-attach-session.c +++ b/usr.bin/tmux/cmd-attach-session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-attach-session.c,v 1.33 2015/04/24 23:17:11 nicm Exp $ */ +/* $OpenBSD: cmd-attach-session.c,v 1.34 2015/04/25 18:09:28 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -69,9 +69,12 @@ cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag, } else { if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) return (CMD_RETURN_ERROR); - w = cmd_lookup_windowid(tflag); - if (w == NULL && (wp = cmd_lookup_paneid(tflag)) != NULL) - w = wp->window; + w = window_find_by_id_str(tflag); + if (w == NULL) { + wp = window_pane_find_by_id_str(tflag); + if (wp != NULL) + w = wp->window; + } if (w != NULL) wl = winlink_find_by_window(&s->windows, w); } diff --git a/usr.bin/tmux/cmd-switch-client.c b/usr.bin/tmux/cmd-switch-client.c index ee29af604cb..ddf3e7f54d8 100644 --- a/usr.bin/tmux/cmd-switch-client.c +++ b/usr.bin/tmux/cmd-switch-client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-switch-client.c,v 1.23 2015/04/20 15:34:56 nicm Exp $ */ +/* $OpenBSD: cmd-switch-client.c,v 1.24 2015/04/25 18:09:28 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -99,10 +99,12 @@ cmd_switch_client_exec(struct cmd *self, struct cmd_q *cmdq) } else { if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) return (CMD_RETURN_ERROR); - w = cmd_lookup_windowid(tflag); - if (w == NULL && - (wp = cmd_lookup_paneid(tflag)) != NULL) - w = wp->window; + w = window_find_by_id_str(tflag); + if (w == NULL) { + wp = window_pane_find_by_id_str(tflag); + if (wp != NULL) + w = wp->window; + } if (w != NULL) wl = winlink_find_by_window(&s->windows, w); } diff --git a/usr.bin/tmux/cmd.c b/usr.bin/tmux/cmd.c index 9f92187179c..f1c3fa69fca 100644 --- a/usr.bin/tmux/cmd.c +++ b/usr.bin/tmux/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.101 2015/04/24 23:17:11 nicm Exp $ */ +/* $OpenBSD: cmd.c,v 1.102 2015/04/25 18:09:28 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -124,7 +124,6 @@ struct session *cmd_choose_session(int); struct client *cmd_choose_client(struct client_list *); struct client *cmd_lookup_client(const char *); struct session *cmd_lookup_session(struct cmd_q *, const char *, int *); -struct session *cmd_lookup_session_id(const char *); struct winlink *cmd_lookup_window(struct session *, const char *, int *); int cmd_lookup_index(struct session *, const char *, int *); struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *); @@ -639,21 +638,6 @@ cmd_lookup_client(const char *name) return (NULL); } -/* Find the target session or report an error and return NULL. */ -struct session * -cmd_lookup_session_id(const char *arg) -{ - char *endptr; - long id; - - if (arg[0] != '$') - return (NULL); - id = strtol(arg + 1, &endptr, 10); - if (arg[1] != '\0' && *endptr == '\0') - return (session_find_by_id(id)); - return (NULL); -} - /* Lookup a session by name. If no session is found, NULL is returned. */ struct session * cmd_lookup_session(struct cmd_q *cmdq, const char *name, int *ambiguous) @@ -665,13 +649,13 @@ cmd_lookup_session(struct cmd_q *cmdq, const char *name, int *ambiguous) *ambiguous = 0; /* Look for $id first. */ - if ((s = cmd_lookup_session_id(name)) != NULL) + if ((s = session_find_by_id_str(name)) != NULL) return (s); /* Try as pane or window id. */ - if ((wp = cmd_lookup_paneid(name)) != NULL) + if ((wp = window_pane_find_by_id_str(name)) != NULL) return (cmd_window_session(cmdq, wp->window, NULL)); - if ((w = cmd_lookup_windowid(name)) != NULL) + if ((w = window_find_by_id_str(name)) != NULL) return (cmd_window_session(cmdq, w, NULL)); /* @@ -722,12 +706,12 @@ cmd_lookup_window(struct session *s, const char *name, int *ambiguous) return (wl); /* Lookup as pane or window id. */ - if ((wp = cmd_lookup_paneid(name)) != NULL) { + if ((wp = window_pane_find_by_id_str(name)) != NULL) { wl = winlink_find_by_window(&s->windows, wp->window); if (wl != NULL) return (wl); } - if ((w = cmd_lookup_windowid(name)) != NULL) { + if ((w = window_find_by_id_str(name)) != NULL) { wl = winlink_find_by_window(&s->windows, w); if (wl != NULL) return (wl); @@ -795,22 +779,6 @@ cmd_lookup_index(struct session *s, const char *name, int *ambiguous) return (-1); } -/* Lookup pane id. An initial % means a pane id. */ -struct window_pane * -cmd_lookup_paneid(const char *arg) -{ - const char *errstr; - u_int paneid; - - if (*arg != '%') - return (NULL); - - paneid = strtonum(arg + 1, 0, UINT_MAX, &errstr); - if (errstr != NULL) - return (NULL); - return (window_pane_find_by_id(paneid)); -} - /* Lookup window id in a session. An initial @ means a window id. */ struct winlink * cmd_lookup_winlink_windowid(struct session *s, const char *arg) @@ -827,22 +795,6 @@ cmd_lookup_winlink_windowid(struct session *s, const char *arg) return (winlink_find_by_window_id(&s->windows, windowid)); } -/* Lookup window id. An initial @ means a window id. */ -struct window * -cmd_lookup_windowid(const char *arg) -{ - const char *errstr; - u_int windowid; - - if (*arg != '@') - return (NULL); - - windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr); - if (errstr != NULL) - return (NULL); - return (window_find_by_id(windowid)); -} - /* Find session and winlink for window. */ struct session * cmd_window_session(struct cmd_q *cmdq, struct window *w, struct winlink **wlp) @@ -1252,7 +1204,7 @@ cmd_find_pane(struct cmd_q *cmdq, } /* Lookup as pane id. */ - if ((*wpp = cmd_lookup_paneid(arg)) != NULL) { + if ((*wpp = window_pane_find_by_id_str(arg)) != NULL) { s = cmd_window_session(cmdq, (*wpp)->window, &wl); if (sp != NULL) *sp = s; diff --git a/usr.bin/tmux/session.c b/usr.bin/tmux/session.c index 09da65c3ed4..f809f06bbf8 100644 --- a/usr.bin/tmux/session.c +++ b/usr.bin/tmux/session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: session.c,v 1.47 2015/04/22 15:32:33 nicm Exp $ */ +/* $OpenBSD: session.c,v 1.48 2015/04/25 18:09:28 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -70,6 +70,22 @@ session_find(const char *name) return (RB_FIND(sessions, &sessions, &s)); } +/* Find session by id parsed from a string. */ +struct session * +session_find_by_id_str(const char *s) +{ + const char *errstr; + u_int id; + + if (*s != '$') + return (NULL); + + id = strtonum(s + 1, 0, UINT_MAX, &errstr); + if (errstr != NULL) + return (NULL); + return (session_find_by_id(id)); +} + /* Find session by id. */ struct session * session_find_by_id(u_int id) diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 3503e8fd1c6..1af1c2861ab 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.499 2015/04/24 23:17:11 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.500 2015/04/25 18:09:28 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -1766,8 +1766,6 @@ int cmd_find_index(struct cmd_q *, const char *, struct winlink *cmd_find_pane(struct cmd_q *, const char *, struct session **, struct window_pane **); char *cmd_template_replace(const char *, const char *, int); -struct window *cmd_lookup_windowid(const char *); -struct window_pane *cmd_lookup_paneid(const char *); extern const struct cmd_entry *cmd_table[]; extern const struct cmd_entry cmd_attach_session_entry; extern const struct cmd_entry cmd_bind_key_entry; @@ -2147,6 +2145,7 @@ struct winlink *winlink_previous_by_number(struct winlink *, struct session *, int); void winlink_stack_push(struct winlink_stack *, struct winlink *); void winlink_stack_remove(struct winlink_stack *, struct winlink *); +struct window *window_find_by_id_str(const char *); struct window *window_find_by_id(u_int); struct window *window_create1(u_int, u_int); struct window *window_create(const char *, int, char **, const char *, @@ -2172,6 +2171,7 @@ struct window_pane *window_pane_previous_by_number(struct window *, int window_pane_index(struct window_pane *, u_int *); u_int window_count_panes(struct window *); void window_destroy_panes(struct window *); +struct window_pane *window_pane_find_by_id_str(const char *); struct window_pane *window_pane_find_by_id(u_int); struct window_pane *window_pane_create(struct window *, u_int, u_int, u_int); void window_pane_destroy(struct window_pane *); @@ -2309,6 +2309,7 @@ int session_cmp(struct session *, struct session *); RB_PROTOTYPE(sessions, session, entry, session_cmp); int session_alive(struct session *); struct session *session_find(const char *); +struct session *session_find_by_id_str(const char *); struct session *session_find_by_id(u_int); struct session *session_create(const char *, int, char **, const char *, int, struct environ *, struct termios *, int, u_int, diff --git a/usr.bin/tmux/window.c b/usr.bin/tmux/window.c index fc0e869b455..c151127a624 100644 --- a/usr.bin/tmux/window.c +++ b/usr.bin/tmux/window.c @@ -1,4 +1,4 @@ -/* $OpenBSD: window.c,v 1.122 2015/04/22 15:30:11 nicm Exp $ */ +/* $OpenBSD: window.c,v 1.123 2015/04/25 18:09:28 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -253,6 +253,21 @@ winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl) } } +struct window * +window_find_by_id_str(const char* s) +{ + const char *errstr; + u_int id; + + if (*s != '@') + return (NULL); + + id = strtonum(s + 1, 0, UINT_MAX, &errstr); + if (errstr != NULL) + return (NULL); + return (window_find_by_id(id)); +} + struct window * window_find_by_id(u_int id) { @@ -653,7 +668,21 @@ window_printable_flags(struct session *s, struct winlink *wl) return (xstrdup(flags)); } -/* Find pane in global tree by id. */ +struct window_pane * +window_pane_find_by_id_str(const char *s) +{ + const char *errstr; + u_int id; + + if (*s != '%') + return (NULL); + + id = strtonum(s + 1, 0, UINT_MAX, &errstr); + if (errstr != NULL) + return (NULL); + return (window_pane_find_by_id(id)); +} + struct window_pane * window_pane_find_by_id(u_int id) { -- 2.20.1