diff --git a/CHANGES b/CHANGES index 3d001be9..281d0252 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,6 @@ 04 October 2007 +* (nicm) set-option command (alias set): "tmux set-option prefix ^A". * (nicm) Key binding and unbinding is back. 03 October 2007 @@ -108,5 +109,5 @@ (including mutt, emacs). No status bar yet and no key remapping or other customisation. -$Id: CHANGES,v 1.27 2007-10-04 09:30:53 nicm Exp $ +$Id: CHANGES,v 1.28 2007-10-04 10:11:32 nicm Exp $ diff --git a/Makefile b/Makefile index 44dd8c02..c6ecbf56 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile,v 1.17 2007-10-04 09:30:53 nicm Exp $ +# $Id: Makefile,v 1.18 2007-10-04 10:11:32 nicm Exp $ .SUFFIXES: .c .o .y .h .PHONY: clean @@ -21,7 +21,8 @@ SRCS= tmux.c server.c server-msg.c server-fn.c buffer.c buffer-poll.c status.c \ session.c local.c log.c client.c client-msg.c client-fn.c key-string.c \ key-bindings.c cmd.c cmd-new-session.c cmd-detach-session.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-unbind-key.c cmd-previous-window.c cmd-last-window.c cmd-list-keys.c \ + cmd-set-option.c YACC= yacc -d diff --git a/cmd-bind-key.c b/cmd-bind-key.c index 81e048a5..4f771c12 100644 --- a/cmd-bind-key.c +++ b/cmd-bind-key.c @@ -1,4 +1,4 @@ -/* $Id: cmd-bind-key.c,v 1.1 2007-10-04 09:30:53 nicm Exp $ */ +/* $Id: cmd-bind-key.c,v 1.2 2007-10-04 10:11:32 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -84,9 +84,7 @@ usage: usage(cause, "%s", cmd_bind_key_usage()); error: - if (data->cmd != NULL) - cmd_free(data->cmd); - xfree(data); + cmd_bind_key_free(data); return (-1); } diff --git a/cmd-detach-session.c b/cmd-detach-session.c index c9f41352..c1dc7005 100644 --- a/cmd-detach-session.c +++ b/cmd-detach-session.c @@ -1,4 +1,4 @@ -/* $Id: cmd-detach-session.c,v 1.2 2007-10-03 23:32:26 nicm Exp $ */ +/* $Id: cmd-detach-session.c,v 1.3 2007-10-04 10:11:32 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -76,7 +76,7 @@ cmd_detach_session_parse(void **ptr, int argc, char **argv, char **cause) usage: usage(cause, "%s", cmd_detach_session_usage()); - xfree(data); + cmd_detach_session_free(data); return (-1); } diff --git a/cmd-new-session.c b/cmd-new-session.c index 0458f5e9..d1beed3b 100644 --- a/cmd-new-session.c +++ b/cmd-new-session.c @@ -1,4 +1,4 @@ -/* $Id: cmd-new-session.c,v 1.3 2007-10-04 00:02:10 nicm Exp $ */ +/* $Id: cmd-new-session.c,v 1.4 2007-10-04 10:11:32 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -79,10 +79,8 @@ cmd_new_session_parse(void **ptr, int argc, char **argv, char **cause) usage: usage(cause, "%s", cmd_new_session_usage()); - - if (data->name != NULL) - xfree(data->name); - xfree(data); + + cmd_new_session_free(data); return (-1); } diff --git a/cmd-new-window.c b/cmd-new-window.c index 556ca961..bdfb5a71 100644 --- a/cmd-new-window.c +++ b/cmd-new-window.c @@ -1,4 +1,4 @@ -/* $Id: cmd-new-window.c,v 1.2 2007-10-04 00:02:10 nicm Exp $ */ +/* $Id: cmd-new-window.c,v 1.3 2007-10-04 10:11:32 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -86,11 +86,7 @@ cmd_new_window_parse(void **ptr, int argc, char **argv, char **cause) usage: usage(cause, "%s", cmd_new_window_usage()); - if (data->name != NULL) - xfree(data->name); - if (data->cmd != NULL) - xfree(data->cmd); - xfree(data); + cmd_new_window_free(data); return (-1); } diff --git a/cmd-set-option.c b/cmd-set-option.c new file mode 100644 index 00000000..c2d836ee --- /dev/null +++ b/cmd-set-option.c @@ -0,0 +1,165 @@ +/* $Id: cmd-set-option.c,v 1.1 2007-10-04 10:11:32 nicm Exp $ */ + +/* + * Copyright (c) 2007 Nicholas Marriott + * + * 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 + +#include +#include +#include + +#include "tmux.h" + +/* + * Set an option. + */ + +int cmd_set_option_parse(void **, int, char **, char **); +const char *cmd_set_option_usage(void); +void cmd_set_option_exec(void *, struct cmd_ctx *); +void cmd_set_option_send(void *, struct buffer *); +void cmd_set_option_recv(void **, struct buffer *); +void cmd_set_option_free(void *); + +struct cmd_set_option_data { + char *option; + char *value; +}; + +const struct cmd_entry cmd_set_option_entry = { + CMD_SETOPTION, "set-option", "set", CMD_NOSESSION, + cmd_set_option_parse, + cmd_set_option_usage, + cmd_set_option_exec, + cmd_set_option_send, + cmd_set_option_recv, + cmd_set_option_free +}; + +int +cmd_set_option_parse(void **ptr, int argc, char **argv, char **cause) +{ + struct cmd_set_option_data *data; + int opt; + + *ptr = data = xmalloc(sizeof *data); + data->option = NULL; + data->value = NULL; + + while ((opt = getopt(argc, argv, "")) != EOF) { + switch (opt) { + default: + goto usage; + } + } + argc -= optind; + argv += optind; + if (argc != 1 && argc != 2) + goto usage; + + data->option = xstrdup(argv[0]); + if (argc == 2) + data->value = xstrdup(argv[1]); + + return (0); + +usage: + usage(cause, "%s", cmd_set_option_usage()); + + cmd_set_option_free(data); + return (-1); +} + +const char * +cmd_set_option_usage(void) +{ + return ("set-option option value"); +} + +void +cmd_set_option_exec(void *ptr, unused struct cmd_ctx *ctx) +{ + struct cmd_set_option_data *data = ptr; + struct client *c = ctx->client; + const char *errstr; + u_int number; + int bool, key; + + if (data == NULL) + return; + + if (*data->option == '\0') { + ctx->error(ctx, "invalid option"); + return; + } + + number = strtonum(data->value, 0, UINT_MAX, &errstr); + + bool = -1; + if (number == 1 || + strcmp(data->value, "on") == 0 || strcmp(data->value, "yes") == 0) + bool = 1; + if (number == 0 || + strcmp(data->value, "off") == 0 || strcmp(data->value, "no") == 0) + bool = 0; + + if (strcmp(data->option, "prefix") == 0) { + key = key_string_lookup_string(data->value); + if (key == KEYC_NONE) { + ctx->error(ctx, "unknown key: %s", data->value); + return; + } + prefix_key = key; + } else { + ctx->error(ctx, "unknown option: %s", data->option); + return; + } + + if (!(ctx->flags & CMD_KEY)) + server_write_client(c, MSG_EXIT, NULL, 0); +} + +void +cmd_set_option_send(void *ptr, struct buffer *b) +{ + struct cmd_set_option_data *data = ptr; + + cmd_send_string(b, data->option); + cmd_send_string(b, data->value); +} + +void +cmd_set_option_recv(void **ptr, struct buffer *b) +{ + struct cmd_set_option_data *data; + + *ptr = data = xmalloc(sizeof *data); + data->option = cmd_recv_string(b); + data->value = cmd_recv_string(b); +} + +void +cmd_set_option_free(void *ptr) +{ + struct cmd_set_option_data *data = ptr; + + if (data->option != NULL) + xfree(data->option); + if (data->value != NULL) + xfree(data->value); + xfree(data); +} diff --git a/cmd-unbind-key.c b/cmd-unbind-key.c index cca0764b..ee351799 100644 --- a/cmd-unbind-key.c +++ b/cmd-unbind-key.c @@ -1,4 +1,4 @@ -/* $Id: cmd-unbind-key.c,v 1.1 2007-10-04 09:30:53 nicm Exp $ */ +/* $Id: cmd-unbind-key.c,v 1.2 2007-10-04 10:11:32 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -63,7 +63,7 @@ cmd_unbind_key_parse(void **ptr, int argc, char **argv, char **cause) } argc -= optind; argv += optind; - if (argc < 1) + if (argc != 1) goto usage; if ((data->key = key_string_lookup_string(argv[0])) == KEYC_NONE) { diff --git a/cmd.c b/cmd.c index 70c9d748..086bb562 100644 --- a/cmd.c +++ b/cmd.c @@ -1,4 +1,4 @@ -/* $Id: cmd.c,v 1.7 2007-10-04 09:30:53 nicm Exp $ */ +/* $Id: cmd.c,v 1.8 2007-10-04 10:11:32 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -33,6 +33,7 @@ const struct cmd_entry *cmd_table[] = { &cmd_new_window_entry, &cmd_next_window_entry, &cmd_previous_window_entry, + &cmd_set_option_entry, &cmd_unbind_key_entry, NULL }; diff --git a/tmux.h b/tmux.h index 1c57091f..2dfb0977 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.42 2007-10-04 09:30:53 nicm Exp $ */ +/* $Id: tmux.h,v 1.43 2007-10-04 10:11:32 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -466,6 +466,7 @@ enum cmd_type { CMD_NEWWINDOW, CMD_NEXTWINDOW, CMD_PREVIOUSWINDOW, + CMD_SETOPTION, CMD_UNBINDKEY, }; @@ -538,6 +539,7 @@ extern const struct cmd_entry cmd_new_session_entry; extern const struct cmd_entry cmd_new_window_entry; extern const struct cmd_entry cmd_next_window_entry; extern const struct cmd_entry cmd_previous_window_entry; +extern const struct cmd_entry cmd_set_option_entry; extern const struct cmd_entry cmd_unbind_key_entry; /* client.c */