1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <unistd.h> |
---|
5 | #include "owl.h" |
---|
6 | |
---|
7 | extern const owl_cmd commands_to_init[]; |
---|
8 | |
---|
9 | /**************************************************************************/ |
---|
10 | /***************************** COMMAND DICT *******************************/ |
---|
11 | /**************************************************************************/ |
---|
12 | |
---|
13 | int owl_cmddict_setup(owl_cmddict *cd) { |
---|
14 | if (0 != owl_cmddict_init(cd)) return(-1); |
---|
15 | if (0 != owl_cmddict_add_from_list(cd, commands_to_init)) return(-1); |
---|
16 | return(0); |
---|
17 | } |
---|
18 | |
---|
19 | int owl_cmddict_init(owl_cmddict *cd) { |
---|
20 | if (owl_dict_create(cd)) return(-1); |
---|
21 | return(0); |
---|
22 | } |
---|
23 | |
---|
24 | /* for bulk initialization at startup */ |
---|
25 | int owl_cmddict_add_from_list(owl_cmddict *cd, const owl_cmd *cmds) { |
---|
26 | const owl_cmd *cur; |
---|
27 | int ret = 0; |
---|
28 | for (cur = cmds; cur->name != NULL; cur++) { |
---|
29 | ret = owl_cmddict_add_cmd(cd, cur); |
---|
30 | if (ret < 0) break; |
---|
31 | } |
---|
32 | return ret; |
---|
33 | } |
---|
34 | |
---|
35 | /* free the list with owl_cmddict_namelist_cleanup */ |
---|
36 | void owl_cmddict_get_names(const owl_cmddict *d, owl_list *l) { |
---|
37 | owl_dict_get_keys(d, l); |
---|
38 | } |
---|
39 | |
---|
40 | const owl_cmd *owl_cmddict_find(const owl_cmddict *d, const char *name) { |
---|
41 | return owl_dict_find_element(d, name); |
---|
42 | } |
---|
43 | |
---|
44 | void owl_cmddict_namelist_cleanup(owl_list *l) |
---|
45 | { |
---|
46 | owl_list_cleanup(l, g_free); |
---|
47 | } |
---|
48 | |
---|
49 | /* creates a new command alias */ |
---|
50 | int owl_cmddict_add_alias(owl_cmddict *cd, const char *alias_from, const char *alias_to) { |
---|
51 | owl_cmd *cmd; |
---|
52 | cmd = g_new(owl_cmd, 1); |
---|
53 | owl_cmd_create_alias(cmd, alias_from, alias_to); |
---|
54 | owl_perlconfig_new_command(cmd->name); |
---|
55 | owl_dict_insert_element(cd, cmd->name, cmd, (void (*)(void *))owl_cmd_delete); |
---|
56 | return(0); |
---|
57 | } |
---|
58 | |
---|
59 | int owl_cmddict_add_cmd(owl_cmddict *cd, const owl_cmd * cmd) { |
---|
60 | owl_cmd * newcmd = g_new(owl_cmd, 1); |
---|
61 | if(owl_cmd_create_from_template(newcmd, cmd) < 0) { |
---|
62 | g_free(newcmd); |
---|
63 | return -1; |
---|
64 | } |
---|
65 | owl_perlconfig_new_command(cmd->name); |
---|
66 | return owl_dict_insert_element(cd, newcmd->name, newcmd, (void (*)(void *))owl_cmd_delete); |
---|
67 | } |
---|
68 | |
---|
69 | char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff) { |
---|
70 | char *retval = NULL; |
---|
71 | const owl_cmd *cmd; |
---|
72 | |
---|
73 | if (!strcmp(argv[0], "")) { |
---|
74 | } else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) { |
---|
75 | retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, buff); |
---|
76 | /* redraw the sepbar; TODO: don't violate layering */ |
---|
77 | owl_global_sepbar_dirty(&g); |
---|
78 | } else { |
---|
79 | owl_function_makemsg("Unknown command '%s'.", buff); |
---|
80 | } |
---|
81 | return retval; |
---|
82 | } |
---|
83 | |
---|
84 | char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff) { |
---|
85 | char **argv; |
---|
86 | int argc; |
---|
87 | char *retval = NULL; |
---|
88 | |
---|
89 | argv = owl_parseline(cmdbuff, &argc); |
---|
90 | if (argv == NULL) { |
---|
91 | owl_function_makemsg("Unbalanced quotes"); |
---|
92 | return NULL; |
---|
93 | } |
---|
94 | |
---|
95 | if (argc < 1) { |
---|
96 | g_strfreev(argv); |
---|
97 | return NULL; |
---|
98 | } |
---|
99 | |
---|
100 | retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff); |
---|
101 | |
---|
102 | g_strfreev(argv); |
---|
103 | return retval; |
---|
104 | } |
---|
105 | |
---|
106 | char *owl_cmddict_execute_argv(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc) { |
---|
107 | GString *buf = g_string_new(""); |
---|
108 | int i; |
---|
109 | char *retval; |
---|
110 | |
---|
111 | /* We weren't given a command line, so fabricate a valid one. */ |
---|
112 | for(i = 0; i < argc; i++) { |
---|
113 | if (i != 0) |
---|
114 | g_string_append_c(buf, ' '); |
---|
115 | owl_string_append_quoted_arg(buf, argv[i]); |
---|
116 | } |
---|
117 | |
---|
118 | retval = _owl_cmddict_execute(cd, ctx, argv, argc, buf->str); |
---|
119 | |
---|
120 | g_string_free(buf, true); |
---|
121 | return retval; |
---|
122 | } |
---|
123 | |
---|
124 | /*********************************************************************/ |
---|
125 | /***************************** COMMAND *******************************/ |
---|
126 | /*********************************************************************/ |
---|
127 | |
---|
128 | /* sets up a new command based on a template, copying strings */ |
---|
129 | int owl_cmd_create_from_template(owl_cmd *cmd, const owl_cmd *templ) { |
---|
130 | *cmd = *templ; |
---|
131 | if (!templ->name) return(-1); |
---|
132 | cmd->name = g_strdup(templ->name); |
---|
133 | if (templ->summary) cmd->summary = g_strdup(templ->summary); |
---|
134 | if (templ->usage) cmd->usage = g_strdup(templ->usage); |
---|
135 | if (templ->description) cmd->description = g_strdup(templ->description); |
---|
136 | if (templ->cmd_aliased_to) cmd->cmd_aliased_to = g_strdup(templ->cmd_aliased_to); |
---|
137 | return(0); |
---|
138 | } |
---|
139 | |
---|
140 | int owl_cmd_create_alias(owl_cmd *cmd, const char *name, const char *aliased_to) { |
---|
141 | memset(cmd, 0, sizeof(owl_cmd)); |
---|
142 | cmd->name = g_strdup(name); |
---|
143 | cmd->cmd_aliased_to = g_strdup(aliased_to); |
---|
144 | cmd->summary = g_strdup_printf("%s%s", OWL_CMD_ALIAS_SUMMARY_PREFIX, aliased_to); |
---|
145 | return(0); |
---|
146 | } |
---|
147 | |
---|
148 | void owl_cmd_cleanup(owl_cmd *cmd) |
---|
149 | { |
---|
150 | if (cmd->name) g_free(cmd->name); |
---|
151 | if (cmd->summary) g_free(cmd->summary); |
---|
152 | if (cmd->usage) g_free(cmd->usage); |
---|
153 | if (cmd->description) g_free(cmd->description); |
---|
154 | if (cmd->cmd_aliased_to) g_free(cmd->cmd_aliased_to); |
---|
155 | if (cmd->cmd_perl) owl_perlconfig_cmd_cleanup(cmd); |
---|
156 | } |
---|
157 | |
---|
158 | void owl_cmd_delete(owl_cmd *cmd) |
---|
159 | { |
---|
160 | owl_cmd_cleanup(cmd); |
---|
161 | g_free(cmd); |
---|
162 | } |
---|
163 | |
---|
164 | int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) { |
---|
165 | if (owl_context_matches(ctx, cmd->validctx)) return 1; |
---|
166 | else return 0; |
---|
167 | } |
---|
168 | |
---|
169 | 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) { |
---|
170 | static int alias_recurse_depth = 0; |
---|
171 | int ival=0; |
---|
172 | const char *cmdbuffargs; |
---|
173 | char *newcmd, *rv=NULL; |
---|
174 | |
---|
175 | if (argc < 1) return(NULL); |
---|
176 | |
---|
177 | /* Recurse if this is an alias */ |
---|
178 | if (cmd->cmd_aliased_to) { |
---|
179 | if (alias_recurse_depth++ > 50) { |
---|
180 | owl_function_makemsg("Alias loop detected for '%s'.", cmdbuff); |
---|
181 | } else { |
---|
182 | cmdbuffargs = skiptokens(cmdbuff, 1); |
---|
183 | newcmd = g_strdup_printf("%s %s", cmd->cmd_aliased_to, cmdbuffargs); |
---|
184 | rv = owl_function_command(newcmd); |
---|
185 | g_free(newcmd); |
---|
186 | } |
---|
187 | alias_recurse_depth--; |
---|
188 | return rv; |
---|
189 | } |
---|
190 | |
---|
191 | /* Do validation and conversions */ |
---|
192 | if (cmd->cmd_ctxargs_fn || cmd->cmd_ctxv_fn || cmd->cmd_ctxi_fn) { |
---|
193 | if (!owl_cmd_is_context_valid(cmd, ctx)) { |
---|
194 | owl_function_makemsg("Invalid context for command '%s'.", cmdbuff); |
---|
195 | return NULL; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | if ((argc != 1) && (cmd->cmd_v_fn || cmd->cmd_ctxv_fn)) { |
---|
200 | owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]); |
---|
201 | return NULL; |
---|
202 | } |
---|
203 | |
---|
204 | if (cmd->cmd_i_fn || cmd->cmd_ctxi_fn) { |
---|
205 | char *ep; |
---|
206 | if (argc != 2) { |
---|
207 | owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]); |
---|
208 | return NULL; |
---|
209 | } |
---|
210 | ival = strtol(argv[1], &ep, 10); |
---|
211 | if (*ep || ep==argv[1]) { |
---|
212 | owl_function_makemsg("Invalid argument '%s' for %s command.", argv[1], argv[0]); |
---|
213 | return(NULL); |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | if (cmd->cmd_args_fn) { |
---|
218 | return cmd->cmd_args_fn(argc, argv, cmdbuff); |
---|
219 | } else if (cmd->cmd_v_fn) { |
---|
220 | cmd->cmd_v_fn(); |
---|
221 | } else if (cmd->cmd_i_fn) { |
---|
222 | cmd->cmd_i_fn(ival); |
---|
223 | } else if (cmd->cmd_ctxargs_fn) { |
---|
224 | return cmd->cmd_ctxargs_fn(owl_context_get_data(ctx), argc, argv, cmdbuff); |
---|
225 | } else if (cmd->cmd_ctxv_fn) { |
---|
226 | cmd->cmd_ctxv_fn(owl_context_get_data(ctx)); |
---|
227 | } else if (cmd->cmd_ctxi_fn) { |
---|
228 | cmd->cmd_ctxi_fn(owl_context_get_data(ctx), ival); |
---|
229 | } else if (cmd->cmd_perl) { |
---|
230 | return owl_perlconfig_perlcmd(cmd, argc, argv); |
---|
231 | } |
---|
232 | |
---|
233 | return NULL; |
---|
234 | } |
---|
235 | |
---|
236 | /* returns a reference */ |
---|
237 | const char *owl_cmd_get_summary(const owl_cmd *cmd) { |
---|
238 | return cmd->summary; |
---|
239 | } |
---|
240 | |
---|
241 | /* returns a summary line describing this keymap. the caller must free. */ |
---|
242 | char *owl_cmd_describe(const owl_cmd *cmd) { |
---|
243 | if (!cmd || !cmd->name || !cmd->summary) return NULL; |
---|
244 | return g_strdup_printf("%-25s - %s", cmd->name, cmd->summary); |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | |
---|
249 | void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) { |
---|
250 | const char *s; |
---|
251 | char *indent; |
---|
252 | owl_cmd *cmd; |
---|
253 | |
---|
254 | if (!name || (cmd = owl_dict_find_element(d, name)) == NULL) { |
---|
255 | owl_fmtext_append_bold(fm, "OWL HELP\n\n"); |
---|
256 | owl_fmtext_append_normal(fm, "No such command...\n"); |
---|
257 | return; |
---|
258 | } |
---|
259 | |
---|
260 | owl_fmtext_append_bold(fm, "OWL HELP\n\n"); |
---|
261 | owl_fmtext_append_bold(fm, "NAME\n\n"); |
---|
262 | owl_fmtext_append_normal(fm, OWL_TABSTR); |
---|
263 | owl_fmtext_append_normal(fm, cmd->name); |
---|
264 | |
---|
265 | if (cmd->summary && *cmd->summary) { |
---|
266 | owl_fmtext_append_normal(fm, " - "); |
---|
267 | owl_fmtext_append_normal(fm, cmd->summary); |
---|
268 | } |
---|
269 | owl_fmtext_append_normal(fm, "\n"); |
---|
270 | |
---|
271 | if (cmd->usage && *cmd->usage) { |
---|
272 | s = cmd->usage; |
---|
273 | indent = owl_text_indent(s, OWL_TAB); |
---|
274 | owl_fmtext_append_bold(fm, "\nSYNOPSIS\n"); |
---|
275 | owl_fmtext_append_normal(fm, indent); |
---|
276 | owl_fmtext_append_normal(fm, "\n"); |
---|
277 | g_free(indent); |
---|
278 | } else { |
---|
279 | owl_fmtext_append_bold(fm, "\nSYNOPSIS\n"); |
---|
280 | owl_fmtext_append_normal(fm, OWL_TABSTR); |
---|
281 | owl_fmtext_append_normal(fm, cmd->name); |
---|
282 | owl_fmtext_append_normal(fm, "\n"); |
---|
283 | } |
---|
284 | |
---|
285 | if (cmd->description && *cmd->description) { |
---|
286 | s = cmd->description; |
---|
287 | indent = owl_text_indent(s, OWL_TAB); |
---|
288 | owl_fmtext_append_bold(fm, "\nDESCRIPTION\n"); |
---|
289 | owl_fmtext_append_normal(fm, indent); |
---|
290 | owl_fmtext_append_normal(fm, "\n"); |
---|
291 | g_free(indent); |
---|
292 | } |
---|
293 | |
---|
294 | owl_fmtext_append_normal(fm, "\n\n"); |
---|
295 | } |
---|