[7d4fbcd] | 1 | #include <stdio.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <string.h> |
---|
| 4 | #include <unistd.h> |
---|
| 5 | #include "owl.h" |
---|
| 6 | |
---|
[0a0fb74] | 7 | extern const owl_cmd commands_to_init[]; |
---|
[7d4fbcd] | 8 | |
---|
| 9 | /**************************************************************************/ |
---|
| 10 | /***************************** COMMAND DICT *******************************/ |
---|
| 11 | /**************************************************************************/ |
---|
| 12 | |
---|
| 13 | int 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 | |
---|
| 19 | int 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 */ |
---|
[0a0fb74] | 25 | int owl_cmddict_add_from_list(owl_cmddict *cd, const owl_cmd *cmds) { |
---|
| 26 | const owl_cmd *cur; |
---|
[eb6cedc] | 27 | int ret = 0; |
---|
| 28 | for (cur = cmds; cur->name != NULL; cur++) { |
---|
| 29 | ret = owl_cmddict_add_cmd(cd, cur); |
---|
| 30 | if (ret < 0) break; |
---|
[7d4fbcd] | 31 | } |
---|
[eb6cedc] | 32 | return ret; |
---|
[7d4fbcd] | 33 | } |
---|
| 34 | |
---|
[8d4b521] | 35 | /* free the list with owl_cmddict_namelist_cleanup */ |
---|
[ff7e289] | 36 | void owl_cmddict_get_names(const owl_cmddict *d, owl_list *l) { |
---|
[7d4fbcd] | 37 | owl_dict_get_keys(d, l); |
---|
| 38 | } |
---|
| 39 | |
---|
[0a0fb74] | 40 | const owl_cmd *owl_cmddict_find(const owl_cmddict *d, const char *name) { |
---|
[4d86e06] | 41 | return owl_dict_find_element(d, name); |
---|
[7d4fbcd] | 42 | } |
---|
| 43 | |
---|
[8d4b521] | 44 | void owl_cmddict_namelist_cleanup(owl_list *l) |
---|
| 45 | { |
---|
[8c59178] | 46 | owl_list_cleanup(l, owl_free); |
---|
[7d4fbcd] | 47 | } |
---|
| 48 | |
---|
| 49 | /* creates a new command alias */ |
---|
[e19eb97] | 50 | int owl_cmddict_add_alias(owl_cmddict *cd, const char *alias_from, const char *alias_to) { |
---|
[7d4fbcd] | 51 | owl_cmd *cmd; |
---|
| 52 | cmd = owl_malloc(sizeof(owl_cmd)); |
---|
| 53 | owl_cmd_create_alias(cmd, alias_from, alias_to); |
---|
[1cc9b615] | 54 | owl_perlconfig_new_command(cmd->name); |
---|
[b0da027] | 55 | owl_dict_insert_element(cd, cmd->name, cmd, (void (*)(void *))owl_cmd_delete); |
---|
[7d4fbcd] | 56 | return(0); |
---|
| 57 | } |
---|
| 58 | |
---|
[0a0fb74] | 59 | int owl_cmddict_add_cmd(owl_cmddict *cd, const owl_cmd * cmd) { |
---|
[6922edd] | 60 | owl_cmd * newcmd = owl_malloc(sizeof(owl_cmd)); |
---|
| 61 | if(owl_cmd_create_from_template(newcmd, cmd) < 0) { |
---|
| 62 | owl_free(newcmd); |
---|
| 63 | return -1; |
---|
| 64 | } |
---|
[eb6cedc] | 65 | owl_perlconfig_new_command(cmd->name); |
---|
[b0da027] | 66 | return owl_dict_insert_element(cd, newcmd->name, newcmd, (void (*)(void *))owl_cmd_delete); |
---|
[6922edd] | 67 | } |
---|
| 68 | |
---|
[3f8514b] | 69 | char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff) { |
---|
[c4ba74d] | 70 | char *retval = NULL; |
---|
[0a0fb74] | 71 | const owl_cmd *cmd; |
---|
[c4ba74d] | 72 | |
---|
| 73 | if (!strcmp(argv[0], "")) { |
---|
[4d86e06] | 74 | } else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) { |
---|
[c4ba74d] | 75 | retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, buff); |
---|
| 76 | } else { |
---|
| 77 | owl_function_makemsg("Unknown command '%s'.", buff); |
---|
| 78 | } |
---|
| 79 | return retval; |
---|
| 80 | } |
---|
| 81 | |
---|
[3f8514b] | 82 | char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff) { |
---|
[7d4fbcd] | 83 | char **argv; |
---|
| 84 | int argc; |
---|
| 85 | char *tmpbuff; |
---|
| 86 | char *retval = NULL; |
---|
| 87 | |
---|
[34509d5] | 88 | tmpbuff=owl_strdup(cmdbuff); |
---|
[7d4fbcd] | 89 | argv=owl_parseline(tmpbuff, &argc); |
---|
| 90 | if (argc < 0) { |
---|
| 91 | owl_free(tmpbuff); |
---|
| 92 | sepbar(NULL); |
---|
| 93 | owl_function_makemsg("Unbalanced quotes"); |
---|
| 94 | return NULL; |
---|
| 95 | } |
---|
| 96 | |
---|
[62a000b] | 97 | if (argc < 1) { |
---|
[40d22cf] | 98 | owl_parse_delete(argv, argc); |
---|
[62a000b] | 99 | owl_free(tmpbuff); |
---|
| 100 | return NULL; |
---|
| 101 | } |
---|
[7d4fbcd] | 102 | |
---|
[c529ac8] | 103 | retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff); |
---|
[c4ba74d] | 104 | |
---|
[40d22cf] | 105 | owl_parse_delete(argv, argc); |
---|
[7d4fbcd] | 106 | owl_free(tmpbuff); |
---|
| 107 | sepbar(NULL); |
---|
| 108 | return retval; |
---|
| 109 | } |
---|
| 110 | |
---|
[3f8514b] | 111 | char *owl_cmddict_execute_argv(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc) { |
---|
[c4ba74d] | 112 | char *buff, *ptr; |
---|
| 113 | int len = 0, i; |
---|
| 114 | char *retval = NULL; |
---|
| 115 | |
---|
| 116 | for(i = 0; i < argc; i++) { |
---|
| 117 | len += strlen(argv[i]) + 1; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | ptr = buff = owl_malloc(len); |
---|
| 121 | |
---|
| 122 | for(i = 0; i < argc; i++) { |
---|
| 123 | strcpy(ptr, argv[i]); |
---|
| 124 | ptr += strlen(argv[i]); |
---|
| 125 | *(ptr++) = ' '; |
---|
| 126 | } |
---|
| 127 | *(ptr - 1) = 0; |
---|
| 128 | |
---|
| 129 | retval = _owl_cmddict_execute(cd, ctx, argv, argc, buff); |
---|
| 130 | |
---|
| 131 | owl_free(buff); |
---|
| 132 | return retval; |
---|
| 133 | } |
---|
| 134 | |
---|
[7d4fbcd] | 135 | /*********************************************************************/ |
---|
| 136 | /***************************** COMMAND *******************************/ |
---|
| 137 | /*********************************************************************/ |
---|
| 138 | |
---|
| 139 | /* sets up a new command based on a template, copying strings */ |
---|
[0a0fb74] | 140 | int owl_cmd_create_from_template(owl_cmd *cmd, const owl_cmd *templ) { |
---|
[7d4fbcd] | 141 | *cmd = *templ; |
---|
| 142 | if (!templ->name) return(-1); |
---|
| 143 | cmd->name = owl_strdup(templ->name); |
---|
[f1e629d] | 144 | if (templ->summary) cmd->summary = owl_strdup(templ->summary); |
---|
| 145 | if (templ->usage) cmd->usage = owl_strdup(templ->usage); |
---|
| 146 | if (templ->description) cmd->description = owl_strdup(templ->description); |
---|
| 147 | if (templ->cmd_aliased_to) cmd->cmd_aliased_to = owl_strdup(templ->cmd_aliased_to); |
---|
[7d4fbcd] | 148 | return(0); |
---|
| 149 | } |
---|
| 150 | |
---|
[e19eb97] | 151 | int owl_cmd_create_alias(owl_cmd *cmd, const char *name, const char *aliased_to) { |
---|
[7d4fbcd] | 152 | memset(cmd, 0, sizeof(owl_cmd)); |
---|
| 153 | cmd->name = owl_strdup(name); |
---|
| 154 | cmd->cmd_aliased_to = owl_strdup(aliased_to); |
---|
[36486be] | 155 | cmd->summary = owl_sprintf("%s%s", OWL_CMD_ALIAS_SUMMARY_PREFIX, aliased_to); |
---|
[7d4fbcd] | 156 | return(0); |
---|
| 157 | } |
---|
| 158 | |
---|
[685e96f] | 159 | void owl_cmd_cleanup(owl_cmd *cmd) |
---|
| 160 | { |
---|
[7d4fbcd] | 161 | if (cmd->name) owl_free(cmd->name); |
---|
| 162 | if (cmd->summary) owl_free(cmd->summary); |
---|
| 163 | if (cmd->usage) owl_free(cmd->usage); |
---|
| 164 | if (cmd->description) owl_free(cmd->description); |
---|
[6922edd] | 165 | if (cmd->cmd_aliased_to) owl_free(cmd->cmd_aliased_to); |
---|
[8f2d9bf] | 166 | if (cmd->cmd_perl) owl_perlconfig_cmd_cleanup(cmd); |
---|
[7d4fbcd] | 167 | } |
---|
| 168 | |
---|
[b0da027] | 169 | void owl_cmd_delete(owl_cmd *cmd) |
---|
| 170 | { |
---|
[685e96f] | 171 | owl_cmd_cleanup(cmd); |
---|
[b0da027] | 172 | owl_free(cmd); |
---|
| 173 | } |
---|
| 174 | |
---|
[0a0fb74] | 175 | int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) { |
---|
[7d4fbcd] | 176 | if (owl_context_matches(ctx, cmd->validctx)) return 1; |
---|
| 177 | else return 0; |
---|
| 178 | } |
---|
| 179 | |
---|
[0a0fb74] | 180 | 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) { |
---|
[7d4fbcd] | 181 | static int alias_recurse_depth = 0; |
---|
| 182 | int ival=0; |
---|
[e19eb97] | 183 | const char *cmdbuffargs; |
---|
[65b2173] | 184 | char *newcmd, *rv=NULL; |
---|
[7d4fbcd] | 185 | |
---|
| 186 | if (argc < 1) return(NULL); |
---|
| 187 | |
---|
| 188 | /* Recurse if this is an alias */ |
---|
| 189 | if (cmd->cmd_aliased_to) { |
---|
| 190 | if (alias_recurse_depth++ > 50) { |
---|
| 191 | owl_function_makemsg("Alias loop detected for '%s'.", cmdbuff); |
---|
| 192 | } else { |
---|
| 193 | cmdbuffargs = skiptokens(cmdbuff, 1); |
---|
[36486be] | 194 | newcmd = owl_sprintf("%s %s", cmd->cmd_aliased_to, cmdbuffargs); |
---|
[7d4fbcd] | 195 | rv = owl_function_command(newcmd); |
---|
| 196 | owl_free(newcmd); |
---|
| 197 | } |
---|
| 198 | alias_recurse_depth--; |
---|
| 199 | return rv; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | /* Do validation and conversions */ |
---|
| 203 | if (cmd->cmd_ctxargs_fn || cmd->cmd_ctxv_fn || cmd->cmd_ctxi_fn) { |
---|
| 204 | if (!owl_cmd_is_context_valid(cmd, ctx)) { |
---|
| 205 | owl_function_makemsg("Invalid context for command '%s'.", cmdbuff); |
---|
| 206 | return NULL; |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | if ((argc != 1) && (cmd->cmd_v_fn || cmd->cmd_ctxv_fn)) { |
---|
| 211 | owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]); |
---|
| 212 | return NULL; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | if (cmd->cmd_i_fn || cmd->cmd_ctxi_fn) { |
---|
[99525be] | 216 | char *ep; |
---|
[7d4fbcd] | 217 | if (argc != 2) { |
---|
| 218 | owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]); |
---|
| 219 | return NULL; |
---|
| 220 | } |
---|
[99525be] | 221 | ival = strtol(argv[1], &ep, 10); |
---|
[7d4fbcd] | 222 | if (*ep || ep==argv[1]) { |
---|
| 223 | owl_function_makemsg("Invalid argument '%s' for %s command.", argv[1], argv[0]); |
---|
| 224 | return(NULL); |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | if (cmd->cmd_args_fn) { |
---|
| 229 | return cmd->cmd_args_fn(argc, argv, cmdbuff); |
---|
| 230 | } else if (cmd->cmd_v_fn) { |
---|
| 231 | cmd->cmd_v_fn(); |
---|
| 232 | } else if (cmd->cmd_i_fn) { |
---|
| 233 | cmd->cmd_i_fn(ival); |
---|
| 234 | } else if (cmd->cmd_ctxargs_fn) { |
---|
| 235 | return cmd->cmd_ctxargs_fn(owl_context_get_data(ctx), argc, argv, cmdbuff); |
---|
| 236 | } else if (cmd->cmd_ctxv_fn) { |
---|
| 237 | cmd->cmd_ctxv_fn(owl_context_get_data(ctx)); |
---|
| 238 | } else if (cmd->cmd_ctxi_fn) { |
---|
| 239 | cmd->cmd_ctxi_fn(owl_context_get_data(ctx), ival); |
---|
[6922edd] | 240 | } else if (cmd->cmd_perl) { |
---|
| 241 | return owl_perlconfig_perlcmd(cmd, argc, argv); |
---|
[7d4fbcd] | 242 | } |
---|
| 243 | |
---|
| 244 | return NULL; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /* returns a reference */ |
---|
[0a0fb74] | 248 | const char *owl_cmd_get_summary(const owl_cmd *cmd) { |
---|
[7d4fbcd] | 249 | return cmd->summary; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | /* returns a summary line describing this keymap. the caller must free. */ |
---|
[0a0fb74] | 253 | char *owl_cmd_describe(const owl_cmd *cmd) { |
---|
[7d4fbcd] | 254 | if (!cmd || !cmd->name || !cmd->summary) return NULL; |
---|
[36486be] | 255 | return owl_sprintf("%-25s - %s", cmd->name, cmd->summary); |
---|
[7d4fbcd] | 256 | } |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | |
---|
[ff7e289] | 260 | void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) { |
---|
[e19eb97] | 261 | const char *s; |
---|
[65b2173] | 262 | char *indent; |
---|
[7d4fbcd] | 263 | owl_cmd *cmd; |
---|
| 264 | |
---|
| 265 | if (!name || (cmd = owl_dict_find_element(d, name)) == NULL) { |
---|
| 266 | owl_fmtext_append_bold(fm, "OWL HELP\n\n"); |
---|
| 267 | owl_fmtext_append_normal(fm, "No such command...\n"); |
---|
| 268 | return; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | owl_fmtext_append_bold(fm, "OWL HELP\n\n"); |
---|
| 272 | owl_fmtext_append_bold(fm, "NAME\n\n"); |
---|
| 273 | owl_fmtext_append_normal(fm, OWL_TABSTR); |
---|
| 274 | owl_fmtext_append_normal(fm, cmd->name); |
---|
| 275 | |
---|
| 276 | if (cmd->summary && *cmd->summary) { |
---|
| 277 | owl_fmtext_append_normal(fm, " - "); |
---|
| 278 | owl_fmtext_append_normal(fm, cmd->summary); |
---|
| 279 | } |
---|
| 280 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 281 | |
---|
| 282 | if (cmd->usage && *cmd->usage) { |
---|
| 283 | s = cmd->usage; |
---|
| 284 | indent = owl_malloc(strlen(s)+(owl_text_num_lines(s)+3)*OWL_TAB+1); |
---|
| 285 | owl_text_indent(indent, s, OWL_TAB); |
---|
| 286 | owl_fmtext_append_bold(fm, "\nSYNOPSIS\n"); |
---|
| 287 | owl_fmtext_append_normal(fm, indent); |
---|
| 288 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 289 | owl_free(indent); |
---|
| 290 | } else { |
---|
| 291 | owl_fmtext_append_bold(fm, "\nSYNOPSIS\n"); |
---|
| 292 | owl_fmtext_append_normal(fm, OWL_TABSTR); |
---|
| 293 | owl_fmtext_append_normal(fm, cmd->name); |
---|
| 294 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | if (cmd->description && *cmd->description) { |
---|
| 298 | s = cmd->description; |
---|
| 299 | indent = owl_malloc(strlen(s)+(owl_text_num_lines(s)+3)*OWL_TAB+1); |
---|
| 300 | owl_text_indent(indent, s, OWL_TAB); |
---|
| 301 | owl_fmtext_append_bold(fm, "\nDESCRIPTION\n"); |
---|
| 302 | owl_fmtext_append_normal(fm, indent); |
---|
| 303 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 304 | owl_free(indent); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | owl_fmtext_append_normal(fm, "\n\n"); |
---|
| 308 | } |
---|