source: cmd.c @ 04166e9

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 04166e9 was 6922edd, checked in by Nelson Elhage <nelhage@mit.edu>, 18 years ago
Adding the ability to install real commands from perl.
  • 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
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 *cmdbuff) {
70  char **argv;
71  int argc;
72  char *tmpbuff;
73  char *retval = NULL;
74  owl_cmd *cmd;
75
76  tmpbuff=strdup(cmdbuff);
77  argv=owl_parseline(tmpbuff, &argc);
78  if (argc < 0) {
79    owl_free(tmpbuff);
80    sepbar(NULL);
81    owl_function_makemsg("Unbalanced quotes");
82    return NULL;
83  } 
84 
85  if (argc < 1) return(NULL);
86
87  if (!strcmp(argv[0], "")) {
88  } else if (NULL != (cmd = (owl_cmd*)owl_dict_find_element(cd, argv[0]))) {
89    retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, cmdbuff);
90  } else {
91    owl_function_makemsg("Unknown command '%s'.", cmdbuff);
92  }
93  owl_parsefree(argv, argc);
94  owl_free(tmpbuff);
95  sepbar(NULL);
96  return retval;
97}
98
99/*********************************************************************/
100/***************************** COMMAND *******************************/
101/*********************************************************************/
102
103/* sets up a new command based on a template, copying strings */
104int owl_cmd_create_from_template(owl_cmd *cmd, owl_cmd *templ) {
105  *cmd = *templ;
106  if (!templ->name) return(-1);
107  cmd->name = owl_strdup(templ->name);
108  if (templ->summary)     cmd->summary     = owl_strdup(templ->summary);
109  if (templ->usage)       cmd->usage       = owl_strdup(templ->usage);
110  if (templ->description) cmd->description = owl_strdup(templ->description);
111  if (templ->cmd_aliased_to) cmd->cmd_aliased_to = owl_strdup(templ->cmd_aliased_to);
112  return(0);
113}
114
115int owl_cmd_create_alias(owl_cmd *cmd, char *name, char *aliased_to) {
116  memset(cmd, 0, sizeof(owl_cmd));
117  cmd->name = owl_strdup(name);
118  cmd->cmd_aliased_to = owl_strdup(aliased_to);
119  cmd->summary = owl_malloc(strlen(aliased_to)+strlen(OWL_CMD_ALIAS_SUMMARY_PREFIX)+2);
120  strcpy(cmd->summary, OWL_CMD_ALIAS_SUMMARY_PREFIX);
121  strcat(cmd->summary, aliased_to);
122  return(0);
123}
124
125void owl_cmd_free(owl_cmd *cmd) {
126  if (cmd->name) owl_free(cmd->name);
127  if (cmd->summary) owl_free(cmd->summary);
128  if (cmd->usage) owl_free(cmd->usage);
129  if (cmd->description) owl_free(cmd->description);
130  if (cmd->cmd_aliased_to) owl_free(cmd->cmd_aliased_to);
131  if (cmd->cmd_perl) owl_perlconfig_cmd_free(cmd);
132}
133
134int owl_cmd_is_context_valid(owl_cmd *cmd, owl_context *ctx) { 
135  if (owl_context_matches(ctx, cmd->validctx)) return 1;
136  else return 0;
137}
138
139char *owl_cmd_execute(owl_cmd *cmd, owl_cmddict *cd, owl_context *ctx, int argc, char **argv, char *cmdbuff) {
140  static int alias_recurse_depth = 0;
141  int ival=0;
142  char *cmdbuffargs, *newcmd, *rv=NULL;
143
144  if (argc < 1) return(NULL);
145
146  /* Recurse if this is an alias */
147  if (cmd->cmd_aliased_to) {
148    if (alias_recurse_depth++ > 50) {
149      owl_function_makemsg("Alias loop detected for '%s'.", cmdbuff);
150    } else {
151      cmdbuffargs = skiptokens(cmdbuff, 1);
152      newcmd = owl_malloc(strlen(cmd->cmd_aliased_to)+strlen(cmdbuffargs)+2);
153      strcpy(newcmd, cmd->cmd_aliased_to);
154      strcat(newcmd, " ");
155      strcat(newcmd, cmdbuffargs);
156      rv = owl_function_command(newcmd);
157      owl_free(newcmd);
158    } 
159    alias_recurse_depth--;
160    return rv;
161  }
162
163  /* Do validation and conversions */
164  if (cmd->cmd_ctxargs_fn || cmd->cmd_ctxv_fn || cmd->cmd_ctxi_fn) {
165    if (!owl_cmd_is_context_valid(cmd, ctx)) {
166      owl_function_makemsg("Invalid context for command '%s'.", cmdbuff);
167      return NULL;
168    }
169  }
170
171  if ((argc != 1) && (cmd->cmd_v_fn || cmd->cmd_ctxv_fn)) {
172    owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
173    return NULL;
174  }
175
176  if (cmd->cmd_i_fn || cmd->cmd_ctxi_fn) {
177      char *ep = "x";
178      if (argc != 2) {
179        owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
180        return NULL;
181      }
182      ival = strtol(argv[1], &ep, 10);
183      if (*ep || ep==argv[1]) {
184        owl_function_makemsg("Invalid argument '%s' for %s command.", argv[1], argv[0]);
185        return(NULL);
186      }
187  }
188
189  if (cmd->cmd_args_fn) {
190    return cmd->cmd_args_fn(argc, argv, cmdbuff);
191  } else if (cmd->cmd_v_fn) {   
192    cmd->cmd_v_fn();
193  } else if (cmd->cmd_i_fn) {
194    cmd->cmd_i_fn(ival);
195  } else if (cmd->cmd_ctxargs_fn) {
196    return cmd->cmd_ctxargs_fn(owl_context_get_data(ctx), argc, argv, cmdbuff);
197  } else if (cmd->cmd_ctxv_fn) {   
198    cmd->cmd_ctxv_fn(owl_context_get_data(ctx));
199  } else if (cmd->cmd_ctxi_fn) {
200    cmd->cmd_ctxi_fn(owl_context_get_data(ctx), ival);
201  } else if (cmd->cmd_perl) {
202    return owl_perlconfig_perlcmd(cmd, argc, argv);
203  }
204
205  return NULL;
206}
207
208/* returns a reference */
209char *owl_cmd_get_summary(owl_cmd *cmd) {
210  return cmd->summary;
211}
212
213/* returns a summary line describing this keymap.  the caller must free. */
214char *owl_cmd_describe(owl_cmd *cmd) {
215  char *s;
216  int slen;
217  if (!cmd || !cmd->name || !cmd->summary) return NULL;
218  slen = strlen(cmd->name)+strlen(cmd->summary)+30;
219  s = owl_malloc(slen);
220  snprintf(s, slen-1, "%-25s - %s", cmd->name, cmd->summary);
221  return s;
222}
223
224
225
226void owl_cmd_get_help(owl_cmddict *d, char *name, owl_fmtext *fm) {
227  char *indent, *s;
228  owl_cmd *cmd;
229
230  if (!name || (cmd = owl_dict_find_element(d, name)) == NULL) {
231    owl_fmtext_append_bold(fm, "OWL HELP\n\n");
232    owl_fmtext_append_normal(fm, "No such command...\n");
233    return;
234  }
235
236  owl_fmtext_append_bold(fm, "OWL HELP\n\n");
237  owl_fmtext_append_bold(fm, "NAME\n\n");
238  owl_fmtext_append_normal(fm, OWL_TABSTR);
239  owl_fmtext_append_normal(fm, cmd->name);
240
241  if (cmd->summary && *cmd->summary) {
242    owl_fmtext_append_normal(fm, " - ");
243    owl_fmtext_append_normal(fm, cmd->summary);
244  }
245  owl_fmtext_append_normal(fm, "\n");
246
247  if (cmd->usage && *cmd->usage) {
248    s = cmd->usage;
249    indent = owl_malloc(strlen(s)+(owl_text_num_lines(s)+3)*OWL_TAB+1);
250    owl_text_indent(indent, s, OWL_TAB);
251    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
252    owl_fmtext_append_normal(fm, indent);
253    owl_fmtext_append_normal(fm, "\n");
254    owl_free(indent);
255  } else {
256    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
257    owl_fmtext_append_normal(fm, OWL_TABSTR);
258    owl_fmtext_append_normal(fm, cmd->name);
259    owl_fmtext_append_normal(fm, "\n");
260  }
261
262  if (cmd->description && *cmd->description) {
263    s = cmd->description;
264    indent = owl_malloc(strlen(s)+(owl_text_num_lines(s)+3)*OWL_TAB+1);
265    owl_text_indent(indent, s, OWL_TAB);
266    owl_fmtext_append_bold(fm, "\nDESCRIPTION\n");
267    owl_fmtext_append_normal(fm, indent);
268    owl_fmtext_append_normal(fm, "\n");
269    owl_free(indent);
270  }
271
272  owl_fmtext_append_normal(fm, "\n\n"); 
273}
Note: See TracBrowser for help on using the repository browser.