source: cmd.c @ 4c7c21f

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