source: cmd.c @ 4f746f8

release-1.10release-1.8release-1.9
Last change on this file since 4f746f8 was aad166a, checked in by Anders Kaseorg <andersk@mit.edu>, 13 years ago
commands_to_init: Move into new function owl_cmd_add_defaults Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 8.1 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include "owl.h"
6
7/**************************************************************************/
8/***************************** COMMAND DICT *******************************/
9/**************************************************************************/
10
11int owl_cmddict_setup(owl_cmddict *cd) {
12  owl_cmddict_init(cd);
13  return owl_cmd_add_defaults(cd);
14}
15
16void owl_cmddict_init(owl_cmddict *cd) {
17  owl_dict_create(cd);
18}
19
20/* for bulk initialization at startup */
21int owl_cmddict_add_from_list(owl_cmddict *cd, const owl_cmd *cmds) {
22  const owl_cmd *cur;
23  int ret = 0;
24  for (cur = cmds; cur->name != NULL; cur++) {
25    ret = owl_cmddict_add_cmd(cd, cur);
26    if (ret < 0) break;
27  }
28  return ret;
29}
30
31void owl_cmddict_get_names(const owl_cmddict *d, owl_list *l) {
32  owl_dict_get_keys(d, l);
33}
34
35const owl_cmd *owl_cmddict_find(const owl_cmddict *d, const char *name) {
36  return owl_dict_find_element(d, name);
37}
38
39/* creates a new command alias */
40int owl_cmddict_add_alias(owl_cmddict *cd, const char *alias_from, const char *alias_to) {
41  owl_cmd *cmd;
42  cmd = g_new(owl_cmd, 1);
43  owl_cmd_create_alias(cmd, alias_from, alias_to);
44  owl_perlconfig_new_command(cmd->name);
45  owl_dict_insert_element(cd, cmd->name, cmd, (void (*)(void *))owl_cmd_delete);
46  return(0);
47}
48
49int owl_cmddict_add_cmd(owl_cmddict *cd, const owl_cmd * cmd) {
50  owl_cmd * newcmd = g_new(owl_cmd, 1);
51  if(owl_cmd_create_from_template(newcmd, cmd) < 0) {
52    g_free(newcmd);
53    return -1;
54  }
55  owl_perlconfig_new_command(cmd->name);
56  return owl_dict_insert_element(cd, newcmd->name, newcmd, (void (*)(void *))owl_cmd_delete);
57}
58
59char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff) {
60  char *retval = NULL;
61  const owl_cmd *cmd;
62
63  if (!strcmp(argv[0], "")) {
64  } else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) {
65    retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, buff);
66    /* redraw the sepbar; TODO: don't violate layering */
67    owl_global_sepbar_dirty(&g);
68  } else {
69    owl_function_makemsg("Unknown command '%s'.", buff);
70  }
71  return retval;
72}
73
74char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff) {
75  char **argv;
76  int argc;
77  char *retval = NULL;
78
79  argv = owl_parseline(cmdbuff, &argc);
80  if (argv == NULL) {
81    owl_function_makemsg("Unbalanced quotes");
82    return NULL;
83  } 
84 
85  if (argc < 1) {
86    g_strfreev(argv);
87    return NULL;
88  }
89
90  retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff);
91
92  g_strfreev(argv);
93  return retval;
94}
95
96char *owl_cmddict_execute_argv(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc) {
97  char *buff;
98  char *retval = NULL;
99
100  buff = g_strjoinv(" ", (char**)argv);
101  retval = _owl_cmddict_execute(cd, ctx, argv, argc, buff);
102  g_free(buff);
103
104  return retval;
105}
106
107/*********************************************************************/
108/***************************** COMMAND *******************************/
109/*********************************************************************/
110
111/* sets up a new command based on a template, copying strings */
112int owl_cmd_create_from_template(owl_cmd *cmd, const owl_cmd *templ) {
113  *cmd = *templ;
114  if (!templ->name) return(-1);
115  cmd->name = g_strdup(templ->name);
116  if (templ->summary)     cmd->summary     = g_strdup(templ->summary);
117  if (templ->usage)       cmd->usage       = g_strdup(templ->usage);
118  if (templ->description) cmd->description = g_strdup(templ->description);
119  if (templ->cmd_aliased_to) cmd->cmd_aliased_to = g_strdup(templ->cmd_aliased_to);
120  return(0);
121}
122
123int owl_cmd_create_alias(owl_cmd *cmd, const char *name, const char *aliased_to) {
124  memset(cmd, 0, sizeof(owl_cmd));
125  cmd->name = g_strdup(name);
126  cmd->cmd_aliased_to = g_strdup(aliased_to);
127  cmd->summary = g_strdup_printf("%s%s", OWL_CMD_ALIAS_SUMMARY_PREFIX, aliased_to);
128  return(0);
129}
130
131void owl_cmd_cleanup(owl_cmd *cmd)
132{
133  g_free(cmd->name);
134  g_free(cmd->summary);
135  g_free(cmd->usage);
136  g_free(cmd->description);
137  g_free(cmd->cmd_aliased_to);
138  if (cmd->cmd_perl) owl_perlconfig_cmd_cleanup(cmd);
139}
140
141void owl_cmd_delete(owl_cmd *cmd)
142{
143  owl_cmd_cleanup(cmd);
144  g_free(cmd);
145}
146
147int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) { 
148  if (owl_context_matches(ctx, cmd->validctx)) return 1;
149  else return 0;
150}
151
152char *owl_cmd_execute(const owl_cmd *cmd, const owl_cmddict *cd, const owl_context *ctx, int argc, const char *const *argv, const char *cmdbuff) {
153  static int alias_recurse_depth = 0;
154  int ival=0;
155  const char *cmdbuffargs;
156  char *newcmd, *rv=NULL;
157
158  if (argc < 1) return(NULL);
159
160  /* Recurse if this is an alias */
161  if (cmd->cmd_aliased_to) {
162    if (alias_recurse_depth++ > 50) {
163      owl_function_makemsg("Alias loop detected for '%s'.", cmdbuff);
164    } else {
165      cmdbuffargs = skiptokens(cmdbuff, 1);
166      newcmd = g_strdup_printf("%s %s", cmd->cmd_aliased_to, cmdbuffargs);
167      rv = owl_function_command(newcmd);
168      g_free(newcmd);
169    } 
170    alias_recurse_depth--;
171    return rv;
172  }
173
174  /* Do validation and conversions */
175  if (cmd->cmd_ctxargs_fn || cmd->cmd_ctxv_fn || cmd->cmd_ctxi_fn) {
176    if (!owl_cmd_is_context_valid(cmd, ctx)) {
177      owl_function_makemsg("Invalid context for command '%s'.", cmdbuff);
178      return NULL;
179    }
180  }
181
182  if ((argc != 1) && (cmd->cmd_v_fn || cmd->cmd_ctxv_fn)) {
183    owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
184    return NULL;
185  }
186
187  if (cmd->cmd_i_fn || cmd->cmd_ctxi_fn) {
188      char *ep;
189      if (argc != 2) {
190        owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
191        return NULL;
192      }
193      ival = strtol(argv[1], &ep, 10);
194      if (*ep || ep==argv[1]) {
195        owl_function_makemsg("Invalid argument '%s' for %s command.", argv[1], argv[0]);
196        return(NULL);
197      }
198  }
199
200  if (cmd->cmd_args_fn) {
201    return cmd->cmd_args_fn(argc, argv, cmdbuff);
202  } else if (cmd->cmd_v_fn) {   
203    cmd->cmd_v_fn();
204  } else if (cmd->cmd_i_fn) {
205    cmd->cmd_i_fn(ival);
206  } else if (cmd->cmd_ctxargs_fn) {
207    return cmd->cmd_ctxargs_fn(owl_context_get_data(ctx), argc, argv, cmdbuff);
208  } else if (cmd->cmd_ctxv_fn) {   
209    cmd->cmd_ctxv_fn(owl_context_get_data(ctx));
210  } else if (cmd->cmd_ctxi_fn) {
211    cmd->cmd_ctxi_fn(owl_context_get_data(ctx), ival);
212  } else if (cmd->cmd_perl) {
213    return owl_perlconfig_perlcmd(cmd, argc, argv);
214  }
215
216  return NULL;
217}
218
219/* returns a reference */
220const char *owl_cmd_get_summary(const owl_cmd *cmd) {
221  return cmd->summary;
222}
223
224/* returns a summary line describing this keymap.  the caller must free. */
225char *owl_cmd_describe(const owl_cmd *cmd) {
226  if (!cmd || !cmd->name || !cmd->summary) return NULL;
227  return g_strdup_printf("%-25s - %s", cmd->name, cmd->summary);
228}
229
230
231
232void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {
233  const char *s;
234  char *indent;
235  owl_cmd *cmd;
236
237  if (!name || (cmd = owl_dict_find_element(d, name)) == NULL) {
238    owl_fmtext_append_bold(fm, "OWL HELP\n\n");
239    owl_fmtext_append_normal(fm, "No such command...\n");
240    return;
241  }
242
243  owl_fmtext_append_bold(fm, "OWL HELP\n\n");
244  owl_fmtext_append_bold(fm, "NAME\n\n");
245  owl_fmtext_append_normal(fm, OWL_TABSTR);
246  owl_fmtext_append_normal(fm, cmd->name);
247
248  if (cmd->summary && *cmd->summary) {
249    owl_fmtext_append_normal(fm, " - ");
250    owl_fmtext_append_normal(fm, cmd->summary);
251  }
252  owl_fmtext_append_normal(fm, "\n");
253
254  if (cmd->usage && *cmd->usage) {
255    s = cmd->usage;
256    indent = owl_text_indent(s, OWL_TAB);
257    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
258    owl_fmtext_append_normal(fm, indent);
259    owl_fmtext_append_normal(fm, "\n");
260    g_free(indent);
261  } else {
262    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
263    owl_fmtext_append_normal(fm, OWL_TABSTR);
264    owl_fmtext_append_normal(fm, cmd->name);
265    owl_fmtext_append_normal(fm, "\n");
266  }
267
268  if (cmd->description && *cmd->description) {
269    s = cmd->description;
270    indent = owl_text_indent(s, OWL_TAB);
271    owl_fmtext_append_bold(fm, "\nDESCRIPTION\n");
272    owl_fmtext_append_normal(fm, indent);
273    owl_fmtext_append_normal(fm, "\n");
274    g_free(indent);
275  }
276
277  owl_fmtext_append_normal(fm, "\n\n"); 
278}
Note: See TracBrowser for help on using the repository browser.