source: cmd.c @ 395d304

release-1.10release-1.9
Last change on this file since 395d304 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
RevLine 
[7d4fbcd]1#include "owl.h"
2
3/**************************************************************************/
4/***************************** COMMAND DICT *******************************/
5/**************************************************************************/
6
[d427f08]7void owl_cmddict_setup(owl_cmddict *cd)
8{
[4c7c21f]9  owl_cmddict_init(cd);
[d427f08]10  owl_cmd_add_defaults(cd);
[7d4fbcd]11}
12
[4c7c21f]13void owl_cmddict_init(owl_cmddict *cd) {
14  owl_dict_create(cd);
[7d4fbcd]15}
16
17/* for bulk initialization at startup */
[d427f08]18void owl_cmddict_add_from_list(owl_cmddict *cd, const owl_cmd *cmds)
19{
[0a0fb74]20  const owl_cmd *cur;
[eb6cedc]21  for (cur = cmds; cur->name != NULL; cur++) {
[d427f08]22    owl_cmddict_add_cmd(cd, cur);
[7d4fbcd]23  }
24}
25
[ce68f23]26GPtrArray *owl_cmddict_get_names(const owl_cmddict *d) {
27  return owl_dict_get_keys(d);
[7d4fbcd]28}
29
[0a0fb74]30const owl_cmd *owl_cmddict_find(const owl_cmddict *d, const char *name) {
[4d86e06]31  return owl_dict_find_element(d, name);
[7d4fbcd]32}
33
34/* creates a new command alias */
[d427f08]35void owl_cmddict_add_alias(owl_cmddict *cd, const char *alias_from, const char *alias_to) {
[7d4fbcd]36  owl_cmd *cmd;
[96828e4]37  cmd = g_new(owl_cmd, 1);
[7d4fbcd]38  owl_cmd_create_alias(cmd, alias_from, alias_to);
[1cc9b615]39  owl_perlconfig_new_command(cmd->name);
[b0da027]40  owl_dict_insert_element(cd, cmd->name, cmd, (void (*)(void *))owl_cmd_delete);
[7d4fbcd]41}
42
[0a0fb74]43int owl_cmddict_add_cmd(owl_cmddict *cd, const owl_cmd * cmd) {
[96828e4]44  owl_cmd * newcmd = g_new(owl_cmd, 1);
[6922edd]45  if(owl_cmd_create_from_template(newcmd, cmd) < 0) {
[ddbbcffa]46    g_free(newcmd);
[6922edd]47    return -1;
48  }
[eb6cedc]49  owl_perlconfig_new_command(cmd->name);
[b0da027]50  return owl_dict_insert_element(cd, newcmd->name, newcmd, (void (*)(void *))owl_cmd_delete);
[6922edd]51}
52
[d427f08]53/* caller must free the return */
[6829afc]54CALLER_OWN char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff)
[d427f08]55{
[c4ba74d]56  char *retval = NULL;
[0a0fb74]57  const owl_cmd *cmd;
[c4ba74d]58
59  if (!strcmp(argv[0], "")) {
[4d86e06]60  } else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) {
[c4ba74d]61    retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, buff);
[044f19f]62    /* redraw the sepbar; TODO: don't violate layering */
63    owl_global_sepbar_dirty(&g);
[c4ba74d]64  } else {
65    owl_function_makemsg("Unknown command '%s'.", buff);
66  }
67  return retval;
68}
69
[d427f08]70/* caller must free the return */
[6829afc]71CALLER_OWN char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff)
[d427f08]72{
[7d4fbcd]73  char **argv;
74  int argc;
75  char *retval = NULL;
76
[e56303f]77  argv = owl_parseline(cmdbuff, &argc);
78  if (argv == NULL) {
[7d4fbcd]79    owl_function_makemsg("Unbalanced quotes");
80    return NULL;
81  } 
82 
[62a000b]83  if (argc < 1) {
[e56303f]84    g_strfreev(argv);
[62a000b]85    return NULL;
86  }
[7d4fbcd]87
[c529ac8]88  retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff);
[c4ba74d]89
[e56303f]90  g_strfreev(argv);
[7d4fbcd]91  return retval;
92}
93
[d427f08]94/* caller must free the return */
[6829afc]95CALLER_OWN char *owl_cmddict_execute_argv(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc)
[d427f08]96{
[6a71113]97  char *buff;
98  char *retval = NULL;
[c4ba74d]99
[6a71113]100  buff = g_strjoinv(" ", (char**)argv);
101  retval = _owl_cmddict_execute(cd, ctx, argv, argc, buff);
102  g_free(buff);
[c4ba74d]103
104  return retval;
105}
106
[7d4fbcd]107/*********************************************************************/
108/***************************** COMMAND *******************************/
109/*********************************************************************/
110
111/* sets up a new command based on a template, copying strings */
[0a0fb74]112int owl_cmd_create_from_template(owl_cmd *cmd, const owl_cmd *templ) {
[7d4fbcd]113  *cmd = *templ;
114  if (!templ->name) return(-1);
[d4927a7]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);
[7d4fbcd]120  return(0);
121}
122
[d427f08]123void owl_cmd_create_alias(owl_cmd *cmd, const char *name, const char *aliased_to) {
[7d4fbcd]124  memset(cmd, 0, sizeof(owl_cmd));
[d4927a7]125  cmd->name = g_strdup(name);
126  cmd->cmd_aliased_to = g_strdup(aliased_to);
[3472845]127  cmd->summary = g_strdup_printf("%s%s", OWL_CMD_ALIAS_SUMMARY_PREFIX, aliased_to);
[7d4fbcd]128}
129
[685e96f]130void owl_cmd_cleanup(owl_cmd *cmd)
131{
[3b8a563]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);
[8f2d9bf]137  if (cmd->cmd_perl) owl_perlconfig_cmd_cleanup(cmd);
[7d4fbcd]138}
139
[b0da027]140void owl_cmd_delete(owl_cmd *cmd)
141{
[685e96f]142  owl_cmd_cleanup(cmd);
[ddbbcffa]143  g_free(cmd);
[b0da027]144}
145
[0a0fb74]146int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) { 
[7d4fbcd]147  if (owl_context_matches(ctx, cmd->validctx)) return 1;
148  else return 0;
149}
150
[d427f08]151/* caller must free the result */
[6829afc]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)
[d427f08]153{
[7d4fbcd]154  static int alias_recurse_depth = 0;
155  int ival=0;
[e19eb97]156  const char *cmdbuffargs;
[65b2173]157  char *newcmd, *rv=NULL;
[7d4fbcd]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);
[3472845]167      newcmd = g_strdup_printf("%s %s", cmd->cmd_aliased_to, cmdbuffargs);
[7d4fbcd]168      rv = owl_function_command(newcmd);
[ddbbcffa]169      g_free(newcmd);
[7d4fbcd]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) {
[99525be]189      char *ep;
[7d4fbcd]190      if (argc != 2) {
191        owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
192        return NULL;
193      }
[99525be]194      ival = strtol(argv[1], &ep, 10);
[7d4fbcd]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);
[6922edd]213  } else if (cmd->cmd_perl) {
214    return owl_perlconfig_perlcmd(cmd, argc, argv);
[7d4fbcd]215  }
216
217  return NULL;
218}
219
220/* returns a reference */
[0a0fb74]221const char *owl_cmd_get_summary(const owl_cmd *cmd) {
[7d4fbcd]222  return cmd->summary;
223}
224
225/* returns a summary line describing this keymap.  the caller must free. */
[6829afc]226CALLER_OWN char *owl_cmd_describe(const owl_cmd *cmd)
[d427f08]227{
[7d4fbcd]228  if (!cmd || !cmd->name || !cmd->summary) return NULL;
[3472845]229  return g_strdup_printf("%-25s - %s", cmd->name, cmd->summary);
[7d4fbcd]230}
231
232
233
[ff7e289]234void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {
[e19eb97]235  const char *s;
[65b2173]236  char *indent;
[7d4fbcd]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;
[14be3a5]258    indent = owl_text_indent(s, OWL_TAB, true);
[7d4fbcd]259    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
260    owl_fmtext_append_normal(fm, indent);
261    owl_fmtext_append_normal(fm, "\n");
[ddbbcffa]262    g_free(indent);
[7d4fbcd]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;
[14be3a5]272    indent = owl_text_indent(s, OWL_TAB, true);
[7d4fbcd]273    owl_fmtext_append_bold(fm, "\nDESCRIPTION\n");
274    owl_fmtext_append_normal(fm, indent);
275    owl_fmtext_append_normal(fm, "\n");
[ddbbcffa]276    g_free(indent);
[7d4fbcd]277  }
278
279  owl_fmtext_append_normal(fm, "\n\n"); 
280}
Note: See TracBrowser for help on using the repository browser.