/* returns 0 on success, prints a status msg if msg is true */ int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) { char *tostring; GValue values[] = {G_VALUE_INIT, G_VALUE_INIT}; GValue return_box = G_VALUE_INIT; int set_successfully; g_value_init(&values[0], G_TYPE_POINTER); g_value_set_pointer(&values[0], NULL); g_value_init(&values[1], G_TYPE_STRING); g_value_set_static_string(&values[1], value); g_value_init(&return_box, G_TYPE_INT); g_closure_invoke(v->set_fromstring_fn, &return_box, 2, values, NULL); set_successfully = g_value_get_int(&return_box); if (0 != set_successfully) { if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v), owl_variable_get_validsettings(v)); } else if (msg) { tostring = owl_variable_get_tostring(v); if (tostring) { owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring); } else { owl_function_makemsg("%s = <null>", owl_variable_get_name(v)); } g_free(tostring); } g_value_unset(&return_box); g_value_unset(&values[1]); g_value_unset(&values[0]); return set_successfully; }
/* returns 0 on success, prints a status msg if msg is true */ int owl_variable_set_fromstring(owl_vardict *d, char *name, char *value, int msg, int requirebool) { owl_variable *v; char buff2[1024]; if (!name) return(-1); v = owl_dict_find_element(d, name); if (v == NULL) { if (msg) owl_function_error("Unknown variable %s", name); return -1; } if (!v->set_fromstring_fn) { if (msg) owl_function_error("Variable %s is read-only", name); return -1; } if (requirebool && v->type!=OWL_VARIABLE_BOOL) { if (msg) owl_function_error("Variable %s is not a boolean", name); return -1; } if (0 != v->set_fromstring_fn(v, value)) { if (msg) owl_function_error("Unable to set %s (must be %s)", name, owl_variable_get_validsettings(v)); return -1; } if (msg && v->get_tostring_fn) { v->get_tostring_fn(v, buff2, 1024, v->val); owl_function_makemsg("%s = '%s'", name, buff2); } return 0; }
void owl_keyhandler_invalidkey(owl_keyhandler *kh) { char *kbbuff = owl_keybinding_stack_tostring(kh->kpstack, kh->kpstackpos+1); owl_function_makemsg("'%s' is not a valid key in this context.", kbbuff); g_free(kbbuff); owl_keyhandler_reset(kh); }
void owl_process_input_char(owl_input j) { int ret; owl_global_set_lastinputtime(&g, time(NULL)); ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j); if (ret!=0 && ret!=1) { owl_function_makemsg("Unable to handle keypress"); } }
/* caller must free the return */ CALLER_OWN char *_owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *const *argv, int argc, const char *buff) { char *retval = NULL; const owl_cmd *cmd; if (!strcmp(argv[0], "")) { } else if (NULL != (cmd = owl_dict_find_element(cd, argv[0]))) { retval = owl_cmd_execute(cmd, cd, ctx, argc, argv, buff); /* redraw the sepbar; TODO: don't violate layering */ owl_global_sepbar_dirty(&g); } else { owl_function_makemsg("Unknown command '%s'.", buff); } return retval; }
static void owl_viewwin_callback_search(owl_editwin *e) { int consider_current = false; const char *line = owl_editwin_get_text(e); owl_viewwin_search_data *data = owl_editwin_get_cbdata(e); /* Given an empty string, just continue the current search. */ if (line && *line) { owl_function_set_search(line); consider_current = true; } if (!owl_viewwin_search(data->v, owl_global_get_search_re(&g), consider_current, data->direction)) owl_function_makemsg("No matches"); }
/* caller must free the return */ CALLER_OWN char *owl_cmddict_execute(const owl_cmddict *cd, const owl_context *ctx, const char *cmdbuff) { char **argv; int argc; char *retval = NULL; argv = owl_parseline(cmdbuff, &argc); if (argv == NULL) { owl_function_makemsg("Unbalanced quotes"); return NULL; } if (argc < 1) { g_strfreev(argv); return NULL; } retval = _owl_cmddict_execute(cd, ctx, strs(argv), argc, cmdbuff); g_strfreev(argv); return retval; }
void owl_process_input_char(owl_input j) { int ret; owl_popwin *pw; owl_editwin *tw; owl_global_set_lastinputtime(&g, time(NULL)); pw=owl_global_get_popwin(&g); tw=owl_global_get_typwin(&g); owl_global_set_lastinputtime(&g, time(NULL)); /* find and activate the current keymap. * TODO: this should really get fixed by activating * keymaps as we switch between windows... */ if (pw && owl_popwin_is_active(pw) && owl_global_get_viewwin(&g)) { owl_context_set_popless(owl_global_get_context(&g), owl_global_get_viewwin(&g)); owl_function_activate_keymap("popless"); } else if (owl_global_is_typwin_active(&g) && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_ONELINE) { /* owl_context_set_editline(owl_global_get_context(&g), tw); owl_function_activate_keymap("editline"); */ } else if (owl_global_is_typwin_active(&g) && owl_editwin_get_style(tw)==OWL_EDITWIN_STYLE_MULTILINE) { owl_context_set_editmulti(owl_global_get_context(&g), tw); owl_function_activate_keymap("editmulti"); } else { owl_context_set_recv(owl_global_get_context(&g)); owl_function_activate_keymap("recv"); } /* now actually handle the keypress */ ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j); if (ret!=0 && ret!=1) { owl_function_makemsg("Unable to handle keypress"); } }
char *owl_viewwin_command_search(owl_viewwin *v, int argc, const char *const *argv, const char *buff) { int direction, consider_current; const char *buffstart; direction=OWL_DIRECTION_DOWNWARDS; buffstart=skiptokens(buff, 1); if (argc>1 && !strcmp(argv[1], "-r")) { direction=OWL_DIRECTION_UPWARDS; buffstart=skiptokens(buff, 2); } if (argc==1 || (argc==2 && !strcmp(argv[1], "-r"))) { consider_current = false; } else { owl_function_set_search(buffstart); consider_current = true; } if (!owl_viewwin_search(v, owl_global_get_search_re(&g), consider_current, direction)) owl_function_makemsg("No more matches"); return NULL; }
/* caller must free the result */ 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) { static int alias_recurse_depth = 0; int ival=0; const char *cmdbuffargs; char *newcmd, *rv=NULL; if (argc < 1) return(NULL); /* Recurse if this is an alias */ if (cmd->cmd_aliased_to) { if (alias_recurse_depth++ > 50) { owl_function_makemsg("Alias loop detected for '%s'.", cmdbuff); } else { cmdbuffargs = skiptokens(cmdbuff, 1); newcmd = g_strdup_printf("%s %s", cmd->cmd_aliased_to, cmdbuffargs); rv = owl_function_command(newcmd); g_free(newcmd); } alias_recurse_depth--; return rv; } /* Do validation and conversions */ if (cmd->cmd_ctxargs_fn || cmd->cmd_ctxv_fn || cmd->cmd_ctxi_fn) { if (!owl_cmd_is_context_valid(cmd, ctx)) { owl_function_makemsg("Invalid context for command '%s'.", cmdbuff); return NULL; } } if ((argc != 1) && (cmd->cmd_v_fn || cmd->cmd_ctxv_fn)) { owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]); return NULL; } if (cmd->cmd_i_fn || cmd->cmd_ctxi_fn) { char *ep; if (argc != 2) { owl_function_makemsg("Wrong number of arguments for %s command.", argv[0]); return NULL; } ival = strtol(argv[1], &ep, 10); if (*ep || ep==argv[1]) { owl_function_makemsg("Invalid argument '%s' for %s command.", argv[1], argv[0]); return(NULL); } } if (cmd->cmd_args_fn) { return cmd->cmd_args_fn(argc, argv, cmdbuff); } else if (cmd->cmd_v_fn) { cmd->cmd_v_fn(); } else if (cmd->cmd_i_fn) { cmd->cmd_i_fn(ival); } else if (cmd->cmd_ctxargs_fn) { return cmd->cmd_ctxargs_fn(owl_context_get_data(ctx), argc, argv, cmdbuff); } else if (cmd->cmd_ctxv_fn) { cmd->cmd_ctxv_fn(owl_context_get_data(ctx)); } else if (cmd->cmd_ctxi_fn) { cmd->cmd_ctxi_fn(owl_context_get_data(ctx), ival); } else if (cmd->cmd_perl) { return owl_perlconfig_perlcmd(cmd, argc, argv); } return NULL; }
/* processes a keypress. returns 0 if the keypress was handled, * 1 if not handled, -1 on error, and -2 if j==ERR. */ int owl_keyhandler_process(owl_keyhandler *kh, owl_input j) { const owl_keymap *km; const owl_keybinding *kb; int i, match; if (!kh->active) { owl_function_makemsg("No active keymap!!!"); return(-1); } /* deal with ESC prefixing */ if (!kh->in_esc && j.ch == 27) { kh->in_esc = 1; return(0); } if (kh->in_esc) { j.ch = OWL_META(j.ch); kh->in_esc = 0; } kh->kpstack[++(kh->kpstackpos)] = j.ch; if (kh->kpstackpos >= OWL_KEYMAP_MAXSTACK) { owl_keyhandler_reset(kh); owl_function_makemsg("Too many prefix keys pressed..."); return(-1); } /* deal with the always_fn for the map and parents */ for (km=kh->active; km; km=km->parent) { if (km->prealways_fn) { km->prealways_fn(j); } } /* search for a match. goes through active keymap and then * through parents... TODO: clean this up so we can pull * keyhandler and keymap apart. */ for (km=kh->active; km; km=km->parent) { for (i=owl_list_get_size(&km->bindings)-1; i>=0; i--) { kb = owl_list_get_element(&km->bindings, i); match = owl_keybinding_match(kb, kh); if (match == 1) { /* subset match */ /* owl_function_debugmsg("processkey: found subset match in %s", km->name); */ return(0); } else if (match == 2) { /* exact match */ /* owl_function_debugmsg("processkey: found exact match in %s", km->name); */ owl_keybinding_execute(kb, j.ch); owl_keyhandler_reset(kh); if (km->postalways_fn) { km->postalways_fn(j); } return(0); } } } /* see if a default action exists for the active keymap */ if (kh->active->default_fn && kh->kpstackpos<1) { /*owl_function_debugmsg("processkey: executing default_fn");*/ kh->active->default_fn(j); owl_keyhandler_reset(kh); if (kh->active->postalways_fn) { kh->active->postalways_fn(j); } return(0); } owl_keyhandler_invalidkey(kh); /* unable to handle */ return(1); }