GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/cmd-list-keys.c Lines: 0 60 0.0 %
Date: 2017-11-13 Branches: 0 40 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: cmd-list-keys.c,v 1.44 2017/05/01 12:20:55 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
24
#include "tmux.h"
25
26
/*
27
 * List key bindings.
28
 */
29
30
static enum cmd_retval	cmd_list_keys_exec(struct cmd *, struct cmdq_item *);
31
32
static enum cmd_retval	cmd_list_keys_commands(struct cmd *,
33
			    struct cmdq_item *);
34
35
const struct cmd_entry cmd_list_keys_entry = {
36
	.name = "list-keys",
37
	.alias = "lsk",
38
39
	.args = { "T:", 0, 0 },
40
	.usage = "[-T key-table]",
41
42
	.flags = CMD_STARTSERVER|CMD_AFTERHOOK,
43
	.exec = cmd_list_keys_exec
44
};
45
46
const struct cmd_entry cmd_list_commands_entry = {
47
	.name = "list-commands",
48
	.alias = "lscm",
49
50
	.args = { "F:", 0, 0 },
51
	.usage = "[-F format]",
52
53
	.flags = CMD_STARTSERVER|CMD_AFTERHOOK,
54
	.exec = cmd_list_keys_exec
55
};
56
57
static enum cmd_retval
58
cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item)
59
{
60
	struct args		*args = self->args;
61
	struct key_table	*table;
62
	struct key_binding	*bd;
63
	const char		*key, *tablename, *r;
64
	char			*cp, tmp[BUFSIZ];
65
	int			 repeat, width, tablewidth, keywidth;
66
67
	if (self->entry == &cmd_list_commands_entry)
68
		return (cmd_list_keys_commands(self, item));
69
70
	tablename = args_get(args, 'T');
71
	if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) {
72
		cmdq_error(item, "table %s doesn't exist", tablename);
73
		return (CMD_RETURN_ERROR);
74
	}
75
76
	repeat = 0;
77
	tablewidth = keywidth = 0;
78
	RB_FOREACH(table, key_tables, &key_tables) {
79
		if (tablename != NULL && strcmp(table->name, tablename) != 0)
80
			continue;
81
		RB_FOREACH(bd, key_bindings, &table->key_bindings) {
82
			key = key_string_lookup_key(bd->key);
83
84
			if (bd->flags & KEY_BINDING_REPEAT)
85
				repeat = 1;
86
87
			width = utf8_cstrwidth(table->name);
88
			if (width > tablewidth)
89
				tablewidth = width;
90
			width = utf8_cstrwidth(key);
91
			if (width > keywidth)
92
				keywidth = width;
93
		}
94
	}
95
96
	RB_FOREACH(table, key_tables, &key_tables) {
97
		if (tablename != NULL && strcmp(table->name, tablename) != 0)
98
			continue;
99
		RB_FOREACH(bd, key_bindings, &table->key_bindings) {
100
			key = key_string_lookup_key(bd->key);
101
102
			if (!repeat)
103
				r = "";
104
			else if (bd->flags & KEY_BINDING_REPEAT)
105
				r = "-r ";
106
			else
107
				r = "   ";
108
			xsnprintf(tmp, sizeof tmp, "%s-T ", r);
109
110
			cp = utf8_padcstr(table->name, tablewidth);
111
			strlcat(tmp, cp, sizeof tmp);
112
			strlcat(tmp, " ", sizeof tmp);
113
			free(cp);
114
115
			cp = utf8_padcstr(key, keywidth);
116
			strlcat(tmp, cp, sizeof tmp);
117
			strlcat(tmp, " ", sizeof tmp);
118
			free(cp);
119
120
			cp = cmd_list_print(bd->cmdlist);
121
			strlcat(tmp, cp, sizeof tmp);
122
			free(cp);
123
124
			cmdq_print(item, "bind-key %s", tmp);
125
		}
126
	}
127
128
	return (CMD_RETURN_NORMAL);
129
}
130
131
static enum cmd_retval
132
cmd_list_keys_commands(struct cmd *self, struct cmdq_item *item)
133
{
134
	struct args		*args = self->args;
135
	const struct cmd_entry	**entryp;
136
	const struct cmd_entry	 *entry;
137
	struct format_tree	 *ft;
138
	const char		 *template, *s;
139
	char			 *line;
140
141
	if ((template = args_get(args, 'F')) == NULL) {
142
		template = "#{command_list_name}"
143
		    "#{?command_list_alias, (#{command_list_alias}),} "
144
		    "#{command_list_usage}";
145
	}
146
147
	ft = format_create(item->client, item, FORMAT_NONE, 0);
148
	format_defaults(ft, NULL, NULL, NULL, NULL);
149
150
	for (entryp = cmd_table; *entryp != NULL; entryp++) {
151
		entry = *entryp;
152
153
		format_add(ft, "command_list_name", "%s", entry->name);
154
		if (entry->alias != NULL)
155
			s = entry->alias;
156
		else
157
			s = "";
158
		format_add(ft, "command_list_alias", "%s", s);
159
		if (entry->usage != NULL)
160
			s = entry->usage;
161
		else
162
			s = "";
163
		format_add(ft, "command_list_usage", "%s", s);
164
165
		line = format_expand(ft, template);
166
		if (*line != '\0')
167
			cmdq_print(item, "%s", line);
168
		free(line);
169
	}
170
171
	format_free(ft);
172
	return (CMD_RETURN_NORMAL);
173
}