-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.c
369 lines (315 loc) · 7.86 KB
/
cmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "common.h"
#include "cmd.h"
#include "main.h"
#include "input.h"
#include "tokenize.h"
#include "stringlist.h"
#include "stringbuffer.h"
#include "table.h"
static void cmd_free_subcmds(struct command *cmd);
static char *cmd_generator(const char *text, int state);
static struct command *cmd_find(const char *name, struct dict *list);
CMD_FUNC(help);
CMD_TAB_FUNC(help);
CMD_FUNC(quit);
CMD_FUNC(commands);
static struct dict *command_list;
static struct dict *cmd_generator_list = NULL;
// Yay, global variables needed because we can't pass custom args to our rl_compentry_func
char *tc_argv_base[32] = { 0 };
char **tc_argv = NULL;
int tc_argc = 0;
static struct command commands[] = {
CMD_TC("help", help, "Display help"),
CMD("quit", quit, "Exit the program"),
CMD("commands", commands, "Display all available commands"),
CMD_LIST_END
};
void cmd_init()
{
command_list = dict_create();
dict_set_free_funcs(command_list, NULL, (dict_free_f *)cmd_free_subcmds);
// Initialize our own commands
cmd_register_list(commands, NULL);
cmd_alias("exit", "quit", NULL);
// Initialize external commands
cmd_server_init();
cmd_link_init();
cmd_conf_init();
cmd_oper_init();
cmd_service_init();
cmd_forward_init();
cmd_feature_init();
cmd_jupe_init();
cmd_class_init();
cmd_pseudo_init();
cmd_client_init();
cmd_webirc_init();
}
void cmd_fini()
{
dict_free(command_list);
}
static void cmd_free_subcmds(struct command *cmd)
{
if(cmd->subcommands)
dict_free(cmd->subcommands);
if(cmd->aliases)
stringlist_free(cmd->aliases);
if(cmd->alias)
free(cmd);
}
void cmd_register(struct command *cmd, const char *parent_name)
{
struct command *parent = parent_name ? cmd_find(parent_name, command_list) : NULL;
assert(!parent_name || parent);
if(parent && !parent->subcommands)
parent->subcommands = dict_create();
dict_insert(parent ? parent->subcommands : command_list, (char *)cmd->name, cmd);
}
void cmd_register_list(struct command *cmd, const char *parent_name)
{
while(cmd->name)
{
cmd_register(cmd, parent_name);
cmd++;
}
}
void cmd_alias(const char *name, const char *cmd_name, const char *subcmd_name)
{
struct command *cmd, *alias;
assert(strchr(name, ' ') == NULL); // not supported
assert(cmd = cmd_find(cmd_name, command_list));
assert(!subcmd_name || (cmd->subcommands && (cmd = cmd_find(subcmd_name, cmd->subcommands))));
alias = malloc(sizeof(struct command));
memcpy(alias, cmd, sizeof(struct command));
alias->name = name;
alias->alias = 1;
dict_insert(command_list, (char *)alias->name, alias);
if(!cmd->aliases)
cmd->aliases = stringlist_create();
stringlist_add(cmd->aliases, strdup(alias->name));
}
void cmd_handle(const char *line, int argc, char **argv, struct command *parent)
{
struct command *cmd;
static char cmdbuf[32];
if(argc < 1)
{
error("No %scommand given", parent ? "sub" : "");
return;
}
cmd = cmd_find(argv[0], parent ? parent->subcommands : command_list);
if(!cmd)
{
if(parent)
error("%s %s: command not found", parent->name, argv[0]);
else
error("%s: command not found", argv[0]);
return;
}
if(cmd->subcommands)
cmd_handle(line, argc - 1, argv + 1, cmd);
else if(!cmd->func)
{
if(parent)
{
snprintf(cmdbuf, sizeof(cmdbuf), "%s %s", parent->name, cmd->name);
argv[0] = cmdbuf;
}
error("%s: command not implemented", argv[0]);
}
else
{
if(parent)
{
snprintf(cmdbuf, sizeof(cmdbuf), "%s %s", parent->name, cmd->name);
argv[0] = cmdbuf;
}
cmd->func(line, argc, argv);
}
}
char **cmd_tabcomp(const char *text, int start, int end)
{
char **list;
char quote;
char *dequoted;
char *tc_line_dup;
static char cmdbuf[32];
struct command *cmd = NULL, *subcmd = NULL;
cmd_tab_func *func = NULL;
tc_line_dup = strdup(rl_line_buffer);
tc_argc = tokenize_quoted(tc_line_dup, tc_argv_base, 32);
tc_argv = tc_argv_base;
if(CAN_COMPLETE_ARG(0))
{
cmd_generator_list = command_list;
func = cmd_generator;
}
else if(CAN_COMPLETE_ARG(1) && (cmd = cmd_find(tc_argv[0], command_list)))
{
if(cmd->subcommands)
{
cmd_generator_list = cmd->subcommands;
func = cmd_generator;
}
else if(cmd->tabfunc)
func = cmd->tabfunc;
}
else if(tc_argc > 0 && (cmd = cmd_find(tc_argv[0], command_list)))
{
if(cmd->tabfunc) // single command
func = cmd->tabfunc;
else if(cmd->subcommands && tc_argc > 1 && (subcmd = cmd_find(tc_argv[1], cmd->subcommands)))
{
snprintf(cmdbuf, sizeof(cmdbuf), "%s %s", cmd->name, subcmd->name);
tc_argv[1] = cmdbuf;
tc_argv++;
tc_argc--;
func = subcmd->tabfunc;
}
}
if(!func)
{
free(tc_line_dup);
return NULL;
}
rl_filename_completion_desired = 1;
quote = ((char_is_quoted(rl_line_buffer, start) &&
strchr(rl_completer_quote_characters, rl_line_buffer[start - 1]))
? rl_line_buffer[start - 1] : 0);
dequoted = bash_dequote_filename(text, quote);
list = rl_completion_matches(dequoted, func);
func(NULL, -1);
free(tc_line_dup);
free(dequoted);
return list;
}
static char *cmd_generator(const char *text, int state)
{
static int idx;
static size_t len;
int skipped = 0;
if(!state) // New word
{
idx = 0;
len = strlen(text);
}
else if(state == -1) // Cleanup
return NULL;
// Return the next name which partially matches from the command list.
dict_iter(node, cmd_generator_list)
{
if(skipped++ < idx)
continue;
idx++;
if(!strncmp(node->key, text, len))
return strdup(node->key);
}
return NULL;
}
static struct command *cmd_find(const char *name, struct dict *list)
{
dict_iter(node, list)
{
if(!strcmp(name, node->key))
return node->data;
}
return NULL;
}
CMD_FUNC(help)
{
struct command *cmd, *subcmd;
if(argc < 2)
{
out("Usage: help <command> [subcommand]");
return;
}
if(!(cmd = cmd_find(argv[1], command_list)))
{
out("No commands match `%s'", argv[1]);
return;
}
if(cmd->subcommands && argc > 2 && (subcmd = cmd_find(argv[2], cmd->subcommands)))
out("%s %s: %s", cmd->name, subcmd->name, subcmd->doc);
else
out("%s: %s", cmd->name, cmd->doc);
}
CMD_TAB_FUNC(help)
{
struct command *cmd, *subcmd;
if(CAN_COMPLETE_ARG(1))
{
cmd_generator_list = command_list;
return cmd_generator(text, state);
}
else if(CAN_COMPLETE_ARG(2) && (cmd = cmd_find(tc_argv[1], command_list)) && cmd->subcommands)
{
cmd_generator_list = cmd->subcommands;
return cmd_generator(text, state);
}
return NULL;
}
CMD_FUNC(quit)
{
quit = 1;
}
static void list_command(struct table *table, unsigned int row, struct command *cmd, const char *parent_cmd_name)
{
struct stringbuffer *buf = stringbuffer_create();
// Show aliases first
if(cmd->aliases)
{
for(unsigned int i = 0; i < cmd->aliases->count; i++)
{
if(!no_colors)
stringbuffer_append_string(buf, "\033[" COLOR_YELLOW "m");
stringbuffer_append_string(buf, cmd->aliases->data[i]);
if(!no_colors)
stringbuffer_append_string(buf, "\033[0m");
stringbuffer_append_string(buf, ", ");
}
}
if(!no_colors)
stringbuffer_append_string(buf, "\033[" COLOR_YELLOW "m");
if(parent_cmd_name)
{
stringbuffer_append_string(buf, parent_cmd_name);
stringbuffer_append_char(buf, ' ');
}
stringbuffer_append_string(buf, cmd->name);
if(!no_colors)
stringbuffer_append_string(buf, "\033[0m");
table_col_str(table, row, 0, strdup(buf->string));
table_col_str(table, row, 1, (char *)cmd->doc);
stringbuffer_free(buf);
}
CMD_FUNC(commands)
{
unsigned int row = 0;
struct table *table = table_create(2, 0);
table->field_len = table_strlen_colors;
table_free_column(table, 0, 1);
dict_iter(node, command_list)
{
struct command *cmd = node->data;
// Skip aliases, we handle them together with the main commands.
if(cmd->alias)
continue;
if(cmd->subcommands)
{
dict_iter(subnode, cmd->subcommands)
{
struct command *subcmd = subnode->data;
list_command(table, row++, subcmd, cmd->name);
}
}
else
{
list_command(table, row++, cmd, NULL);
}
}
table_sort(table, 0);
table_send(table);
table_free(table);
}