summaryrefslogtreecommitdiffstats
path: root/tmux.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2009-10-21 20:11:47 +0000
committerNicholas Marriott <nicm@openbsd.org>2009-10-21 20:11:47 +0000
commit90ad041fa581a907a6efd1d9764b80f37d373447 (patch)
treecb3b7dddfb7c705f10fdbac6b3e623507fb849cc /tmux.c
parent59e667906f2530a9dc7a6bdb2871c7f2a27a4906 (diff)
Client tidying: get rid of client_ctx struct in favour of two variables in
client.c, and move the functions in client-fn.c into other files.
Diffstat (limited to 'tmux.c')
-rw-r--r--tmux.c93
1 files changed, 64 insertions, 29 deletions
diff --git a/tmux.c b/tmux.c
index 6e747bbe..f6d96c54 100644
--- a/tmux.c
+++ b/tmux.c
@@ -53,9 +53,10 @@ char *socket_path;
int login_shell;
__dead void usage(void);
+void fill_session(struct msg_command_data *);
char *makesockpath(const char *);
int prepare_cmd(enum msgtype *, void **, size_t *, int, char **);
-int dispatch_imsg(struct client_ctx *, const char *, int *);
+int dispatch_imsg(struct imsgbuf *, const char *, int *);
__dead void shell_exec(const char *, const char *);
__dead void
@@ -215,6 +216,44 @@ areshell(const char *shell)
return (0);
}
+void
+fill_session(struct msg_command_data *data)
+{
+ char *env, *ptr1, *ptr2, buf[256];
+ size_t len;
+ const char *errstr;
+ long long ll;
+
+ data->pid = -1;
+ if ((env = getenv("TMUX")) == NULL)
+ return;
+
+ if ((ptr2 = strrchr(env, ',')) == NULL || ptr2 == env)
+ return;
+ for (ptr1 = ptr2 - 1; ptr1 > env && *ptr1 != ','; ptr1--)
+ ;
+ if (*ptr1 != ',')
+ return;
+ ptr1++;
+ ptr2++;
+
+ len = ptr2 - ptr1 - 1;
+ if (len > (sizeof buf) - 1)
+ return;
+ memcpy(buf, ptr1, len);
+ buf[len] = '\0';
+
+ ll = strtonum(buf, 0, LONG_MAX, &errstr);
+ if (errstr != NULL)
+ return;
+ data->pid = ll;
+
+ ll = strtonum(ptr2, 0, UINT_MAX, &errstr);
+ if (errstr != NULL)
+ return;
+ data->idx = ll;
+}
+
char *
makesockpath(const char *label)
{
@@ -248,7 +287,7 @@ prepare_cmd(enum msgtype *msg, void **buf, size_t *len, int argc, char **argv)
{
static struct msg_command_data cmddata;
- client_fill_session(&cmddata);
+ fill_session(&cmddata);
cmddata.argc = argc;
if (cmd_pack_argv(argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
@@ -266,20 +305,19 @@ prepare_cmd(enum msgtype *msg, void **buf, size_t *len, int argc, char **argv)
int
main(int argc, char **argv)
{
- struct client_ctx cctx;
- struct cmd_list *cmdlist;
- struct cmd *cmd;
- struct pollfd pfd;
- enum msgtype msg;
- struct passwd *pw;
- struct options *so, *wo;
- struct keylist *keylist;
- char *s, *shellcmd, *path, *label, *home, *cause;
- char cwd[MAXPATHLEN], **var;
- void *buf;
- size_t len;
- int retcode, opt, flags, cmdflags = 0;
- int nfds;
+ struct cmd_list *cmdlist;
+ struct cmd *cmd;
+ struct pollfd pfd;
+ enum msgtype msg;
+ struct passwd *pw;
+ struct options *so, *wo;
+ struct keylist *keylist;
+ struct imsgbuf *ibuf;
+ char *s, *shellcmd, *path, *label, *home, *cause;
+ char cwd[MAXPATHLEN], **var;
+ void *buf;
+ size_t len;
+ int nfds, retcode, opt, flags, cmdflags = 0;
flags = 0;
shellcmd = label = path = NULL;
@@ -518,19 +556,17 @@ main(int argc, char **argv)
cmd_list_free(cmdlist);
}
- memset(&cctx, 0, sizeof cctx);
- if (client_init(path, &cctx, cmdflags, flags) != 0)
+ if ((ibuf = client_init(path, cmdflags, flags)) == NULL)
exit(1);
xfree(path);
- client_write_server(&cctx, msg, buf, len);
- memset(buf, 0, len);
+ imsg_compose(ibuf, msg, PROTOCOL_VERSION, -1, -1, buf, len);
retcode = 0;
for (;;) {
- pfd.fd = cctx.ibuf.fd;
+ pfd.fd = ibuf->fd;
pfd.events = POLLIN;
- if (cctx.ibuf.w.queued != 0)
+ if (ibuf->w.queued != 0)
pfd.events |= POLLOUT;
if ((nfds = poll(&pfd, 1, INFTIM)) == -1) {
@@ -545,12 +581,12 @@ main(int argc, char **argv)
fatalx("socket error");
if (pfd.revents & POLLIN) {
- if (dispatch_imsg(&cctx, shellcmd, &retcode) != 0)
+ if (dispatch_imsg(ibuf, shellcmd, &retcode) != 0)
break;
}
if (pfd.revents & POLLOUT) {
- if (msgbuf_write(&cctx.ibuf.w) < 0)
+ if (msgbuf_write(&ibuf->w) < 0)
fatalx("msgbuf_write failed");
}
}
@@ -562,18 +598,18 @@ main(int argc, char **argv)
}
int
-dispatch_imsg(struct client_ctx *cctx, const char *shellcmd, int *retcode)
+dispatch_imsg(struct imsgbuf *ibuf, const char *shellcmd, int *retcode)
{
struct imsg imsg;
ssize_t n, datalen;
struct msg_print_data printdata;
struct msg_shell_data shelldata;
- if ((n = imsg_read(&cctx->ibuf)) == -1 || n == 0)
+ if ((n = imsg_read(ibuf)) == -1 || n == 0)
fatalx("imsg_read failed");
for (;;) {
- if ((n = imsg_get(&cctx->ibuf, &imsg)) == -1)
+ if ((n = imsg_get(ibuf, &imsg)) == -1)
fatalx("imsg_get failed");
if (n == 0)
return (0);
@@ -601,8 +637,7 @@ dispatch_imsg(struct client_ctx *cctx, const char *shellcmd, int *retcode)
if (datalen != 0)
fatalx("bad MSG_READY size");
- *retcode = client_main(cctx);
- return (-1);
+ client_main(); /* doesn't return */
case MSG_VERSION:
if (datalen != 0)
fatalx("bad MSG_VERSION size");