diff options
author | Nicholas Marriott <nicholas.marriott@gmail.com> | 2008-12-08 16:19:51 +0000 |
---|---|---|
committer | Nicholas Marriott <nicholas.marriott@gmail.com> | 2008-12-08 16:19:51 +0000 |
commit | 7a82e86827e3d863a6dc4f1d50985f287d1ff86b (patch) | |
tree | 34bcc703d5d79065c5ef364be2a6ebfbc13cf6d0 /cmd-generic.c | |
parent | f008d303e75c185eebcbb493b4e6d49bb400f694 (diff) |
Make window options work the same was as session options, add mode-fg/mode-bg options, force -g for global on set/show/setw/showw/
Diffstat (limited to 'cmd-generic.c')
-rw-r--r-- | cmd-generic.c | 161 |
1 files changed, 157 insertions, 4 deletions
diff --git a/cmd-generic.c b/cmd-generic.c index f1322d1c..c3632055 100644 --- a/cmd-generic.c +++ b/cmd-generic.c @@ -1,4 +1,4 @@ -/* $Id: cmd-generic.c,v 1.13 2008-09-25 23:28:12 nicm Exp $ */ +/* $Id: cmd-generic.c,v 1.14 2008-12-08 16:19:51 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net> @@ -44,7 +44,7 @@ cmd_target_parse(struct cmd *self, int argc, char **argv, char **cause) cmd_target_init(self, 0); data = self->data; - while ((opt = getopt(argc, argv, GETOPT_PREFIX "dkt:")) != EOF) { + while ((opt = getopt(argc, argv, GETOPT_PREFIX "dgkt:")) != EOF) { switch (opt) { case 'd': if (self->entry->flags & CMD_DFLAG) { @@ -52,6 +52,12 @@ cmd_target_parse(struct cmd *self, int argc, char **argv, char **cause) break; } goto usage; + case 'g': + if (self->entry->flags & CMD_GFLAG) { + data->flags |= CMD_GFLAG; + break; + } + goto usage; case 'k': if (self->entry->flags & CMD_KFLAG) { data->flags |= CMD_KFLAG; @@ -137,6 +143,8 @@ cmd_target_print(struct cmd *self, char *buf, size_t len) return; if (off < len && data->flags & CMD_DFLAG) off += xsnprintf(buf + off, len - off, " -d"); + if (off < len && data->flags & CMD_GFLAG) + off += xsnprintf(buf + off, len - off, " -g"); if (off < len && data->flags & CMD_KFLAG) off += xsnprintf(buf + off, len - off, " -k"); if (off < len && data->target != NULL) @@ -166,7 +174,7 @@ cmd_srcdst_parse(struct cmd *self, int argc, char **argv, char **cause) cmd_srcdst_init(self, 0); data = self->data; - while ((opt = getopt(argc, argv, GETOPT_PREFIX "dks:t:")) != EOF) { + while ((opt = getopt(argc, argv, GETOPT_PREFIX "dgks:t:")) != EOF) { switch (opt) { case 'd': if (self->entry->flags & CMD_DFLAG) { @@ -174,6 +182,12 @@ cmd_srcdst_parse(struct cmd *self, int argc, char **argv, char **cause) break; } goto usage; + case 'g': + if (self->entry->flags & CMD_GFLAG) { + data->flags |= CMD_GFLAG; + break; + } + goto usage; case 'k': if (self->entry->flags & CMD_KFLAG) { data->flags |= CMD_KFLAG; @@ -267,6 +281,8 @@ cmd_srcdst_print(struct cmd *self, char *buf, size_t len) return; if (off < len && data->flags & CMD_DFLAG) off += xsnprintf(buf + off, len - off, " -d"); + if (off < len && data->flags & CMD_GFLAG) + off += xsnprintf(buf + off, len - off, " -g"); if (off < len && data->flags & CMD_KFLAG) off += xsnprintf(buf + off, len - off, " -k"); if (off < len && data->src != NULL) @@ -299,7 +315,7 @@ cmd_buffer_parse(struct cmd *self, int argc, char **argv, char **cause) cmd_buffer_init(self, 0); data = self->data; - while ((opt = getopt(argc, argv, GETOPT_PREFIX "b:dkt:")) != EOF) { + while ((opt = getopt(argc, argv, GETOPT_PREFIX "b:dgkt:")) != EOF) { switch (opt) { case 'b': if (data->buffer == -1) { @@ -318,6 +334,12 @@ cmd_buffer_parse(struct cmd *self, int argc, char **argv, char **cause) break; } goto usage; + case 'g': + if (self->entry->flags & CMD_GFLAG) { + data->flags |= CMD_GFLAG; + break; + } + goto usage; case 'k': if (self->entry->flags & CMD_KFLAG) { data->flags |= CMD_KFLAG; @@ -404,6 +426,8 @@ cmd_buffer_print(struct cmd *self, char *buf, size_t len) return; if (off < len && data->flags & CMD_DFLAG) off += xsnprintf(buf + off, len - off, " -d"); + if (off < len && data->flags & CMD_GFLAG) + off += xsnprintf(buf + off, len - off, " -g"); if (off < len && data->flags & CMD_KFLAG) off += xsnprintf(buf + off, len - off, " -k"); if (off < len && data->buffer != -1) @@ -413,3 +437,132 @@ cmd_buffer_print(struct cmd *self, char *buf, size_t len) if (off < len && data->arg != NULL) off += xsnprintf(buf + off, len - off, " %s", data->arg); } + +void +cmd_option_init(struct cmd *self, unused int key) +{ + struct cmd_option_data *data; + + self->data = data = xmalloc(sizeof *data); + data->flags = 0; + data->target = NULL; + data->option = NULL; + data->value = NULL; +} + +int +cmd_option_parse(struct cmd *self, int argc, char **argv, char **cause) +{ + struct cmd_option_data *data; + int opt; + + /* Don't use the entry version since it may be dependent on key. */ + cmd_option_init(self, 0); + data = self->data; + + while ((opt = getopt(argc, argv, GETOPT_PREFIX "dgkt:")) != EOF) { + switch (opt) { + case 'd': + if (self->entry->flags & CMD_DFLAG) { + data->flags |= CMD_DFLAG; + break; + } + goto usage; + case 'g': + if (self->entry->flags & CMD_GFLAG) { + data->flags |= CMD_GFLAG; + break; + } + goto usage; + case 'k': + if (self->entry->flags & CMD_KFLAG) { + data->flags |= CMD_KFLAG; + break; + } + goto usage; + case 't': + if (data->target == NULL) + data->target = xstrdup(optarg); + break; + default: + goto usage; + } + } + argc -= optind; + argv += optind; + + if (argc == 2) { + data->option = xstrdup(argv[0]); + data->value = xstrdup(argv[1]); + } else if (argc == 1) + data->option = xstrdup(argv[0]); + else + goto usage; + return (0); + +usage: + xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage); + + self->entry->free(self); + return (-1); +} + +void +cmd_option_send(struct cmd *self, struct buffer *b) +{ + struct cmd_option_data *data = self->data; + + buffer_write(b, data, sizeof *data); + cmd_send_string(b, data->target); + cmd_send_string(b, data->option); + cmd_send_string(b, data->value); +} + +void +cmd_option_recv(struct cmd *self, struct buffer *b) +{ + struct cmd_option_data *data; + + self->data = data = xmalloc(sizeof *data); + buffer_read(b, data, sizeof *data); + data->target = cmd_recv_string(b); + data->option = cmd_recv_string(b); + data->value = cmd_recv_string(b); +} + +void +cmd_option_free(struct cmd *self) +{ + struct cmd_option_data *data = self->data; + + if (data->target != NULL) + xfree(data->target); + if (data->option != NULL) + xfree(data->option); + if (data->value != NULL) + xfree(data->value); + xfree(data); +} + +void +cmd_option_print(struct cmd *self, char *buf, size_t len) +{ + struct cmd_option_data *data = self->data; + size_t off = 0; + + off += xsnprintf(buf, len, "%s", self->entry->name); + if (data == NULL) + return; + if (off < len && data->flags & CMD_DFLAG) + off += xsnprintf(buf + off, len - off, " -d"); + if (off < len && data->flags & CMD_GFLAG) + off += xsnprintf(buf + off, len - off, " -g"); + if (off < len && data->flags & CMD_KFLAG) + off += xsnprintf(buf + off, len - off, " -k"); + if (off < len && data->target != NULL) + off += xsnprintf(buf + off, len - off, " -t %s", data->target); + if (off < len && data->option != NULL) + off += xsnprintf(buf + off, len - off, " %s", data->option); + if (off < len && data->value != NULL) + off += xsnprintf(buf + off, len - off, " %s", data->value); +} |