Server function naming, still not happy with server stuff though.

pull/1/head
Nicholas Marriott 2007-09-26 18:09:23 +00:00
parent 65eeb7e421
commit 302a35da85
6 changed files with 76 additions and 78 deletions

View File

@ -1,4 +1,4 @@
/* $Id: client-cmd.c,v 1.1 2007-09-26 13:43:14 nicm Exp $ */ /* $Id: client-cmd.c,v 1.2 2007-09-26 18:09:23 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -62,7 +62,6 @@ struct cmd client_cmd_table[] = {
}; };
#define NCLIENTCMD (sizeof client_cmd_table / sizeof client_cmd_table[0]) #define NCLIENTCMD (sizeof client_cmd_table / sizeof client_cmd_table[0])
/* Dispatch to a command. */
int int
client_cmd_dispatch(int key, struct client_ctx *cctx, const char **error) client_cmd_dispatch(int key, struct client_ctx *cctx, const char **error)
{ {

View File

@ -1,4 +1,4 @@
/* $Id: client-msg.c,v 1.1 2007-09-26 13:43:14 nicm Exp $ */ /* $Id: client-msg.c,v 1.2 2007-09-26 18:09:23 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -69,7 +69,7 @@ client_msg_dispatch(struct client_ctx *cctx, const char **error)
} }
} }
/* Output message from client. */ /* Output message from server. */
int int
client_msg_fn_output( client_msg_fn_output(
struct hdr *hdr, struct client_ctx *cctx, unused const char **error) struct hdr *hdr, struct client_ctx *cctx, unused const char **error)

View File

@ -1,4 +1,4 @@
/* $Id: server-fn.c,v 1.1 2007-09-26 10:35:24 nicm Exp $ */ /* $Id: server-fn.c,v 1.2 2007-09-26 18:09:23 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -24,7 +24,7 @@
/* Write command to a client. */ /* Write command to a client. */
void void
write_client(struct client *c, u_int cmd, void *buf, size_t len) server_write_client(struct client *c, u_int cmd, void *buf, size_t len)
{ {
struct hdr hdr; struct hdr hdr;
@ -38,7 +38,7 @@ write_client(struct client *c, u_int cmd, void *buf, size_t len)
/* Write command to a client with two buffers. */ /* Write command to a client with two buffers. */
void void
write_client2(struct client *c, server_write_client2(struct client *c,
u_int cmd, void *buf1, size_t len1, void *buf2, size_t len2) u_int cmd, void *buf1, size_t len1, void *buf2, size_t len2)
{ {
struct hdr hdr; struct hdr hdr;
@ -55,7 +55,7 @@ write_client2(struct client *c,
/* Write command to all clients attached to a specific window. */ /* Write command to all clients attached to a specific window. */
void void
write_clients(struct window *w, u_int cmd, void *buf, size_t len) server_write_clients(struct window *w, u_int cmd, void *buf, size_t len)
{ {
struct client *c; struct client *c;
struct hdr hdr; struct hdr hdr;
@ -78,19 +78,19 @@ write_clients(struct window *w, u_int cmd, void *buf, size_t len)
/* Changed client window. */ /* Changed client window. */
void void
changed_window(struct client *c) server_window_changed(struct client *c)
{ {
struct window *w; struct window *w;
w = c->session->window; w = c->session->window;
if (c->sx != w->screen.sx || c->sy != w->screen.sy) if (c->sx != w->screen.sx || c->sy != w->screen.sy)
window_resize(w, c->sx, c->sy); window_resize(w, c->sx, c->sy);
draw_client(c, 0, c->sy - 1); server_draw_client(c, 0, c->sy - 1);
} }
/* Draw window on client. */ /* Draw window on client. */
void void
draw_client(struct client *c, u_int py_upper, u_int py_lower) server_draw_client(struct client *c, u_int py_upper, u_int py_lower)
{ {
struct hdr hdr; struct hdr hdr;
size_t size; size_t size;
@ -115,7 +115,7 @@ draw_client(struct client *c, u_int py_upper, u_int py_lower)
/* Write message command to a client. */ /* Write message command to a client. */
void void
write_message(struct client *c, const char *fmt, ...) server_write_message(struct client *c, const char *fmt, ...)
{ {
struct hdr hdr; struct hdr hdr;
va_list ap; va_list ap;

View File

@ -1,4 +1,4 @@
/* $Id: server-msg.c,v 1.2 2007-09-26 13:43:15 nicm Exp $ */ /* $Id: server-msg.c,v 1.3 2007-09-26 18:09:23 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -114,7 +114,7 @@ server_msg_fn_new(struct hdr *hdr, struct client *c)
if (*data.name != '\0' && session_find(data.name) != NULL) { if (*data.name != '\0' && session_find(data.name) != NULL) {
xasprintf(&msg, "duplicate session: %s", data.name); xasprintf(&msg, "duplicate session: %s", data.name);
write_client(c, MSG_ERROR, msg, strlen(msg)); server_write_client(c, MSG_ERROR, msg, strlen(msg));
xfree(msg); xfree(msg);
return (0); return (0);
} }
@ -128,7 +128,7 @@ server_msg_fn_new(struct hdr *hdr, struct client *c)
fatalx("session_create failed"); fatalx("session_create failed");
xfree(cmd); xfree(cmd);
draw_client(c, 0, c->sy - 1); server_draw_client(c, 0, c->sy - 1);
return (0); return (0);
} }
@ -157,12 +157,12 @@ server_msg_fn_attach(struct hdr *hdr, struct client *c)
c->session = session_find(data.name); c->session = session_find(data.name);
if (c->session == NULL) { if (c->session == NULL) {
xasprintf(&msg, "session not found: %s", data.name); xasprintf(&msg, "session not found: %s", data.name);
write_client(c, MSG_ERROR, msg, strlen(msg)); server_write_client(c, MSG_ERROR, msg, strlen(msg));
xfree(msg); xfree(msg);
return (0); return (0);
} }
draw_client(c, 0, c->sy - 1); server_draw_client(c, 0, c->sy - 1);
return (0); return (0);
} }
@ -187,7 +187,7 @@ server_msg_fn_create(struct hdr *hdr, struct client *c)
fatalx("session_new failed"); fatalx("session_new failed");
xfree(cmd); xfree(cmd);
draw_client(c, 0, c->sy - 1); server_draw_client(c, 0, c->sy - 1);
return (0); return (0);
} }
@ -202,9 +202,9 @@ server_msg_fn_next(struct hdr *hdr, struct client *c)
fatalx("bad MSG_NEXT size"); fatalx("bad MSG_NEXT size");
if (session_next(c->session) == 0) if (session_next(c->session) == 0)
changed_window(c); server_window_changed(c);
else else
write_message(c, "No next window"); server_write_message(c, "No next window");
return (0); return (0);
} }
@ -219,9 +219,9 @@ server_msg_fn_previous(struct hdr *hdr, struct client *c)
fatalx("bad MSG_PREVIOUS size"); fatalx("bad MSG_PREVIOUS size");
if (session_previous(c->session) == 0) if (session_previous(c->session) == 0)
changed_window(c); server_window_changed(c);
else else
write_message(c, "No previous window"); server_write_message(c, "No previous window");
return (0); return (0);
} }
@ -246,7 +246,7 @@ server_msg_fn_size(struct hdr *hdr, struct client *c)
c->sy = 25; c->sy = 25;
if (window_resize(c->session->window, c->sx, c->sy) != 0) if (window_resize(c->session->window, c->sx, c->sy) != 0)
draw_client(c, 0, c->sy - 1); server_draw_client(c, 0, c->sy - 1);
return (0); return (0);
} }
@ -274,7 +274,7 @@ server_msg_fn_refresh(struct hdr *hdr, struct client *c)
if (hdr->size != 0 && hdr->size != sizeof data) if (hdr->size != 0 && hdr->size != sizeof data)
fatalx("bad MSG_REFRESH size"); fatalx("bad MSG_REFRESH size");
draw_client(c, 0, c->sy - 1); server_draw_client(c, 0, c->sy - 1);
return (0); return (0);
} }
@ -294,9 +294,9 @@ server_msg_fn_select(struct hdr *hdr, struct client *c)
if (c->session == NULL) if (c->session == NULL)
return (0); return (0);
if (session_select(c->session, data.idx) == 0) if (session_select(c->session, data.idx) == 0)
changed_window(c); server_window_changed(c);
else else
write_message(c, "Window %u not present", data.idx); server_write_message(c, "Window %u not present", data.idx);
return (0); return (0);
} }
@ -319,7 +319,7 @@ server_msg_fn_sessions(struct hdr *hdr, struct client *c)
if (ARRAY_ITEM(&sessions, i) != NULL) if (ARRAY_ITEM(&sessions, i) != NULL)
data.sessions++; data.sessions++;
} }
write_client2(c, MSG_SESSIONS, server_write_client2(c, MSG_SESSIONS,
&data, sizeof data, NULL, data.sessions * sizeof entry); &data, sizeof data, NULL, data.sessions * sizeof entry);
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
@ -356,7 +356,7 @@ server_msg_fn_windows(struct hdr *hdr, struct client *c)
s = session_find(data.name); s = session_find(data.name);
if (s == NULL) { if (s == NULL) {
data.windows = 0; data.windows = 0;
write_client(c, MSG_WINDOWS, &data, sizeof data); server_write_client(c, MSG_WINDOWS, &data, sizeof data);
return (0); return (0);
} }
@ -365,7 +365,7 @@ server_msg_fn_windows(struct hdr *hdr, struct client *c)
if (ARRAY_ITEM(&windows, i) != NULL) if (ARRAY_ITEM(&windows, i) != NULL)
data.windows++; data.windows++;
} }
write_client2(c, MSG_WINDOWS, server_write_client2(c, MSG_WINDOWS,
&data, sizeof data, NULL, data.windows * sizeof entry); &data, sizeof data, NULL, data.windows * sizeof entry);
for (i = 0; i < ARRAY_LENGTH(&windows); i++) { for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
@ -405,9 +405,9 @@ server_msg_fn_last(struct hdr *hdr, struct client *c)
fatalx("bad MSG_LAST size"); fatalx("bad MSG_LAST size");
if (session_last(c->session) == 0) if (session_last(c->session) == 0)
changed_window(c); server_window_changed(c);
else else
write_message(c, "No last window"); server_write_message(c, "No last window");
return (0); return (0);
} }
@ -441,7 +441,7 @@ server_msg_fn_windowlist(struct hdr *hdr, struct client *c)
break; break;
} }
write_message(c, "%s", buf); server_write_message(c, "%s", buf);
xfree(buf); xfree(buf);
return (0); return (0);

View File

@ -1,4 +1,4 @@
/* $Id: server.c,v 1.13 2007-09-26 13:43:15 nicm Exp $ */ /* $Id: server.c,v 1.14 2007-09-26 18:09:23 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -45,22 +45,23 @@
struct clients clients; struct clients clients;
int server_main(char *, int); int server_main(char *, int);
void fill_windows(struct pollfd **); void server_fill_windows(struct pollfd **);
void handle_windows(struct pollfd **); void server_handle_windows(struct pollfd **);
void fill_clients(struct pollfd **); void server_fill_clients(struct pollfd **);
void handle_clients(struct pollfd **); void server_handle_clients(struct pollfd **);
struct client *accept_client(int); struct client *server_accept_client(int);
void lost_client(struct client *); void server_lost_client(struct client *);
void lost_window(struct window *); void server_lost_window(struct window *);
/* Fork new server. */
int int
server_start(char *path) server_start(char *path)
{ {
struct sockaddr_un sa; struct sockaddr_un sa;
size_t sz; size_t sz;
pid_t pid; pid_t pid;
mode_t mode; mode_t mask;
int fd; int fd, mode;
switch (pid = fork()) { switch (pid = fork()) {
case -1: case -1:
@ -74,10 +75,8 @@ server_start(char *path)
logfile("server"); logfile("server");
setproctitle("server (%s)", path); setproctitle("server (%s)", path);
log_debug("server started, pid %ld", (long) getpid()); log_debug("server started, pid %ld", (long) getpid());
/* Create the socket. */
memset(&sa, 0, sizeof sa); memset(&sa, 0, sizeof sa);
sa.sun_family = AF_UNIX; sa.sun_family = AF_UNIX;
sz = strlcpy(sa.sun_path, path, sizeof sa.sun_path); sz = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
@ -90,14 +89,19 @@ server_start(char *path)
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
fatal("socket failed"); fatal("socket failed");
mode = umask(S_IXUSR|S_IRWXG|S_IRWXO); mask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
fatal("bind failed"); fatal("bind failed");
umask(mode); umask(mask);
if (listen(fd, 16) == -1) if (listen(fd, 16) == -1)
fatal("listen failed"); fatal("listen failed");
if ((mode = fcntl(fd, F_GETFL)) == -1)
fatal("fcntl failed");
if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed");
/* /*
* Detach into the background. This means the PID changes which will * Detach into the background. This means the PID changes which will
* have to be fixed in some way at some point... XXX * have to be fixed in some way at some point... XXX
@ -113,8 +117,8 @@ server_start(char *path)
int int
server_main(char *srv_path, int srv_fd) server_main(char *srv_path, int srv_fd)
{ {
struct pollfd *pfds, *pfd; struct pollfd *pfds, *pfd;
int nfds, mode; int nfds;
siginit(); siginit();
@ -122,11 +126,6 @@ server_main(char *srv_path, int srv_fd)
ARRAY_INIT(&clients); ARRAY_INIT(&clients);
ARRAY_INIT(&sessions); ARRAY_INIT(&sessions);
if ((mode = fcntl(srv_fd, F_GETFL)) == -1)
fatal("fcntl failed");
if (fcntl(srv_fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed");
pfds = NULL; pfds = NULL;
while (!sigterm) { while (!sigterm) {
/* Initialise pollfd array. */ /* Initialise pollfd array. */
@ -140,8 +139,8 @@ server_main(char *srv_path, int srv_fd)
pfd++; pfd++;
/* Fill window and client sockets. */ /* Fill window and client sockets. */
fill_windows(&pfd); server_fill_windows(&pfd);
fill_clients(&pfd); server_fill_clients(&pfd);
/* Do the poll. */ /* Do the poll. */
if (poll(pfds, nfds, INFTIM) == -1) { if (poll(pfds, nfds, INFTIM) == -1) {
@ -155,7 +154,7 @@ server_main(char *srv_path, int srv_fd)
if (pfd->revents & (POLLERR|POLLNVAL|POLLHUP)) if (pfd->revents & (POLLERR|POLLNVAL|POLLHUP))
fatalx("lost server socket"); fatalx("lost server socket");
if (pfd->revents & POLLIN) { if (pfd->revents & POLLIN) {
accept_client(srv_fd); server_accept_client(srv_fd);
continue; continue;
} }
pfd++; pfd++;
@ -165,8 +164,8 @@ server_main(char *srv_path, int srv_fd)
* windows, so windows must come first to avoid messing up by * windows, so windows must come first to avoid messing up by
* increasing the array size. * increasing the array size.
*/ */
handle_windows(&pfd); server_handle_windows(&pfd);
handle_clients(&pfd); server_handle_clients(&pfd);
} }
close(srv_fd); close(srv_fd);
@ -177,7 +176,7 @@ server_main(char *srv_path, int srv_fd)
/* Fill window pollfds. */ /* Fill window pollfds. */
void void
fill_windows(struct pollfd **pfd) server_fill_windows(struct pollfd **pfd)
{ {
struct window *w; struct window *w;
u_int i; u_int i;
@ -197,7 +196,7 @@ fill_windows(struct pollfd **pfd)
/* Handle window pollfds. */ /* Handle window pollfds. */
void void
handle_windows(struct pollfd **pfd) server_handle_windows(struct pollfd **pfd)
{ {
struct window *w; struct window *w;
u_int i; u_int i;
@ -206,12 +205,12 @@ handle_windows(struct pollfd **pfd)
for (i = 0; i < ARRAY_LENGTH(&windows); i++) { for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
if ((w = ARRAY_ITEM(&windows, i)) != NULL) { if ((w = ARRAY_ITEM(&windows, i)) != NULL) {
if (window_poll(w, *pfd) != 0) if (window_poll(w, *pfd) != 0)
lost_window(w); server_lost_window(w);
else { else {
b = buffer_create(BUFSIZ); b = buffer_create(BUFSIZ);
window_output(w, b); window_output(w, b);
if (BUFFER_USED(b) != 0) { if (BUFFER_USED(b) != 0) {
write_clients(w, MSG_OUTPUT, server_write_clients(w, MSG_OUTPUT,
BUFFER_OUT(b), BUFFER_USED(b)); BUFFER_OUT(b), BUFFER_USED(b));
} }
buffer_destroy(b); buffer_destroy(b);
@ -223,7 +222,7 @@ handle_windows(struct pollfd **pfd)
/* Fill client pollfds. */ /* Fill client pollfds. */
void void
fill_clients(struct pollfd **pfd) server_fill_clients(struct pollfd **pfd)
{ {
struct client *c; struct client *c;
u_int i; u_int i;
@ -243,7 +242,7 @@ fill_clients(struct pollfd **pfd)
/* Handle client pollfds. */ /* Handle client pollfds. */
void void
handle_clients(struct pollfd *(*pfd)) server_handle_clients(struct pollfd *(*pfd))
{ {
struct client *c; struct client *c;
u_int i; u_int i;
@ -251,7 +250,7 @@ handle_clients(struct pollfd *(*pfd))
for (i = 0; i < ARRAY_LENGTH(&clients); i++) { for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
if ((c = ARRAY_ITEM(&clients, i)) != NULL) { if ((c = ARRAY_ITEM(&clients, i)) != NULL) {
if (buffer_poll((*pfd), c->in, c->out) != 0) if (buffer_poll((*pfd), c->in, c->out) != 0)
lost_client(c); server_lost_client(c);
else else
server_msg_dispatch(c); server_msg_dispatch(c);
} }
@ -261,7 +260,7 @@ handle_clients(struct pollfd *(*pfd))
/* accept(2) and create new client. */ /* accept(2) and create new client. */
struct client * struct client *
accept_client(int srv_fd) server_accept_client(int srv_fd)
{ {
struct client *c; struct client *c;
struct sockaddr_storage sa; struct sockaddr_storage sa;
@ -298,7 +297,7 @@ accept_client(int srv_fd)
/* Lost a client. */ /* Lost a client. */
void void
lost_client(struct client *c) server_lost_client(struct client *c)
{ {
u_int i; u_int i;
@ -315,7 +314,7 @@ lost_client(struct client *c)
/* Lost window: move clients on to next window. */ /* Lost window: move clients on to next window. */
void void
lost_window(struct window *w) server_lost_window(struct window *w)
{ {
struct client *c; struct client *c;
struct session *s; struct session *s;
@ -337,9 +336,9 @@ lost_window(struct window *w)
continue; continue;
if (destroyed) { if (destroyed) {
c->session = NULL; c->session = NULL;
write_client(c, MSG_EXIT, NULL, 0); server_write_client(c, MSG_EXIT, NULL, 0);
} else } else
changed_window(c); server_window_changed(c);
} }
} }
} }

16
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.12 2007-09-26 13:43:15 nicm Exp $ */ /* $Id: tmux.h,v 1.13 2007-09-26 18:09:23 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -462,15 +462,15 @@ int server_start(char *);
int server_msg_dispatch(struct client *); int server_msg_dispatch(struct client *);
/* server-fn.c */ /* server-fn.c */
void write_message(struct client *, const char *, ...); void server_write_message(struct client *, const char *, ...);
void write_client(struct client *, u_int, void *, size_t); void server_write_client(struct client *, u_int, void *, size_t);
void write_client2( void server_write_client2(
struct client *, u_int, void *, size_t, void *, size_t); struct client *, u_int, void *, size_t, void *, size_t);
void write_clients(struct window *, u_int, void *, size_t); void server_write_clients(struct window *, u_int, void *, size_t);
void changed_window(struct client *); void server_window_changed(struct client *);
void draw_client(struct client *, u_int, u_int); void server_draw_client(struct client *, u_int, u_int);
/* ansi.c */ /* input.c */
void input_key(struct buffer *, int); void input_key(struct buffer *, int);
size_t input_parse(u_char *, size_t, struct buffer *, struct screen *); size_t input_parse(u_char *, size_t, struct buffer *, struct screen *);
uint8_t input_extract8(struct buffer *); uint8_t input_extract8(struct buffer *);