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