source: cmd.c @ 0e98d87

release-1.7release-1.8release-1.9
Last change on this file since 0e98d87 was 0e98d87, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
Remove a pointless instance of sepbar_dirty
  • Property mode set to 100644
File size: 8.9 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include "owl.h"
6
7extern const owl_cmd commands_to_init[];
8
9/**************************************************************************/
10/***************************** COMMAND DICT *******************************/
11/**************************************************************************/
12
13int 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
19int 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 */
25int 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 */
36void owl_cmddict_get_names(const owl_cmddict *d, owl_list *l) {
37  owl_dict_get_keys(d, l);
38}
39
40const owl_cmd *owl_cmddict_find(const owl_cmddict *d, const char *name) {
41  return owl_dict_find_element(d, name);
42}
43
44void owl_cmddict_namelist_cleanup(owl_list *l)
45{
46  owl_list_cleanup(l, owl_free);
47}
48
49/* creates a new command alias */
50int owl_cmddict_add_alias(owl_cmddict *cd, const char *alias_from, const char *alias_to) {
51  owl_cmd *cmd;
52  cmd = owl_malloc(sizeof(owl_cmd));
53  owl_cmd_create_alias(cmd, alias_from, alias_to);
54  owl_dict_insert_element(cd, cmd->name, cmd, (void (*)(void *))owl_cmd_delete);
55  return(0);
56}
57
58int owl_cmddict_add_cmd(owl_cmddict *cd, const owl_cmd * cmd) {
59  owl_cmd * newcmd = owl_malloc(sizeof(owl_cmd));
60  if(owl_cmd_create_from_template(newcmd, cmd) < 0) {
61    owl_free(newcmd);
62    return -1;
63  }
64  owl_perlconfig_new_command(cmd->name);
65  return owl_dict_insert_element(cd, newcmd->name, newcmd, (void (*)(void *))owl_cmd_delete);
66}
67
68char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff) {
69  char *retval = NULL;
70  const owl_cmd *cmd;
71
72  if (!strcmp(argv[0], "")) {
73  } else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) {
74    retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, buff);
75  } else {
76    owl_function_makemsg("Unknown command '%s'.", buff);
77  }
78  return retval;
79}
80
81char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff) {
82  char **argv;
83  int argc;
84  char *tmpbuff;
85  char *retval = NULL;
86
87  tmpbuff=owl_strdup(cmdbuff);
88  argv=owl_parseline(tmpbuff, &argc);
89  if (argc < 0) {
90    owl_free(tmpbuff);
91    owl_function_makemsg("Unbalanced quotes");
92    return NULL;
93  } 
94 
95  if (argc < 1) {
96    owl_parse_delete(argv, argc);
97    owl_free(tmpbuff);
98    return NULL;
99  }
100
101  retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff);
102
103  owl_parse_delete(argv, argc);
104  owl_free(tmpbuff);
105  sepbar_dirty();
106  return retval;
107}
108
109char *owl_cmddict_execute_argv(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc) {
110  char *buff, *ptr;
111  int len = 0, i;
112  char *retval = NULL;
113
114  for(i = 0; i < argc; i++) {
115    len += strlen(argv[i]) + 1;
116  }
117
118  ptr = buff = owl_malloc(len);
119
120  for(i = 0; i < argc; i++) {
121    strcpy(ptr, argv[i]);
122    ptr += strlen(argv[i]);
123    *(ptr++) = ' ';
124  }
125  *(ptr - 1) = 0;
126
127  retval = _owl_cmddict_execute(cd, ctx, argv, argc, buff);
128
129  owl_free(buff);
130  return retval;
131}
132
133/*********************************************************************/
134/***************************** COMMAND *******************************/
135/*********************************************************************/
136
137/* sets up a new command based on a template, copying strings */
138int owl_cmd_create_from_template(owl_cmd *cmd, const owl_cmd *templ) {
139  *cmd = *templ;
140  if (!templ->name) return(-1);
141  cmd->name = owl_strdup(templ->name);
142  if (templ->summary)     cmd->summary     = owl_strdup(templ->summary);
143  if (templ->usage)       cmd->usage       = owl_strdup(templ->usage);
144  if (templ->description) cmd->description = owl_strdup(templ->description);
145  if (templ->cmd_aliased_to) cmd->cmd_aliased_to = owl_strdup(templ->cmd_aliased_to);
146  return(0);
147}
148
149int owl_cmd_create_alias(owl_cmd *cmd, const char *name, const char *aliased_to) {
150  memset(cmd, 0, sizeof(owl_cmd));
151  cmd->name = owl_strdup(name);
152  cmd->cmd_aliased_to = owl_strdup(aliased_to);
153  cmd->summary = owl_sprintf("%s%s", OWL_CMD_ALIAS_SUMMARY_PREFIX, aliased_to);
154  return(0);
155}
156
157void owl_cmd_cleanup(owl_cmd *cmd)
158{
159  if (cmd->name) owl_free(cmd->name);
160  if (cmd->summary) owl_free(cmd->summary);
161  if (cmd->usage) owl_free(cmd->usage);
162  if (cmd->description) owl_free(cmd->description);
163  if (cmd->cmd_aliased_to) owl_free(cmd->cmd_aliased_to);
164  if (cmd->cmd_perl) owl_perlconfig_cmd_cleanup(cmd);
165}
166
167void owl_cmd_delete(owl_cmd *cmd)
168{
169  owl_cmd_cleanup(cmd);
170  owl_free(cmd);
171}
172
173int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) { 
174  if (owl_context_matches(ctx, cmd->validctx)) return 1;
175  else return 0;
176}
177
178char *owl_cmd_execute(const owl_cmd *cmd, const owl_cmddict *cd, const owl_context *ctx, int argc, const char *const *argv, const char *cmdbuff) {
179  static int alias_recurse_depth = 0;
180  int ival=0;
181  const char *cmdbuffargs;
182  char *newcmd, *rv=NULL;
183
184  if (argc < 1) return(NULL);
185
186  /* Recurse if this is an alias */
187  if (cmd->cmd_aliased_to) {
188    if (alias_recurse_depth++ > 50) {
189      owl_function_makemsg("Alias loop detected for '%s'.", cmdbuff);
190    } else {
191      cmdbuffargs = skiptokens(cmdbuff, 1);
192      newcmd = owl_sprintf("%s %s", cmd->cmd_aliased_to, cmdbuffargs);
193      rv = owl_function_command(newcmd);
194      owl_free(newcmd);
195    } 
196    alias_recurse_depth--;
197    return rv;
198  }
199
200  /* Do validation and conversions */
201  if (cmd->cmd_ctxargs_fn || cmd->cmd_ctxv_fn || cmd->cmd_ctxi_fn) {
202    if (!owl_cmd_is_context_valid(cmd, ctx)) {
203      owl_function_makemsg("Invalid context for command '%s'.", cmdbuff);
204      return NULL;
205    }
206  }
207
208  if ((argc != 1) && (cmd->cmd_v_fn || cmd->cmd_ctxv_fn)) {
209    owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
210    return NULL;
211  }
212
213  if (cmd->cmd_i_fn || cmd->cmd_ctxi_fn) {
214      char *ep;
215      if (argc != 2) {
216        owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
217        return NULL;
218      }
219      ival = strtol(argv[1], &ep, 10);
220      if (*ep || ep==argv[1]) {
221        owl_function_makemsg("Invalid argument '%s' for %s command.", argv[1], argv[0]);
222        return(NULL);
223      }
224  }
225
226  if (cmd->cmd_args_fn) {
227    return cmd->cmd_args_fn(argc, argv, cmdbuff);
228  } else if (cmd->cmd_v_fn) {   
229    cmd->cmd_v_fn();
230  } else if (cmd->cmd_i_fn) {
231    cmd->cmd_i_fn(ival);
232  } else if (cmd->cmd_ctxargs_fn) {
233    return cmd->cmd_ctxargs_fn(owl_context_get_data(ctx), argc, argv, cmdbuff);
234  } else if (cmd->cmd_ctxv_fn) {   
235    cmd->cmd_ctxv_fn(owl_context_get_data(ctx));
236  } else if (cmd->cmd_ctxi_fn) {
237    cmd->cmd_ctxi_fn(owl_context_get_data(ctx), ival);
238  } else if (cmd->cmd_perl) {
239    return owl_perlconfig_perlcmd(cmd, argc, argv);
240  }
241
242  return NULL;
243}
244
245/* returns a reference */
246const char *owl_cmd_get_summary(const owl_cmd *cmd) {
247  return cmd->summary;
248}
249
250/* returns a summary line describing this keymap.  the caller must free. */
251char *owl_cmd_describe(const owl_cmd *cmd) {
252  if (!cmd || !cmd->name || !cmd->summary) return NULL;
253  return owl_sprintf("%-25s - %s", cmd->name, cmd->summary);
254}
255
256
257
258void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {
259  const char *s;
260  char *indent;
261  owl_cmd *cmd;
262
263  if (!name || (cmd = owl_dict_find_element(d, name)) == NULL) {
264    owl_fmtext_append_bold(fm, "OWL HELP\n\n");
265    owl_fmtext_append_normal(fm, "No such command...\n");
266    return;
267  }
268
269  owl_fmtext_append_bold(fm, "OWL HELP\n\n");
270  owl_fmtext_append_bold(fm, "NAME\n\n");
271  owl_fmtext_append_normal(fm, OWL_TABSTR);
272  owl_fmtext_append_normal(fm, cmd->name);
273
274  if (cmd->summary && *cmd->summary) {
275    owl_fmtext_append_normal(fm, " - ");
276    owl_fmtext_append_normal(fm, cmd->summary);
277  }
278  owl_fmtext_append_normal(fm, "\n");
279
280  if (cmd->usage && *cmd->usage) {
281    s = cmd->usage;
282    indent = owl_malloc(strlen(s)+(owl_text_num_lines(s)+3)*OWL_TAB+1);
283    owl_text_indent(indent, s, OWL_TAB);
284    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
285    owl_fmtext_append_normal(fm, indent);
286    owl_fmtext_append_normal(fm, "\n");
287    owl_free(indent);
288  } else {
289    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
290    owl_fmtext_append_normal(fm, OWL_TABSTR);
291    owl_fmtext_append_normal(fm, cmd->name);
292    owl_fmtext_append_normal(fm, "\n");
293  }
294
295  if (cmd->description && *cmd->description) {
296    s = cmd->description;
297    indent = owl_malloc(strlen(s)+(owl_text_num_lines(s)+3)*OWL_TAB+1);
298    owl_text_indent(indent, s, OWL_TAB);
299    owl_fmtext_append_bold(fm, "\nDESCRIPTION\n");
300    owl_fmtext_append_normal(fm, indent);
301    owl_fmtext_append_normal(fm, "\n");
302    owl_free(indent);
303  }
304
305  owl_fmtext_append_normal(fm, "\n\n"); 
306}
Note: See TracBrowser for help on using the repository browser.