source: cmd.c @ 4c7c21f

release-1.8release-1.9
Last change on this file since 4c7c21f was 4c7c21f, checked in by David Benjamin <davidben@mit.edu>, 10 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
RevLine 
[7d4fbcd]1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include "owl.h"
6
[0a0fb74]7extern const owl_cmd commands_to_init[];
[7d4fbcd]8
9/**************************************************************************/
10/***************************** COMMAND DICT *******************************/
11/**************************************************************************/
12
13int owl_cmddict_setup(owl_cmddict *cd) {
[4c7c21f]14  owl_cmddict_init(cd);
[7d4fbcd]15  if (0 != owl_cmddict_add_from_list(cd, commands_to_init)) return(-1);
16  return(0);
17}
18
[4c7c21f]19void owl_cmddict_init(owl_cmddict *cd) {
20  owl_dict_create(cd);
[7d4fbcd]21}
22
23/* for bulk initialization at startup */
[0a0fb74]24int owl_cmddict_add_from_list(owl_cmddict *cd, const owl_cmd *cmds) {
25  const owl_cmd *cur;
[eb6cedc]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;
[7d4fbcd]30  }
[eb6cedc]31  return ret;
[7d4fbcd]32}
33
[ff7e289]34void owl_cmddict_get_names(const owl_cmddict *d, owl_list *l) {
[7d4fbcd]35  owl_dict_get_keys(d, l);
36}
37
[0a0fb74]38const owl_cmd *owl_cmddict_find(const owl_cmddict *d, const char *name) {
[4d86e06]39  return owl_dict_find_element(d, name);
[7d4fbcd]40}
41
42/* creates a new command alias */
[e19eb97]43int owl_cmddict_add_alias(owl_cmddict *cd, const char *alias_from, const char *alias_to) {
[7d4fbcd]44  owl_cmd *cmd;
[96828e4]45  cmd = g_new(owl_cmd, 1);
[7d4fbcd]46  owl_cmd_create_alias(cmd, alias_from, alias_to);
[1cc9b615]47  owl_perlconfig_new_command(cmd->name);
[b0da027]48  owl_dict_insert_element(cd, cmd->name, cmd, (void (*)(void *))owl_cmd_delete);
[7d4fbcd]49  return(0);
50}
51
[0a0fb74]52int owl_cmddict_add_cmd(owl_cmddict *cd, const owl_cmd * cmd) {
[96828e4]53  owl_cmd * newcmd = g_new(owl_cmd, 1);
[6922edd]54  if(owl_cmd_create_from_template(newcmd, cmd) < 0) {
[ddbbcffa]55    g_free(newcmd);
[6922edd]56    return -1;
57  }
[eb6cedc]58  owl_perlconfig_new_command(cmd->name);
[b0da027]59  return owl_dict_insert_element(cd, newcmd->name, newcmd, (void (*)(void *))owl_cmd_delete);
[6922edd]60}
61
[3f8514b]62char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff) {
[c4ba74d]63  char *retval = NULL;
[0a0fb74]64  const owl_cmd *cmd;
[c4ba74d]65
66  if (!strcmp(argv[0], "")) {
[4d86e06]67  } else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) {
[c4ba74d]68    retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, buff);
[044f19f]69    /* redraw the sepbar; TODO: don't violate layering */
70    owl_global_sepbar_dirty(&g);
[c4ba74d]71  } else {
72    owl_function_makemsg("Unknown command '%s'.", buff);
73  }
74  return retval;
75}
76
[3f8514b]77char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff) {
[7d4fbcd]78  char **argv;
79  int argc;
80  char *retval = NULL;
81
[e56303f]82  argv = owl_parseline(cmdbuff, &argc);
83  if (argv == NULL) {
[7d4fbcd]84    owl_function_makemsg("Unbalanced quotes");
85    return NULL;
86  } 
87 
[62a000b]88  if (argc < 1) {
[e56303f]89    g_strfreev(argv);
[62a000b]90    return NULL;
91  }
[7d4fbcd]92
[c529ac8]93  retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff);
[c4ba74d]94
[e56303f]95  g_strfreev(argv);
[7d4fbcd]96  return retval;
97}
98
[3f8514b]99char *owl_cmddict_execute_argv(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc) {
[6a71113]100  char *buff;
101  char *retval = NULL;
[c4ba74d]102
[6a71113]103  buff = g_strjoinv(" ", (char**)argv);
104  retval = _owl_cmddict_execute(cd, ctx, argv, argc, buff);
105  g_free(buff);
[c4ba74d]106
107  return retval;
108}
109
[7d4fbcd]110/*********************************************************************/
111/***************************** COMMAND *******************************/
112/*********************************************************************/
113
114/* sets up a new command based on a template, copying strings */
[0a0fb74]115int owl_cmd_create_from_template(owl_cmd *cmd, const owl_cmd *templ) {
[7d4fbcd]116  *cmd = *templ;
117  if (!templ->name) return(-1);
[d4927a7]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);
[7d4fbcd]123  return(0);
124}
125
[e19eb97]126int owl_cmd_create_alias(owl_cmd *cmd, const char *name, const char *aliased_to) {
[7d4fbcd]127  memset(cmd, 0, sizeof(owl_cmd));
[d4927a7]128  cmd->name = g_strdup(name);
129  cmd->cmd_aliased_to = g_strdup(aliased_to);
[3472845]130  cmd->summary = g_strdup_printf("%s%s", OWL_CMD_ALIAS_SUMMARY_PREFIX, aliased_to);
[7d4fbcd]131  return(0);
132}
133
[685e96f]134void owl_cmd_cleanup(owl_cmd *cmd)
135{
[3b8a563]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);
[8f2d9bf]141  if (cmd->cmd_perl) owl_perlconfig_cmd_cleanup(cmd);
[7d4fbcd]142}
143
[b0da027]144void owl_cmd_delete(owl_cmd *cmd)
145{
[685e96f]146  owl_cmd_cleanup(cmd);
[ddbbcffa]147  g_free(cmd);
[b0da027]148}
149
[0a0fb74]150int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) { 
[7d4fbcd]151  if (owl_context_matches(ctx, cmd->validctx)) return 1;
152  else return 0;
153}
154
[0a0fb74]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) {
[7d4fbcd]156  static int alias_recurse_depth = 0;
157  int ival=0;
[e19eb97]158  const char *cmdbuffargs;
[65b2173]159  char *newcmd, *rv=NULL;
[7d4fbcd]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);
[3472845]169      newcmd = g_strdup_printf("%s %s", cmd->cmd_aliased_to, cmdbuffargs);
[7d4fbcd]170      rv = owl_function_command(newcmd);
[ddbbcffa]171      g_free(newcmd);
[7d4fbcd]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) {
[99525be]191      char *ep;
[7d4fbcd]192      if (argc != 2) {
193        owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
194        return NULL;
195      }
[99525be]196      ival = strtol(argv[1], &ep, 10);
[7d4fbcd]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);
[6922edd]215  } else if (cmd->cmd_perl) {
216    return owl_perlconfig_perlcmd(cmd, argc, argv);
[7d4fbcd]217  }
218
219  return NULL;
220}
221
222/* returns a reference */
[0a0fb74]223const char *owl_cmd_get_summary(const owl_cmd *cmd) {
[7d4fbcd]224  return cmd->summary;
225}
226
227/* returns a summary line describing this keymap.  the caller must free. */
[0a0fb74]228char *owl_cmd_describe(const owl_cmd *cmd) {
[7d4fbcd]229  if (!cmd || !cmd->name || !cmd->summary) return NULL;
[3472845]230  return g_strdup_printf("%-25s - %s", cmd->name, cmd->summary);
[7d4fbcd]231}
232
233
234
[ff7e289]235void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {
[e19eb97]236  const char *s;
[65b2173]237  char *indent;
[7d4fbcd]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;
[4d24650]259    indent = owl_text_indent(s, OWL_TAB);
[7d4fbcd]260    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
261    owl_fmtext_append_normal(fm, indent);
262    owl_fmtext_append_normal(fm, "\n");
[ddbbcffa]263    g_free(indent);
[7d4fbcd]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;
[4d24650]273    indent = owl_text_indent(s, OWL_TAB);
[7d4fbcd]274    owl_fmtext_append_bold(fm, "\nDESCRIPTION\n");
275    owl_fmtext_append_normal(fm, indent);
276    owl_fmtext_append_normal(fm, "\n");
[ddbbcffa]277    g_free(indent);
[7d4fbcd]278  }
279
280  owl_fmtext_append_normal(fm, "\n\n"); 
281}
Note: See TracBrowser for help on using the repository browser.