source: cmd.c @ 0ccf5ab

release-1.8release-1.9
Last change on this file since 0ccf5ab was 6a71113, checked in by David Benjamin <davidben@mit.edu>, 12 years ago
Use g_strjoinv in owl_cmddict_execute_argv, don't quote correctly This reverts commit e3c8332fa85642544dba1222912b77cf6e32ce8c and uses g_strjoinv instead. The correctly quoted form caused the j keybinding in BarnOwl::Jabber to break. Without it, the world isn't sane this way either: :perl BarnOwl::bindkey(recv => 'C-f C-f' => command => 'recv:next') But a solution to this will probably be far more complex and bear little relation to this commit. In the meantime master really shouldn't have this obnoxious of a regression on it.
  • Property mode set to 100644
File size: 8.3 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) {
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 */
[0a0fb74]25int owl_cmddict_add_from_list(owl_cmddict *cd, const owl_cmd *cmds) {
26  const owl_cmd *cur;
[eb6cedc]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;
[7d4fbcd]31  }
[eb6cedc]32  return ret;
[7d4fbcd]33}
34
[ff7e289]35void owl_cmddict_get_names(const owl_cmddict *d, owl_list *l) {
[7d4fbcd]36  owl_dict_get_keys(d, l);
37}
38
[0a0fb74]39const owl_cmd *owl_cmddict_find(const owl_cmddict *d, const char *name) {
[4d86e06]40  return owl_dict_find_element(d, name);
[7d4fbcd]41}
42
43/* creates a new command alias */
[e19eb97]44int owl_cmddict_add_alias(owl_cmddict *cd, const char *alias_from, const char *alias_to) {
[7d4fbcd]45  owl_cmd *cmd;
[96828e4]46  cmd = g_new(owl_cmd, 1);
[7d4fbcd]47  owl_cmd_create_alias(cmd, alias_from, alias_to);
[1cc9b615]48  owl_perlconfig_new_command(cmd->name);
[b0da027]49  owl_dict_insert_element(cd, cmd->name, cmd, (void (*)(void *))owl_cmd_delete);
[7d4fbcd]50  return(0);
51}
52
[0a0fb74]53int owl_cmddict_add_cmd(owl_cmddict *cd, const owl_cmd * cmd) {
[96828e4]54  owl_cmd * newcmd = g_new(owl_cmd, 1);
[6922edd]55  if(owl_cmd_create_from_template(newcmd, cmd) < 0) {
[ddbbcffa]56    g_free(newcmd);
[6922edd]57    return -1;
58  }
[eb6cedc]59  owl_perlconfig_new_command(cmd->name);
[b0da027]60  return owl_dict_insert_element(cd, newcmd->name, newcmd, (void (*)(void *))owl_cmd_delete);
[6922edd]61}
62
[3f8514b]63char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff) {
[c4ba74d]64  char *retval = NULL;
[0a0fb74]65  const owl_cmd *cmd;
[c4ba74d]66
67  if (!strcmp(argv[0], "")) {
[4d86e06]68  } else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) {
[c4ba74d]69    retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, buff);
[044f19f]70    /* redraw the sepbar; TODO: don't violate layering */
71    owl_global_sepbar_dirty(&g);
[c4ba74d]72  } else {
73    owl_function_makemsg("Unknown command '%s'.", buff);
74  }
75  return retval;
76}
77
[3f8514b]78char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff) {
[7d4fbcd]79  char **argv;
80  int argc;
81  char *retval = NULL;
82
[e56303f]83  argv = owl_parseline(cmdbuff, &argc);
84  if (argv == NULL) {
[7d4fbcd]85    owl_function_makemsg("Unbalanced quotes");
86    return NULL;
87  } 
88 
[62a000b]89  if (argc < 1) {
[e56303f]90    g_strfreev(argv);
[62a000b]91    return NULL;
92  }
[7d4fbcd]93
[c529ac8]94  retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff);
[c4ba74d]95
[e56303f]96  g_strfreev(argv);
[7d4fbcd]97  return retval;
98}
99
[3f8514b]100char *owl_cmddict_execute_argv(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc) {
[6a71113]101  char *buff;
102  char *retval = NULL;
[c4ba74d]103
[6a71113]104  buff = g_strjoinv(" ", (char**)argv);
105  retval = _owl_cmddict_execute(cd, ctx, argv, argc, buff);
106  g_free(buff);
[c4ba74d]107
108  return retval;
109}
110
[7d4fbcd]111/*********************************************************************/
112/***************************** COMMAND *******************************/
113/*********************************************************************/
114
115/* sets up a new command based on a template, copying strings */
[0a0fb74]116int owl_cmd_create_from_template(owl_cmd *cmd, const owl_cmd *templ) {
[7d4fbcd]117  *cmd = *templ;
118  if (!templ->name) return(-1);
[d4927a7]119  cmd->name = g_strdup(templ->name);
120  if (templ->summary)     cmd->summary     = g_strdup(templ->summary);
121  if (templ->usage)       cmd->usage       = g_strdup(templ->usage);
122  if (templ->description) cmd->description = g_strdup(templ->description);
123  if (templ->cmd_aliased_to) cmd->cmd_aliased_to = g_strdup(templ->cmd_aliased_to);
[7d4fbcd]124  return(0);
125}
126
[e19eb97]127int owl_cmd_create_alias(owl_cmd *cmd, const char *name, const char *aliased_to) {
[7d4fbcd]128  memset(cmd, 0, sizeof(owl_cmd));
[d4927a7]129  cmd->name = g_strdup(name);
130  cmd->cmd_aliased_to = g_strdup(aliased_to);
[3472845]131  cmd->summary = g_strdup_printf("%s%s", OWL_CMD_ALIAS_SUMMARY_PREFIX, aliased_to);
[7d4fbcd]132  return(0);
133}
134
[685e96f]135void owl_cmd_cleanup(owl_cmd *cmd)
136{
[ddbbcffa]137  if (cmd->name) g_free(cmd->name);
138  if (cmd->summary) g_free(cmd->summary);
139  if (cmd->usage) g_free(cmd->usage);
140  if (cmd->description) g_free(cmd->description);
141  if (cmd->cmd_aliased_to) g_free(cmd->cmd_aliased_to);
[8f2d9bf]142  if (cmd->cmd_perl) owl_perlconfig_cmd_cleanup(cmd);
[7d4fbcd]143}
144
[b0da027]145void owl_cmd_delete(owl_cmd *cmd)
146{
[685e96f]147  owl_cmd_cleanup(cmd);
[ddbbcffa]148  g_free(cmd);
[b0da027]149}
150
[0a0fb74]151int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) { 
[7d4fbcd]152  if (owl_context_matches(ctx, cmd->validctx)) return 1;
153  else return 0;
154}
155
[0a0fb74]156char *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]157  static int alias_recurse_depth = 0;
158  int ival=0;
[e19eb97]159  const char *cmdbuffargs;
[65b2173]160  char *newcmd, *rv=NULL;
[7d4fbcd]161
162  if (argc < 1) return(NULL);
163
164  /* Recurse if this is an alias */
165  if (cmd->cmd_aliased_to) {
166    if (alias_recurse_depth++ > 50) {
167      owl_function_makemsg("Alias loop detected for '%s'.", cmdbuff);
168    } else {
169      cmdbuffargs = skiptokens(cmdbuff, 1);
[3472845]170      newcmd = g_strdup_printf("%s %s", cmd->cmd_aliased_to, cmdbuffargs);
[7d4fbcd]171      rv = owl_function_command(newcmd);
[ddbbcffa]172      g_free(newcmd);
[7d4fbcd]173    } 
174    alias_recurse_depth--;
175    return rv;
176  }
177
178  /* Do validation and conversions */
179  if (cmd->cmd_ctxargs_fn || cmd->cmd_ctxv_fn || cmd->cmd_ctxi_fn) {
180    if (!owl_cmd_is_context_valid(cmd, ctx)) {
181      owl_function_makemsg("Invalid context for command '%s'.", cmdbuff);
182      return NULL;
183    }
184  }
185
186  if ((argc != 1) && (cmd->cmd_v_fn || cmd->cmd_ctxv_fn)) {
187    owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
188    return NULL;
189  }
190
191  if (cmd->cmd_i_fn || cmd->cmd_ctxi_fn) {
[99525be]192      char *ep;
[7d4fbcd]193      if (argc != 2) {
194        owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
195        return NULL;
196      }
[99525be]197      ival = strtol(argv[1], &ep, 10);
[7d4fbcd]198      if (*ep || ep==argv[1]) {
199        owl_function_makemsg("Invalid argument '%s' for %s command.", argv[1], argv[0]);
200        return(NULL);
201      }
202  }
203
204  if (cmd->cmd_args_fn) {
205    return cmd->cmd_args_fn(argc, argv, cmdbuff);
206  } else if (cmd->cmd_v_fn) {   
207    cmd->cmd_v_fn();
208  } else if (cmd->cmd_i_fn) {
209    cmd->cmd_i_fn(ival);
210  } else if (cmd->cmd_ctxargs_fn) {
211    return cmd->cmd_ctxargs_fn(owl_context_get_data(ctx), argc, argv, cmdbuff);
212  } else if (cmd->cmd_ctxv_fn) {   
213    cmd->cmd_ctxv_fn(owl_context_get_data(ctx));
214  } else if (cmd->cmd_ctxi_fn) {
215    cmd->cmd_ctxi_fn(owl_context_get_data(ctx), ival);
[6922edd]216  } else if (cmd->cmd_perl) {
217    return owl_perlconfig_perlcmd(cmd, argc, argv);
[7d4fbcd]218  }
219
220  return NULL;
221}
222
223/* returns a reference */
[0a0fb74]224const char *owl_cmd_get_summary(const owl_cmd *cmd) {
[7d4fbcd]225  return cmd->summary;
226}
227
228/* returns a summary line describing this keymap.  the caller must free. */
[0a0fb74]229char *owl_cmd_describe(const owl_cmd *cmd) {
[7d4fbcd]230  if (!cmd || !cmd->name || !cmd->summary) return NULL;
[3472845]231  return g_strdup_printf("%-25s - %s", cmd->name, cmd->summary);
[7d4fbcd]232}
233
234
235
[ff7e289]236void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {
[e19eb97]237  const char *s;
[65b2173]238  char *indent;
[7d4fbcd]239  owl_cmd *cmd;
240
241  if (!name || (cmd = owl_dict_find_element(d, name)) == NULL) {
242    owl_fmtext_append_bold(fm, "OWL HELP\n\n");
243    owl_fmtext_append_normal(fm, "No such command...\n");
244    return;
245  }
246
247  owl_fmtext_append_bold(fm, "OWL HELP\n\n");
248  owl_fmtext_append_bold(fm, "NAME\n\n");
249  owl_fmtext_append_normal(fm, OWL_TABSTR);
250  owl_fmtext_append_normal(fm, cmd->name);
251
252  if (cmd->summary && *cmd->summary) {
253    owl_fmtext_append_normal(fm, " - ");
254    owl_fmtext_append_normal(fm, cmd->summary);
255  }
256  owl_fmtext_append_normal(fm, "\n");
257
258  if (cmd->usage && *cmd->usage) {
259    s = cmd->usage;
[4d24650]260    indent = owl_text_indent(s, OWL_TAB);
[7d4fbcd]261    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
262    owl_fmtext_append_normal(fm, indent);
263    owl_fmtext_append_normal(fm, "\n");
[ddbbcffa]264    g_free(indent);
[7d4fbcd]265  } else {
266    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
267    owl_fmtext_append_normal(fm, OWL_TABSTR);
268    owl_fmtext_append_normal(fm, cmd->name);
269    owl_fmtext_append_normal(fm, "\n");
270  }
271
272  if (cmd->description && *cmd->description) {
273    s = cmd->description;
[4d24650]274    indent = owl_text_indent(s, OWL_TAB);
[7d4fbcd]275    owl_fmtext_append_bold(fm, "\nDESCRIPTION\n");
276    owl_fmtext_append_normal(fm, indent);
277    owl_fmtext_append_normal(fm, "\n");
[ddbbcffa]278    g_free(indent);
[7d4fbcd]279  }
280
281  owl_fmtext_append_normal(fm, "\n\n"); 
282}
Note: See TracBrowser for help on using the repository browser.