source: cmd.c @ 0982901

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