source: cmd.c @ 40d22cf

release-1.10release-1.6release-1.7release-1.8release-1.9
Last change on this file since 40d22cf was 40d22cf, checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
Rename owl_parsefree to owl_parse_delete. Signed-off-by: Anders Kaseorg <andersk@mit.edu> Reviewed-by: Nelson Elhage <nelhage@mit.edu>
  • Property mode set to 100644
File size: 8.9 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
35/* free the list with owl_cmddict_namelist_free */
[ff7e289]36void owl_cmddict_get_names(const owl_cmddict *d, owl_list *l) {
[7d4fbcd]37  owl_dict_get_keys(d, l);
38}
39
[0a0fb74]40const owl_cmd *owl_cmddict_find(const owl_cmddict *d, const char *name) {
[4d86e06]41  return owl_dict_find_element(d, name);
[7d4fbcd]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 */
[e19eb97]49int owl_cmddict_add_alias(owl_cmddict *cd, const char *alias_from, const char *alias_to) {
[7d4fbcd]50  owl_cmd *cmd;
51  cmd = owl_malloc(sizeof(owl_cmd));
52  owl_cmd_create_alias(cmd, alias_from, alias_to);
[b0da027]53  owl_dict_insert_element(cd, cmd->name, cmd, (void (*)(void *))owl_cmd_delete);
[7d4fbcd]54  return(0);
55}
56
[0a0fb74]57int owl_cmddict_add_cmd(owl_cmddict *cd, const owl_cmd * cmd) {
[6922edd]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  }
[eb6cedc]63  owl_perlconfig_new_command(cmd->name);
[b0da027]64  return owl_dict_insert_element(cd, newcmd->name, newcmd, (void (*)(void *))owl_cmd_delete);
[6922edd]65}
66
[3f8514b]67char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff) {
[c4ba74d]68  char *retval = NULL;
[0a0fb74]69  const owl_cmd *cmd;
[c4ba74d]70
71  if (!strcmp(argv[0], "")) {
[4d86e06]72  } else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) {
[c4ba74d]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
[3f8514b]80char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff) {
[7d4fbcd]81  char **argv;
82  int argc;
83  char *tmpbuff;
84  char *retval = NULL;
85
[34509d5]86  tmpbuff=owl_strdup(cmdbuff);
[7d4fbcd]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 
[62a000b]95  if (argc < 1) {
[40d22cf]96    owl_parse_delete(argv, argc);
[62a000b]97    owl_free(tmpbuff);
98    return NULL;
99  }
[7d4fbcd]100
[c529ac8]101  retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff);
[c4ba74d]102
[40d22cf]103  owl_parse_delete(argv, argc);
[7d4fbcd]104  owl_free(tmpbuff);
105  sepbar(NULL);
106  return retval;
107}
108
[3f8514b]109char *owl_cmddict_execute_argv(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc) {
[c4ba74d]110  char *buff, *ptr;
111  int len = 0, i;
112  char *retval = NULL;
113
114  for(i = 0; i < argc; i++) {
115    len += strlen(argv[i]) + 1;
116  }
117
118  ptr = buff = owl_malloc(len);
119
120  for(i = 0; i < argc; i++) {
121    strcpy(ptr, argv[i]);
122    ptr += strlen(argv[i]);
123    *(ptr++) = ' ';
124  }
125  *(ptr - 1) = 0;
126
127  retval = _owl_cmddict_execute(cd, ctx, argv, argc, buff);
128
129  owl_free(buff);
130  return retval;
131}
132
[7d4fbcd]133/*********************************************************************/
134/***************************** COMMAND *******************************/
135/*********************************************************************/
136
137/* sets up a new command based on a template, copying strings */
[0a0fb74]138int owl_cmd_create_from_template(owl_cmd *cmd, const owl_cmd *templ) {
[7d4fbcd]139  *cmd = *templ;
140  if (!templ->name) return(-1);
141  cmd->name = owl_strdup(templ->name);
[f1e629d]142  if (templ->summary)     cmd->summary     = owl_strdup(templ->summary);
143  if (templ->usage)       cmd->usage       = owl_strdup(templ->usage);
144  if (templ->description) cmd->description = owl_strdup(templ->description);
145  if (templ->cmd_aliased_to) cmd->cmd_aliased_to = owl_strdup(templ->cmd_aliased_to);
[7d4fbcd]146  return(0);
147}
148
[e19eb97]149int owl_cmd_create_alias(owl_cmd *cmd, const char *name, const char *aliased_to) {
[7d4fbcd]150  memset(cmd, 0, sizeof(owl_cmd));
151  cmd->name = owl_strdup(name);
152  cmd->cmd_aliased_to = owl_strdup(aliased_to);
[36486be]153  cmd->summary = owl_sprintf("%s%s", OWL_CMD_ALIAS_SUMMARY_PREFIX, aliased_to);
[7d4fbcd]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);
[6922edd]162  if (cmd->cmd_aliased_to) owl_free(cmd->cmd_aliased_to);
163  if (cmd->cmd_perl) owl_perlconfig_cmd_free(cmd);
[7d4fbcd]164}
165
[b0da027]166void owl_cmd_delete(owl_cmd *cmd)
167{
168  owl_cmd_free(cmd);
169  owl_free(cmd);
170}
171
[0a0fb74]172int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) { 
[7d4fbcd]173  if (owl_context_matches(ctx, cmd->validctx)) return 1;
174  else return 0;
175}
176
[0a0fb74]177char *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]178  static int alias_recurse_depth = 0;
179  int ival=0;
[e19eb97]180  const char *cmdbuffargs;
[65b2173]181  char *newcmd, *rv=NULL;
[7d4fbcd]182
183  if (argc < 1) return(NULL);
184
185  /* Recurse if this is an alias */
186  if (cmd->cmd_aliased_to) {
187    if (alias_recurse_depth++ > 50) {
188      owl_function_makemsg("Alias loop detected for '%s'.", cmdbuff);
189    } else {
190      cmdbuffargs = skiptokens(cmdbuff, 1);
[36486be]191      newcmd = owl_sprintf("%s %s", cmd->cmd_aliased_to, cmdbuffargs);
[7d4fbcd]192      rv = owl_function_command(newcmd);
193      owl_free(newcmd);
194    } 
195    alias_recurse_depth--;
196    return rv;
197  }
198
199  /* Do validation and conversions */
200  if (cmd->cmd_ctxargs_fn || cmd->cmd_ctxv_fn || cmd->cmd_ctxi_fn) {
201    if (!owl_cmd_is_context_valid(cmd, ctx)) {
202      owl_function_makemsg("Invalid context for command '%s'.", cmdbuff);
203      return NULL;
204    }
205  }
206
207  if ((argc != 1) && (cmd->cmd_v_fn || cmd->cmd_ctxv_fn)) {
208    owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
209    return NULL;
210  }
211
212  if (cmd->cmd_i_fn || cmd->cmd_ctxi_fn) {
[99525be]213      char *ep;
[7d4fbcd]214      if (argc != 2) {
215        owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
216        return NULL;
217      }
[99525be]218      ival = strtol(argv[1], &ep, 10);
[7d4fbcd]219      if (*ep || ep==argv[1]) {
220        owl_function_makemsg("Invalid argument '%s' for %s command.", argv[1], argv[0]);
221        return(NULL);
222      }
223  }
224
225  if (cmd->cmd_args_fn) {
226    return cmd->cmd_args_fn(argc, argv, cmdbuff);
227  } else if (cmd->cmd_v_fn) {   
228    cmd->cmd_v_fn();
229  } else if (cmd->cmd_i_fn) {
230    cmd->cmd_i_fn(ival);
231  } else if (cmd->cmd_ctxargs_fn) {
232    return cmd->cmd_ctxargs_fn(owl_context_get_data(ctx), argc, argv, cmdbuff);
233  } else if (cmd->cmd_ctxv_fn) {   
234    cmd->cmd_ctxv_fn(owl_context_get_data(ctx));
235  } else if (cmd->cmd_ctxi_fn) {
236    cmd->cmd_ctxi_fn(owl_context_get_data(ctx), ival);
[6922edd]237  } else if (cmd->cmd_perl) {
238    return owl_perlconfig_perlcmd(cmd, argc, argv);
[7d4fbcd]239  }
240
241  return NULL;
242}
243
244/* returns a reference */
[0a0fb74]245const char *owl_cmd_get_summary(const owl_cmd *cmd) {
[7d4fbcd]246  return cmd->summary;
247}
248
249/* returns a summary line describing this keymap.  the caller must free. */
[0a0fb74]250char *owl_cmd_describe(const owl_cmd *cmd) {
[7d4fbcd]251  if (!cmd || !cmd->name || !cmd->summary) return NULL;
[36486be]252  return owl_sprintf("%-25s - %s", cmd->name, cmd->summary);
[7d4fbcd]253}
254
255
256
[ff7e289]257void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {
[e19eb97]258  const char *s;
[65b2173]259  char *indent;
[7d4fbcd]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.