Window attachment, malloc debugging, fix a segfault with no sessions.

This commit is contained in:
Nicholas Marriott 2007-10-04 11:52:03 +00:00
parent ff56ed7bd6
commit 68a5d5c00b
11 changed files with 121 additions and 29 deletions

View File

@ -1,5 +1,7 @@
04 October 2007
* (nicm) attach-session (can't believe I forgot it until now!) and list-windows
commands.
* (nicm) rename-window and select-window commands.
* (nicm) set-option command (alias set): "tmux set-option prefix ^A".
* (nicm) Key binding and unbinding is back.
@ -110,5 +112,5 @@
(including mutt, emacs). No status bar yet and no key remapping or other
customisation.
$Id: CHANGES,v 1.30 2007-10-04 10:54:20 nicm Exp $
$Id: CHANGES,v 1.31 2007-10-04 11:52:02 nicm Exp $

View File

@ -1,4 +1,4 @@
# $Id: Makefile,v 1.21 2007-10-04 11:23:17 nicm Exp $
# $Id: Makefile,v 1.22 2007-10-04 11:52:02 nicm Exp $
.SUFFIXES: .c .o .y .h
.PHONY: clean
@ -22,7 +22,7 @@ SRCS= tmux.c server.c server-msg.c server-fn.c buffer.c buffer-poll.c status.c \
cmd-list-sessions.c cmd-new-window.c cmd-next-window.c cmd-bind-key.c \
cmd-unbind-key.c cmd-previous-window.c cmd-last-window.c cmd-list-keys.c \
cmd-set-option.c cmd-rename-window.c cmd-select-window.c \
cmd-list-windows.c
cmd-list-windows.c cmd-attach-session.c
YACC= yacc -d

View File

@ -1,4 +1,4 @@
/* $Id: client-msg.c,v 1.6 2007-10-03 21:31:07 nicm Exp $ */
/* $Id: client-msg.c,v 1.7 2007-10-04 11:52:02 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -27,6 +27,7 @@
int client_msg_fn_data(struct hdr *, struct client_ctx *, char **);
int client_msg_fn_detach(struct hdr *, struct client_ctx *, char **);
int client_msg_fn_error(struct hdr *, struct client_ctx *, char **);
int client_msg_fn_exit(struct hdr *, struct client_ctx *, char **);
int client_msg_fn_okay(struct hdr *, struct client_ctx *, char **);
int client_msg_fn_pause(struct hdr *, struct client_ctx *, char **);
@ -39,6 +40,7 @@ struct client_msg client_msg_table[] = {
{ MSG_DATA, client_msg_fn_data },
{ MSG_DETACH, client_msg_fn_detach },
{ MSG_ERROR, client_msg_fn_error },
{ MSG_EXIT, client_msg_fn_exit },
{ MSG_PAUSE, client_msg_fn_pause },
};
#define NCLIENTMSG (sizeof client_msg_table / sizeof client_msg_table[0])
@ -102,6 +104,18 @@ client_msg_fn_error(struct hdr *hdr, struct client_ctx *cctx, char **error)
return (-1);
}
int
client_msg_fn_exit(
struct hdr *hdr, unused struct client_ctx *cctx, char **error)
{
if (hdr->size != 0)
fatalx("bad MSG_EXIT size");
*error = xstrdup("");
return (-1);
}
int
client_msg_fn_detach(
struct hdr *hdr, unused struct client_ctx *cctx, char **error)

View File

@ -1,4 +1,4 @@
/* $Id: client.c,v 1.11 2007-10-03 21:31:07 nicm Exp $ */
/* $Id: client.c,v 1.12 2007-10-04 11:52:02 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -174,6 +174,10 @@ client_main(struct client_ctx *cctx)
local_done();
if (error != NULL) {
if (*error == '\0') {
printf("[exited]\n", error);
return (0);
}
printf("[error: %s]\n", error);
return (1);
}

59
cmd-attach-session.c Normal file
View File

@ -0,0 +1,59 @@
/* $Id: cmd-attach-session.c,v 1.1 2007-10-04 11:52:03 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <getopt.h>
#include "tmux.h"
/*
* Attach existing session to the current terminal.
*/
void cmd_attach_session_exec(void *, struct cmd_ctx *);
const struct cmd_entry cmd_attach_session_entry = {
CMD_ATTACHSESSION, "attach-session", "attach", 0,
NULL,
NULL,
cmd_attach_session_exec,
NULL,
NULL,
NULL
};
void
cmd_attach_session_exec(unused void *ptr, struct cmd_ctx *ctx)
{
struct client *c = ctx->client;
struct session *s = ctx->session;
if (ctx->flags & CMD_KEY)
return;
if (!(c->flags & CLIENT_TERMINAL)) {
ctx->error(ctx, "not a terminal");
return;
}
c->session = s;
server_write_client(c, MSG_READY, NULL, 0);
server_redraw_client(c);
}

3
cmd.c
View File

@ -1,4 +1,4 @@
/* $Id: cmd.c,v 1.11 2007-10-04 11:23:17 nicm Exp $ */
/* $Id: cmd.c,v 1.12 2007-10-04 11:52:03 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -24,6 +24,7 @@
#include "tmux.h"
const struct cmd_entry *cmd_table[] = {
&cmd_attach_session_entry,
&cmd_bind_key_entry,
&cmd_detach_session_entry,
&cmd_last_window_entry,

View File

@ -1,4 +1,4 @@
/* $Id: server-msg.c,v 1.22 2007-10-04 00:02:10 nicm Exp $ */
/* $Id: server-msg.c,v 1.23 2007-10-04 11:52:03 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -118,6 +118,12 @@ server_msg_fn_command(struct hdr *hdr, struct client *c)
log_debug("got command %u %s from client %d",
cmd->entry->type, cmd->entry->name, c->fd);
ctx.error = server_msg_fn_command_error;
ctx.print = server_msg_fn_command_print;
ctx.client = c;
ctx.flags = 0;
if (cmd->entry->flags & CMD_NOSESSION)
ctx.session = NULL;
else {
@ -129,12 +135,6 @@ server_msg_fn_command(struct hdr *hdr, struct client *c)
}
}
ctx.error = server_msg_fn_command_error;
ctx.print = server_msg_fn_command_print;
ctx.client = c;
ctx.flags = 0;
cmd_exec(cmd, &ctx);
cmd_free(cmd);

View File

@ -1,4 +1,4 @@
/* $Id: server.c,v 1.22 2007-10-04 00:02:10 nicm Exp $ */
/* $Id: server.c,v 1.23 2007-10-04 11:52:03 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -62,7 +62,7 @@ server_start(char *path)
size_t sz;
pid_t pid;
mode_t mask;
int fd, mode;
int n, fd, mode;
switch (pid = fork()) {
case -1:
@ -74,6 +74,8 @@ server_start(char *path)
return (0);
}
xmalloc_clear();
logfile("server");
setproctitle("server (%s)", path);
log_debug("server started, pid %ld", (long) getpid());
@ -103,15 +105,15 @@ server_start(char *path)
if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
fatal("fcntl failed");
/*
* Detach into the background. This means the PID changes which will
* have to be fixed in some way at some point... XXX
*/
if (daemon(1, 1) != 0)
fatal("daemon failed");
log_debug("server daemonised, pid now %ld", (long) getpid());
exit(server_main(path, fd));
n = server_main(path, fd);
#ifdef DEBUG
xmalloc_report(getpid(), "server");
#endif
exit(n);
}
/* Main server loop. */

20
tmux.c
View File

@ -1,4 +1,4 @@
/* $Id: tmux.c,v 1.28 2007-10-04 11:23:17 nicm Exp $ */
/* $Id: tmux.c,v 1.29 2007-10-04 11:52:03 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -167,7 +167,7 @@ main(int argc, char **argv)
struct hdr hdr;
const char *shell;
char *path, *cause, name[MAXNAMELEN];
int opt;
int n, opt;
*name = '\0';
path = NULL;
@ -245,7 +245,8 @@ main(int argc, char **argv)
switch (hdr.type) {
case MSG_EXIT:
exit(0);
n = 0;
goto out;
case MSG_PRINT:
if (hdr.size > INT_MAX - 1)
fatalx("bad MSG_PRINT size");
@ -259,14 +260,21 @@ main(int argc, char **argv)
log_warnx("%.*s",
(int) hdr.size, BUFFER_OUT(cctx.srv_in));
buffer_remove(cctx.srv_in, hdr.size);
exit(1);
n = 1;
goto out;
case MSG_READY:
exit(client_main(&cctx));
n = client_main(&cctx);
goto out;
default:
fatalx("unexpected command");
}
}
/* NOTREACHED */
out:
#ifdef DEBUG
xmalloc_report(getpid(), "client");
#endif
return (n);
usage:
usage(&cause, NULL);

4
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.46 2007-10-04 11:23:17 nicm Exp $ */
/* $Id: tmux.h,v 1.47 2007-10-04 11:52:03 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -457,6 +457,7 @@ struct client_ctx {
/* Key/command line command. */
enum cmd_type {
CMD_ATTACHSESSION,
CMD_BINDKEY,
CMD_DETACHSESSION,
CMD_LASTWINDOW,
@ -533,6 +534,7 @@ struct cmd *cmd_recv(struct buffer *);
void cmd_free(struct cmd *);
void cmd_send_string(struct buffer *, const char *);
char *cmd_recv_string(struct buffer *);
extern const struct cmd_entry cmd_attach_session_entry;
extern const struct cmd_entry cmd_bind_key_entry;
extern const struct cmd_entry cmd_detach_session_entry;
extern const struct cmd_entry cmd_last_window_entry;

View File

@ -1,4 +1,4 @@
/* $Id: xmalloc-debug.c,v 1.1 2007-07-25 23:13:18 nicm Exp $ */
/* $Id: xmalloc-debug.c,v 1.2 2007-10-04 11:52:03 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -51,7 +51,7 @@ u_int xmalloc_mallocs;
u_int xmalloc_reallocs;
/* Print function. */
#define XMALLOC_PRINT log_debug3
#define XMALLOC_PRINT log_debug
/* Bytes of unallocated blocks and number of allocated blocks to show. */
#define XMALLOC_BYTES 8