From 22990a6595fdff840010aa6df9a60ada61331b5e Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Thu, 27 Sep 2007 09:52:03 +0000 Subject: New session selection rules: - find by name if given - otherwise try current index from $TMUX - otherwise if only one session, use it - otherwise error --- server-fn.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'server-fn.c') diff --git a/server-fn.c b/server-fn.c index 0429af94..9309b343 100644 --- a/server-fn.c +++ b/server-fn.c @@ -1,4 +1,4 @@ -/* $Id: server-fn.c,v 1.3 2007-09-27 09:15:58 nicm Exp $ */ +/* $Id: server-fn.c,v 1.4 2007-09-27 09:52:03 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -19,9 +19,60 @@ #include #include +#include #include "tmux.h" +/* Find session from sessid. */ +struct session * +server_find_sessid(struct sessid *sid, char **cause) +{ + struct session *s; + u_int i, n; + + if (*sid->name != '\0') { + if ((s = session_find(sid->name)) == NULL) { + xasprintf(cause, "session not found: %s", sid->name); + return (NULL); + } + return (s); + } + + if (sid->pid != -1) { + if (sid->pid != getpid()) { + xasprintf(cause, "wrong server: %lld", sid->pid); + return (NULL); + } + if (sid->idx > ARRAY_LENGTH(&sessions)) { + xasprintf(cause, "index out of range: %u", sid->idx); + return (NULL); + } + if ((s = ARRAY_ITEM(&sessions, sid->idx) = NULL)) { + xasprintf(cause, "session doesn't exist: %u", sid->idx); + return (NULL); + } + return (s); + } + + s = NULL; + n = 0; + for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { + if (ARRAY_ITEM(&sessions, i) != NULL) { + s = ARRAY_ITEM(&sessions, i); + n++; + } + } + if (s == NULL) { + xasprintf(cause, "no sessions"); + return (NULL); + } + if (n != 1) { + xasprintf(cause, "multiple sessions and session not specified"); + return (NULL); + } + return (s); +} + /* Write command to a client. */ void server_write_client(struct client *c, enum hdrtype type, void *buf, size_t len) -- cgit