source: cmd.c @ f907cf0

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