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