Only send end guard if begin was sent, from George Nachman.
[tmux-openbsd.git] / cmd-show-options.c
blob57e49a33699e4ca39fc569c65cdb6f46889f1be2
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
27 * Show options.
30 enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmd_q *);
32 enum cmd_retval cmd_show_options_one(struct cmd *, struct cmd_q *,
33 struct options *, int);
34 enum cmd_retval cmd_show_options_all(struct cmd *, struct cmd_q *,
35 const struct options_table_entry *, struct options *);
37 const struct cmd_entry cmd_show_options_entry = {
38 "show-options", "show",
39 "gqst:vw", 0, 1,
40 "[-gqsvw] [-t target-session|target-window] [option]",
42 NULL,
43 NULL,
44 cmd_show_options_exec
47 const struct cmd_entry cmd_show_window_options_entry = {
48 "show-window-options", "showw",
49 "gvt:", 0, 1,
50 "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
52 NULL,
53 NULL,
54 cmd_show_options_exec
57 enum cmd_retval
58 cmd_show_options_exec(struct cmd *self, struct cmd_q *cmdq)
60 struct args *args = self->args;
61 struct session *s;
62 struct winlink *wl;
63 const struct options_table_entry *table;
64 struct options *oo;
65 int quiet;
67 if (args_has(self->args, 's')) {
68 oo = &global_options;
69 table = server_options_table;
70 } else if (args_has(self->args, 'w') ||
71 self->entry == &cmd_show_window_options_entry) {
72 table = window_options_table;
73 if (args_has(self->args, 'g'))
74 oo = &global_w_options;
75 else {
76 wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
77 if (wl == NULL)
78 return (CMD_RETURN_ERROR);
79 oo = &wl->window->options;
81 } else {
82 table = session_options_table;
83 if (args_has(self->args, 'g'))
84 oo = &global_s_options;
85 else {
86 s = cmd_find_session(cmdq, args_get(args, 't'), 0);
87 if (s == NULL)
88 return (CMD_RETURN_ERROR);
89 oo = &s->options;
93 quiet = args_has(self->args, 'q');
94 if (args->argc == 0)
95 return (cmd_show_options_all(self, cmdq, table, oo));
96 else
97 return (cmd_show_options_one(self, cmdq, oo, quiet));
100 enum cmd_retval
101 cmd_show_options_one(struct cmd *self, struct cmd_q *cmdq,
102 struct options *oo, int quiet)
104 struct args *args = self->args;
105 const struct options_table_entry *table, *oe;
106 struct options_entry *o;
107 const char *optval;
109 if (*args->argv[0] == '@') {
110 if ((o = options_find1(oo, args->argv[0])) == NULL) {
111 if (quiet)
112 return (CMD_RETURN_NORMAL);
113 cmdq_error(cmdq, "unknown option: %s", args->argv[0]);
114 return (CMD_RETURN_ERROR);
116 if (args_has(self->args, 'v'))
117 cmdq_print(cmdq, "%s", o->str);
118 else
119 cmdq_print(cmdq, "%s \"%s\"", o->name, o->str);
120 return (CMD_RETURN_NORMAL);
123 table = oe = NULL;
124 if (options_table_find(args->argv[0], &table, &oe) != 0) {
125 cmdq_error(cmdq, "ambiguous option: %s", args->argv[0]);
126 return (CMD_RETURN_ERROR);
128 if (oe == NULL) {
129 if (quiet)
130 return (CMD_RETURN_NORMAL);
131 cmdq_error(cmdq, "unknown option: %s", args->argv[0]);
132 return (CMD_RETURN_ERROR);
134 if ((o = options_find1(oo, oe->name)) == NULL)
135 return (CMD_RETURN_NORMAL);
136 optval = options_table_print_entry(oe, o, args_has(self->args, 'v'));
137 if (args_has(self->args, 'v'))
138 cmdq_print(cmdq, "%s", optval);
139 else
140 cmdq_print(cmdq, "%s %s", oe->name, optval);
141 return (CMD_RETURN_NORMAL);
144 enum cmd_retval
145 cmd_show_options_all(struct cmd *self, struct cmd_q *cmdq,
146 const struct options_table_entry *table, struct options *oo)
148 const struct options_table_entry *oe;
149 struct options_entry *o;
150 const char *optval;
152 RB_FOREACH(o, options_tree, &oo->tree) {
153 if (*o->name == '@') {
154 if (args_has(self->args, 'v'))
155 cmdq_print(cmdq, "%s", o->str);
156 else
157 cmdq_print(cmdq, "%s \"%s\"", o->name, o->str);
161 for (oe = table; oe->name != NULL; oe++) {
162 if ((o = options_find1(oo, oe->name)) == NULL)
163 continue;
164 optval = options_table_print_entry(oe, o,
165 args_has(self->args, 'v'));
166 if (args_has(self->args, 'v'))
167 cmdq_print(cmdq, "%s", optval);
168 else
169 cmdq_print(cmdq, "%s %s", oe->name, optval);
172 return (CMD_RETURN_NORMAL);