source: cmd.c @ 851a0e0

release-1.10release-1.8release-1.9
Last change on this file since 851a0e0 was 6a71113, checked in by David Benjamin <davidben@mit.edu>, 14 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
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
35void owl_cmddict_get_names(const owl_cmddict *d, owl_list *l) {
36  owl_dict_get_keys(d, l);
37}
38
39const owl_cmd *owl_cmddict_find(const owl_cmddict *d, const char *name) {
40  return owl_dict_find_element(d, name);
41}
42
43/* creates a new command alias */
44int owl_cmddict_add_alias(owl_cmddict *cd, const char *alias_from, const char *alias_to) {
45  owl_cmd *cmd;
46  cmd = g_new(owl_cmd, 1);
47  owl_cmd_create_alias(cmd, alias_from, alias_to);
48  owl_perlconfig_new_command(cmd->name);
49  owl_dict_insert_element(cd, cmd->name, cmd, (void (*)(void *))owl_cmd_delete);
50  return(0);
51}
52
53int owl_cmddict_add_cmd(owl_cmddict *cd, const owl_cmd * cmd) {
54  owl_cmd * newcmd = g_new(owl_cmd, 1);
55  if(owl_cmd_create_from_template(newcmd, cmd) < 0) {
56    g_free(newcmd);
57    return -1;
58  }
59  owl_perlconfig_new_command(cmd->name);
60  return owl_dict_insert_element(cd, newcmd->name, newcmd, (void (*)(void *))owl_cmd_delete);
61}
62
63char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff) {
64  char *retval = NULL;
65  const owl_cmd *cmd;
66
67  if (!strcmp(argv[0], "")) {
68  } else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) {
69    retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, buff);
70    /* redraw the sepbar; TODO: don't violate layering */
71    owl_global_sepbar_dirty(&g);
72  } else {
73    owl_function_makemsg("Unknown command '%s'.", buff);
74  }
75  return retval;
76}
77
78char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff) {
79  char **argv;
80  int argc;
81  char *retval = NULL;
82
83  argv = owl_parseline(cmdbuff, &argc);
84  if (argv == NULL) {
85    owl_function_makemsg("Unbalanced quotes");
86    return NULL;
87  } 
88 
89  if (argc < 1) {
90    g_strfreev(argv);
91    return NULL;
92  }
93
94  retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff);
95
96  g_strfreev(argv);
97  return retval;
98}
99
100char *owl_cmddict_execute_argv(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc) {
101  char *buff;
102  char *retval = NULL;
103
104  buff = g_strjoinv(" ", (char**)argv);
105  retval = _owl_cmddict_execute(cd, ctx, argv, argc, buff);
106  g_free(buff);
107
108  return retval;
109}
110
111/*********************************************************************/
112/***************************** COMMAND *******************************/
113/*********************************************************************/
114
115/* sets up a new command based on a template, copying strings */
116int owl_cmd_create_from_template(owl_cmd *cmd, const owl_cmd *templ) {
117  *cmd = *templ;
118  if (!templ->name) return(-1);
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);
124  return(0);
125}
126
127int owl_cmd_create_alias(owl_cmd *cmd, const char *name, const char *aliased_to) {
128  memset(cmd, 0, sizeof(owl_cmd));
129  cmd->name = g_strdup(name);
130  cmd->cmd_aliased_to = g_strdup(aliased_to);
131  cmd->summary = g_strdup_printf("%s%s", OWL_CMD_ALIAS_SUMMARY_PREFIX, aliased_to);
132  return(0);
133}
134
135void owl_cmd_cleanup(owl_cmd *cmd)
136{
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);
142  if (cmd->cmd_perl) owl_perlconfig_cmd_cleanup(cmd);
143}
144
145void owl_cmd_delete(owl_cmd *cmd)
146{
147  owl_cmd_cleanup(cmd);
148  g_free(cmd);
149}
150
151int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) { 
152  if (owl_context_matches(ctx, cmd->validctx)) return 1;
153  else return 0;
154}
155
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) {
157  static int alias_recurse_depth = 0;
158  int ival=0;
159  const char *cmdbuffargs;
160  char *newcmd, *rv=NULL;
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);
170      newcmd = g_strdup_printf("%s %s", cmd->cmd_aliased_to, cmdbuffargs);
171      rv = owl_function_command(newcmd);
172      g_free(newcmd);
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) {
192      char *ep;
193      if (argc != 2) {
194        owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
195        return NULL;
196      }
197      ival = strtol(argv[1], &ep, 10);
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);
216  } else if (cmd->cmd_perl) {
217    return owl_perlconfig_perlcmd(cmd, argc, argv);
218  }
219
220  return NULL;
221}
222
223/* returns a reference */
224const char *owl_cmd_get_summary(const owl_cmd *cmd) {
225  return cmd->summary;
226}
227
228/* returns a summary line describing this keymap.  the caller must free. */
229char *owl_cmd_describe(const owl_cmd *cmd) {
230  if (!cmd || !cmd->name || !cmd->summary) return NULL;
231  return g_strdup_printf("%-25s - %s", cmd->name, cmd->summary);
232}
233
234
235
236void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {
237  const char *s;
238  char *indent;
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;
260    indent = owl_text_indent(s, OWL_TAB);
261    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
262    owl_fmtext_append_normal(fm, indent);
263    owl_fmtext_append_normal(fm, "\n");
264    g_free(indent);
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;
274    indent = owl_text_indent(s, OWL_TAB);
275    owl_fmtext_append_bold(fm, "\nDESCRIPTION\n");
276    owl_fmtext_append_normal(fm, indent);
277    owl_fmtext_append_normal(fm, "\n");
278    g_free(indent);
279  }
280
281  owl_fmtext_append_normal(fm, "\n\n"); 
282}
Note: See TracBrowser for help on using the repository browser.