[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 | { |
---|
[ddbbcffa] | 46 | owl_list_cleanup(l, g_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; |
---|
[96828e4] | 52 | cmd = g_new(owl_cmd, 1); |
---|
[7d4fbcd] | 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) { |
---|
[96828e4] | 60 | owl_cmd * newcmd = g_new(owl_cmd, 1); |
---|
[6922edd] | 61 | if(owl_cmd_create_from_template(newcmd, cmd) < 0) { |
---|
[ddbbcffa] | 62 | g_free(newcmd); |
---|
[6922edd] | 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); |
---|
[044f19f] | 76 | /* redraw the sepbar; TODO: don't violate layering */ |
---|
| 77 | owl_global_sepbar_dirty(&g); |
---|
[c4ba74d] | 78 | } else { |
---|
| 79 | owl_function_makemsg("Unknown command '%s'.", buff); |
---|
| 80 | } |
---|
| 81 | return retval; |
---|
| 82 | } |
---|
| 83 | |
---|
[3f8514b] | 84 | char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff) { |
---|
[7d4fbcd] | 85 | char **argv; |
---|
| 86 | int argc; |
---|
| 87 | char *tmpbuff; |
---|
| 88 | char *retval = NULL; |
---|
| 89 | |
---|
[d4927a7] | 90 | tmpbuff=g_strdup(cmdbuff); |
---|
[7d4fbcd] | 91 | argv=owl_parseline(tmpbuff, &argc); |
---|
| 92 | if (argc < 0) { |
---|
[ddbbcffa] | 93 | g_free(tmpbuff); |
---|
[7d4fbcd] | 94 | owl_function_makemsg("Unbalanced quotes"); |
---|
| 95 | return NULL; |
---|
| 96 | } |
---|
| 97 | |
---|
[62a000b] | 98 | if (argc < 1) { |
---|
[40d22cf] | 99 | owl_parse_delete(argv, argc); |
---|
[ddbbcffa] | 100 | g_free(tmpbuff); |
---|
[62a000b] | 101 | return NULL; |
---|
| 102 | } |
---|
[7d4fbcd] | 103 | |
---|
[c529ac8] | 104 | retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff); |
---|
[c4ba74d] | 105 | |
---|
[40d22cf] | 106 | owl_parse_delete(argv, argc); |
---|
[ddbbcffa] | 107 | g_free(tmpbuff); |
---|
[7d4fbcd] | 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) { |
---|
[e3c8332] | 112 | GString *buf = g_string_new(""); |
---|
| 113 | int i; |
---|
| 114 | char *retval; |
---|
[c4ba74d] | 115 | |
---|
[e3c8332] | 116 | /* We weren't given a command line, so fabricate a valid one. */ |
---|
[c4ba74d] | 117 | for(i = 0; i < argc; i++) { |
---|
[e3c8332] | 118 | if (i != 0) |
---|
| 119 | g_string_append_c(buf, ' '); |
---|
| 120 | owl_string_append_quoted_arg(buf, argv[i]); |
---|
[c4ba74d] | 121 | } |
---|
| 122 | |
---|
[e3c8332] | 123 | retval = _owl_cmddict_execute(cd, ctx, argv, argc, buf->str); |
---|
[c4ba74d] | 124 | |
---|
[e3c8332] | 125 | g_string_free(buf, true); |
---|
[c4ba74d] | 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); |
---|
[d4927a7] | 137 | cmd->name = g_strdup(templ->name); |
---|
| 138 | if (templ->summary) cmd->summary = g_strdup(templ->summary); |
---|
| 139 | if (templ->usage) cmd->usage = g_strdup(templ->usage); |
---|
| 140 | if (templ->description) cmd->description = g_strdup(templ->description); |
---|
| 141 | if (templ->cmd_aliased_to) cmd->cmd_aliased_to = g_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)); |
---|
[d4927a7] | 147 | cmd->name = g_strdup(name); |
---|
| 148 | cmd->cmd_aliased_to = g_strdup(aliased_to); |
---|
[36486be] | 149 | cmd->summary = owl_sprintf("%s%s", OWL_CMD_ALIAS_SUMMARY_PREFIX, aliased_to); |
---|
[7d4fbcd] | 150 | return(0); |
---|
| 151 | } |
---|
| 152 | |
---|
[685e96f] | 153 | void owl_cmd_cleanup(owl_cmd *cmd) |
---|
| 154 | { |
---|
[ddbbcffa] | 155 | if (cmd->name) g_free(cmd->name); |
---|
| 156 | if (cmd->summary) g_free(cmd->summary); |
---|
| 157 | if (cmd->usage) g_free(cmd->usage); |
---|
| 158 | if (cmd->description) g_free(cmd->description); |
---|
| 159 | if (cmd->cmd_aliased_to) g_free(cmd->cmd_aliased_to); |
---|
[8f2d9bf] | 160 | if (cmd->cmd_perl) owl_perlconfig_cmd_cleanup(cmd); |
---|
[7d4fbcd] | 161 | } |
---|
| 162 | |
---|
[b0da027] | 163 | void owl_cmd_delete(owl_cmd *cmd) |
---|
| 164 | { |
---|
[685e96f] | 165 | owl_cmd_cleanup(cmd); |
---|
[ddbbcffa] | 166 | g_free(cmd); |
---|
[b0da027] | 167 | } |
---|
| 168 | |
---|
[0a0fb74] | 169 | int owl_cmd_is_context_valid(const owl_cmd *cmd, const owl_context *ctx) { |
---|
[7d4fbcd] | 170 | if (owl_context_matches(ctx, cmd->validctx)) return 1; |
---|
| 171 | else return 0; |
---|
| 172 | } |
---|
| 173 | |
---|
[0a0fb74] | 174 | 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] | 175 | static int alias_recurse_depth = 0; |
---|
| 176 | int ival=0; |
---|
[e19eb97] | 177 | const char *cmdbuffargs; |
---|
[65b2173] | 178 | char *newcmd, *rv=NULL; |
---|
[7d4fbcd] | 179 | |
---|
| 180 | if (argc < 1) return(NULL); |
---|
| 181 | |
---|
| 182 | /* Recurse if this is an alias */ |
---|
| 183 | if (cmd->cmd_aliased_to) { |
---|
| 184 | if (alias_recurse_depth++ > 50) { |
---|
| 185 | owl_function_makemsg("Alias loop detected for '%s'.", cmdbuff); |
---|
| 186 | } else { |
---|
| 187 | cmdbuffargs = skiptokens(cmdbuff, 1); |
---|
[36486be] | 188 | newcmd = owl_sprintf("%s %s", cmd->cmd_aliased_to, cmdbuffargs); |
---|
[7d4fbcd] | 189 | rv = owl_function_command(newcmd); |
---|
[ddbbcffa] | 190 | g_free(newcmd); |
---|
[7d4fbcd] | 191 | } |
---|
| 192 | alias_recurse_depth--; |
---|
| 193 | return rv; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /* Do validation and conversions */ |
---|
| 197 | if (cmd->cmd_ctxargs_fn || cmd->cmd_ctxv_fn || cmd->cmd_ctxi_fn) { |
---|
| 198 | if (!owl_cmd_is_context_valid(cmd, ctx)) { |
---|
| 199 | owl_function_makemsg("Invalid context for command '%s'.", cmdbuff); |
---|
| 200 | return NULL; |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | if ((argc != 1) && (cmd->cmd_v_fn || cmd->cmd_ctxv_fn)) { |
---|
| 205 | owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]); |
---|
| 206 | return NULL; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | if (cmd->cmd_i_fn || cmd->cmd_ctxi_fn) { |
---|
[99525be] | 210 | char *ep; |
---|
[7d4fbcd] | 211 | if (argc != 2) { |
---|
| 212 | owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]); |
---|
| 213 | return NULL; |
---|
| 214 | } |
---|
[99525be] | 215 | ival = strtol(argv[1], &ep, 10); |
---|
[7d4fbcd] | 216 | if (*ep || ep==argv[1]) { |
---|
| 217 | owl_function_makemsg("Invalid argument '%s' for %s command.", argv[1], argv[0]); |
---|
| 218 | return(NULL); |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | if (cmd->cmd_args_fn) { |
---|
| 223 | return cmd->cmd_args_fn(argc, argv, cmdbuff); |
---|
| 224 | } else if (cmd->cmd_v_fn) { |
---|
| 225 | cmd->cmd_v_fn(); |
---|
| 226 | } else if (cmd->cmd_i_fn) { |
---|
| 227 | cmd->cmd_i_fn(ival); |
---|
| 228 | } else if (cmd->cmd_ctxargs_fn) { |
---|
| 229 | return cmd->cmd_ctxargs_fn(owl_context_get_data(ctx), argc, argv, cmdbuff); |
---|
| 230 | } else if (cmd->cmd_ctxv_fn) { |
---|
| 231 | cmd->cmd_ctxv_fn(owl_context_get_data(ctx)); |
---|
| 232 | } else if (cmd->cmd_ctxi_fn) { |
---|
| 233 | cmd->cmd_ctxi_fn(owl_context_get_data(ctx), ival); |
---|
[6922edd] | 234 | } else if (cmd->cmd_perl) { |
---|
| 235 | return owl_perlconfig_perlcmd(cmd, argc, argv); |
---|
[7d4fbcd] | 236 | } |
---|
| 237 | |
---|
| 238 | return NULL; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | /* returns a reference */ |
---|
[0a0fb74] | 242 | const char *owl_cmd_get_summary(const owl_cmd *cmd) { |
---|
[7d4fbcd] | 243 | return cmd->summary; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | /* returns a summary line describing this keymap. the caller must free. */ |
---|
[0a0fb74] | 247 | char *owl_cmd_describe(const owl_cmd *cmd) { |
---|
[7d4fbcd] | 248 | if (!cmd || !cmd->name || !cmd->summary) return NULL; |
---|
[36486be] | 249 | return owl_sprintf("%-25s - %s", cmd->name, cmd->summary); |
---|
[7d4fbcd] | 250 | } |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | |
---|
[ff7e289] | 254 | void owl_cmd_get_help(const owl_cmddict *d, const char *name, owl_fmtext *fm) { |
---|
[e19eb97] | 255 | const char *s; |
---|
[65b2173] | 256 | char *indent; |
---|
[7d4fbcd] | 257 | owl_cmd *cmd; |
---|
| 258 | |
---|
| 259 | if (!name || (cmd = owl_dict_find_element(d, name)) == NULL) { |
---|
| 260 | owl_fmtext_append_bold(fm, "OWL HELP\n\n"); |
---|
| 261 | owl_fmtext_append_normal(fm, "No such command...\n"); |
---|
| 262 | return; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | owl_fmtext_append_bold(fm, "OWL HELP\n\n"); |
---|
| 266 | owl_fmtext_append_bold(fm, "NAME\n\n"); |
---|
| 267 | owl_fmtext_append_normal(fm, OWL_TABSTR); |
---|
| 268 | owl_fmtext_append_normal(fm, cmd->name); |
---|
| 269 | |
---|
| 270 | if (cmd->summary && *cmd->summary) { |
---|
| 271 | owl_fmtext_append_normal(fm, " - "); |
---|
| 272 | owl_fmtext_append_normal(fm, cmd->summary); |
---|
| 273 | } |
---|
| 274 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 275 | |
---|
| 276 | if (cmd->usage && *cmd->usage) { |
---|
| 277 | s = cmd->usage; |
---|
[4d24650] | 278 | indent = owl_text_indent(s, OWL_TAB); |
---|
[7d4fbcd] | 279 | owl_fmtext_append_bold(fm, "\nSYNOPSIS\n"); |
---|
| 280 | owl_fmtext_append_normal(fm, indent); |
---|
| 281 | owl_fmtext_append_normal(fm, "\n"); |
---|
[ddbbcffa] | 282 | g_free(indent); |
---|
[7d4fbcd] | 283 | } else { |
---|
| 284 | owl_fmtext_append_bold(fm, "\nSYNOPSIS\n"); |
---|
| 285 | owl_fmtext_append_normal(fm, OWL_TABSTR); |
---|
| 286 | owl_fmtext_append_normal(fm, cmd->name); |
---|
| 287 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | if (cmd->description && *cmd->description) { |
---|
| 291 | s = cmd->description; |
---|
[4d24650] | 292 | indent = owl_text_indent(s, OWL_TAB); |
---|
[7d4fbcd] | 293 | owl_fmtext_append_bold(fm, "\nDESCRIPTION\n"); |
---|
| 294 | owl_fmtext_append_normal(fm, indent); |
---|
| 295 | owl_fmtext_append_normal(fm, "\n"); |
---|
[ddbbcffa] | 296 | g_free(indent); |
---|
[7d4fbcd] | 297 | } |
---|
| 298 | |
---|
| 299 | owl_fmtext_append_normal(fm, "\n\n"); |
---|
| 300 | } |
---|