Split list into ls/lsw.

pull/1/head
Nicholas Marriott 2007-09-27 10:09:37 +00:00
parent 22990a6595
commit 58affec94c
5 changed files with 130 additions and 82 deletions

1
TODO
View File

@ -22,7 +22,6 @@
rethought rethought
- figure out once and for all what is going on with backspace and del - figure out once and for all what is going on with backspace and del
- deal properly with ambiguous ops... list-sessions & list-windows - deal properly with ambiguous ops... list-sessions & list-windows
- split list into list-sessions and list-windows
- command form: - command form:
tmux <flags> cmd [-s session] <flags> data tmux <flags> cmd [-s session] <flags> data
eg eg

168
op-list.c
View File

@ -1,4 +1,4 @@
/* $Id: op-list.c,v 1.4 2007-09-27 09:52:03 nicm Exp $ */ /* $Id: op-list.c,v 1.5 2007-09-27 10:09:37 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -25,47 +25,21 @@
#include "tmux.h" #include "tmux.h"
int int
op_list(char *path, int argc, char **argv) op_list_sessions(char *path, int argc, unused char **argv)
{ {
struct client_ctx cctx; struct client_ctx cctx;
char name[MAXNAMELEN], *tim; char *tim;
int opt; struct sessions_data data;
struct sessions_data sdata; struct sessions_entry ent;
struct sessions_entry sent;
struct windows_data wdata;
struct windows_entry went;
struct pollfd pfd; struct pollfd pfd;
struct hdr hdr; struct hdr hdr;
*name = '\0'; if (argc != 1)
optind = 1; return (usage("list-sessions"));
while ((opt = getopt(argc, argv, "s:?")) != EOF) {
switch (opt) {
case 's':
if (strlcpy(name, optarg, sizeof name) >= sizeof name) {
log_warnx("%s: session name too long", optarg);
return (1);
}
break;
case '?':
default:
return (usage("list [-s session]"));
}
}
argc -= optind;
argv += optind;
if (argc != 0)
return (usage("list [-s session]"));
if (client_init(path, &cctx, 0) != 0) if (client_init(path, &cctx, 0) != 0)
return (1); return (1);
client_write_server(&cctx, MSG_SESSIONS, &data, sizeof data);
if (*name == '\0')
client_write_server(&cctx, MSG_SESSIONS, &sdata, sizeof sdata);
else {
client_fill_sessid(&wdata.sid, name);
client_write_server(&cctx, MSG_WINDOWS, &wdata, sizeof wdata);
}
for (;;) { for (;;) {
pfd.fd = cctx.srv_fd; pfd.fd = cctx.srv_fd;
@ -92,48 +66,120 @@ op_list(char *path, int argc, char **argv)
continue; continue;
buffer_remove(cctx.srv_in, sizeof hdr); buffer_remove(cctx.srv_in, sizeof hdr);
switch (hdr.type) { if (hdr.type != MSG_SESSIONS)
case MSG_SESSIONS: fatalx("unexpected message");
if (hdr.size < sizeof sdata)
if (hdr.size < sizeof data)
fatalx("bad MSG_SESSIONS size"); fatalx("bad MSG_SESSIONS size");
buffer_read(cctx.srv_in, &sdata, sizeof sdata); buffer_read(cctx.srv_in, &data, sizeof data);
hdr.size -= sizeof sdata; hdr.size -= sizeof data;
if (sdata.sessions == 0 && hdr.size == 0) if (data.sessions == 0 && hdr.size == 0)
return (0); return (0);
if (hdr.size < sdata.sessions * sizeof sent) if (hdr.size < data.sessions * sizeof ent)
fatalx("bad MSG_SESSIONS size"); fatalx("bad MSG_SESSIONS size");
while (sdata.sessions-- > 0) {
buffer_read(cctx.srv_in, &sent, sizeof sent); while (data.sessions-- > 0) {
tim = ctime(&sent.tim); buffer_read(cctx.srv_in, &ent, sizeof ent);
tim = ctime(&ent.tim);
*strchr(tim, '\n') = '\0'; *strchr(tim, '\n') = '\0';
printf("%s: %u windows (created %s)\n",
sent.name, sent.windows, tim); printf("%s: %u windows "
"(created %s)\n", ent.name, ent.windows, tim);
} }
return (0); return (0);
case MSG_WINDOWS: }
if (hdr.size < sizeof wdata) }
int
op_list_windows(char *path, int argc, char **argv)
{
struct client_ctx cctx;
char name[MAXNAMELEN];
int opt;
struct windows_data data;
struct windows_entry ent;
struct pollfd pfd;
struct hdr hdr;
*name = '\0';
optind = 1;
while ((opt = getopt(argc, argv, "s:?")) != EOF) {
switch (opt) {
case 's':
if (strlcpy(name, optarg, sizeof name) >= sizeof name) {
log_warnx("%s: session name too long", optarg);
return (1);
}
break;
case '?':
default:
return (usage("list-windows [-s session]"));
}
}
argc -= optind;
argv += optind;
if (argc != 0)
return (usage("list-windows [-s session]"));
if (client_init(path, &cctx, 0) != 0)
return (1);
client_fill_sessid(&data.sid, name);
client_write_server(&cctx, MSG_WINDOWS, &data, sizeof data);
for (;;) {
pfd.fd = cctx.srv_fd;
pfd.events = POLLIN;
if (BUFFER_USED(cctx.srv_out) > 0)
pfd.events |= POLLOUT;
if (poll(&pfd, 1, INFTIM) == -1) {
if (errno == EAGAIN || errno == EINTR)
continue;
log_warn("poll");
return (-1);
}
if (buffer_poll(&pfd, cctx.srv_in, cctx.srv_out) != 0) {
log_warnx("lost server");
return (-1);
}
if (BUFFER_USED(cctx.srv_in) < sizeof hdr)
continue;
memcpy(&hdr, BUFFER_OUT(cctx.srv_in), sizeof hdr);
if (BUFFER_USED(cctx.srv_in) < (sizeof hdr) + hdr.size)
continue;
buffer_remove(cctx.srv_in, sizeof hdr);
if (hdr.type != MSG_WINDOWS)
fatalx("unexpected message");
if (hdr.size < sizeof data)
fatalx("bad MSG_WINDOWS size"); fatalx("bad MSG_WINDOWS size");
buffer_read(cctx.srv_in, &wdata, sizeof wdata); buffer_read(cctx.srv_in, &data, sizeof data);
hdr.size -= sizeof wdata; hdr.size -= sizeof data;
if (wdata.windows == 0 && hdr.size == 0) { if (data.windows == 0 && hdr.size == 0) {
log_warnx("session not found: %s", name); log_warnx("session not found: %s", name);
return (1); return (1);
} }
if (hdr.size < wdata.windows * sizeof went) if (hdr.size < data.windows * sizeof ent)
fatalx("bad MSG_WINDOWS size"); fatalx("bad MSG_WINDOWS size");
while (wdata.windows-- > 0) {
buffer_read(cctx.srv_in, &went, sizeof went); while (data.windows-- > 0) {
if (*went.title != '\0') { buffer_read(cctx.srv_in, &ent, sizeof ent);
printf("%u: %s \"%s\" (%s)\n", went.idx,
went.name, went.title, went.tty); if (*ent.title != '\0') {
printf("%u: %s \"%s\" (%s)\n", ent.idx,
ent.name, ent.title, ent.tty);
} else { } else {
printf("%u: %s (%s)\n", printf("%u: %s (%s)\n",
went.idx, went.name, went.tty); ent.idx, ent.name, ent.tty);
} }
} }
return (0); return (0);
default:
fatalx("unexpected message");
}
} }
} }

View File

@ -1,4 +1,4 @@
/* $Id: server-fn.c,v 1.4 2007-09-27 09:52:03 nicm Exp $ */ /* $Id: server-fn.c,v 1.5 2007-09-27 10:09:37 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -47,7 +47,7 @@ server_find_sessid(struct sessid *sid, char **cause)
xasprintf(cause, "index out of range: %u", sid->idx); xasprintf(cause, "index out of range: %u", sid->idx);
return (NULL); return (NULL);
} }
if ((s = ARRAY_ITEM(&sessions, sid->idx) = NULL)) { if ((s = ARRAY_ITEM(&sessions, sid->idx)) == NULL) {
xasprintf(cause, "session doesn't exist: %u", sid->idx); xasprintf(cause, "session doesn't exist: %u", sid->idx);
return (NULL); return (NULL);
} }

6
tmux.c
View File

@ -1,4 +1,4 @@
/* $Id: tmux.c,v 1.14 2007-09-27 09:15:58 nicm Exp $ */ /* $Id: tmux.c,v 1.15 2007-09-27 10:09:37 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -45,8 +45,10 @@ struct op {
}; };
struct op op_table[] = { struct op op_table[] = {
{ "attach", NULL, op_attach }, { "attach", NULL, op_attach },
{ "list-sessions", "ls", op_list/*_sessions*/ }, { "list-sessions", "ls", op_list_sessions },
{ "list-windows", "lsw", op_list_windows },
{ "new-session", "new", op_new/*_session*/ }, { "new-session", "new", op_new/*_session*/ },
// { "new-window", "neww", op_new_window },
// { "rename-window", "rw", op_rename_window }, // { "rename-window", "rw", op_rename_window },
// { "rename-session", "rs", op_rename_session }, // { "rename-session", "rs", op_rename_session },
}; };

5
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.17 2007-09-27 09:52:03 nicm Exp $ */ /* $Id: tmux.h,v 1.18 2007-09-27 10:09:37 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -450,7 +450,8 @@ int op_new(char *, int, char **);
int op_attach(char *, int, char **); int op_attach(char *, int, char **);
/* op-list.c */ /* op-list.c */
int op_list(char *, int, char **); int op_list_sessions(char *, int, char **);
int op_list_windows(char *, int, char **);
/* client.c */ /* client.c */
int client_init(char *, struct client_ctx *, int); int client_init(char *, struct client_ctx *, int);