forked from davidben/barnowl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.c
281 lines (237 loc) · 8.18 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "owl.h"
extern const owl_cmd commands_to_init[];
/**************************************************************************/
/***************************** COMMAND DICT *******************************/
/**************************************************************************/
int owl_cmddict_setup(owl_cmddict *cd) {
owl_cmddict_init(cd);
if (0 != owl_cmddict_add_from_list(cd, commands_to_init)) return(-1);
return(0);
}
void owl_cmddict_init(owl_cmddict *cd) {
owl_dict_create(cd);
}
/* for bulk initialization at startup */
int owl_cmddict_add_from_list(owl_cmddict *cd, const owl_cmd *cmds) {
const owl_cmd *cur;
int ret = 0;
for (cur = cmds; cur->name != NULL; cur++) {
ret = owl_cmddict_add_cmd(cd, cur);
if (ret < 0) break;
}
return ret;
}
void owl_cmddict_get_names(const owl_cmddict *d, owl_list *l) {
owl_dict_get_keys(d, l);
}
const owl_cmd *owl_cmddict_find(const owl_cmddict *d, const char *name) {
return owl_dict_find_element(d, name);
}
/* creates a new command alias */
int owl_cmddict_add_alias(owl_cmddict *cd, const char *alias_from, const char *alias_to) {
owl_cmd *cmd;
cmd = g_new(owl_cmd, 1);
owl_cmd_create_alias(cmd, alias_from, alias_to);
owl_perlconfig_new_command(cmd->name);
owl_dict_insert_element(cd, cmd->name, cmd, (void (*)(void *))owl_cmd_delete);
return(0);
}
int owl_cmddict_add_cmd(owl_cmddict *cd, const owl_cmd * cmd) {
owl_cmd * newcmd = g_new(owl_cmd, 1);
if(owl_cmd_create_from_template(newcmd, cmd) < 0) {
g_free(newcmd);
return -1;
}
owl_perlconfig_new_command(cmd->name);
return owl_dict_insert_element(cd, newcmd->name, newcmd, (void (*)(void *))owl_cmd_delete);
}
char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff) {
char *retval = NULL;
const owl_cmd *cmd;
if (!strcmp(argv[0], "")) {
} else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) {
retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, buff);
/* redraw the sepbar; TODO: don't violate layering */
owl_global_sepbar_dirty(&g);
} else {
owl_function_makemsg("Unknown command '%s'.", buff);
}
return retval;
}
char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff) {
char **argv;
int argc;
char *retval = NULL;
argv = owl_parseline(cmdbuff, &argc);
if (argv == NULL) {
owl_function_makemsg("Unbalanced quotes");
return NULL;
}
if (argc < 1) {
g_strfreev(argv);
return NULL;
}
retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff);
g_strfreev(argv);
return retval;
}
char *owl_cmddict_execute_argv(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc) {
char *buff;
char *retval = NULL;
buff = g_strjoinv(" ", (char**)argv);
retval = _owl_cmddict_execute(cd, ctx, argv, argc, buff);
g_free(buff);
return retval;
}
/*********************************************************************/
/***************************** COMMAND *******************************/
/*********************************************************************/
/* sets up a new command based on a template, copying strings */
int owl_cmd_create_from_template(owl_cmd *cmd, const owl_cmd *templ) {
*cmd = *templ;
if (!templ->name) return(-1);
cmd->name = g_strdup(templ->name);
if (templ->summary) cmd->summary = g_strdup(templ->summary);
if (templ->usage) cmd->usage = g_strdup(templ->usage);
if (templ->description) cmd->description = g_strdup(templ->description);
if (templ->cmd_aliased_to) cmd->cmd_aliased_to = g_strdup(templ->cmd_aliased_to);
return(0);
}
int owl_cmd_create_alias(owl_cmd *cmd, const char *name, const char *aliased_to) {
memset(cmd, 0, sizeof(owl_cmd));
cmd->name = g_strdup(name);
cmd->cmd_aliased_to = g_strdup(aliased_to);
cmd->summary = g_strdup_printf("%s%s", OWL_CMD_ALIAS_SUMMARY_PREFIX, aliased_to);
return(0);
}
void owl_cmd_cleanup(owl_cmd *cmd)
{
g_free(cmd->name);
g_free(cmd->summary);
g_free(cmd->usage);
g_free(cmd->description);
g_free(cmd->cmd_aliased_to);
if (cmd->cmd_perl) owl_perlconfig_cmd_cleanup(cmd);
}
void owl_cmd_delete(owl_cmd *cmd)
{
owl_cmd_cleanup(cmd);
g_free(cmd);
}
int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) {
if (owl_context_matches(ctx, cmd->validctx)) return 1;
else return 0;
}
char *owl_cmd_execute(const owl_cmd *cmd, const owl_cmddict *cd, const owl_context *ctx, int argc, const char *const *argv, const char *cmdbuff) {
static int alias_recurse_depth = 0;
int ival=0;
const char *cmdbuffargs;
char *newcmd, *rv=NULL;
if (argc < 1) return(NULL);
/* Recurse if this is an alias */
if (cmd->cmd_aliased_to) {
if (alias_recurse_depth++ > 50) {
owl_function_makemsg("Alias loop detected for '%s'.", cmdbuff);
} else {
cmdbuffargs = skiptokens(cmdbuff, 1);
newcmd = g_strdup_printf("%s %s", cmd->cmd_aliased_to, cmdbuffargs);
rv = owl_function_command(newcmd);
g_free(newcmd);
}
alias_recurse_depth--;
return rv;
}
/* Do validation and conversions */
if (cmd->cmd_ctxargs_fn || cmd->cmd_ctxv_fn || cmd->cmd_ctxi_fn) {
if (!owl_cmd_is_context_valid(cmd, ctx)) {
owl_function_makemsg("Invalid context for command '%s'.", cmdbuff);
return NULL;
}
}
if ((argc != 1) && (cmd->cmd_v_fn || cmd->cmd_ctxv_fn)) {
owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
return NULL;
}
if (cmd->cmd_i_fn || cmd->cmd_ctxi_fn) {
char *ep;
if (argc != 2) {
owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
return NULL;
}
ival = strtol(argv[1], &ep, 10);
if (*ep || ep==argv[1]) {
owl_function_makemsg("Invalid argument '%s' for %s command.", argv[1], argv[0]);
return(NULL);
}
}
if (cmd->cmd_args_fn) {
return cmd->cmd_args_fn(argc, argv, cmdbuff);
} else if (cmd->cmd_v_fn) {
cmd->cmd_v_fn();
} else if (cmd->cmd_i_fn) {
cmd->cmd_i_fn(ival);
} else if (cmd->cmd_ctxargs_fn) {
return cmd->cmd_ctxargs_fn(owl_context_get_data(ctx), argc, argv, cmdbuff);
} else if (cmd->cmd_ctxv_fn) {
cmd->cmd_ctxv_fn(owl_context_get_data(ctx));
} else if (cmd->cmd_ctxi_fn) {
cmd->cmd_ctxi_fn(owl_context_get_data(ctx), ival);
} else if (cmd->cmd_perl) {
return owl_perlconfig_perlcmd(cmd, argc, argv);
}
return NULL;
}
/* returns a reference */
const char *owl_cmd_get_summary(const owl_cmd *cmd) {
return cmd->summary;
}
/* returns a summary line describing this keymap. the caller must free. */
char *owl_cmd_describe(const owl_cmd *cmd) {
if (!cmd || !cmd->name || !cmd->summary) return NULL;
return g_strdup_printf("%-25s - %s", cmd->name, cmd->summary);
}
void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {
const char *s;
char *indent;
owl_cmd *cmd;
if (!name || (cmd = owl_dict_find_element(d, name)) == NULL) {
owl_fmtext_append_bold(fm, "OWL HELP\n\n");
owl_fmtext_append_normal(fm, "No such command...\n");
return;
}
owl_fmtext_append_bold(fm, "OWL HELP\n\n");
owl_fmtext_append_bold(fm, "NAME\n\n");
owl_fmtext_append_normal(fm, OWL_TABSTR);
owl_fmtext_append_normal(fm, cmd->name);
if (cmd->summary && *cmd->summary) {
owl_fmtext_append_normal(fm, " - ");
owl_fmtext_append_normal(fm, cmd->summary);
}
owl_fmtext_append_normal(fm, "\n");
if (cmd->usage && *cmd->usage) {
s = cmd->usage;
indent = owl_text_indent(s, OWL_TAB);
owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
owl_fmtext_append_normal(fm, indent);
owl_fmtext_append_normal(fm, "\n");
g_free(indent);
} else {
owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
owl_fmtext_append_normal(fm, OWL_TABSTR);
owl_fmtext_append_normal(fm, cmd->name);
owl_fmtext_append_normal(fm, "\n");
}
if (cmd->description && *cmd->description) {
s = cmd->description;
indent = owl_text_indent(s, OWL_TAB);
owl_fmtext_append_bold(fm, "\nDESCRIPTION\n");
owl_fmtext_append_normal(fm, indent);
owl_fmtext_append_normal(fm, "\n");
g_free(indent);
}
owl_fmtext_append_normal(fm, "\n\n");
}