source: cmd.c @ 7d4fbcd

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7d4fbcd was 7d4fbcd, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
Initial check in
  • Property mode set to 100644
File size: 7.7 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include "owl.h"
6
7extern owl_cmd commands_to_init[];
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 */
25int owl_cmddict_add_from_list(owl_cmddict *cd, owl_cmd *cmds) {
26  owl_cmd *cur, *cmd;
27  for (cur = cmds; cur->name != NULL; cur++) { 
28    cmd = owl_malloc(sizeof(owl_cmd));
29    owl_cmd_create_from_template(cmd, cur);
30    owl_dict_insert_element(cd, cmd->name, (void*)cmd, NULL);
31  }
32  return 0;
33}
34
35/* free the list with owl_cmddict_namelist_free */
36void owl_cmddict_get_names(owl_cmddict *d, owl_list *l) {
37  owl_dict_get_keys(d, l);
38}
39
40owl_cmd *owl_cmddict_find(owl_cmddict *d, char *name) {
41  return (owl_cmd*)owl_dict_find_element(d, name);
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 */
49int owl_cmddict_add_alias(owl_cmddict *cd, char *alias_from, char *alias_to) {
50  owl_cmd *cmd;
51  cmd = owl_malloc(sizeof(owl_cmd));
52  owl_cmd_create_alias(cmd, alias_from, alias_to);
53  owl_dict_insert_element(cd, cmd->name, (void*)cmd, (void(*)(void*))owl_cmd_free);   
54  return(0);
55}
56
57char *owl_cmddict_execute(owl_cmddict *cd, owl_context *ctx, char *cmdbuff) {
58  char **argv;
59  int argc;
60  char *tmpbuff;
61  char *retval = NULL;
62  owl_cmd *cmd;
63
64  tmpbuff=strdup(cmdbuff);
65  argv=owl_parseline(tmpbuff, &argc);
66  if (argc < 0) {
67    owl_free(tmpbuff);
68    sepbar(NULL);
69    owl_function_makemsg("Unbalanced quotes");
70    return NULL;
71  } 
72 
73  if (argc < 1) return(NULL);
74
75  if (!strcmp(argv[0], "")) {
76  } else if (NULL != (cmd = (owl_cmd*)owl_dict_find_element(cd, argv[0]))) {
77    retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, cmdbuff);
78  } else {
79    owl_function_makemsg("Unknown command '%s'.", cmdbuff);
80  }
81  owl_parsefree(argv, argc);
82  owl_free(tmpbuff);
83  sepbar(NULL);
84  return retval;
85}
86
87/*********************************************************************/
88/***************************** COMMAND *******************************/
89/*********************************************************************/
90
91/* sets up a new command based on a template, copying strings */
92int owl_cmd_create_from_template(owl_cmd *cmd, owl_cmd *templ) {
93  *cmd = *templ;
94  if (!templ->name) return(-1);
95  cmd->name = owl_strdup(templ->name);
96  if (cmd->summary)     cmd->summary     = owl_strdup(templ->summary);
97  if (cmd->usage)       cmd->usage       = owl_strdup(templ->usage);
98  if (cmd->description) cmd->description = owl_strdup(templ->description);
99  if (cmd->cmd_aliased_to) cmd->cmd_aliased_to = owl_strdup(templ->cmd_aliased_to);
100  return(0);
101}
102
103int owl_cmd_create_alias(owl_cmd *cmd, char *name, char *aliased_to) {
104  memset(cmd, 0, sizeof(owl_cmd));
105  cmd->name = owl_strdup(name);
106  cmd->cmd_aliased_to = owl_strdup(aliased_to);
107  cmd->summary = owl_malloc(strlen(name)+strlen(OWL_CMD_ALIAS_SUMMARY_PREFIX)+2);
108  strcpy(cmd->summary, OWL_CMD_ALIAS_SUMMARY_PREFIX);
109  strcat(cmd->summary, name);
110  return(0);
111}
112
113void owl_cmd_free(owl_cmd *cmd) {
114  if (cmd->name) owl_free(cmd->name);
115  if (cmd->summary) owl_free(cmd->summary);
116  if (cmd->usage) owl_free(cmd->usage);
117  if (cmd->description) owl_free(cmd->description);
118}
119
120int owl_cmd_is_context_valid(owl_cmd *cmd, owl_context *ctx) { 
121  if (owl_context_matches(ctx, cmd->validctx)) return 1;
122  else return 0;
123}
124
125char *owl_cmd_execute(owl_cmd *cmd, owl_cmddict *cd, owl_context *ctx, int argc, char **argv, char *cmdbuff) {
126  static int alias_recurse_depth = 0;
127  int ival=0;
128  char *cmdbuffargs, *newcmd, *rv=NULL;
129
130  if (argc < 1) return(NULL);
131
132  /* Recurse if this is an alias */
133  if (cmd->cmd_aliased_to) {
134    if (alias_recurse_depth++ > 50) {
135      owl_function_makemsg("Alias loop detected for '%s'.", cmdbuff);
136    } else {
137      cmdbuffargs = skiptokens(cmdbuff, 1);
138      newcmd = owl_malloc(strlen(cmd->cmd_aliased_to)+strlen(cmdbuffargs)+2);
139      strcpy(newcmd, cmd->cmd_aliased_to);
140      strcat(newcmd, " ");
141      strcat(newcmd, cmdbuffargs);
142      rv = owl_function_command(newcmd);
143      owl_free(newcmd);
144    } 
145    alias_recurse_depth--;
146    return rv;
147  }
148
149  /* Do validation and conversions */
150  if (cmd->cmd_ctxargs_fn || cmd->cmd_ctxv_fn || cmd->cmd_ctxi_fn) {
151    if (!owl_cmd_is_context_valid(cmd, ctx)) {
152      owl_function_makemsg("Invalid context for command '%s'.", cmdbuff);
153      return NULL;
154    }
155  }
156
157  if ((argc != 1) && (cmd->cmd_v_fn || cmd->cmd_ctxv_fn)) {
158    owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
159    return NULL;
160  }
161
162  if (cmd->cmd_i_fn || cmd->cmd_ctxi_fn) {
163      char *ep = "x";
164      if (argc != 2) {
165        owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]);
166        return NULL;
167      }
168      ival = strtol(argv[1], &ep, 10);
169      if (*ep || ep==argv[1]) {
170        owl_function_makemsg("Invalid argument '%s' for %s command.", argv[1], argv[0]);
171        return(NULL);
172      }
173  }
174
175  if (cmd->cmd_args_fn) {
176    return cmd->cmd_args_fn(argc, argv, cmdbuff);
177  } else if (cmd->cmd_v_fn) {   
178    cmd->cmd_v_fn();
179  } else if (cmd->cmd_i_fn) {
180    cmd->cmd_i_fn(ival);
181  } else if (cmd->cmd_ctxargs_fn) {
182    return cmd->cmd_ctxargs_fn(owl_context_get_data(ctx), argc, argv, cmdbuff);
183  } else if (cmd->cmd_ctxv_fn) {   
184    cmd->cmd_ctxv_fn(owl_context_get_data(ctx));
185  } else if (cmd->cmd_ctxi_fn) {
186    cmd->cmd_ctxi_fn(owl_context_get_data(ctx), ival);
187  }
188
189  return NULL;
190}
191
192/* returns a reference */
193char *owl_cmd_get_summary(owl_cmd *cmd) {
194  return cmd->summary;
195}
196
197/* returns a summary line describing this keymap.  the caller must free. */
198char *owl_cmd_describe(owl_cmd *cmd) {
199  char *s;
200  int slen;
201  if (!cmd || !cmd->name || !cmd->summary) return NULL;
202  slen = strlen(cmd->name)+strlen(cmd->summary)+30;
203  s = owl_malloc(slen);
204  snprintf(s, slen-1, "%-25s - %s", cmd->name, cmd->summary);
205  return s;
206}
207
208
209
210void owl_cmd_get_help(owl_cmddict *d, char *name, owl_fmtext *fm) {
211  char *indent, *s;
212  owl_cmd *cmd;
213
214  if (!name || (cmd = owl_dict_find_element(d, name)) == NULL) {
215    owl_fmtext_append_bold(fm, "OWL HELP\n\n");
216    owl_fmtext_append_normal(fm, "No such command...\n");
217    return;
218  }
219
220  owl_fmtext_append_bold(fm, "OWL HELP\n\n");
221  owl_fmtext_append_bold(fm, "NAME\n\n");
222  owl_fmtext_append_normal(fm, OWL_TABSTR);
223  owl_fmtext_append_normal(fm, cmd->name);
224
225  if (cmd->summary && *cmd->summary) {
226    owl_fmtext_append_normal(fm, " - ");
227    owl_fmtext_append_normal(fm, cmd->summary);
228  }
229  owl_fmtext_append_normal(fm, "\n");
230
231  if (cmd->usage && *cmd->usage) {
232    s = cmd->usage;
233    indent = owl_malloc(strlen(s)+(owl_text_num_lines(s)+3)*OWL_TAB+1);
234    owl_text_indent(indent, s, OWL_TAB);
235    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
236    owl_fmtext_append_normal(fm, indent);
237    owl_fmtext_append_normal(fm, "\n");
238    owl_free(indent);
239  } else {
240    owl_fmtext_append_bold(fm, "\nSYNOPSIS\n");
241    owl_fmtext_append_normal(fm, OWL_TABSTR);
242    owl_fmtext_append_normal(fm, cmd->name);
243    owl_fmtext_append_normal(fm, "\n");
244  }
245
246  if (cmd->description && *cmd->description) {
247    s = cmd->description;
248    indent = owl_malloc(strlen(s)+(owl_text_num_lines(s)+3)*OWL_TAB+1);
249    owl_text_indent(indent, s, OWL_TAB);
250    owl_fmtext_append_bold(fm, "\nDESCRIPTION\n");
251    owl_fmtext_append_normal(fm, indent);
252    owl_fmtext_append_normal(fm, "\n");
253    owl_free(indent);
254  }
255
256  owl_fmtext_append_normal(fm, "\n\n"); 
257}
Note: See TracBrowser for help on using the repository browser.