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