/src/tmux/cmd-list-commands.c
Line | Count | Source |
1 | | /* $OpenBSD$ */ |
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 all commands. |
28 | | */ |
29 | | |
30 | | #define LIST_COMMANDS_TEMPLATE \ |
31 | 0 | "#{command_list_name}" \ |
32 | 0 | "#{?command_list_alias, (#{command_list_alias}),} " \ |
33 | 0 | "#{command_list_usage}" |
34 | | |
35 | | static enum cmd_retval cmd_list_commands(struct cmd *, struct cmdq_item *); |
36 | | |
37 | | const struct cmd_entry cmd_list_commands_entry = { |
38 | | .name = "list-commands", |
39 | | .alias = "lscm", |
40 | | |
41 | | .args = { "F:", 0, 1, NULL }, |
42 | | .usage = "[-F format] [command]", |
43 | | |
44 | | .flags = CMD_STARTSERVER|CMD_AFTERHOOK, |
45 | | .exec = cmd_list_commands |
46 | | }; |
47 | | |
48 | | static void |
49 | | cmd_list_single_command(const struct cmd_entry *entry, struct format_tree *ft, |
50 | | const char *template, struct cmdq_item *item) |
51 | 0 | { |
52 | 0 | const char *s; |
53 | 0 | char *line; |
54 | |
|
55 | 0 | format_add(ft, "command_list_name", "%s", entry->name); |
56 | 0 | if (entry->alias != NULL) |
57 | 0 | s = entry->alias; |
58 | 0 | else |
59 | 0 | s = ""; |
60 | 0 | format_add(ft, "command_list_alias", "%s", s); |
61 | 0 | if (entry->usage != NULL) |
62 | 0 | s = entry->usage; |
63 | 0 | else |
64 | 0 | s = ""; |
65 | 0 | format_add(ft, "command_list_usage", "%s", s); |
66 | |
|
67 | 0 | line = format_expand(ft, template); |
68 | 0 | if (*line != '\0') |
69 | 0 | cmdq_print(item, "%s", line); |
70 | 0 | free(line); |
71 | 0 | } |
72 | | |
73 | | static enum cmd_retval |
74 | | cmd_list_commands(struct cmd *self, struct cmdq_item *item) |
75 | 0 | { |
76 | 0 | struct args *args = cmd_get_args(self); |
77 | 0 | const struct cmd_entry **entryp; |
78 | 0 | const struct cmd_entry *entry; |
79 | 0 | struct format_tree *ft; |
80 | 0 | const char *template, *command; |
81 | 0 | char *cause; |
82 | |
|
83 | 0 | if ((template = args_get(args, 'F')) == NULL) |
84 | 0 | template = LIST_COMMANDS_TEMPLATE; |
85 | |
|
86 | 0 | ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0); |
87 | 0 | format_defaults(ft, NULL, NULL, NULL, NULL); |
88 | |
|
89 | 0 | command = args_string(args, 0); |
90 | 0 | if (command == NULL) { |
91 | 0 | for (entryp = cmd_table; *entryp != NULL; entryp++) |
92 | 0 | cmd_list_single_command(*entryp, ft, template, item); |
93 | 0 | } else { |
94 | 0 | entry = cmd_find(command, &cause); |
95 | 0 | if (entry != NULL) |
96 | 0 | cmd_list_single_command(entry, ft, template, item); |
97 | 0 | else { |
98 | 0 | cmdq_error(item, "%s", cause); |
99 | 0 | free(cause); |
100 | 0 | format_free(ft); |
101 | 0 | return (CMD_RETURN_ERROR); |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | 0 | format_free(ft); |
106 | 0 | return (CMD_RETURN_NORMAL); |
107 | 0 | } |