GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/cmd-show-options.c Lines: 0 75 0.0 %
Date: 2017-11-13 Branches: 0 38 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: cmd-show-options.c,v 1.42 2017/05/10 13:05:41 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5
 *
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.
9
 *
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.
17
 */
18
19
#include <sys/types.h>
20
21
#include <stdlib.h>
22
#include <string.h>
23
#include <vis.h>
24
25
#include "tmux.h"
26
27
/*
28
 * Show options.
29
 */
30
31
static enum cmd_retval	cmd_show_options_exec(struct cmd *, struct cmdq_item *);
32
33
static enum cmd_retval	cmd_show_options_one(struct cmd *, struct cmdq_item *,
34
			    struct options *);
35
static enum cmd_retval	cmd_show_options_all(struct cmd *, struct cmdq_item *,
36
		    	    struct options *);
37
38
const struct cmd_entry cmd_show_options_entry = {
39
	.name = "show-options",
40
	.alias = "show",
41
42
	.args = { "gqst:vw", 0, 1 },
43
	.usage = "[-gqsvw] [-t target-session|target-window] [option]",
44
45
	.target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
46
47
	.flags = CMD_AFTERHOOK,
48
	.exec = cmd_show_options_exec
49
};
50
51
const struct cmd_entry cmd_show_window_options_entry = {
52
	.name = "show-window-options",
53
	.alias = "showw",
54
55
	.args = { "gvt:", 0, 1 },
56
	.usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
57
58
	.target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
59
60
	.flags = CMD_AFTERHOOK,
61
	.exec = cmd_show_options_exec
62
};
63
64
static enum cmd_retval
65
cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
66
{
67
	struct args			*args = self->args;
68
	struct cmd_find_state		*fs = &item->target;
69
	struct options			*oo;
70
	enum options_table_scope	 scope;
71
	char				*cause;
72
	int				 window;
73
74
	window = (self->entry == &cmd_show_window_options_entry);
75
	scope = options_scope_from_flags(args, window, fs, &oo, &cause);
76
	if (scope == OPTIONS_TABLE_NONE) {
77
		cmdq_error(item, "%s", cause);
78
		free(cause);
79
		return (CMD_RETURN_ERROR);
80
	}
81
82
	if (args->argc == 0)
83
		return (cmd_show_options_all(self, item, oo));
84
	else
85
		return (cmd_show_options_one(self, item, oo));
86
}
87
88
static void
89
cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
90
    struct options_entry *o, int idx)
91
{
92
	const char	*name;
93
	const char	*value;
94
	char		*tmp, *escaped;
95
	u_int		 size, i;
96
97
	if (idx != -1) {
98
		xasprintf(&tmp, "%s[%d]", options_name(o), idx);
99
		name = tmp;
100
	} else {
101
		if (options_array_size(o, &size) != -1) {
102
			for (i = 0; i < size; i++) {
103
				if (options_array_get(o, i) == NULL)
104
					continue;
105
				cmd_show_options_print(self, item, o, i);
106
			}
107
			return;
108
		}
109
		tmp = NULL;
110
		name = options_name(o);
111
	}
112
113
	value = options_tostring(o, idx, 0);
114
	if (args_has(self->args, 'v'))
115
		cmdq_print(item, "%s", value);
116
	else if (options_isstring(o)) {
117
		utf8_stravis(&escaped, value, VIS_OCTAL|VIS_TAB|VIS_NL|VIS_DQ);
118
		cmdq_print(item, "%s \"%s\"", name, escaped);
119
		free(escaped);
120
	} else
121
		cmdq_print(item, "%s %s", name, value);
122
123
	free(tmp);
124
}
125
126
static enum cmd_retval
127
cmd_show_options_one(struct cmd *self, struct cmdq_item *item,
128
    struct options *oo)
129
{
130
	struct args		*args = self->args;
131
	struct client		*c = cmd_find_client(item, NULL, 1);
132
	struct session		*s = item->target.s;
133
	struct winlink		*wl = item->target.wl;
134
	struct options_entry	*o;
135
	int			 idx, ambiguous;
136
	char			*name;
137
138
	name = format_single(item, args->argv[0], c, s, wl, NULL);
139
	o = options_match_get(oo, name, &idx, 1, &ambiguous);
140
	if (o == NULL) {
141
		if (args_has(args, 'q')) {
142
			free(name);
143
			return (CMD_RETURN_NORMAL);
144
		}
145
		if (ambiguous) {
146
			cmdq_error(item, "ambiguous option: %s", name);
147
			free(name);
148
			return (CMD_RETURN_ERROR);
149
		}
150
		if (*name != '@' &&
151
		    options_match_get(oo, name, &idx, 0, &ambiguous) != NULL) {
152
			free(name);
153
			return (CMD_RETURN_NORMAL);
154
		}
155
		cmdq_error(item, "unknown option: %s", name);
156
		free(name);
157
		return (CMD_RETURN_ERROR);
158
	}
159
	cmd_show_options_print(self, item, o, idx);
160
	free(name);
161
	return (CMD_RETURN_NORMAL);
162
}
163
164
static enum cmd_retval
165
cmd_show_options_all(struct cmd *self, struct cmdq_item *item,
166
    struct options *oo)
167
{
168
	struct options_entry			 *o;
169
	const struct options_table_entry	*oe;
170
	u_int					 size, idx;
171
172
	o = options_first(oo);
173
	while (o != NULL) {
174
		oe = options_table_entry(o);
175
		if (oe != NULL && oe->style != NULL) {
176
			o = options_next(o);
177
			continue;
178
		}
179
		if (options_array_size(o, &size) == -1)
180
			cmd_show_options_print(self, item, o, -1);
181
		else {
182
			for (idx = 0; idx < size; idx++) {
183
				if (options_array_get(o, idx) == NULL)
184
					continue;
185
				cmd_show_options_print(self, item, o, idx);
186
			}
187
		}
188
		o = options_next(o);
189
	}
190
	return (CMD_RETURN_NORMAL);
191
}