source: cmd.c @ 9e86f6f

release-1.8release-1.9
Last change on this file since 9e86f6f was aad166a, checked in by Anders Kaseorg <andersk@mit.edu>, 10 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
RevLine 
[7d4fbcd]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) {
[4c7c21f]12  owl_cmddict_init(cd);
[aad166a]13  return owl_cmd_add_defaults(cd);
[7d4fbcd]14}
15
[4c7c21f]16void owl_cmddict_init(owl_cmddict *cd) {
17  owl_dict_create(cd);
[7d4fbcd]18}
19
20/* for bulk initialization at startup */
[0a0fb74]21int owl_cmddict_add_from_list(owl_cmddict *cd, const owl_cmd *cmds) {
22  const owl_cmd *cur;
[eb6cedc]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;
[7d4fbcd]27  }
[eb6cedc]28  return ret;
[7d4fbcd]29}
30
[ff7e289]31void owl_cmddict_get_names(const owl_cmddict *d, owl_list *l) {
[7d4fbcd]32  owl_dict_get_keys(d, l);
33}
34
[0a0fb74]35const owl_cmd *owl_cmddict_find(const owl_cmddict *d, const char *name) {
[4d86e06]36  return owl_dict_find_element(d, name);
[7d4fbcd]37}
38
39/* creates a new command alias */
[e19eb97]40int owl_cmddict_add_alias(owl_cmddict *cd, const char *alias_from, const char *alias_to) {
[7d4fbcd]41  owl_cmd *cmd;
[96828e4]42  cmd = g_new(owl_cmd, 1);
[7d4fbcd]43  owl_cmd_create_alias(cmd, alias_from, alias_to);
[1cc9b615]44  owl_perlconfig_new_command(cmd->name);
[b0da027]45  owl_dict_insert_element(cd, cmd->name, cmd, (void (*)(void *))owl_cmd_delete);
[7d4fbcd]46  return(0);
47}
48
[0a0fb74]49int owl_cmddict_add_cmd(owl_cmddict *cd, const owl_cmd * cmd) {
[96828e4]50  owl_cmd * newcmd = g_new(owl_cmd, 1);
[6922edd]51  if(owl_cmd_create_from_template(newcmd, cmd) < 0) {
[ddbbcffa]52    g_free(newcmd);
[6922edd]53    return -1;
54  }
[eb6cedc]55  owl_perlconfig_new_command(cmd->name);
[b0da027]56  return owl_dict_insert_element(cd, newcmd->name, newcmd, (void (*)(void *))owl_cmd_delete);
[6922edd]57}
58
[3f8514b]59char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff) {
[c4ba74d]60  char *retval = NULL;
[0a0fb74]61  const owl_cmd *cmd;
[c4ba74d]62
63  if (!strcmp(argv[0], "")) {
[4d86e06]64  } else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) {
[c4ba74d]65    retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, buff);
[044f19f]66    /* redraw the sepbar; TODO: don't violate layering */
67    owl_global_sepbar_dirty(&g);
[c4ba74d]68  } else {
69    owl_function_makemsg("Unknown command '%s'.", buff);
70  }
71  return retval;
72}
73
[3f8514b]74char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff) {
[7d4fbcd]75  char **argv;
76  int argc;
77  char *retval = NULL;
78
[e56303f]79  argv = owl_parseline(cmdbuff, &argc);
80  if (argv == NULL) {
[7d4fbcd]81    owl_function_makemsg("Unbalanced quotes");
82    return NULL;
83  } 
84 
[62a000b]85  if (argc < 1) {
[e56303f]86    g_strfreev(argv);
[62a000b]87    return NULL;
88  }
[7d4fbcd]89
[c529ac8]90  retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff);
[c4ba74d]91
[e56303f]92  g_strfreev(argv);
[7d4fbcd]93  return retval;
94}
95
[3f8514b]96char *owl_cmddict_execute_argv(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc) {
[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
[e19eb97]123int 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  return(0);
129}
130
[685e96f]131void owl_cmd_cleanup(owl_cmd *cmd)
132{
[3b8a563]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);
[8f2d9bf]138  if (cmd->cmd_perl) owl_perlconfig_cmd_cleanup(cmd);
[7d4fbcd]139}
140
[b0da027]141void owl_cmd_delete(owl_cmd *cmd)
142{
[685e96f]143  owl_cmd_cleanup(cmd);
[ddbbcffa]144  g_free(cmd);
[b0da027]145}
146
[0a0fb74]147int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) { 
[7d4fbcd]148  if (owl_context_matches(ctx, cmd->validctx)) return 1;
149  else return 0;
150}
151
[0a0fb74]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) {
[7d4fbcd]153  static int alias_recurse_depth = 0;
154  int ival=0;
[e19eb97]155  const char *cmdbuffargs;
[65b2173]156  char *newcmd, *rv=NULL;
[7d4fbcd]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);
[3472845]166      newcmd = g_strdup_printf("%s %s", cmd->cmd_aliased_to, cmdbuffargs);
[7d4fbcd]167      rv = owl_function_command(newcmd);
[ddbbcffa]168      g_free(newcmd);
[7d4fbcd]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) {
[99525be]188      char *ep;
[7d4fbcd]189      if (argc != 2) {
190        owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
191        return NULL;
192      }
[99525be]193      ival = strtol(argv[1], &ep, 10);
[7d4fbcd]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);
[6922edd]212  } else if (cmd->cmd_perl) {
213    return owl_perlconfig_perlcmd(cmd, argc, argv);
[7d4fbcd]214  }
215
216  return NULL;
217}
218
219/* returns a reference */
[0a0fb74]220const char *owl_cmd_get_summary(const owl_cmd *cmd) {
[7d4fbcd]221  return cmd->summary;
222}
223
224/* returns a summary line describing this keymap.  the caller must free. */
[0a0fb74]225char *owl_cmd_describe(const owl_cmd *cmd) {
[7d4fbcd]226  if (!cmd || !cmd->name || !cmd->summary) return NULL;
[3472845]227  return g_strdup_printf("%-25s - %s", cmd->name, cmd->summary);
[7d4fbcd]228}
229
230
231
[ff7e289]232void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) {
[e19eb97]233  const char *s;
[65b2173]234  char *indent;
[7d4fbcd]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;
[4d24650]256    indent = owl_text_indent(s, OWL_TAB);
[7d4fbcd]257    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
258    owl_fmtext_append_normal(fm, indent);
259    owl_fmtext_append_normal(fm, "\n");
[ddbbcffa]260    g_free(indent);
[7d4fbcd]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;
[4d24650]270    indent = owl_text_indent(s, OWL_TAB);
[7d4fbcd]271    owl_fmtext_append_bold(fm, "\nDESCRIPTION\n");
272    owl_fmtext_append_normal(fm, indent);
273    owl_fmtext_append_normal(fm, "\n");
[ddbbcffa]274    g_free(indent);
[7d4fbcd]275  }
276
277  owl_fmtext_append_normal(fm, "\n\n"); 
278}
Note: See TracBrowser for help on using the repository browser.