source: cmd.c @ 7efa163

release-1.10release-1.9
Last change on this file since 7efa163 was f271129, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Fix up headers The additions to owl.h and some of the removals were done by Alejandro Sedeño <asedeno@mit.edu> in commit 77a0258b3919468fc9d7f7602588ac427ab36e6c. Notes: * I think owl.c lost the need for sys/time.h when we punted select() in favor of glib's main loop. * We don't actually need to include things like stdarg.h, stdio.h, glib/gstdio.h, glib-object.h. I think they get indirectly included via owl.h and/or glib.h. They're left in (or added in to) the files that use functions/types from them. * I'm not entirely sure what sys/socket.h is doing in message.c. It is there from the initial commit. I suspect it might have had something to do with the call to getnameinfo. message.c compiles without it, but http://pubs.opengroup.org/onlinepubs/009695399/functions/getnameinfo.html suggests that we're supposed to include it? *shrugs* I'm leaving it in, for now. (Rather, I'll leave one copy of the #include in.)
  • Property mode set to 100644
File size: 8.1 KB
Line 
1#include "owl.h"
2
3/**************************************************************************/
4/***************************** COMMAND DICT *******************************/
5/**************************************************************************/
6
7void owl_cmddict_setup(owl_cmddict *cd)
8{
9  owl_cmddict_init(cd);
10  owl_cmd_add_defaults(cd);
11}
12
13void owl_cmddict_init(owl_cmddict *cd) {
14  owl_dict_create(cd);
15}
16
17/* for bulk initialization at startup */
18void owl_cmddict_add_from_list(owl_cmddict *cd, const owl_cmd *cmds)
19{
20  const owl_cmd *cur;
21  for (cur = cmds; cur->name != NULL; cur++) {
22    owl_cmddict_add_cmd(cd, cur);
23  }
24}
25
26GPtrArray *owl_cmddict_get_names(const owl_cmddict *d) {
27  return owl_dict_get_keys(d);
28}
29
30const owl_cmd *owl_cmddict_find(const owl_cmddict *d, const char *name) {
31  return owl_dict_find_element(d, name);
32}
33
34/* creates a new command alias */
35void owl_cmddict_add_alias(owl_cmddict *cd, const char *alias_from, const char *alias_to) {
36  owl_cmd *cmd;
37  cmd = g_new(owl_cmd, 1);
38  owl_cmd_create_alias(cmd, alias_from, alias_to);
39  owl_perlconfig_new_command(cmd->name);
40  owl_dict_insert_element(cd, cmd->name, cmd, (void (*)(void *))owl_cmd_delete);
41}
42
43int owl_cmddict_add_cmd(owl_cmddict *cd, const owl_cmd * cmd) {
44  owl_cmd * newcmd = g_new(owl_cmd, 1);
45  if(owl_cmd_create_from_template(newcmd, cmd) < 0) {
46    g_free(newcmd);
47    return -1;
48  }
49  owl_perlconfig_new_command(cmd->name);
50  return owl_dict_insert_element(cd, newcmd->name, newcmd, (void (*)(void *))owl_cmd_delete);
51}
52
53/* caller must free the return */
54CALLER_OWN char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff)
55{
56  char *retval = NULL;
57  const owl_cmd *cmd;
58
59  if (!strcmp(argv[0], "")) {
60  } else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) {
61    retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, buff);
62    /* redraw the sepbar; TODO: don't violate layering */
63    owl_global_sepbar_dirty(&g);
64  } else {
65    owl_function_makemsg("Unknown command '%s'.", buff);
66  }
67  return retval;
68}
69
70/* caller must free the return */
71CALLER_OWN char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff)
72{
73  char **argv;
74  int argc;
75  char *retval = NULL;
76
77  argv = owl_parseline(cmdbuff, &argc);
78  if (argv == NULL) {
79    owl_function_makemsg("Unbalanced quotes");
80    return NULL;
81  } 
82 
83  if (argc < 1) {
84    g_strfreev(argv);
85    return NULL;
86  }
87
88  retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff);
89
90  g_strfreev(argv);
91  return retval;
92}
93
94/* caller must free the return */
95CALLER_OWN char *owl_cmddict_execute_argv(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc)
96{
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
123void 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}
129
130void owl_cmd_cleanup(owl_cmd *cmd)
131{
132  g_free(cmd->name);
133  g_free(cmd->summary);
134  g_free(cmd->usage);
135  g_free(cmd->description);
136  g_free(cmd->cmd_aliased_to);
137  if (cmd->cmd_perl) owl_perlconfig_cmd_cleanup(cmd);
138}
139
140void owl_cmd_delete(owl_cmd *cmd)
141{
142  owl_cmd_cleanup(cmd);
143  g_free(cmd);
144}
145
146int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) { 
147  if (owl_context_matches(ctx, cmd->validctx)) return 1;
148  else return 0;
149}
150
151/* caller must free the result */
152CALLER_OWN char *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{
154  static int alias_recurse_depth = 0;
155  int ival=0;
156  const char *cmdbuffargs;
157  char *newcmd, *rv=NULL;
158
159  if (argc < 1) return(NULL);
160
161  /* Recurse if this is an alias */
162  if (cmd->cmd_aliased_to) {
163    if (alias_recurse_depth++ > 50) {
164      owl_function_makemsg("Alias loop detected for '%s'.", cmdbuff);
165    } else {
166      cmdbuffargs = skiptokens(cmdbuff, 1);
167      newcmd = g_strdup_printf("%s %s", cmd->cmd_aliased_to, cmdbuffargs);
168      rv = owl_function_command(newcmd);
169      g_free(newcmd);
170    } 
171    alias_recurse_depth--;
172    return rv;
173  }
174
175  /* Do validation and conversions */
176  if (cmd->cmd_ctxargs_fn || cmd->cmd_ctxv_fn || cmd->cmd_ctxi_fn) {
177    if (!owl_cmd_is_context_valid(cmd, ctx)) {
178      owl_function_makemsg("Invalid context for command '%s'.", cmdbuff);
179      return NULL;
180    }
181  }
182
183  if ((argc != 1) && (cmd->cmd_v_fn || cmd->cmd_ctxv_fn)) {
184    owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
185    return NULL;
186  }
187
188  if (cmd->cmd_i_fn || cmd->cmd_ctxi_fn) {
189      char *ep;
190      if (argc != 2) {
191        owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
192        return NULL;
193      }
194      ival = strtol(argv[1], &ep, 10);
195      if (*ep || ep==argv[1]) {
196        owl_function_makemsg("Invalid argument '%s' for %s command.", argv[1], argv[0]);
197        return(NULL);
198      }
199  }
200
201  if (cmd->cmd_args_fn) {
202    return cmd->cmd_args_fn(argc, argv, cmdbuff);
203  } else if (cmd->cmd_v_fn) {   
204    cmd->cmd_v_fn();
205  } else if (cmd->cmd_i_fn) {
206    cmd->cmd_i_fn(ival);
207  } else if (cmd->cmd_ctxargs_fn) {
208    return cmd->cmd_ctxargs_fn(owl_context_get_data(ctx), argc, argv, cmdbuff);
209  } else if (cmd->cmd_ctxv_fn) {   
210    cmd->cmd_ctxv_fn(owl_context_get_data(ctx));
211  } else if (cmd->cmd_ctxi_fn) {
212    cmd->cmd_ctxi_fn(owl_context_get_data(ctx), ival);
213  } else if (cmd->cmd_perl) {
214    return owl_perlconfig_perlcmd(cmd, argc, argv);
215  }
216
217  return NULL;
218}
219
220/* returns a reference */
221const char *owl_cmd_get_summary(const owl_cmd *cmd) {
222  return cmd->summary;
223}
224
225/* returns a summary line describing this keymap.  the caller must free. */
226CALLER_OWN char *owl_cmd_describe(const owl_cmd *cmd)
227{
228  if (!cmd || !cmd->name || !cmd->summary) return NULL;
229  return g_strdup_printf("%-25s - %s", cmd->name, cmd->summary);
230}
231
232
233
234void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {
235  const char *s;
236  char *indent;
237  owl_cmd *cmd;
238
239  if (!name || (cmd = owl_dict_find_element(d, name)) == NULL) {
240    owl_fmtext_append_bold(fm, "OWL HELP\n\n");
241    owl_fmtext_append_normal(fm, "No such command...\n");
242    return;
243  }
244
245  owl_fmtext_append_bold(fm, "OWL HELP\n\n");
246  owl_fmtext_append_bold(fm, "NAME\n\n");
247  owl_fmtext_append_normal(fm, OWL_TABSTR);
248  owl_fmtext_append_normal(fm, cmd->name);
249
250  if (cmd->summary && *cmd->summary) {
251    owl_fmtext_append_normal(fm, " - ");
252    owl_fmtext_append_normal(fm, cmd->summary);
253  }
254  owl_fmtext_append_normal(fm, "\n");
255
256  if (cmd->usage && *cmd->usage) {
257    s = cmd->usage;
258    indent = owl_text_indent(s, OWL_TAB, true);
259    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
260    owl_fmtext_append_normal(fm, indent);
261    owl_fmtext_append_normal(fm, "\n");
262    g_free(indent);
263  } else {
264    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
265    owl_fmtext_append_normal(fm, OWL_TABSTR);
266    owl_fmtext_append_normal(fm, cmd->name);
267    owl_fmtext_append_normal(fm, "\n");
268  }
269
270  if (cmd->description && *cmd->description) {
271    s = cmd->description;
272    indent = owl_text_indent(s, OWL_TAB, true);
273    owl_fmtext_append_bold(fm, "\nDESCRIPTION\n");
274    owl_fmtext_append_normal(fm, indent);
275    owl_fmtext_append_normal(fm, "\n");
276    g_free(indent);
277  }
278
279  owl_fmtext_append_normal(fm, "\n\n"); 
280}
Note: See TracBrowser for help on using the repository browser.